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 to_dict(self): |
124
|
|
|
""" |
125
|
|
|
Return a dictionary representing this player object |
126
|
|
|
:return: |
127
|
|
|
""" |
128
|
|
|
def filter_none(t): |
129
|
|
|
_, v = t |
130
|
|
|
return v is not None |
131
|
|
|
return dict(filter(filter_none, ( |
132
|
|
|
('id', self.id), |
|
|
|
|
133
|
|
|
('login', self.login), |
|
|
|
|
134
|
|
|
('global_rating', self.global_rating), |
135
|
|
|
('ladder_rating', self.ladder_rating), |
136
|
|
|
('number_of_games', self.numGames), |
137
|
|
|
('avatar', self.avatar), |
138
|
|
|
('country', self.country), |
139
|
|
|
('clan', self.clan) |
140
|
|
|
))) |
141
|
|
|
|
142
|
|
|
def __str__(self): |
143
|
|
|
return "Player({}, {}, {}, {})".format(self.login, self.id, self.global_rating, self.ladder_rating) |
|
|
|
|
144
|
|
|
|
145
|
|
|
def __repr__(self): |
146
|
|
|
return self.__str__() |
147
|
|
|
|
148
|
|
|
def __hash__(self): |
149
|
|
|
return self.id |
|
|
|
|
150
|
|
|
|
151
|
|
|
def __eq__(self, other): |
152
|
|
|
if not isinstance(other, BasePlayer): |
153
|
|
|
return False |
154
|
|
|
else: |
155
|
|
|
return self.id == other.id |
|
|
|
|
156
|
|
|
|