summaryrefslogtreecommitdiff
path: root/src/KingSystem/System/PlayReport.cpp
blob: 1ff33d0897c0f942dc0e9b13c6abaf8f31599c10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <nn/account.h>
#include <nn/prepo.h>
#include "KingSystem/System/Account.h"
#include "KingSystem/System/PlayReportMgr.h"
#include "KingSystem/System/ProductReporter.h"

namespace ksys {

bool PlayReport::setEventId(sead::BufferedSafeString& event_id) {
    if (!mHasNinPrepoReport)
        return false;

    event_id.replaceCharList("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz");
    return mNinPlayReport->SetEventId(event_id.cstr()).IsSuccess();
}

[[gnu::noinline]] bool PlayReport::add(const sead::FixedSafeString<48>& key, u32 value) {
    return mHasNinPrepoReport && mNinPlayReport->Add(key.cstr(), s64(value)).IsSuccess();
}

[[gnu::noinline]] bool PlayReport::add(const sead::FixedSafeString<48>& key, s32 value) {
    return mHasNinPrepoReport && mNinPlayReport->Add(key.cstr(), s64(value)).IsSuccess();
}

[[gnu::noinline]] bool PlayReport::add(const sead::FixedSafeString<48>& key, f32 value) {
    return mHasNinPrepoReport && mNinPlayReport->Add(key.cstr(), value).IsSuccess();
}

[[gnu::noinline]] bool PlayReport::add(const sead::FixedSafeString<48>& key,
                                       const sead::SafeString& value) {
    return mHasNinPrepoReport && mNinPlayReport->Add(key.cstr(), value.cstr()).IsSuccess();
}

bool PlayReport::save() {
    if (!mHasNinPrepoReport)
        return false;

    nn::account::Uid uid;
    Account::instance()->getUserId(&uid);
    if (uid.IsValid()) {
        static_cast<void>(mNinPlayReport->GetCount());
        return mNinPlayReport->Save(uid).IsSuccess();
    }
    static_cast<void>(ProductReporter::getSomeBool());
    static_cast<void>(mNinPlayReport->GetCount());
    return mNinPlayReport->Save().IsSuccess();
}

void PlayReport::init(s32 num_entries, sead::Heap* heap) {
    if (mHasNinPrepoReport)
        return;

    auto* report = new (heap) nn::prepo::PlayReport;
    const u32 buffer_size = nn::prepo::PlayReport::CalcBufferSize(num_entries);
    mNinPlayReport = report;
    if (buffer_size > 0 && mBuffer.tryAllocBuffer(buffer_size, heap)) {
        mNinPlayReport->SetBuffer(mBuffer.getBufferPtr(), buffer_size);
        mHasNinPrepoReport = true;
    }
}

PlayReport::~PlayReport() {
    if (!mHasNinPrepoReport)
        return;

    if (mNinPlayReport) {
        delete mNinPlayReport;
        mNinPlayReport = nullptr;
    }

    if (mHasNinPrepoReport)
        mBuffer.freeBuffer();

    mHasNinPrepoReport = false;
}

}  // namespace ksys