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 test_kill_player(self): # fmt: off gm = create_map_with([[E, E, E], [E, E, E], [1, 1, 1]]) # fmt: on p1 = PlayerState(id=1, name="1", game_map=gm, position=Position(1, 1)) gs = GameState(gm, [p1]) p1.tail = [Position(1, 1), Position(1, 2)] p1.position = p1.tail[-1] gs.kill_player(p1) self.assertEqual(p1.tail, [p1.spawn_position]) self.assertEqual(p1.position, p1.spawn_position) self.assertTrue(p1.killed) self.assertEqual(0, p1.stats.stats[PlayerStats.CONQUERED])
def test_relocate_item(self): # fmt: off gm = create_map_with([[1, E, E], [E, W, E], [E, E, E]]) # fmt: on ps = PlayerState(id=1, name="dummy", game_map=gm, position=Position(1, 1)) gs = GameState(gm, [ps]) ps.position = Position(3, 1) ps.tail = [Position(2, 1), Position(3, 1)] asteroids_pos = Position(2, 2) self.assertTrue(gm.is_asteroids(asteroids_pos)) planet_pos = Position(3, 3) gm.set_tile(planet_pos, GameMap.PLANET) self.assertTrue(gm.is_planet(planet_pos)) black_hole_pos = Position(1, 2) gm.set_tile(black_hole_pos, GameMap.BLACK_HOLE) self.assertTrue(gm.is_black_hole(black_hole_pos)) blitzium_pos = Position(1, 3) gm.set_tile(blitzium_pos, GameMap.BLITZIUM) self.assertTrue(gm.is_blitzium(blitzium_pos)) # Cannot relocate asteroids and planets with self.assertRaises(Exception): gs.relocate_item(asteroids_pos) with self.assertRaises(Exception): gs.relocate_item(planet_pos) # relocate black holes and blitziums for i in range(30): new_black_hole_pos = gs.relocate_item(black_hole_pos) self.assertTrue(new_black_hole_pos not in list( {asteroids_pos, planet_pos, black_hole_pos, blitzium_pos}) + ps.tail) black_hole_pos = new_black_hole_pos new_blitzium_pos = gs.relocate_item(blitzium_pos) self.assertTrue(new_blitzium_pos != blitzium_pos)
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)