| 1 |  |  | import weakref | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | from enum import IntEnum, unique | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | from .abc.base_player import BasePlayer | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | @unique | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class PlayerState(IntEnum): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     IDLE = 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     PLAYING = 2, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     HOSTING = 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     JOINING = 4, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     SEARCHING_LADDER = 5, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class Player(BasePlayer): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     Standard player object used for representing signed-in players. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     In the context of a game, the Game object holds game-specific | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     information about players. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     def __init__(self, login=None, session=0, ip=None, port=None, id=0, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |                  global_rating=None, ladder_rating=None, clan=None, numGames=0, permissionGroup=0, lobby_connection=None): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         super().__init__(id, login) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         # The id of the user in the `login` table of the database. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         self.session = session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         self.ip = ip | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         self._game_port = port | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         if global_rating is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             global_rating = (1500, 500) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         if ladder_rating is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             ladder_rating = (1500, 500) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         self.global_rating = global_rating | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         self.ladder_rating = ladder_rating | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         #social | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         self.avatar = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         self.clan = clan | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         self.country = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         self.friends = set() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         self.foes = set() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         self.league = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         self.admin = permissionGroup >= 2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         self.mod = permissionGroup >= 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         self.numGames = numGames | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         self.state = PlayerState.IDLE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         self.expandLadder = 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         self.faction = 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         self._lobby_connection = lambda: None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         if lobby_connection is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             self.lobby_connection = lobby_connection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         self._game = lambda: None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         self._game_connection = lambda: None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     def lobby_connection(self) -> "LobbyConnection": | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         Weak reference to the LobbyConnection of this player | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         return self._lobby_connection() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     @lobby_connection.setter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     def lobby_connection(self, value: "LobbyConnection"): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         self._lobby_connection = weakref.ref(value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     def game(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         Weak reference to the Game object that this player wants to join or is currently in | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return self._game() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     @game.setter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     def game(self, value): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         self._game = weakref.ref(value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     @game.deleter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     def game(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         self._game = lambda: None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     def game_connection(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         Weak reference to the GameConnection object for this player | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         :return: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         return self._game_connection() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     @game_connection.setter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     def game_connection(self, value): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         self._game_connection = weakref.ref(value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     @game_connection.deleter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     def game_connection(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         self._game_connection = lambda: None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     def in_game(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         return self.game is not None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     def game_port(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         return self._game_port or 6112 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     @game_port.setter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     def game_port(self, value): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         self._game_port = value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     def address_and_port(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         return "{}:{}".format(self.ip, self.game_port) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     def notify_potential_match(self, player, potential): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         if self.lobby_connection: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             self.lobby_connection.sendJSON({ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 'command': 'matchmaker_info', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 'potential': potential | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             }) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |     def to_dict(self): | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |         """ | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |         Return a dictionary representing this player object | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |         :return: | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |         """ | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |         def filter_none(t): | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |             _, v = t | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |             return v is not None | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |         return dict(filter(filter_none, ( | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |             ('id', self.id), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |             ('login', self.login), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |             ('global_rating', self.global_rating), | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |             ('ladder_rating', self.ladder_rating), | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |             ('number_of_games', self.numGames), | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |             ('avatar', self.avatar), | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |             ('country', self.country), | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |             ('clan', self.clan) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         ))) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     def __str__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         return "Player({}, {}, {}, {})".format(self.login, self.id, self.global_rating, self.ladder_rating) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     def __repr__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         return self.__str__() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     def __hash__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         return self.id | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     def __eq__(self, other): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         if not isinstance(other, BasePlayer): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |             return False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         else: | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 162 |  |  |             return self.id == other.id | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 163 |  |  |  |