2.4 KiB
2.4 KiB
Remote Snake API
General Description
- All transmissions will be based on UDP
- All UDP packet will contain plain json data
Communications
Initialisation
- Server wait for a client
-
Client can send:
{ "type": "new-game" }
-
Server can reply:
{ "type": "state", "syn": 1, "game-id": 1, "game-over": false, "snake": [(1,2),(1,3)], "food": [(6,7)] }
Note that, syn entry is used to keep packet ordering consistent and detecting packet inversion on the network. Thus, syn entry indicate the expected syn that the client should send on the next UDP packet.
Gameplay
Change Direction
-
When client is playing a game it can ask to the server to change snake direction:
{ "type": "update", "syn": 1, "game-id": 1, "direction": "left", }
-
Server can reply
{ "type": "state", "syn": 2, "game-id": 1, "game-over": false, "snake": [(0,2),(1,2)], "food": [(6,7)] }
Refresh Screen
-
When no key are press (the snake is simply going straigth forward). So, client can send:
{ "type": "update", "syn": 2, "game-id": 1, "direction": null }
-
Server can reply:
{ "type": "state", "syn": 3, "game-id": 1, "game-over": false, "snake": [(1,2),(0,2)], "food": [(6,7)] }
End Game
-
When game is over server will send the following state message (switch game-over to true):
{ "type": "state", "syn": null, "game-id": 1, "game-over": true, "snake": [(0,2),(1,2)], "food": [(6,7)] }
- No reply is expected from the client and server will be in charge to free local memory. Note that syn=null.