Skip to content

Commit 63e41a9

Browse files
committed
Updating API code example
1 parent 9484cc8 commit 63e41a9

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

README.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,7 @@ The only requirement is a regular Python installation.
3939

4040
### About this project
4141

42-
This server was developed for use in a university programming course, where students learn Python as their first programming language and work on projects in small groups. Both the framework and the API are designed so that the programming skills acquired during the first term are sufficient to implement new games and clients. However, the use of the server is not limited to educational scenarios.
43-
44-
## Operating the server
45-
46-
To run the server in a network, edit IP and port in the configuration file (`server/config.py`). TLS along with other settings can also be configured there. If you intend to run the server as a systemd service, you can use the provided unit file. Server and API are implemented in plain Python. Only modules from the standard library are used. This makes the server easy to handle.
47-
48-
Learn more in the [Wiki](https://github.com/feberts/python-game-server/wiki).
42+
This server was developed for use in a programming course, where students learn Python as their first programming language and work on projects in small groups. Both the framework and the API are designed so that basic programming skills are sufficient to implement new games and clients. However, the use of the server is not limited to educational scenarios. Learn more in the [Wiki](https://github.com/feberts/python-game-server/wiki).
4943

5044
## Implementing clients
5145

@@ -56,23 +50,27 @@ Module `game_server_api` provides an API for communicating with the server. It a
5650
- retrieve the game state
5751
- passively observe another player
5852
- restart a game within the current session
59-
- enable TLS
6053

6154
Here is a short demo of the API usage:
6255

6356
```py
64-
from game_server_api import GameServerAPI
57+
from game_server_api import GameServerAPI, IllegalMove
6558

6659
game = GameServerAPI(server='127.0.0.1', port=4711, game='TicTacToe', players=2,
6760
session='mygame') # pass 'auto' to auto-join a session (default)
6861

69-
my_id = game.join() # start/join a session - each client is assigned an ID
70-
game.move(position=5) # perform a move - the function accepts keyword arguments (**kwargs)
71-
state = game.state() # returns a dictionary representing the game state, including
72-
# the ID(s) of the current player(s)
62+
my_id = game.join() # start/join a session - each client is assigned an ID
63+
64+
try:
65+
game.move(position=5) # perform a move - the function accepts keyword arguments
66+
except IllegalMove as e:
67+
print(e)
68+
69+
state = game.state() # returns a dictionary representing the game state, including
70+
# a list of player IDs indicating whose turn it is
7371
```
7472

75-
The [API module](client/game_server_api.py) itself is documented in detail. You should also take a look at the demo clients and the [Wiki](https://github.com/feberts/python-game-server/wiki).
73+
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).
7674

7775
## Adding new games
7876

@@ -87,6 +85,12 @@ To make things even easier, you can use the [template](server/games/template.py)
8785

8886
No modifications to the API are required when adding new games. It was designed to be compatible with any game. The function to submit a move accepts keyword arguments (`**kwargs`). These are sent to the server, where they are passed to the corresponding function of the game class as a dictionary. The game state is also sent back as a dictionary. This allows for a maximum of flexibility.
8987

88+
## Operating the server
89+
90+
To run the server in a network, edit IP and port in the configuration file (`server/config.py`). TLS along with other settings can also be configured there. If you intend to run the server as a systemd service, you can use the provided unit file. Server and API are implemented in plain Python. Only modules from the standard library are used. This makes the server easy to handle.
91+
92+
Learn more in the [Wiki](https://github.com/feberts/python-game-server/wiki).
93+
9094
## Observer mode
9195

9296
An observer will receive the same data as the observed player does when retrieving the game state. This can be useful in a number of ways:

0 commit comments

Comments
 (0)