Skip to content

Commit 2d5f52a

Browse files
committed
API (__init__): rename parameter token to session
1 parent 2a3b71e commit 2d5f52a

18 files changed

Lines changed: 73 additions & 70 deletions

.github/workflows/test_api.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,38 @@ def fail(msg):
1616
# ========== join ==========
1717

1818
GAME = 'Echo'
19-
TOKEN = 'test'
19+
SESSION = 'test'
2020
NAME = 'bob'
2121

2222
try:
23-
game_err = GameServerAPI(SERVER, PORT, 'InvalidGame', TOKEN, 1)
23+
game_err = GameServerAPI(SERVER, PORT, 'InvalidGame', SESSION, 1)
2424
my_id = game_err.join()
2525
fail('no exception when trying to start non-existent game')
2626
except GameServerError:
2727
pass
2828

2929
try:
30-
game_err = GameServerAPI(SERVER, 9999, GAME, TOKEN, 1)
30+
game_err = GameServerAPI(SERVER, 9999, GAME, SESSION, 1)
3131
my_id = game_err.join()
3232
fail('no exception despite invalid port')
3333
except GameServerError:
3434
pass
3535

3636
try:
37-
game_err = GameServerAPI('127.0.0.13', PORT, GAME, TOKEN, 1)
37+
game_err = GameServerAPI('127.0.0.13', PORT, GAME, SESSION, 1)
3838
my_id = game_err.join()
3939
fail('no exception despite invalid ip')
4040
except GameServerError:
4141
pass
4242

4343
try:
44-
game_err = GameServerAPI(SERVER, PORT, GAME, TOKEN)
44+
game_err = GameServerAPI(SERVER, PORT, GAME, SESSION)
4545
my_id = game_err.join()
4646
fail('no exception when trying to join non-existent session')
4747
except GameServerError:
4848
pass
4949

50-
game = GameServerAPI(SERVER, PORT, GAME, TOKEN, 1, NAME)
50+
game = GameServerAPI(SERVER, PORT, GAME, SESSION, 1, NAME)
5151

5252
try:
5353
game.move(msg='invalid')
@@ -58,7 +58,7 @@ def fail(msg):
5858
my_id = game.join()
5959

6060
try:
61-
game_err = GameServerAPI(SERVER, PORT, GAME, TOKEN)
61+
game_err = GameServerAPI(SERVER, PORT, GAME, SESSION)
6262
my_id = game_err.join()
6363
fail('no exception when trying to join full session')
6464
except GameServerError:
@@ -119,13 +119,13 @@ def fail(msg):
119119
# ========== observe ==========
120120

121121
try:
122-
observer = GameServerAPI(SERVER, PORT, GAME, TOKEN, name='invalid')
122+
observer = GameServerAPI(SERVER, PORT, GAME, SESSION, name='invalid')
123123
observer.observe()
124124
fail('observer: no exception despite invalid name')
125125
except GameServerError:
126126
pass
127127

128-
observer = GameServerAPI(SERVER, PORT, GAME, TOKEN, name=NAME)
128+
observer = GameServerAPI(SERVER, PORT, GAME, SESSION, name=NAME)
129129
observer.observe()
130130

131131
game.move(msg='hello observer')

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ Here is a short demo of the API usage:
5353
```py
5454
from game_server_api import GameServerAPI
5555

56-
game = GameServerAPI(server='127.0.0.1', port=4711, game='TicTacToe', players=2,
57-
token='mygame') # pass 'auto' to auto-join a session (default)
56+
game = GameServerAPI(server='127.0.0.1', port=4711, game='Yahtzee', players=2,
57+
session='mygame') # pass 'auto' to auto-join a session (default)
5858

5959
my_id = game.join() # start/join a session - each client is assigned an ID
6060
game.move(position=5) # perform a move - the function accepts keyword arguments (**kwargs)
61-
state = game.state() # returns a dictionary representing the game state,
62-
# including the ID of the current player(s)
61+
state = game.state() # returns a dictionary representing the game state, including
62+
# the ID of the current player(s)
6363
```
6464

