Exemplo n.º 1
0
    def __init__(
        self,
        id: int,
        name: str,
        game_map: GameMap,
        position: Optional[Position] = None,
        direction: Optional[Direction] = None,
        init: bool = True,
    ) -> None:
        self.id: int = id
        self.name = name
        self.game_map = game_map

        self.active = True  # True if the player is still playing
        self.killed = False  # When killed, player need to respawn
        self.score = 0.0
        self.stats: PlayerStats = PlayerStats()
        self.max_ticks_in_history = 10
        self.history: Deque[HistoryItem] = deque()

        self.spawn_position: Position = position or self.game_map.get_random_empty_position(
        )
        self.position: Optional[Position] = None

        self.spawn_direction: Direction = direction or dir_to_center(
            self.spawn_position, self.game_map.size)
        self.direction: Optional[Direction] = None

        self.tail: List[Position] = []
        if init:
            self.reset_position()
Exemplo n.º 2
0
    def test_set_stat(self):
        stats = PlayerStats()

        stats.set_stat(PlayerStats.NEMESIS, "abc")
        self.assertEqual("abc", stats.stats[PlayerStats.NEMESIS])

        # OK to set number for counted stats
        stats.set_stat(PlayerStats.CONQUERED, 123)
        self.assertEqual(123, stats.stats[PlayerStats.CONQUERED])

        # countable stats need a number
        with self.assertRaises(Exception):
            stats.set_stat(PlayerStats.KILLS, "abc")
Exemplo n.º 3
0
    def test_kill_player(self):
        stats = PlayerStats()

        player = "abc"

        stats.kill_player(player)
        self.assertEqual(1, stats.stats[PlayerStats.KILLS])
        self.assertEqual(1, stats.kills[player])
        self.assertEqual(1, len(stats.stats))
        self.assertEqual(0, len(stats.killed_by_players))

        stats.kill_player(player)
        self.assertEqual(2, stats.stats[PlayerStats.KILLS])
        self.assertEqual(2, stats.kills[player])

        player2 = "xyz"
        stats.kill_player(player2)
        self.assertEqual(3, stats.stats[PlayerStats.KILLS])
        self.assertEqual(1, stats.kills[player2])

        with self.assertRaises(Exception):
            stats.kill_player("")
Exemplo n.º 4
0
def dict_to_player_stats(data: Dict) -> PlayerStats:
    ps = PlayerStats()
    for k, v in data.items():
        if k == "players_killed":
            ps.kills = v
        elif k == "killed_by_players":
            ps.killed_by_players = v
        else:
            ps.set_stat(k, v)
    return ps
Exemplo n.º 5
0
 def __deepcopy__(self, memodict: Dict) -> PlayerState:
     gm = memodict[id(self.game_map)]
     obj = type(self)(self.id,
                      self.name,
                      gm,
                      self.spawn_position.copy(),
                      self.spawn_direction.copy(),
                      init=False)
     obj.active = self.active
     obj.killed = self.killed
     obj.score = self.score
     if self.position:
         obj.position = self.position.copy()
     if self.direction:
         obj.direction = self.direction.copy()
     obj.tail = self.tail.copy()
     obj.stats = PlayerStats(self.stats.stats.copy(),
                             self.stats.kills.copy(),
                             self.stats.killed_by_players.copy())
     obj.max_ticks_in_history = 0
     return obj
Exemplo n.º 6
0
    def test_killed_by_player(self):
        stats = PlayerStats()

        player = "b"

        stats.killed_by_player(player)
        self.assertEqual(1, stats.stats[PlayerStats.KILLED])
        self.assertEqual(1, stats.killed_by_players[player])
        self.assertEqual(player, stats.stats[PlayerStats.NEMESIS])
        self.assertEqual(2, len(stats.stats))
        self.assertEqual(0, len(stats.kills))

        player2 = "a"

        stats.killed_by_player(player2)
        stats.killed_by_player(player2)
        self.assertEqual(3, stats.stats[PlayerStats.KILLED])
        self.assertEqual(1, stats.killed_by_players[player])
        self.assertEqual(2, stats.killed_by_players[player2])
        self.assertEqual(player2, stats.stats[PlayerStats.NEMESIS])
        self.assertEqual(0, len(stats.kills))

        with self.assertRaises(Exception):
            stats.killed_by_player("")
Exemplo n.º 7
0
    def test_add_stat(self):
        stats = PlayerStats()

        # countable stats
        stats.add_stat(PlayerStats.KILLS)
        self.assertEqual(1, stats.stats[PlayerStats.KILLS])

        stats.add_stat(PlayerStats.KILLS)
        self.assertEqual(2, stats.stats[PlayerStats.KILLS])

        stats.add_stat(PlayerStats.KILLED)
        stats.add_stat(PlayerStats.PLANETS)
        stats.add_stat(PlayerStats.BLITZIUMS)
        stats.add_stat(PlayerStats.SUICIDES)
        stats.add_stat(PlayerStats.CONQUERED)

        # not countable:
        with self.assertRaises(Exception):
            stats.add_stat(PlayerStats.NEMESIS)