1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-10 00:43:59 +02:00

Load & decode high score creatures on first access

This commit is contained in:
Ivan Savenko 2024-06-17 17:08:41 +00:00
parent e08f76ddf0
commit ffa5c03515

View File

@ -62,17 +62,43 @@ auto HighScoreCalculation::calculate()
return summary; return summary;
} }
struct HighScoreCreature
{
CreatureID creature;
int min;
int max;
};
static std::vector<HighScoreCreature> getHighscoreCreaturesList()
{
JsonNode configCreatures(JsonPath::builtin("CONFIG/highscoreCreatures.json"));
std::vector<HighScoreCreature> ret;
for(auto & json : configCreatures["creatures"].Vector())
{
HighScoreCreature entry;
entry.creature = CreatureID::decode(json["creature"].String());
entry.max = json["max"].isNull() ? std::numeric_limits<int>::max() : json["max"].Integer();
entry.min = json["min"].isNull() ? std::numeric_limits<int>::min() : json["min"].Integer();
ret.push_back(entry);
}
return ret;
}
CreatureID HighScoreCalculation::getCreatureForPoints(int points, bool campaign) CreatureID HighScoreCalculation::getCreatureForPoints(int points, bool campaign)
{ {
static const JsonNode configCreatures(JsonPath::builtin("CONFIG/highscoreCreatures.json")); static const std::vector<HighScoreCreature> creatures = getHighscoreCreaturesList();
auto creatures = configCreatures["creatures"].Vector();
int divide = campaign ? 5 : 1; int divide = campaign ? 5 : 1;
for(auto & creature : creatures) for(auto & creature : creatures)
if(points / divide <= creature["max"].Integer() && points / divide >= creature["min"].Integer()) if(points / divide <= creature.max && points / divide >= creature.min)
return CreatureID::decode(creature["creature"].String()); return creature.creature;
return -1; throw std::runtime_error("Unable to find creature for score " + std::to_string(points));
} }
CHighScoreScreen::CHighScoreScreen(HighScorePage highscorepage, int highlighted) CHighScoreScreen::CHighScoreScreen(HighScorePage highscorepage, int highlighted)