6565
The [API module](client/game_server_api.py) itself is documented in detail. You can also take a look at the demo clients and the [wiki](https://github.com/feberts/python-game-server/wiki).

client/chat_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from game_server_api import GameServerAPI, GameServerError, IllegalMove
1414

15-
game = GameServerAPI(server='127.0.0.1', port=4711, game='Chat', token='mychat', players=2)
15+
game = GameServerAPI(server='127.0.0.1', port=4711, game='Chat', session='mychat', players=2)
1616

1717
def thread_output(text_area):
1818
while True:

client/echo_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from game_server_api import GameServerAPI, GameServerError, IllegalMove
1414

15-
game = GameServerAPI(server='127.0.0.1', port=4711, game='Echo', token='mygame', players=1)
15+
game = GameServerAPI(server='127.0.0.1', port=4711, game='Echo', session='mygame', players=1)
1616

1717
my_id = game.join()
1818
state = game.state()

client/game_server_api.py

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,16 @@ class GameServerAPI:
4646
This class provides API functions to communicate with the game server.
4747
"""
4848

49-
def __init__(self, server, port, game, token='auto', players=None, name=''):
49+
def __init__(self, server, port, game, session='auto', players=None, name=''):
5050
"""
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
5454
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. Alternatively, you can have the
57-
server automatically assign you to a session by passing 'auto' as the
58-
token (this is the default). Refer to function join for more
55+
need to agree on a session token and pass it to the constructor. The
56+
token is used to identify the game session. Alternatively, you can have
57+
the server automatically assign you to a session by passing 'auto' as
58+
the token (this is the default). Refer to function join for more
5959
information.
6060
6161
The optional parameter players is required by function join in order to
@@ -73,7 +73,7 @@ def __init__(self, server, port, game, token='auto', players=None, name=''):
7373
server (str): server
7474
port (int): port number
7575
game (str): name of the game
76-
token (str): name of the session (optional), 'auto' for auto-join (default)
76+
session (str): name of the game session (optional), 'auto' for auto-join (default)
7777
players (int): total number of players (optional)
7878
name (str): player name (optional)
7979
@@ -83,7 +83,7 @@ def __init__(self, server, port, game, token='auto', players=None, name=''):
8383
assert type(server) == str and len(server) > 0, self._error('server')
8484
assert type(port) == int and 0 <= port <= 65535, self._error('port')
8585
assert type(game) == str and len(game) > 0, self._error('game')
86-
assert type(token) == str and len(token) > 0, self._error('token')
86+
assert type(session) == str and len(session) > 0, self._error('session')
8787
assert players is None or type(players) == int and players > 0, self._error('players')
8888
assert type(name) == str, self._error('name')
8989

@@ -93,7 +93,7 @@ def __init__(self, server, port, game, token='auto', players=None, name=''):
9393

9494
# game session:
9595
self._game = game
96-
self._token = token
96+
self._session = session
9797
self._players = players
9898
self._name = name
9999
self._player_id = None
@@ -117,16 +117,16 @@ def join(self):
117117
118118
There are two ways to start or join a game session:
119119
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.
120+
- By providing a shared session token to the constructor. All clients
121+
using the same token will join this specific game session. If such a
122+
session exists but is already fully occupied by players, it is
123+
terminated and a new session is started.
124+
- By passing the string 'auto' as the session token. This causes the
125+
server to automatically assign you to an open session. If no session
126+
exists that can be joined, a new one is started. Existing sessions are
127+
never terminated. This method of starting and joining sessions does
128+
not interfere with the above method. To achieve this, the server
129+
creates unique tokens internally.
130130
131131
The game starts as soon as the required number of clients has joined the
132132
game. The function then returns the player ID. The server assigns IDs in
@@ -141,15 +141,15 @@ def join(self):
141141
response, err, _ = self._send({
142142
'type':'join',
143143
'game':self._game,
144-
'token':self._token,
144+
'session':self._session,
145145
'players':self._players,
146146
'name':self._name})
147147

148148
if err: raise GameServerError(err)
149149

150150
self._player_id = response['player_id']
151151
self._key = response['key']
152-
self._token = response['token']
152+
self._session = response['session']
153153
self._request_size_max = response['request_size_max']
154154

155155
return self._player_id
@@ -180,7 +180,7 @@ def move(self, **kwargs):
180180
_, err, status = self._send({
181181
'type':'move',
182182
'game':self._game,
183-
'token':self._token,
183+
'session':self._session,
184184
'player_id':self._player_id,
185185
'key':self._key,
186186
'move':kwargs})
@@ -204,17 +204,20 @@ def state(self):
204204
205205
Independent of the game, the dictionary always contains these two keys:
206206
207-
'current': a list of player IDs, indicating whose player's turn it is
208-
'gameover': a boolean value indicating whether the game is over or still active
207+
- 'current': a list of player IDs, indicating whose player's turn it is
208+
- 'gameover': a boolean value indicating whether the game is over or
209+
still active
209210
210211
This function will block until the game state changes. Only then does
211212
the server respond with the updated state. This is more efficient than
212213
polling. To avoid deadlocks, the function never blocks in these
213214
situations:
214215
215-
- when the game has just started to allow clients to get the initial state
216+
- when the game has just started to allow clients to get the initial
217+
state
216218
- after a move was performed to allow clients to get the new state
217-
- when the game was restarted and a client still has to get the old game's state
219+
- when the game was restarted and a client still has to get the old
220+
game's state
218221
219222
Returns:
220223
dict: game state
@@ -227,7 +230,7 @@ def state(self):
227230
state, err, _ = self._send({
228231
'type':'state',
229232
'game':self._game,
230-
'token':self._token,
233+
'session':self._session,
231234
'player_id':self._player_id,
232235
'key':self._key,
233236
'observer':self._observer})
@@ -258,13 +261,13 @@ def observe(self):
258261
if type(self._name) != str or len(self._name) == 0:
259262
raise GameServerError('a valid name must be passed to the constructor')
260263

261-
if self._token == 'auto':
264+
if self._session == 'auto':
262265
raise GameServerError('observer mode not available for auto-join sessions')
263266

264267
response, err, _ = self._send({
265268
'type':'observe',
266269
'game':self._game,
267-
'token':self._token,
270+
'session':self._session,
268271
'name':self._name})
269272

270273
if err: raise GameServerError(err)
@@ -291,7 +294,7 @@ def restart(self):
291294
_, err, _ = self._send({
292295
'type':'restart',
293296
'game':self._game,
294-
'token':self._token,
297+
'session':self._session,
295298
'player_id':self._player_id,
296299
'key':self._key})
297300

client/more_examples/api_wrapper/tictactoe_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class TicTacToeAPI:
1515
This class provides API wrapper functions for tic-tac-toe.
1616
"""
1717

18-
def __init__(self, token, name=''):
19-
self._api = GameServerAPI('127.0.0.1', 4711, 'TicTacToe', token, 2, name)
18+
def __init__(self, session, name=''):
19+
self._api = GameServerAPI('127.0.0.1', 4711, 'TicTacToe', session, 2, name)
2020
self.my_id = None
2121

2222
def join(self):

client/more_examples/api_wrapper/tictactoe_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from tictactoe_api import TicTacToeAPI, GameServerError, IllegalMove
1111

12-
game = TicTacToeAPI(token='mygame')
12+
game = TicTacToeAPI(session='mygame')
1313

1414
symbols = ('x', 'o')
1515

client/more_examples/api_wrapper/yahtzee_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class YahtzeeAPI:
1515
This class provides API wrapper functions for Yahtzee.
1616
"""
1717

18-
def __init__(self, token, players, name=''):
19-
self._api = GameServerAPI('127.0.0.1', 4711, 'Yahtzee', token, players, name)
18+
def __init__(self, session, players, name=''):
19+
self._api = GameServerAPI('127.0.0.1', 4711, 'Yahtzee', session, players, name)
2020
self.my_id = None
2121

2222
def join(self):

client/more_examples/api_wrapper/yahtzee_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from yahtzee_api import YahtzeeAPI, GameServerError, IllegalMove
1111

12-
game = YahtzeeAPI(token='mygame', players=1)
12+
game = YahtzeeAPI(session='mygame', players=1)
1313

1414
def print_scorecard(scorecard):
1515
print('\n' * 100)

client/more_examples/observer_mode/input_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from game_server_api import GameServerAPI, GameServerError, IllegalMove
1313

14-
game = GameServerAPI(server='127.0.0.1', port=4711, game='TicTacToe', token='mygame', players=2, name='bob')
14+
game = GameServerAPI(server='127.0.0.1', port=4711, game='TicTacToe', session='mygame', players=2, name='bob')
1515

1616
def user_input(prompt):
1717
while True:

0 commit comments

Comments
 (0)