-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_session.py
More file actions
360 lines (290 loc) · 11.4 KB
/
game_session.py
File metadata and controls
360 lines (290 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
"""
Game session.
This module provides a class that handles a single game session.
"""
import copy
import random
import string
import threading
import time
class GameSession:
"""
Class GameSession.
This class contains a game instance and all data associated with the
specific game session. It assigns player IDs, passes requests like player
moves to the game instance, and provides the framework with information
about the game session.
"""
def __init__(self, game_class, players):
"""
Instantiating a game class object and initializing other attributes.
Parameters:
game_class (derived from AbstractGame): the game class itself, not an instance
players (int): number of players
"""
self._game_class = game_class
self._n_players = players
self._game = None
self._next_id = 0
self._player_ids = {} # player name -> ID
self._keys = {} # player ID -> key
self._last_access = time.time()
self._lock = threading.Lock()
self._state_change = threading.Event()
self._no_delay = [] # IDs, players will receive the state immediately
self._in_previous_game = [] # IDs, after restart, receive state of previous game once
self._previous_game = None # previous game instance stored upon restart
self._new_game()
self._timed_out = False
self._overwritten = False
def next_id(self, player_name):
"""
Returning a player ID and a key.
This function returns a new ID and a key for each player joining a game
session. If a none empty string is passed as the player name, this name
together with the assigned ID is added to a dictionary.
Parameters:
player_name (str): player name, can be an empty string
Returns:
tuple(int, str, str):
int: the next player ID
str: a generated key
str: error message, if a problem occurred, None otherwise
"""
with self._lock:
if player_name in self._player_ids:
return None, None, 'name already in use'
# player ID:
player_id = self._next_id
self._next_id += 1
# associate player name with ID:
if player_name != '':
self._player_ids[player_name] = player_id
# generate key:
key = self._key()
self._keys[player_id] = key
return player_id, key, None
def full(self):
"""
Game session is full as soon as all players have joined the game.
Returns:
bool: True, if session is full
"""
return self._n_players == self._next_id
def get_id(self, player_name):
"""
Return player ID and key by name.
This function returns the ID and key that were assigned to the player.
Parameters:
player_name (str): name of a player that has already joined the game
Returns:
tuple(int, str, str):
int: player ID, None if no such player exists
str: key, None if no such player exists
str: error message, if a problem occurred, None otherwise
"""
if not player_name in self._player_ids:
return None, None, 'no such player'
player_id = self._player_ids[player_name]
return player_id, self._keys[player_id], None
def game_over(self):
"""
Retrieve the game status from the game instance.
Returns:
bool: True, if game has ended, else False
"""
return self._game.game_over()
def current_player(self, game=None):
"""
Retrieve current player(s) from the game instance. Returns an empty list
when the game has ended.
Parameters:
game (AbstractGame): game instance (optional, default: current game)
Returns:
list: player IDs
"""
if not game: game = self._game
if game.game_over():
return []
else:
current = game.current_player()
if type(current) == int:
return [current]
return current
def last_access(self):
"""
Return time of last access.
"""
return self._last_access
def _update_last_access(self):
"""
Update time of last access.
"""
self._last_access = time.time()
def game_move(self, move, player_id):
"""
Pass player's move to the game instance.
When this function is called, an event is triggered to wake up other
threads waiting for the game state to change.
Parameters:
move (dict): the player's move
player_id (int): ID of the player submitting the move
Returns:
error message in case the move was illegal, None otherwise (see AbstractGame.move)
"""
with self._lock:
ret = self._game.move(move, player_id)
self._update_last_access()
self.wake_up_threads()
return ret
def game_state(self, player_id, observer):
"""
Retrieve the game state from the game instance.
In addition to the information returned from the game instance, the IDs
of the current players and the game status are added to the returned
state.
This function will block until the game state changes. Only then will
the updated state be sent back to the client. This is more efficient
than polling. To avoid deadlocks, the function never blocks in these
situations:
- when the game has just started to allow clients to get the initial state
- after a move was performed to allow clients to get the new state
- when the game was restarted and a client still has to get the old game's state
To achieve this, the thread that changes the state triggers an event to
wake up other threads waiting for that event.
If the game has been restarted by some client, then for a single time
the state of the previous game is returned to each client. This is
necessary for a client to be able to detect the end of the previous
game. See function restart_game for details.
A list containing IDs is used for deciding whether the state should be
returned immediately or only after it changes. At the same time, the
function has to distinguish between active players and passive
observers. Since observers are assigned the same IDs as the observed
clients, the IDs have to be converted internally. The observer's IDs are
increased so they come after the regular IDs: 0...n-1 = players,
n...2n-1 = observers.
Parameters:
player_id (int): player ID
observer (bool): if True, client requesting the state is a passive observer
Returns:
dict: game state
"""
p_id = player_id
if observer:
p_id += self._n_players # convert observer IDs
# wait for game state to change:
if p_id not in self._no_delay and p_id not in self._in_previous_game:
self._state_change.clear()
self._state_change.wait()
# if required, return the previous game's state:
# (this must NOT be done inside the lock below to avoid deadlocks)
if p_id in self._in_previous_game:
self._in_previous_game.remove(p_id)
return self._assemble_state(self._previous_game, player_id)
# return current game's state:
with self._lock:
self._update_last_access()
self._no_delay.remove(p_id)
return self._assemble_state(self._game, player_id)
def _assemble_state(self, game, player_id):
"""
Prepare the state to be returned to the client by adding current
player(s) and game status.
Parameters:
game (AbstractGame): game instance
player_id (int): player ID
Returns:
dict: game state
"""
state = game.state(player_id)
state['current'] = self.current_player(game)
state['gameover'] = game.game_over()
return state
def _new_game(self):
"""
Instantiate a new game and add IDs to the no-delay list so that all
clients can retrieve the state even before any move has been performed.
The observer's IDs are appended to the regular IDs. See function
game_state for details.
"""
self._game = self._game_class(self._n_players)
self._no_delay = self._all_ids()
def restart_game(self, player_id):
"""
Restart the game.
The game instance is replaced with a new one. The old instance is
stored. This is necessary to allow the other clients to detect the end
of the previous game. Otherwise, they would suddenly find themselves in
a new game without being notified about it. To achieve this, a list of
client IDs is created upon restarting a game. When a client then calls
the state function, the state of the previous game is returned a single
time, and the client's ID is removed from the list. From then on, the
client will receive the game state of the new game instance.
Parameters:
player_id (int): player ID
"""
# store old game instance and a list of player IDs:
if self._game.game_over():
self._in_previous_game = self._all_ids()
self._in_previous_game.remove(player_id) # exclude client that restarted the game
self._previous_game = copy.deepcopy(self._game)
# create new game instance and wake up waiting threads:
self._new_game()
self.wake_up_threads()
def _key(self, length=5):
"""
Generate a unique key.
Parameters:
length (int): length of the key (optional)
Returns:
str: the key
"""
chars = string.ascii_letters + string.digits
return ''.join(random.choice(chars) for _ in range(length))
def key_valid(self, player_id, key):
"""
Check if key and player ID match.
Parameters:
player_id (int): player ID
key (str): key
Returns:
bool: True, if key valid
"""
return player_id in self._keys and self._keys[player_id] == key
def wake_up_threads(self):
"""
Wake up other threads waiting for the game state to change.
"""
self._no_delay = self._all_ids()
self._state_change.set()
def mark_timed_out(self):
"""
Mark game session as timed out.
"""
self._timed_out = True
def timed_out(self):
"""
Check if game session has timed out.
Returns:
bool: True, if game session has timed out
"""
return self._timed_out
def mark_overwritten(self):
"""
Mark game session as overwritten.
"""
self._overwritten = True
def overwritten(self):
"""
Check if game session is overwritten.
Returns:
bool: True, if game session is overwritten
"""
return self._overwritten
def _all_ids(self):
"""
Return a list of all player IDs, including observer IDs.
Returns:
list: all player IDs
"""
return list(range(self._n_players * 2))