|
6 | 6 |
|
7 | 7 | class LivePlayerManager(PlayerManager): |
8 | 8 |
|
9 | | - def register_player_name_on_tag_change(self, player, tag, value): |
10 | | - """ |
11 | | - Triggers on every TAG_CHANGE where the corresponding entity is a LazyPlayer. |
12 | | - Will attempt to return a new value instead |
13 | | - """ |
14 | | - if tag == GameTag.ENTITY_ID: |
15 | | - # This is the simplest check. When a player entity is declared, |
16 | | - # its ENTITY_ID is not available immediately (in pre-6.0). |
17 | | - # If we get a matching ENTITY_ID, then we can use that to match it. |
18 | | - return self.register_player_name(player.name, value) |
19 | | - elif tag == GameTag.LAST_CARD_PLAYED: |
20 | | - # This is a fallback to register_player_name_mulligan in case the mulligan |
21 | | - # phase is not available in this game (spectator mode, reconnects). |
22 | | - if value not in self._entity_controller_map: |
23 | | - raise ParsingError("Unknown entity ID on TAG_CHANGE: %r" % (value)) |
24 | | - player_id = self._entity_controller_map[value] |
25 | | - entity_id = int(self._players_by_player_id[player_id]) |
26 | | - return self.register_player_name(player.name, entity_id) |
27 | | - elif tag == GameTag.MULLIGAN_STATE: |
28 | | - return None |
29 | | - return player |
| 9 | + def __init__(self): |
| 10 | + super(LivePlayerManager, self).__init__() |
| 11 | + |
| 12 | + self.actual_player_names = set() |
| 13 | + self.names_used = set() |
| 14 | + self.name_assignment_done = False |
| 15 | + |
| 16 | + def register_player_name_on_tag_change(self, player, tag, value): |
| 17 | + """ |
| 18 | + Triggers on every TAG_CHANGE where the corresponding entity is a LazyPlayer. |
| 19 | + Will attempt to return a new value instead |
| 20 | + """ |
| 21 | + if tag == GameTag.ENTITY_ID: |
| 22 | + # This is the simplest check. When a player entity is declared, |
| 23 | + # its ENTITY_ID is not available immediately (in pre-6.0). |
| 24 | + # If we get a matching ENTITY_ID, then we can use that to match it. |
| 25 | + return self.register_player_name(player.name, value) |
| 26 | + elif tag == GameTag.LAST_CARD_PLAYED: |
| 27 | + # This is a fallback to register_player_name_mulligan in case the mulligan |
| 28 | + # phase is not available in this game (spectator mode, reconnects). |
| 29 | + if value not in self._entity_controller_map: |
| 30 | + raise ParsingError("Unknown entity ID on TAG_CHANGE: %r" % (value)) |
| 31 | + player_id = self._entity_controller_map[value] |
| 32 | + entity_id = int(self._players_by_player_id[player_id]) |
| 33 | + return self.register_player_name(player.name, entity_id) |
| 34 | + else: |
| 35 | + return None |
| 36 | + return player |
| 37 | + |
| 38 | + def set_initial_player_name(self, player_id, player_name, current_game): |
| 39 | + players = current_game.liveExporter.game.players |
| 40 | + for p in players: |
| 41 | + if p.player_id == int(player_id): |
| 42 | + p.name = player_name |
| 43 | + |
| 44 | + def complete_player_names(self, player_name, current_game): |
| 45 | + # populate names if they haven"t been used already |
| 46 | + if player_name not in self.names_used: |
| 47 | + self.actual_player_names.add(player_name) |
| 48 | + |
| 49 | + # if there are two names available we have enough to assign |
| 50 | + if len(self.actual_player_names) == 2 and not self.name_assignment_done: |
| 51 | + unnamed = None |
| 52 | + for p in current_game.liveExporter.game.players: |
| 53 | + if p.name in self.actual_player_names: |
| 54 | + self.names_used.add(p.name) |
| 55 | + self.actual_player_names.remove(p.name) |
| 56 | + else: |
| 57 | + unnamed = p |
| 58 | + other_player_name = self.actual_player_names.pop() |
| 59 | + self.names_used.add(other_player_name) |
| 60 | + unnamed.name = other_player_name |
| 61 | + self.name_assignment_done = True |
0 commit comments