1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-13 19:54:17 +02:00

Extend loadKey function

This commit is contained in:
nordsoft
2023-04-08 00:38:51 +04:00
committed by Nordsoft91
parent fe21acb0d0
commit 6c5f3f21a5

View File

@@ -46,26 +46,27 @@ namespace JsonRandom
return rng.getIntRange(min, max)(); return rng.getIntRange(min, max)();
} }
DLL_LINKAGE std::string loadKey(const JsonNode & value, CRandomGenerator & rng, std::string defaultValue) std::string loadKey(const JsonNode & value, CRandomGenerator & rng, std::set<std::string> valuesSet)
{ {
if (value.isNull()) if (value.isNull())
return defaultValue; return valuesSet.empty() ? "" : *RandomGeneratorUtil::nextItem(valuesSet, rng);
if (value.isString()) if (value.isString())
return value.String(); return value.String();
if (!value["type"].isNull()) if (!value["type"].isNull())
return value["type"].String(); return value["type"].String();
if (value["list"].isNull()) if(!value["list"].isNull())
return defaultValue; {
return RandomGeneratorUtil::nextItem(value["list"].Vector(), rng)->String();
}
const auto & resourceList = value["list"].Vector(); if(!value["except"].isNull())
{
for(auto & s : value["except"].Vector())
valuesSet.erase(s.String());
}
if (resourceList.empty()) return valuesSet.empty() ? "" : *RandomGeneratorUtil::nextItem(valuesSet, rng);
return defaultValue;
si32 index = rng.getIntRange(0, resourceList.size() - 1 )();
return resourceList[index].String();
} }
TResources loadResources(const JsonNode & value, CRandomGenerator & rng) TResources loadResources(const JsonNode & value, CRandomGenerator & rng)