2023-06-20 04:33:09 +00:00
|
|
|
#include "StarStatisticsDatabase.hpp"
|
|
|
|
#include "StarJsonExtra.hpp"
|
|
|
|
|
|
|
|
namespace Star {
|
|
|
|
|
|
|
|
StatisticsDatabase::StatisticsDatabase() : m_cacheMutex(), m_eventCache() {
|
|
|
|
auto assets = Root::singleton().assets();
|
|
|
|
|
2024-03-15 10:28:11 +00:00
|
|
|
auto& eventFiles = assets->scanExtension("event");
|
2023-06-20 04:33:09 +00:00
|
|
|
assets->queueJsons(eventFiles);
|
2024-03-15 10:28:11 +00:00
|
|
|
auto& achievementFiles = assets->scanExtension("achievement");
|
2023-06-20 04:33:09 +00:00
|
|
|
assets->queueJsons(achievementFiles);
|
|
|
|
|
2024-03-15 10:28:11 +00:00
|
|
|
for (auto& file : eventFiles) {
|
2023-06-20 04:33:09 +00:00
|
|
|
try {
|
|
|
|
String name = assets->json(file).getString("eventName");
|
|
|
|
if (m_eventPaths.contains(name))
|
2023-06-27 10:23:44 +00:00
|
|
|
Logger::error("Event {} defined twice, second time from {}", name, file);
|
2023-06-20 04:33:09 +00:00
|
|
|
else
|
|
|
|
m_eventPaths[name] = file;
|
|
|
|
} catch (std::exception const& e) {
|
2023-06-27 10:23:44 +00:00
|
|
|
Logger::error("Error loading event file {}: {}", file, outputException(e, true));
|
2023-06-20 04:33:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-15 10:28:11 +00:00
|
|
|
for (auto& file : achievementFiles) {
|
2023-06-20 04:33:09 +00:00
|
|
|
try {
|
|
|
|
Json achievement = assets->json(file);
|
|
|
|
String name = achievement.getString("name");
|
|
|
|
if (m_achievementPaths.contains(name))
|
2023-06-27 10:23:44 +00:00
|
|
|
Logger::error("Achievement {} defined twice, second time from {}", name, file);
|
2023-06-20 04:33:09 +00:00
|
|
|
else
|
|
|
|
m_achievementPaths[name] = file;
|
|
|
|
|
|
|
|
for (Json const& stat : achievement.getArray("triggers", {})) {
|
|
|
|
m_statAchievements[stat.toString()].append(name);
|
|
|
|
}
|
|
|
|
} catch (std::exception const& e) {
|
2023-06-27 10:23:44 +00:00
|
|
|
Logger::error("Error loading achievement file {}: {}", file, outputException(e, true));
|
2023-06-20 04:33:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StatEventPtr StatisticsDatabase::event(String const& name) const {
|
|
|
|
MutexLocker locker(m_cacheMutex);
|
|
|
|
return m_eventCache.get(name, [this](String const& name) -> StatEventPtr {
|
|
|
|
if (auto path = m_eventPaths.maybe(name))
|
|
|
|
return readEvent(*path);
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
AchievementPtr StatisticsDatabase::achievement(String const& name) const {
|
|
|
|
MutexLocker locker(m_cacheMutex);
|
|
|
|
return m_achievementCache.get(name, [this](String const& name) -> AchievementPtr {
|
|
|
|
if (auto path = m_achievementPaths.maybe(name))
|
|
|
|
return readAchievement(*path);
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
StringList StatisticsDatabase::allAchievements() const {
|
|
|
|
return m_achievementPaths.keys();
|
|
|
|
}
|
|
|
|
|
|
|
|
StringList StatisticsDatabase::achievementsForStat(String const& statName) const {
|
|
|
|
return m_statAchievements.value(statName);
|
|
|
|
}
|
|
|
|
|
|
|
|
StatEventPtr StatisticsDatabase::readEvent(String const& path) {
|
|
|
|
auto assets = Root::singleton().assets();
|
|
|
|
Json config = assets->json(path);
|
|
|
|
|
|
|
|
return make_shared<StatEvent>(StatEvent {
|
|
|
|
config.getString("eventName"),
|
|
|
|
jsonToStringList(config.get("scripts")),
|
|
|
|
config
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
AchievementPtr StatisticsDatabase::readAchievement(String const& path) {
|
|
|
|
auto assets = Root::singleton().assets();
|
|
|
|
Json config = assets->json(path);
|
|
|
|
|
|
|
|
return make_shared<Achievement>(Achievement {
|
|
|
|
config.getString("name"),
|
|
|
|
jsonToStringList(config.get("triggers")),
|
|
|
|
jsonToStringList(config.get("scripts")),
|
|
|
|
config
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|