コード例 #1
0
ファイル: utils.py プロジェクト: Martimax21/Blitz2020
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
コード例 #2
0
    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)