A server for playing tic tac toe, made using sockets and JSON
To run this project, you need:
- pyenv with python 3.8 installed
- pipenv
and install the dependencies:
$ make setup
After that you can:
-
start the server:
$ make run
-
start testing client:
$ make run_client
The server responses are in JSON, containing the following fields:
{
"status": "status of the player",
"error_type": "type of the error",
"message": "error context",
"board": "array representing the board"
}
-
The
error_type
will only appear in case of error, so check if thestatus
field iserror
first. -
The
message
field is only used when the status iserror
-
The
board
field is only used when the status is eitheropponent
orplay
-
The
defeat
orvictory
will wait for an JSON with thecontinue
field, which may containy
orn
status | description |
---|---|
error | An error ocurred. Check the error_type field |
waiting | Name saved. Waiting for another player |
matched | Matched with another player. The game will begin |
play | The server is waiting for your move |
opponent | Wait your opponent's move |
victory | You won! |
defeat | You lost... |
error_type | description |
---|---|
OPPONENT_LEFT | Your opponent has left the game, you will be moved back to the matcher. The client should wait for the message with status "matched" again. |
INVALID_FORMAT | The client is not sending the correct format (JSON). See client message format. The server will wait for another message |
INVALID_VALUES | The values are either out of bounds (1 <= value <= 3) or targetting a position already chosen on the board |
The first message should be the player's name, in the following format:
{
"my_name":"player name"
}
WARNING!
: If the message doesn't contain the field my_name
the connection will be closed by the server without any response.
upon receiving my_name
message, the server will register it and send to the matcher.
after being matched with another player, the game will start. The game messages should follow the format:
{
"line": "integer value between 1 and 3",
"column": "integer value between 1 and 3"
}
The Client will receive an status
with defeat
or victory
. The Client must send a JSON with the continue
field, which may contain y
or n
.
-
y
: The server will keep the connection and try to start another game -
n
: The server will stop the connection
{
"continue": "y",
}
Server | message direction | Client_1 |
---|---|---|
- | <-- | my_name |
waiting | --> | - |
matched | --> | - |
play | --> | - |
- | <-- | {line, column} |
opponent | --> | - |
play | --> | - |
- | <-- | {line, column} |