Implementing an auto-join mode · feberts/python-game-server@e1c2700 · GitHub
Skip to content

Commit e1c2700

Browse files
committed
Implementing an auto-join mode
1 parent c64c1f7 commit e1c2700

3 files changed

Lines changed: 97 additions & 18 deletions

File tree

‎README.md‎

Lines changed: 6 additions & 4 deletions

‎client/game_server_api.py‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,12 @@ def __init__(self, server, port, game, token, players=None, name=''):
5151
Parameters needed in order to connect to the server and to start or join
5252
a game session are passed to this constructor. Parameter game specifies
5353
the game to be started. It corresponds to the name of the game class on
54-
the server. To be able to join the game session, all participants need
55-
to agree on a token and pass it to the constructor. The token is used to
56-
identify the game session. It can be any string.
54+
the server. To be able to join a specific game session, all participants
55+
need to agree on a token and pass it to the constructor. The token is
56+
used to identify the game session. It can be any string. Alternatively,
57+
you can have the server automatically assign you to a session by passing
58+
the string 'auto' as the token. Refer to function join for more
59+
information.
5760
5861
The optional parameter players is required by function join in order to
5962
start a new game session with the specified number of players. If the
@@ -70,7 +73,7 @@ def __init__(self, server, port, game, token, players=None, name=''):
7073
server (str): server
7174
port (int): port number
7275
game (str): name of the game
73-
token (str): name of the game session
76+
token (str): name of the game session, 'auto' for automatic assignment
7477
players (int): total number of players (optional)
7578
name (str): player name (optional)
7679
@@ -81,7 +84,7 @@ def __init__(self, server, port, game, token, players=None, name=''):
8184
assert type(port) == int and 0 <= port <= 65535, self._error('port')
8285
assert type(game) == str and len(game) > 0, self._error('game')
8386
assert type(token) == str and len(token) > 0, self._error('token')
84-
assert players == None or type(players) == int and players > 0, self._error('players')
87+
assert players is None or type(players) == int and players > 0, self._error('players')
8588
assert type(name) == str, self._error('name')
8689

8790
# server:
@@ -110,8 +113,20 @@ def join(self):
110113
players must be passed to the constructor. If the argument is omitted,
111114
this function will only try to join an existing session but never start
112115
a new one. The argument is ignored when an existing session can be
113-
joined. If a session exists but is already fully occupied by players,
114-
it is terminated and a new session is started.
116+
joined.
117+
118+
There are two ways to start or join a game session:
119+
120+
- By providing a shared token to the constructor. All clients using the
121+
same token will join this specific game session. If such a session
122+
exists but is already fully occupied by players, it is terminated and
123+
a new session is started.
124+
- By passing the string 'auto' as the token. This causes the server to
125+
automatically assign you to an open session. If no session exists that
126+
can be joined, a new one is started. Existing sessions are never
127+
terminated. This method of starting and joining sessions does not
128+
interfere with the above method. To achieve this, the server creates
129+
unique tokens internally.
115130
116131
The game starts as soon as the required number of clients has joined the
117132
game. The function then returns the player ID. The server assigns IDs in
@@ -134,6 +149,7 @@ def join(self):
134149

135150
self._player_id = response['player_id']
136151
self._key = response['key']
152+
self._token = response['token']
137153
self._request_size_max = response['request_size_max']
138154

139155
return self._player_id
@@ -230,7 +246,8 @@ def observe(self):
230246
This function will return the player ID of the observed player.
231247
232248
This function can only be called, after the specified game session has
233-
already been started.
249+
already been started. The observer mode is not available for auto-join
250+
sessions.
234251
235252
Returns:
236253
int: ID of the observed player
@@ -241,6 +258,9 @@ def observe(self):
241258
if type(self._name) != str or len(self._name) == 0:
242259
raise GameServerError('a valid name must be passed to the constructor')
243260

261+
if self._token == 'auto':
262+
raise GameServerError('observer mode not available for auto-join sessions')
263+
244264
response, err, _ = self._send({
245265
'type':'observe',
246266
'game':self._game,

‎server/game_framework.py‎

Lines changed: 63 additions & 6 deletions

0 commit comments

Comments
 (0)