def testGetSurroundingEntities(self): Database.create_game_object_tables(test_database) # Assert there's nothing in them right now self.assertEqual([], Database.get_all_players(test_database)) self.assertEqual([], Database.get_all_monsters(test_database)) # Create players and monsters to serialize players = [ Player("One", 3, 0, 1000, 5, 0, 0), Player("Two", 1, 9, 1, 999, 1, 100), Player("Three", 0, 5, 50, 50, 50, 50), Player("Four", 9, 3, 1000, 5, 0, 0), Player("Five", 3, 2, 1000, 5, 0, 0), Player("Six", 1, 7, 1, 5, 1, 50), Player("Seven", 3, 5, 1000, 5, 0, 0), Player("Eight", 5, 3, 1000, 5, 0, 0), ] monsters = [ Monster("NotAMonster", 2, 1, 10, 1, False, {}), Monster("NotABoss", 0, 1, 10, 5, True, {"NotZe"}), ] new_game = Game(10, 10, players, monsters) self.assertEqual((1000, 5, 0, 0, 0), new_game.get_player_stats("One")) self.assertEqual((1, 999, 1, 100, 0), new_game.get_player_stats("Two")) self.assertEqual((50, 50, 50, 50, 0), new_game.get_player_stats("Three")) self.assertEqual((1000, 5, 0, 0, 0), new_game.get_player_stats("Four")) self.assertEqual((1, 5, 1, 50, 0), new_game.get_player_stats("Six")) game_objects = players + monsters Serialize.updateGameObjects(game_objects, test_database) # delete all tables created in this test Database.delete_tables(test_database)
def testUpdateGameObjects(self): # Create the tables Database.create_game_object_tables(test_database) # Assert there's nothing in them right now self.assertEqual([], Database.get_all_players(test_database)) self.assertEqual([], Database.get_all_monsters(test_database)) # Create players and monsters to serialize players = [ Player("NotZe", 2, 4, 1000, 5, 0, 0, 0), Player("DefinitelyZe", 2, 1, 1, 999, 1, 100, 0) ] monsters = [ Monster("NotAMonster", 2, 1, 10, 1, False, {}), Monster("NotABoss", 0, 1, 10, 5, True, {"NotZe"}) ] game_objects = players + monsters # Serialize and check if database is updated Serialize.updateGameObjects(game_objects, test_database) self.assertEqual([('NotAMonster', 2, 1, 10, 1, 'False', 'set()'), ('NotABoss', 0, 1, 10, 5, 'True', "{'NotZe'}")], Database.get_all_monsters(test_database)) self.assertEqual([('NotZe', 2, 4, 1000, 5, 0, 0, 0), ('DefinitelyZe', 2, 1, 1, 999, 1, 100, 0)], Database.get_all_players(test_database)) # delete all tables created in this test Database.delete_tables(test_database)
def testUpdateGameObjects(self): # Create the tables Database.create_game_object_tables(test_database) # Assert there's nothing in them right now self.assertEqual([], Database.get_all_players(test_database)) self.assertEqual([], Database.get_all_monsters(test_database)) # Create players and monsters to serialize players = [ Player("MoveLeftWall", 3, 0, 1, 1000, 5, 0, 0), Player("MoveRightWall", 1, 9, 2, 1, 999, 1, 100), Player("MoveUpWall", 0, 5, 3, 1000, 5, 0, 0), Player("MoveDownWall", 9, 3, 4, 1000, 5, 0, 0), Player("MoveLeft", 3, 2, 5, 1000, 5, 0, 0), Player("MoveRight", 1, 7, 6, 1, 999, 1, 100), Player("MoveUp", 3, 5, 7, 1000, 5, 0, 0), Player("MoveDown", 5, 3, 8, 1000, 5, 0, 0), ] monsters = [ Monster("NotAMonster", 2, 1, 10, 1, False, {}), Monster("NotABoss", 0, 1, 10, 5, True, {"NotZe"}) ] new_game = Game(10, 10, players, monsters) game_objects = [] game_objects.extend( new_game.execute(player_id="MoveLeftWall", action="left")) game_objects.extend( new_game.execute(player_id="MoveRightWall", action="right")) game_objects.extend( new_game.execute(player_id="MoveUpWall", action="up")) game_objects.extend( new_game.execute(player_id="MoveDownWall", action="down")) game_objects.extend( new_game.execute(player_id="MoveLeft", action="left")) game_objects.extend( new_game.execute(player_id="MoveRight", action="right")) game_objects.extend(new_game.execute(player_id="MoveUp", action="up")) game_objects.extend( new_game.execute(player_id="MoveDown", action="down")) game_objects = game_objects + monsters # Serialize and check if database is updated Serialize.updateGameObjects(game_objects, test_database) self.assertEqual([('NotAMonster', 2, 1, 10, 1, 'False', 'set()'), ('NotABoss', 0, 1, 10, 5, 'True', "{'NotZe'}")], Database.get_all_monsters(test_database)) self.assertEqual([('MoveLeftWall', 3, 0, 1, 1000, 5, 0, 0), ('MoveRightWall', 1, 9, 2, 1, 999, 1, 100), ('MoveUpWall', 0, 5, 3, 1000, 5, 0, 0), ('MoveDownWall', 9, 3, 4, 1000, 5, 0, 0), ('MoveLeft', 3, 1, 5, 1000, 5, 0, 0), ('MoveRight', 1, 8, 6, 1, 999, 1, 100), ('MoveUp', 2, 5, 7, 1000, 5, 0, 0), ('MoveDown', 6, 3, 8, 1000, 5, 0, 0)], Database.get_all_players(test_database)) # delete all tables created in this test Database.delete_tables(test_database)
def _create_expected_game(self): players = [ Player("Ze", 4, 4, 5, 5, 0, 0), Player("Teemo", 2, 1, 10, 100, 1, 5) ] monsters = [ Monster("A_monster", 2, 1, 10, 5, False, {"Teemo"}), Monster("Boss", 0, 1, 10, 5, True, {}) ] rows = 5 columns = 5 return Game(rows, columns, players, monsters)
def createMonsterObject(cls, monster_info): """ Returns a Monster object using monster_info :param monster_info: (tuple) of (id, row, col, health, power, is_boss, defeated_by) :return: a new Monster object """ id, row, col, health, power, is_boss, defeated_by = monster_info is_boss = eval(is_boss) # is_boss was a string of "True" or "False" defeated_by = eval(defeated_by) # convert from string to set return Monster(id, row, col, health, power, is_boss, defeated_by)
def test_get_server_map(self): rows = 5 columns = 5 players = [ Player("Lucian", 0, 0), Player("Caitlyn", 1, 1), Player("Teemo", 2, 4), Player("Ze", 4, 2) ] monsters = [ Monster("Baron", 0, 0, 9001, 200), Monster("Dragon", 1, 2, 1000, 100), Monster("Raptor", 4, 3, 100, 5) ] game = Game(rows, columns, players, monsters) expected = '<xmp>---------------------\n| 0| 1| 2| 3| 4|\n---------------------\n| 5| 6| 7| 8| 9|\n---------------------\n| 10| 11| 12| 13| 14|\n---------------------\n| 15| 16| 17| 18| 19|\n---------------------\n| 20| 21| 22| 23| 24|\n---------------------\n\n[0]:\n\tPlayer(s):\n\t\t(id: Lucian, health: 10, power: 5, luck: 5, gold: 0, bosses defeated: 0)\n\tMonster(s):\n\t\t(id: Baron, health: 9001, power: 200, is boss: False, defeated by: set())\n[6]:\n\tPlayer(s):\n\t\t(id: Caitlyn, health: 10, power: 5, luck: 5, gold: 0, bosses defeated: 0)\n[7]:\n\tMonster(s):\n\t\t(id: Dragon, health: 1000, power: 100, is boss: False, defeated by: set())\n[14]:\n\tPlayer(s):\n\t\t(id: Teemo, health: 10, power: 5, luck: 5, gold: 0, bosses defeated: 0)\n[22]:\n\tPlayer(s):\n\t\t(id: Ze, health: 10, power: 5, luck: 5, gold: 0, bosses defeated: 0)\n[23]:\n\tMonster(s):\n\t\t(id: Raptor, health: 100, power: 5, is boss: False, defeated by: set())</xmp>' result = game.get_server_map() self.assertEqual(expected, result)
def testGetSurroundingEntities(self): Database.create_game_object_tables(test_database) # Assert there's nothing in them right now self.assertEqual([], Database.get_all_players(test_database)) self.assertEqual([], Database.get_all_monsters(test_database)) # Create players and monsters to serialize players = [ Player("One", 3, 0, 1000, 5, 0, 0), Player("OneNeighbor", 4, 0, 100, 5, 0, 0), Player("Two", 1, 9, 1, 999, 1, 100), Player("Three", 0, 5, 1000, 5, 0, 0), Player("Four", 9, 3, 1000, 5, 0, 0), Player("Five", 3, 2, 1000, 5, 0, 0), Player("Six", 1, 7, 1, 999, 1, 100), Player("Seven", 3, 5, 1000, 5, 0, 0), Player("Eight", 5, 3, 1000, 5, 0, 0), Player("Nine", 0, 1, 1000, 5, 0, 0), Player("Ten", 0, 1, 1000, 5, 0, 0) ] monsters = [ Monster("NotAMonster", 2, 1, 10, 1, False, {}), Monster("NotABoss", 0, 1, 10, 5, True, {"NotZe", "Ten"}), Monster("OnTopOfEight", 5, 3, 3, 3, True, {"One"}) ] new_game = Game(10, 10, players, monsters) response_creator = ResponseCreator(new_game) self.assertEqual("XX,__,M_,XX,_P,__,XX,_P,__,", response_creator.get_surrounding_entities("One")) self.assertEqual("__,__,__,__,_P,__,__,__,__,", response_creator.get_surrounding_entities("Seven")) self.assertEqual("XX,XX,XX,__,BP,__,__,__,__,", response_creator.get_surrounding_entities("Nine")) self.assertEqual("XX,XX,XX,__,_P,__,__,__,__,", response_creator.get_surrounding_entities("Ten")) self.assertEqual("__,__,__,__,BP,__,__,__,__,", response_creator.get_surrounding_entities("Eight")) game_objects = players + monsters Serialize.updateGameObjects(game_objects, test_database) # delete all tables created in this test Database.delete_tables(test_database)
def create_default_monsters(): """ :return: list of default monsters """ with open(constants.Database.MONSTER_PATH) as monster_file: monster_data = json.load(monster_file) monsters = [] for monster_info in monster_data['monsters']: id = monster_info['id'] row = monster_info['row'] col = monster_info['col'] health = monster_info['health'] power = monster_info['power'] is_boss = monster_info['is_boss'] defeated_by = set(monster_info['defeated_by']) monsters.append( Monster(id, row, col, health, power, is_boss, defeated_by)) return monsters
return None def get_server_map(self): return ResponseCreator(self).get_server_map() def __str__(self): return self.get_server_map() if __name__ == '__main__': # Run this code if testing is true, set to off otherwise! (real test cases are probably better :)) if constants.TESTING: # Code for creating a dummy game and printing it from containers.player import Player from containers.monster import Monster rows = 5 columns = 5 players = [ Player("Lucian", 0, 0), Player("Caitlyn", 1, 1), Player("Teemo", 2, 4), Player("Ze", 4, 2) ] monsters = [ Monster("Baron", 0, 0, 9001, 200), Monster("Dragon", 1, 2, 1000, 100), Monster("Raptor", 4, 3, 100, 5) ] game = Game(rows, columns, players, monsters) print(game)