| 6 | namespace Star { |
| 7 | |
| 8 | StatCollection::StatCollection(Json const& config) { |
| 9 | for (auto const& stat : config.getObject("stats", {})) |
| 10 | m_stats.addStat(stat.first, stat.second.getFloat("baseValue", 0.0)); |
| 11 | |
| 12 | for (auto const& resource : config.getObject("resources", {})) { |
| 13 | auto statOrValue = [&resource](String const& statName, String const& valueName, MVariant<String, float> def = {}) -> MVariant<String, float> { |
| 14 | if (auto maxStat = resource.second.optString(statName)) |
| 15 | return *maxStat; |
| 16 | else if (auto maxValue = resource.second.optFloat(valueName)) |
| 17 | return *maxValue; |
| 18 | else |
| 19 | return def; |
| 20 | }; |
| 21 | |
| 22 | MVariant<String, float> resourceMax = statOrValue("maxStat", "maxValue"); |
| 23 | MVariant<String, float> resourceDelta = statOrValue("deltaStat", "deltaValue"); |
| 24 | m_stats.addResource(resource.first, resourceMax, resourceDelta); |
| 25 | |
| 26 | if (auto initialValue = resource.second.optFloat("initialValue")) { |
| 27 | m_stats.setResourceValue(resource.first, *initialValue); |
| 28 | m_defaultResourceValues[resource.first] = makeLeft(*initialValue); |
| 29 | } else if (auto percentage = resource.second.optFloat("initialPercentage")) { |
| 30 | m_stats.setResourcePercentage(resource.first, *percentage); |
| 31 | m_defaultResourceValues[resource.first] = makeRight(*percentage); |
| 32 | } else { |
| 33 | if (m_stats.resourceMax(resource.first)) { |
| 34 | m_stats.setResourcePercentage(resource.first, 1.0f); |
| 35 | m_defaultResourceValues[resource.first] = makeRight(1.0f); |
| 36 | } else { |
| 37 | m_stats.setResourceValue(resource.first, 0.0f); |
| 38 | m_defaultResourceValues[resource.first] = makeLeft(0.0f); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | addNetElement(&m_statModifiersNetState); |
| 44 | |
| 45 | // Sort resource names alphabetically to ensure the same order on master and |
| 46 | // slaves. |
| 47 | for (auto const& resource : m_stats.resourceNames().sorted()) { |
| 48 | auto& resourceNetState = m_resourceValuesNetStates[resource]; |
| 49 | addNetElement(&resourceNetState); |
| 50 | addNetElement(&m_resourceLockedNetStates[resource]); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | StringList StatCollection::statNames() const { |
| 55 | return m_stats.effectiveStatNames(); |
nothing calls this directly
no test coverage detected