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 _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 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("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 createPlayerObject(cls, player_info): """ Returns a Player object using player_info :param player_info: (tuple) of (id, row, col, health, power, gold, num_bosses_defeated) :return: a new Player object """ id, row, col, health, power, luck, gold, num_bosses_defeated = player_info return Player(id, row, col, health, power, luck, gold, num_bosses_defeated)
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 handle_post(request): """ Handle POST requests :param request: request containing 'player_id' and 'action' inside the 'form' of the request :return: response based on request """ # Get arguments from the request action_info = request.get('form', {}) player_id = action_info['player_id'] action = action_info['action'] changed_game_objects = [] # list of game objects that have changed # Create a game from the database game = Deserialize.createGameFromDatabase() # If the player_id does not exist, create a new player and add it to the game if player_id not in game.id_to_players: # check if a record for the player exists already player = Player( player_id, num_boss_defeated=Deserialize.getNumBossesDefeated(player_id)) game.add_player(player) changed_game_objects.append(player) # Execute the player's action and keep track of game objects that changed changed_game_objects.extend(game.execute(**action_info)) # Update and store all game objects that have changed Serialize.updateGameObjects(changed_game_objects) game = Deserialize.createGameFromDatabase() # Get and return the response for an action if action == constants.Game.FIGHT_RESULT: return ResponseCreator(game).get_action_response_without_monster_info( player_id) else: return ResponseCreator(game).get_action_response(player_id)
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)
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)