Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion core/libs/prx/libSceNpTrophy2/include/TrophyPackage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,27 @@ struct TrophyPackage {

TrophyPackage ParseTrophyPackage(std::string_view ucp, std::string_view language);

enum class StatType { UInt32, UInt64 };
enum class StatType { UInt32, UInt64, Int32 };

enum class StatAggregation { Latest, Max };

struct StatDefinition {
StatType type = StatType::UInt64;
StatAggregation aggregation = StatAggregation::Latest;
std::uint64_t minimum = 0;
std::uint64_t maximum = UINT64_MAX;
std::uint64_t initial = 0;
};

struct StatFilter {
std::string path;
std::uint64_t value = 0;
};

struct StatRule {
std::string input;
std::uint64_t statId = 0;
std::optional<StatFilter> filter;
};

struct StatRules {
Expand Down
1 change: 1 addition & 0 deletions core/libs/prx/libSceNpTrophy2/include/TrophyStore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class StatStore {
explicit StatStore(std::filesystem::path directory);

void Set(std::int32_t userId, std::uint32_t serviceLabel, std::uint64_t statId, std::uint64_t value);
std::uint64_t Raise(std::int32_t userId, std::uint32_t serviceLabel, std::uint64_t statId, std::uint64_t value);
std::optional<std::uint64_t> Value(std::int32_t userId, std::uint32_t serviceLabel, std::uint64_t statId);
std::map<std::uint64_t, std::uint64_t> Values(std::int32_t userId, std::uint32_t serviceLabel);
std::filesystem::path RecordPath(std::int32_t userId, std::uint32_t serviceLabel) const;
Expand Down
6 changes: 6 additions & 0 deletions core/libs/prx/libSceNpTrophy2/src/TitleTrophies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ StatChanges ApplyStatRules(const StatRules& rules, const NpTrophy2Event& event,
const auto matching = rules.events.find(event.name);
if (matching == rules.events.end()) return changes;
for (const auto& rule : matching->second) {
if (rule.filter) {
const auto filtered = std::find_if(event.properties.begin(), event.properties.end(), [&rule](const NpTrophy2EventProperty& candidate) { return candidate.path == rule.filter->path; });
if (filtered == event.properties.end() || filtered->value != rule.filter->value) continue;
}
const auto& definition = rules.stats.at(rule.statId);
const auto property = std::find_if(event.properties.begin(), event.properties.end(), [&rule](const NpTrophy2EventProperty& candidate) { return candidate.path == rule.input; });
const auto where = event.name + " $." + rule.input + " for stat " + std::to_string(rule.statId);
Expand All @@ -148,6 +152,8 @@ StatChanges ApplyStatRules(const StatRules& rules, const NpTrophy2Event& event,
changes.problems.push_back({where, property->text + " is not an unsigned integer"});
} else if (*property->value < definition.minimum || *property->value > definition.maximum) {
changes.problems.push_back({where, property->text + " is not from " + std::to_string(definition.minimum) + " to " + std::to_string(definition.maximum)});
} else if (definition.aggregation == StatAggregation::Max) {
changes.updates.push_back({rule.statId, stats.Raise(userId, serviceLabel, rule.statId, std::max(*property->value, definition.initial)), rule.input});
} else {
stats.Set(userId, serviceLabel, rule.statId, *property->value);
changes.updates.push_back({rule.statId, *property->value, rule.input});
Expand Down
48 changes: 38 additions & 10 deletions core/libs/prx/libSceNpTrophy2/src/TrophyPackage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,26 +363,34 @@ void readStat(const Json& definition, std::size_t index, StatRules& rules) {
}
const auto name = "stat " + std::to_string(*statId);
const auto* aggregation = definition.Find("aggregation");
if (aggregation == nullptr || aggregation->kind != Json::Kind::String || aggregation->text != "latest") {
rules.skipped.push_back(name + " has aggregation " + (aggregation != nullptr ? describe(*aggregation) : std::string("none")) + "; only \"latest\" is supported");
const auto named = [](const Json* value, std::string_view text) { return value != nullptr && value->kind == Json::Kind::String && value->text == text; };
StatDefinition stat;
if (named(aggregation, "latest")) {
stat.aggregation = StatAggregation::Latest;
} else if (named(aggregation, "max")) {
stat.aggregation = StatAggregation::Max;
} else {
rules.skipped.push_back(name + " has aggregation " + (aggregation != nullptr ? describe(*aggregation) : std::string("none")) + "; only \"latest\" and \"max\" are supported");
return;
}
const auto* type = definition.Find("dataType");
StatDefinition stat;
if (type != nullptr && type->kind == Json::Kind::String && type->text == "uint32") {
if (named(type, "uint32")) {
stat.type = StatType::UInt32;
stat.maximum = UINT32_MAX;
} else if (type != nullptr && type->kind == Json::Kind::String && type->text == "uint64") {
} else if (named(type, "uint64")) {
stat.type = StatType::UInt64;
stat.maximum = UINT64_MAX;
} else if (named(type, "int32")) {
stat.type = StatType::Int32;
stat.maximum = INT32_MAX;
} else {
rules.skipped.push_back(name + " has dataType " + (type != nullptr ? describe(*type) : std::string("none")) + "; only \"uint32\" and \"uint64\" are supported");
rules.skipped.push_back(name + " has dataType " + (type != nullptr ? describe(*type) : std::string("none")) + "; only \"uint32\", \"uint64\" and \"int32\" are supported");
return;
}
if (const auto* minimum = definition.Find("minValue")) {
const auto value = minimum->Unsigned();
if (!value || *value > stat.maximum) {
rules.skipped.push_back(name + " has minValue " + describe(*minimum) + ", which its " + type->text + " type cannot hold");
rules.skipped.push_back(name + " has minValue " + describe(*minimum) + (stat.type == StatType::Int32 ? ", which is not from 0 to its int32 limit" : ", which its " + type->text + " type cannot hold"));
return;
}
stat.minimum = *value;
Expand All @@ -395,6 +403,15 @@ void readStat(const Json& definition, std::size_t index, StatRules& rules) {
}
stat.maximum = *value;
}
stat.initial = stat.minimum;
if (const auto* initial = definition.Find("initialValue"); initial != nullptr && stat.aggregation == StatAggregation::Max) {
const auto value = initial->Unsigned();
if (!value || *value < stat.minimum || *value > stat.maximum) {
rules.skipped.push_back(name + " has initialValue " + describe(*initial) + ", which is not from its minValue to its maxValue");
return;
}
stat.initial = *value;
}
if (!rules.stats.emplace(*statId, stat).second) rules.skipped.push_back(name + " is defined twice; the first definition is used");
}

Expand All @@ -411,9 +428,20 @@ void readRule(const Json& rule, std::size_t index, StatRules& rules) {
rules.skipped.push_back(where + " has no eventName");
return;
}
std::optional<StatFilter> filter;
if (condition->members.size() != 1) {
rules.skipped.push_back(where + " filters " + event->text + " by more than its eventName; only eventName conditions are supported");
return;
const auto* property = condition->Find("property");
const auto* path = property != nullptr && property->kind == Json::Kind::Object ? property->Find("path") : nullptr;
const auto* comparator = property != nullptr && property->kind == Json::Kind::Object ? property->Find("comparator") : nullptr;
const auto* value = property != nullptr && property->kind == Json::Kind::Object ? property->Find("value") : nullptr;
const auto filterPath = path != nullptr && path->kind == Json::Kind::String ? propertyPath(path->text) : std::nullopt;
const auto filterValue = value != nullptr ? value->Unsigned() : std::nullopt;
const bool equal = comparator != nullptr && comparator->kind == Json::Kind::String && comparator->text == "==";
if (condition->members.size() != 2 || property == nullptr || property->kind != Json::Kind::Object || property->members.size() != 3 || !filterPath || !equal || !filterValue) {
rules.skipped.push_back(where + " filters " + event->text + " by more than its eventName; only an eventName with one \"$.name\" property equal to an unsigned integer is supported");
return;
}
filter = StatFilter{*filterPath, *filterValue};
}
const auto* output = action->Find("output");
const auto* id = output != nullptr ? output->Find("statId") : nullptr;
Expand All @@ -432,7 +460,7 @@ void readRule(const Json& rule, std::size_t index, StatRules& rules) {
rules.skipped.push_back(where + " for " + event->text + " reads " + (input != nullptr ? describe(*input) : std::string("no input")) + "; only \"$.name\" property paths are supported");
return;
}
rules.events[event->text].push_back({*path, *statId});
rules.events[event->text].push_back({*path, *statId, std::move(filter)});
}

}
Expand Down
12 changes: 12 additions & 0 deletions core/libs/prx/libSceNpTrophy2/src/TrophyStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,18 @@ void StatStore::Set(std::int32_t userId, std::uint32_t serviceLabel, std::uint64
stats = std::move(updated);
}

std::uint64_t StatStore::Raise(std::int32_t userId, std::uint32_t serviceLabel, std::uint64_t statId, std::uint64_t value) {
std::lock_guard lock(mutex);
const Key key{userId, serviceLabel};
auto& stats = record(key);
if (const auto found = stats.find(statId); found != stats.end() && found->second >= value) return found->second;
auto updated = stats;
updated[statId] = value;
writeRecord(directory, RecordPath(userId, serviceLabel), updated, "stat record");
stats = std::move(updated);
return value;
}

std::optional<std::uint64_t> StatStore::Value(std::int32_t userId, std::uint32_t serviceLabel, std::uint64_t statId) {
std::lock_guard lock(mutex);
const auto& stats = record({userId, serviceLabel});
Expand Down
66 changes: 58 additions & 8 deletions core/libs/prx/libSceNpTrophy2/tests/TrophyPackage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ const char StatsDefinition[] = R"({"statDefinitionArray":[
{"statId":50,"dataType":"uint64","aggregation":"sum"},
{"statId":60,"dataType":"int32","aggregation":"latest"},
{"statId":70,"dataType":"uint32","aggregation":"latest","maxValue":"5000000000"},
{"dataType":"uint32","aggregation":"latest"}
{"dataType":"uint32","aggregation":"latest"},
{"statId":80,"dataType":"int32","aggregation":"max","minValue":"0","initialValue":"0"},
{"statId":90,"dataType":"uint32","aggregation":"max","initialValue":"3"},
{"statId":100,"dataType":"int32","aggregation":"max","initialValue":"-1"},
{"statId":110,"dataType":"int32","aggregation":"latest","minValue":"-5"}
]})";

const char StatsExtraction[] = R"({"statsExtractionRuleArray":[
Expand All @@ -106,7 +110,12 @@ const char StatsExtraction[] = R"({"statsExtractionRuleArray":[
{"condition":{"eventName":"FILTERED","property":{"path":"$.level"}},"action":{"input":"$.counter","output":{"statId":10}}},
{"condition":{"eventName":"COUNT"},"action":{"output":{"statId":10}}},
{"condition":{"eventName":"BRACKETS"},"action":{"input":"$['counter']","output":{"statId":10}}},
{"bad":1}
{"bad":1},
{"condition":{"eventName":"PROGRESS","property":{"path":"$._trophy_id","comparator":"==","value":"5"}},"action":{"input":"$._trophy_progress","output":{"statId":80}}},
{"condition":{"eventName":"PROGRESS","property":{"path":"$._trophy_id","comparator":"==","value":6}},"action":{"input":"$._trophy_progress","output":{"statId":90}}},
{"condition":{"eventName":"RANGED","property":{"path":"$._trophy_id","comparator":">=","value":"5"}},"action":{"input":"$._trophy_progress","output":{"statId":80}}},
{"condition":{"eventName":"NAMED","property":{"path":"$.mode","comparator":"==","value":"delta"}},"action":{"input":"$._trophy_progress","output":{"statId":80}}},
{"condition":{"eventName":"EXTRA","property":{"path":"$._trophy_id","comparator":"==","value":"5"},"other":1},"action":{"input":"$._trophy_progress","output":{"statId":80}}}
]})";

