def test_is_closed_tail(self): # fmt: off gm = create_map_with([[1, E, 1], [E, E, E], [1, E, E]]) # fmt: on # player without tail player = PlayerState(id=1, name="1", game_map=gm, position=Position(1, 1)) self.assertEqual(player.tail, [Position(1, 1)]) self.assertFalse(GameState.is_closed_tail(player, gm)) # player with tail player.tail = [Position(1, 1), Position(2, 1)] self.assertFalse(GameState.is_closed_tail(player, gm)) player.tail = [Position(1, 1), Position(2, 1), Position(3, 1)] player.direction = Direction(Direction.UP) self.assertTrue(GameState.is_closed_tail(player, gm)) player.tail = [Position(1, 1), Position(2, 1), Position(2, 2)] self.assertFalse(GameState.is_closed_tail(player, gm)) player.tail = [ Position(1, 1), Position(2, 1), Position(2, 2), Position(1, 2), Position(1, 1) ] self.assertTrue(GameState.is_closed_tail(player, gm))
def test_reset_position(self): game_map = GameMap(5) pos = Position(1, 1) ps = PlayerState(id=1, name="dummy", game_map=game_map, position=pos, direction=Direction(Direction.DOWN)) ps.direction = Direction(Direction.LEFT) ps.reset_position() self.assertEqual(ps.direction, Direction(Direction.DOWN))
def test_move_player(self): gm = GameMap(5) player = PlayerState(id=1, name="1", game_map=gm, position=Position(1, 1)) player.direction = Direction(Direction.RIGHT) gs = GameState(gm, [player]) gs.move_player(player, Action.FORWARD) gs.move_player(player, Action.TURN_RIGHT) gs.move_player(player, Action.TURN_RIGHT) gs.move_player(player, Action.TURN_LEFT) self.assertEqual(Position(1, 3), player.position)
def test_fill_kill_other_player_tail(self): # fmt: off game_map = create_map_with([[E, E, E, E], [E, E, E, E], [0, 0, 0, 0], [E, 1, E, E]]) # fmt: on p0 = PlayerState(id=0, name="dummy", game_map=game_map, position=Position(1, 3)) p1 = PlayerState(id=1, name="dummy", game_map=game_map, position=Position(2, 4)) gs = GameState(game_map, [p0, p1]) p0.tail = [ p0.spawn_position.copy(), Position(1, 2), Position(1, 1), Position(2, 1), Position(3, 1), Position(4, 1), Position(4, 2), ] p0.position = p0.tail[-1].copy() p0.direction = Direction(Direction.DOWN) p1.tail = [ p1.spawn_position.copy(), Position(2, 3), Position(2, 2), Position(3, 2), Position(3, 3), Position(3, 4), ] p1.position = p1.tail[-1].copy() p1.direction = Direction(Direction.DOWN) # print(game_state_to_dict(0, 0, gs, 999)['game']['pretty_map']) gs.apply_action(0, p0, Action.FORWARD) # print(game_state_to_dict(0, 0, gs, 999)['game']['pretty_map']) self.assertTrue(p1.killed)
def test_conquer_some_player_tile(self): # fmt: off gm = create_map_with([ [1, 1, E], [1, 2, 2], [E, 2, 2], ]) # fmt: on p1 = PlayerState(id=1, name="p1", game_map=gm, position=Position(1, 1)) p2 = PlayerState(id=2, name="p2", game_map=gm, position=Position(2, 2)) gs = GameState(gm, [p1, p2]) p1.position = Position(2, 2) p1.tail = [Position(1, 2), Position(2, 2)] p2.position = Position(2, 3) p2.tail = [Position(2, 3)] p1.direction = Direction(Direction.RIGHT) gs.apply_action(0, p1, Action.TURN_LEFT) self.assertFalse(p2.killed) self.assertFalse(p1.killed)
def test_respawn_on_someone_tail(self): gm = GameMap(5) p1 = PlayerState(id=1, name="p1", game_map=gm, position=Position(1, 1)) p2 = PlayerState(id=2, name="p2", game_map=gm, position=Position(1, 2)) gs = GameState(gm, [p1, p2]) # p1: spawn position = 1,1, current position=3,1, tail [2,1 and 3,1] # p2: spawn position = 1,2, current position=2,2, tail [1,1, 1,2 and 2,2] p1.position = Position(3, 1) p1.tail = [Position(2, 1), Position(3, 1)] gm.set_tile(Position(1, 1), GameMap.EMPTY, 2) p2.position = Position(2, 2) p2.tail = [Position(1, 1), Position(1, 2), Position(2, 2)] p2.direction = Direction(Direction.UP) gs.apply_action(0, p2, Action.FORWARD) self.assertTrue(p1.killed) self.assertFalse(p2.killed) # respawn gs.respawn_player(p1) self.assertFalse(p1.killed) self.assertTrue(p2.killed)
def dict_to_player_state(data: Dict, game_map: GameMap) -> PlayerState: position = dict_to_position(data["position"]) ps = PlayerState(id=data["id"], name=data["name"], game_map=game_map, position=position, init=False) ps.active = data["active"] ps.killed = data["killed"] ps.position = position ps.spawn_position = dict_to_position(data["spawn_position"]) ps.direction = Direction(data["direction"]) ps.spawn_direction = Direction(data["spawn_direction"]) ps.tail = [dict_to_position(p) for p in data["tail"]] ps.score = data["score"] ps.stats = dict_to_player_stats(data["stats"]) for hist in data["history"]: dt = datetime.fromisoformat(hist["timestamp"]) ps.history.append( HistoryItem(game_tick=hist["tick"], message=hist["message"], ts=dt)) return ps
def test_game_state_to_json_player(self): gm = create_map_with([[E, E, E], [E, E, E], [E, E, E]]) gm.set_tile(Position(1, 3), GameMap.BLACK_HOLE) gm.set_tile(Position(2, 3), GameMap.BLITZIUM) gm.set_tile(Position(3, 3), GameMap.PLANET) gm.set_tile(Position(3, 2), GameMap.PLANET, 0) p0 = PlayerState(0, "p0", gm, Position(1, 1), direction=Direction(Direction.UP)) p0.killed = True p0.position = Position(3, 1) p0.direction = Direction(Direction.RIGHT) p0.tail = [p0.spawn_position, Position(2, 1), p0.position] p0.score = 123.4 p0.stats.kill_player("p1") p0.stats.killed_by_player("p2") p0.stats.add_stat(PlayerStats.SUICIDES) p0.history.append( HistoryItem(11, "message-11", datetime.datetime(1900, 1, 1, 13, 14, 15, 555))) p0.history.append( HistoryItem(10, "message-10", datetime.datetime(1900, 1, 1, 13, 14, 15, 444))) p1 = PlayerState(1, "p1", gm, position=Position(1, 2), direction=Direction(Direction.DOWN)) p1.active = False p1.direction = Direction(Direction.LEFT) p2 = PlayerState(2, "p2", gm, Position(2, 2), direction=Direction(Direction.RIGHT)) p2.direction = Direction(Direction.LEFT) gs = GameState(gm, [p0, p1, p2]) self.maxDiff = 4000 expected = { "type": "tick", "game": { "map": [ ["W", "W", "W", "W", "W"], ["W", "C-0", " ", " ", "W"], ["W", "C-1", "C-2", "%-0", "W"], ["W", "!", "$", "%", "W"], ["W", "W", "W", "W", "W"], ], "player_id": 1, # fmt: off "pretty_map": '[[W W W W W ]\n' ' [W C0T0 T0 P0 W ]\n' ' [W C1P1 C2P2 %0 W ]\n' ' [W ! $ % W ]\n' ' [W W W W W ]]\n' 'DISCLAMER: this map does not have the same content as ' 'the json game map. Symbols are combined to help you ' 'visualize every spot on this turn. Hyphen are also ' 'removed. See the documentation for the json map ' 'symbol signification.\n' 'Symbols added or modified on this map for ' 'visualization purpose are:\n' 'Px: Position of player x - Cx: Conquered by player x ' '- Tx: Tail of player x', # fmt: on "tick": 123, "ticks_left": 500, }, "players": [ { "active": True, "killed": True, "position": { "x": 3, "y": 1 }, "spawn_position": { "x": 1, "y": 1 }, "direction": "RIGHT", "spawn_direction": "UP", "tail": [{ "x": 1, "y": 1 }, { "x": 2, "y": 1 }, { "x": 3, "y": 1 }], "id": 0, "name": "p0", "score": 123.4, "stats": { PlayerStats.CONQUERED: 1, PlayerStats.KILLS: 1, PlayerStats.KILLED: 1, PlayerStats.SUICIDES: 1, PlayerStats.NEMESIS: "p2", "players_killed": { "p1": 1 }, "killed_by_players": { "p2": 1 }, }, "history": [ { "timestamp": "1900-01-01T13:14:15.000555", "tick": 11, "message": "message-11" }, { "timestamp": "1900-01-01T13:14:15.000444", "tick": 10, "message": "message-10" }, ], }, { "active": False, "killed": False, "position": { "x": 1, "y": 2 }, "spawn_position": { "x": 1, "y": 2 }, "direction": "LEFT", "spawn_direction": "DOWN", "tail": [{ "x": 1, "y": 2 }], "id": 1, "name": "p1", "score": 0, "stats": { PlayerStats.CONQUERED: 1, "players_killed": {}, "killed_by_players": {} }, "history": [], }, { "active": True, "killed": False, "position": { "x": 2, "y": 2 }, "spawn_position": { "x": 2, "y": 2 }, "direction": "LEFT", "spawn_direction": "RIGHT", "tail": [{ "x": 2, "y": 2 }], "id": 2, "name": "p2", "score": 0, "stats": { PlayerStats.CONQUERED: 1, "players_killed": {}, "killed_by_players": {} }, "history": [], }, ], } # players have an id message = game_state_to_json(game_tick=123, player_id=1, game_state=gs, ticks_left=500) # print(message) data = json.loads(message) self.assertEqual(expected, data)