Browse Source

Bonuse::toJsonNode uses string constants for type/subtype

Henning Koehler 8 years ago
parent
commit
05838b3827
3 changed files with 39 additions and 6 deletions
  1. 12 0
      Global.h
  2. 25 4
      lib/HeroBonus.cpp
  3. 2 2
      lib/JsonNode.cpp

+ 12 - 0
Global.h

@@ -369,6 +369,18 @@ namespace vstd
 		return std::find(c.begin(),c.end(),i);
 	}
 
+	//returns first key that maps to given value if present, returns success via found if provided
+	template <typename Key, typename T>
+	Key findKey(const std::map<Key, T> & map, const T & value, bool * found = nullptr)
+	{
+		for(auto iter = map.cbegin(); iter != map.cend(); iter++)
+		{
+			if(iter->second == value)
+				return iter->first;
+		}
+		return Key();
+	}
+
 	//removes element i from container c, returns false if c does not contain i
 	template <typename Container, typename Item>
 	typename Container::size_type operator-=(Container &c, const Item &i)

+ 25 - 4
lib/HeroBonus.cpp

@@ -20,6 +20,7 @@
 #include "CSkillHandler.h"
 #include "CStack.h"
 #include "CArtHandler.h"
+#include "StringConstants.h"
 
 #define FOREACH_PARENT(pname) 	TNodes lparents; getParents(lparents); for(CBonusSystemNode *pname : lparents)
 #define FOREACH_CPARENT(pname) 	TCNodes lparents; getParents(lparents); for(const CBonusSystemNode *pname : lparents)
@@ -1163,17 +1164,37 @@ std::string Bonus::Description() const
 	return str.str();
 }
 
+JsonNode subtypeToJson(Bonus::BonusType type, int subtype)
+{
+	JsonNode node;
+
+	switch(type)
+	{
+	case Bonus::PRIMARY_SKILL:
+		node.String() = PrimarySkill::names[subtype];
+		break;
+	case Bonus::SECONDARY_SKILL_PREMY:
+		node.String() = NSecondarySkill::names[subtype];
+		break;
+	default:
+		node.Integer() = subtype;
+		break;
+	}
+
+	return node;
+}
+
 JsonNode Bonus::toJsonNode() const
 {
 	JsonNode root(JsonNode::DATA_STRUCT);
 
-	root["type"].Float() = type;
+	root["type"].String() = vstd::findKey(bonusNameMap, type);
 	if(subtype != -1)
-		root["subtype"].Float() = subtype;
+		root["subtype"] = subtypeToJson(type, subtype);
 	if(val != 0)
-		root["val"].Float() = val;
+		root["val"].Integer() = val;
 	if(valType != ADDITIVE_VALUE)
-		root["valType"].Float() = valType;
+		root["valType"].String() = vstd::findKey(bonusValueMap, valType);
 	if(limiter)
 		root["limiter"] = limiter->toJsonNode();
 	if(updater)

+ 2 - 2
lib/JsonNode.cpp

@@ -611,9 +611,9 @@ bool JsonUtils::parseBonus(const JsonNode &ability, Bonus *b)
 		{
 			std::shared_ptr<ScalingUpdater> updater = std::make_shared<ScalingUpdater>();
 			const JsonVector param = updaterJson["parameters"].Vector();
-			updater->valPer20 = param[0].Float();
+			updater->valPer20 = param[0].Integer();
 			if(param.size() > 1)
-				updater->stepSize = param[1].Float();
+				updater->stepSize = param[1].Integer();
 			b->addUpdater(updater);
 		}
 		else