def test_from_json(self): json_string = { 'class': 'Weapon', 'name': 'The Axe of Destiny', 'damage': 20 } self.assertEqual(Weapon.from_json(json_string), self.weapon)
def extract_treasure(self, treasure_dict): if treasure_dict["class"] == 'Weapon': return Weapon.from_json(treasure_dict) if treasure_dict["class"] == 'Spell': return Spell.from_json(treasure_dict) if treasure_dict["class"] == 'Potion': return treasure_dict raise Exception('Invalid treasure_dict!')
def from_json(cls, json_dict): player = PlayerMixin(health=json_dict['health'], mana=json_dict['mana']) weapon = Weapon.from_json(json_dict['weapon']) if json_dict['weapon'] is not None\ else None spell = Spell.from_json(json_dict['spell']) if json_dict['spell'] is not None\ else None player.equip(weapon) player.learn(spell) return player
def setUp(self): self.spell = Spell(name="Fireball", damage=5, mana_cost=20, cast_range=1) self.weapon = Weapon(name="The Axe of Destiny", damage=10) self.hero = Hero(name="Bron", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2) self.map = [['S', '.', '#', '#', '.', '.', '.', '.', '.', 'T'], ['#', 'T', '#', '#', '.', '.', '#', '#', '#', '.'], ['#', '.', '#', '#', '#', 'E', '#', '#', '#', 'E'], ['#', '.', 'E', '.', '.', '.', '#', '#', '#', '.'], ['#', '#', '#', 'T', '#', 'T', '#', '#', '#', 'G']] self.enemies = { "2,5": { "damage": 10, "health": 40, "mana": 20, "weapon": None, "spell": self.spell.to_json() }, "2,9": { "damage": 10, "health": 40, "mana": 20, "weapon": self.weapon.to_json(), "spell": None }, "3,2": { "damage": 10, "health": 40, "mana": 20, "weapon": None, "spell": None } } self.enemy1 = Enemy.from_json(self.enemies['2,5']) self.enemy2 = Enemy.from_json(self.enemies['2,9']) self.enemy3 = Enemy.from_json(self.enemies['3,2']) self.treasures = { "1,1": { "class": "Potion", "type": "mana", "amount": 50 }, "0,9": { "class": "Potion", "type": "health", "amount": 50 }, "4,3": { "class": "Weapon", "name": "The Axe of Destiny", "damage": 10 }, "4,5": { 'class': "Spell", 'name': 'Fireball', 'damage': 5, 'mana_cost': 20, 'cast_range': 1 } } self.tr1 = self.treasures['0,9'] self.tr2 = self.treasures['1,1'] self.tr3 = Weapon.from_json(self.treasures['4,3']) self.tr4 = Spell.from_json(self.treasures['4,5']) self.json_data = { "map": [ "S.##.....T", "#T##..###.", "#.###E###E", "#.E...###.", "###T#T###G" ], "enemies": self.enemies, "treasures": self.treasures } self.dungeon = Dungeon(self.json_data)