public static Creature fromJSONObject(JSONObject json) { int creatureId = (int)json.GetField("creatureId").n; string creatureName = json.GetField("creatureName").str; CreatureStats baseStats = CreatureStats.fromJSONObject(json.GetField("baseStats")); //TODO deserialize Abilities Ability ability = null; HashSet <Move> moves = new HashSet <Move>(); foreach (JSONObject moveName in json.GetField("moves").list) { string n = moveName.str; moves.Add(MoveLibrary.get(n)); } FocalPoints focalPoints = FocalPoints.fromJSONObject(json.GetField("focalPoints")); CreatureForm baseForm = CreatureForm.fromJSONObject(json.GetField("baseForm")); List <CreatureForm> availableForms = new List <CreatureForm>(); foreach (JSONObject formJSON in json.GetField("availableForms").list) { CreatureForm creatureForm = CreatureForm.fromJSONObject(formJSON); availableForms.Add(CreatureForm.fromJSONObject(formJSON)); } Creature result = new Creature(creatureId, creatureName, moves, focalPoints, ability, baseStats, baseForm, availableForms); return(result); }
public void testCreatureDeserialization() { LibraryLoader.loadMoveLibrary(); string creatureJSONString = "{\"creatureId\":1,\n\"creatureName\":\"\",\n\"baseStats\":[10,10,10,10],\n\"abilityId\":1,\n\"moves\":[\"Strike\",\"Slam\"],\n\"focalPoints\":[\n{\n\"description\":\"Small boost to strength\",\n\"statMods\":{\n\"STR\":[5,1]\n}\n},\n{\n\"description\":\"Small boost to armor\",\n\"statMods\":{\n\"ARM\":[5, 1]\n}\n},\n{\n\"description\":\"Additional armor scaling\",\n\"statMods\":{\n\"ARM\":[0, 1.1]\n}\n},\n{\n\"description\":\"Small boost to speed\",\n\"statMods\":{\n\"SPD\":[5, 1]\n}\n}\n],\n\"baseForm\":{\n\"creatureTypes\":[\"Vital\"],\n\"abilityId\":1,\n\"revealAction\":null\n},\n\"availableForms\":[\n{\n\"creatureTypes\":[\"Flame\"],\n\"statMods\":{\n\"STR\":[5, 1],\n\"ARM\":[0, 1],\n\"SPD\":[0, 1]\n},\n\"moveId\":null,\n\"abilityId\":1,\n\"revealAction\":null\n}\n]\n}"; JSONObject creatureJSON = JSONObject.Create(creatureJSONString); Creature actual = Creature.fromJSONObject(creatureJSON); HashSet <Move> expectedMoves = new HashSet <Move>(); expectedMoves.Add(MoveLibrary.get("Strike")); expectedMoves.Add(MoveLibrary.get("Slam")); StatModifier sm1 = new StatModifier(5, 1); StatModifier sm2 = new StatModifier(0, 1.1); StatModifier sm3 = new StatModifier(0, 1); FocusPoint fp1 = new FocusPoint(null, null, makeListOfStatMod(StatName.STR, sm1), "Small boost to strength"); FocusPoint fp2 = new FocusPoint(null, null, makeListOfStatMod(StatName.ARM, sm1), "Small boost to armor"); FocusPoint fp3 = new FocusPoint(null, null, makeListOfStatMod(StatName.ARM, sm2), "Additional armor scaling"); FocusPoint fp4 = new FocusPoint(null, null, makeListOfStatMod(StatName.SPD, sm1), "Small boost to speed"); FocalPoints expectedFocalPoints = new FocalPoints(fp1, fp2, fp3, fp4); CreatureStats expectedBaseStats = new CreatureStats(10, 10, 10, 10); Ability expectedAbility = null; HashSet <CreatureType> expectedTypesBase = new HashSet <CreatureType>(); expectedTypesBase.Add(CreatureType.VITAL); CreatureForm expectedBaseForm = new CreatureForm(expectedTypesBase, null, null); HashSet <CreatureType> expectedTypesForm = new HashSet <CreatureType>(); expectedTypesForm.Add(CreatureType.FLAME); List <Pair <StatName, StatModifier> > expectedFormMods = new List <Pair <StatName, StatModifier> >(); expectedFormMods.Add(new Pair <StatName, StatModifier>(StatName.STR, sm1)); expectedFormMods.Add(new Pair <StatName, StatModifier>(StatName.ARM, sm3)); expectedFormMods.Add(new Pair <StatName, StatModifier>(StatName.SPD, sm3)); CreatureForm expectedAltForm = new CreatureForm(expectedTypesForm, expectedFormMods, null, null, null); //"availableForms\":[\n{\n\"creatureTypes\":[\"Flame\"],\n\"statMods\":{\n\"STR\":[5, 1],\n\"ARM\":[0, 1],\n\"SPD\":[0, 1]\n},\n\"moveId\":null,\n\"abilityId\":1,\n\"revealAction\":null\n}\n] List <CreatureForm> expectedAvailableForms = new List <CreatureForm>(); expectedAvailableForms.Add(expectedAltForm); foreach (Pair <StatName, StatModifier> pair in expectedAltForm.statMods) { Debug.Log("Alt form mod: " + pair.ToString()); } Creature expected = new Creature(1, "", expectedMoves, expectedFocalPoints, expectedAbility, expectedBaseStats, expectedBaseForm, expectedAvailableForms); Assert.That(expected.Equals(actual)); }