std::string trophyPackage() {
Expand Down Expand Up @@ -310,10 +319,13 @@ void testProgressAndPlatinum() {

void testStatRules() {
const auto rules = NpTrophy2::ParseStatRules(statPackage());
check(rules.stats.size() == 4 && rules.stats.at(10).type == NpTrophy2::StatType::UInt64 && rules.stats.at(30).type == NpTrophy2::StatType::UInt32 && rules.stats.at(30).maximum == UINT32_MAX &&
rules.stats.at(40).maximum == 50 && rules.stats.at(10).minimum == 0,
check(rules.stats.size() == 7 && rules.stats.at(10).type == NpTrophy2::StatType::UInt64 && rules.stats.at(30).type == NpTrophy2::StatType::UInt32 && rules.stats.at(30).maximum == UINT32_MAX &&
rules.stats.at(40).maximum == 50 && rules.stats.at(10).minimum == 0 && rules.stats.at(10).aggregation == NpTrophy2::StatAggregation::Latest,
"stat definitions were not read");
check(rules.events.size() == 3 && rules.events.at("COLLECT").size() == 2 && rules.events.at("COLLECT")[1].input == "total" && rules.events.at("COLLECT")[1].statId == 20 &&
check(rules.stats.at(60).type == NpTrophy2::StatType::Int32 && rules.stats.at(60).maximum == INT32_MAX && rules.stats.at(80).aggregation == NpTrophy2::StatAggregation::Max &&
rules.stats.at(80).type == NpTrophy2::StatType::Int32 && rules.stats.at(80).initial == 0 && rules.stats.at(90).aggregation == NpTrophy2::StatAggregation::Max && rules.stats.at(90).initial == 3,
"int32 and max stat definitions were not read");
check(rules.events.size() == 4 && rules.events.at("COLLECT").size() == 2 && rules.events.at("COLLECT")[1].input == "total" && rules.events.at("COLLECT")[1].statId == 20 &&
rules.events.at("STEPS")[0].input == "player.steps" && rules.events.at("TIME")[0].statId == 30,
"extraction rules were not read");
std::set<std::string> skipped(rules.skipped.begin(), rules.skipped.end());
Expand All @@ -323,11 +335,17 @@ void testStatRules() {
}
return false;
};
check(rules.skipped.size() == 9, "unsupported definitions and rules were not all reported");
check(reported("stat 50 has aggregation \"sum\"") && reported("stat 60 has dataType \"int32\"") && reported("stat 70 has maxValue \"5000000000\"") && reported("statDefinitionArray[7] has no unsigned statId"),
check(rules.skipped.size() == 13, "unsupported definitions and rules were not all reported");
check(reported("stat 50 has aggregation \"sum\"; only \"latest\" and \"max\" are supported") && reported("stat 70 has maxValue \"5000000000\"") && reported("statDefinitionArray[7] has no unsigned statId") &&
reported("stat 100 has initialValue \"-1\"") && reported("stat 110 has minValue \"-5\", which is not from 0 to its int32 limit"),
"unsupported definitions were not reported");
check(reported("for SUM writes stat 50") && reported("filters FILTERED") && reported("for COUNT reads no input") && reported("for BRACKETS reads \"$['counter']\"") && reported("[8] has no condition"),
check(reported("for SUM writes stat 50") && reported("filters FILTERED") && reported("for COUNT reads no input") && reported("for BRACKETS reads \"$['counter']\"") && reported("[8] has no condition") &&
reported("filters RANGED") && reported("filters NAMED") && reported("filters EXTRA"),
"unsupported rules were not reported");
const auto& progress = rules.events.at("PROGRESS");
check(progress.size() == 2 && progress[0].filter && progress[0].filter->path == "_trophy_id" && progress[0].filter->value == 5 && progress[0].input == "_trophy_progress" && progress[0].statId == 80 &&
progress[1].filter && progress[1].filter->value == 6 && progress[1].statId == 90 && !rules.events.at("COLLECT")[0].filter,
"property filters were not read");
for (const char* input : {"$", "$.", "$..a", "$.a.", "$.a-b", "counter", "$.a b"}) {
const std::string extraction = std::string(R"({"statsExtractionRuleArray":[{"condition":{"eventName":"E"},"action":{"input":")") + input + R"(","output":{"statId":10}}}]})";
check(NpTrophy2::ParseStatRules(ucp({{"stats_definition.json", StatsDefinition}, {"stats_extraction.json", extraction}})).events.empty(), "an unsupported input path was accepted");
Expand Down Expand Up @@ -412,6 +430,37 @@ void testApplyStatRules() {
check(latest.updates.size() == 2 && stats.Value(InitialUser, 0, 10) == std::optional<std::uint64_t>(2), "a stat did not keep the latest value");
}

void testProgressRules() {
const auto rules = NpTrophy2::ParseStatRules(statPackage());
NpTrophy2::StatStore stats("progress");
const auto posted = [&](std::vector<NpTrophy2EventProperty> properties) { return NpTrophy2::ApplyStatRules(rules, event("PROGRESS", std::move(properties)), stats, InitialUser, 0); };
const auto progress = [&](std::uint64_t trophy, std::uint64_t value) { return posted({{"_trophy_id", trophy, std::to_string(trophy)}, {"_trophy_progress", value, std::to_string(value)}}); };
const auto first = progress(5, 4);
check(first.problems.empty() && first.updates.size() == 1 && first.updates[0].statId == 80 && first.updates[0].value == 4 && stats.Value(InitialUser, 0, 80) == std::optional<std::uint64_t>(4) &&
!stats.Value(InitialUser, 0, 90),
"a filtered rule did not take its trophy's progress, or another trophy's rule did");
const auto lower = progress(5, 2);
check(lower.updates.size() == 1 && lower.updates[0].value == 4 && stats.Value(InitialUser, 0, 80) == std::optional<std::uint64_t>(4), "a max stat went down");
const auto higher = progress(5, 7);
check(higher.updates.size() == 1 && higher.updates[0].value == 7 && stats.Value(InitialUser, 0, 80) == std::optional<std::uint64_t>(7), "a max stat did not rise");
const auto floor = progress(6, 1);
check(floor.updates.size() == 1 && floor.updates[0].statId == 90 && floor.updates[0].value == 3 && stats.Value(InitialUser, 0, 90) == std::optional<std::uint64_t>(3), "a max stat started below its initialValue");
const auto signedMax = progress(5, 2147483648ull);
check(signedMax.updates.empty() && signedMax.problems.size() == 1 && signedMax.problems[0].reason == "2147483648 is not from 0 to 2147483647", "an int32 stat took a value it cannot hold");
const auto other = progress(7, 9);
const auto unnamed = posted({{"_trophy_progress", 9, "9"}});
const auto negative = posted({{"_trophy_id", std::nullopt, "-5"}, {"_trophy_progress", 9, "9"}});
check(other.updates.empty() && other.problems.empty() && unnamed.updates.empty() && unnamed.problems.empty() && negative.updates.empty() && negative.problems.empty() &&
stats.Value(InitialUser, 0, 80) == std::optional<std::uint64_t>(7) && stats.Value(InitialUser, 0, 90) == std::optional<std::uint64_t>(3),
"a filtered rule took an event for another trophy, or without its trophy id");
const auto missing = posted({{"_trophy_id", 5, "5"}});
check(missing.updates.empty() && missing.problems.size() == 1 && missing.problems[0].rule == "PROGRESS $._trophy_progress for stat 80", "a matching event without its input was not reported");
NpTrophy2::StatStore restarted("progress");
check(restarted.Raise(InitialUser, 0, 80, 6) == 7 && restarted.Raise(InitialUser, 0, 80, 8) == 8 && restarted.Raise(InitialUser, 0, 120, 1) == 1, "a raise did not keep the larger value");
NpTrophy2::StatStore reread("progress");
check(reread.Value(InitialUser, 0, 80) == std::optional<std::uint64_t>(8) && reread.Value(InitialUser, 0, 120) == std::optional<std::uint64_t>(1), "a raised stat was not stored");
}

}

int main() {
Expand All @@ -427,6 +476,7 @@ int main() {
testConditions();
testStatStore();
testApplyStatRules();
testProgressRules();
std::filesystem::current_path(previous);
std::filesystem::remove_all(root);
std::puts("Trophy package tests passed");
Expand Down
Loading
Loading