For a simple game like chess, it is really just about defining the mediatype.
Here is an example of what is probably an over simplified mediatype to model a chess game.
I'm going to skip the management of multiple games that may be running on the same server and just model an already running game.
The first step is usually to define an index to the application.
index
The entry point for the game. Fetch this to discover information about the game.
The payload might look something like this:
{
"links": {
"self": "http://my-chess-game.host/games/123",
"player": "http://my-chess-game.host/players/1",
"player": "http://my-chess-game.host/players/2",
"me": "http://my-chess-game.host/players/1",
...
}
"board": [
{
"x": 0,
"y": 1,
"piece": null,
"rel": "space",
"href": "http://my-chess-game/.../boards/123/0/1/something-random-to-discourage-uri-construction"
},
{
"x": 1,
"y": 2,
"rel": "space",
"href": "...",
"piece": {
"player": "http://my-chess-game/.../players/1",
"type": "http://my-chess-game/pieces/Bishop",
"rel": "piece",
"href": "http://my-chess-game/games/123/pieces/player1/Bishop/1",
"links": [
{ "rel": "move": "href": "http://my-chess-game/.../boards/123/..." },
...
]
}
},
...
]
}
move
POST a JSON payload to links marked with a rel
of move
to move a piece. The following fields MUST be included:
- location: the URI of the space to move to
Successful responses have a status code of 200 and will contain an entity that the same as the index
payload with the updated state of the game.
400 if the user is not allowed to move his piece there, or if it isn't his turn.
player
GET a description of a player.
The following fields MUST be in the response:
- username: User name of the player
- href: URI identifying the player of this game.
piece
Pieces are embedded in the index
payload, but MAY exist on their own. Each piece
MUST have the following fields:
- type: A URI identifying the type of the piece. E.G. Bishop, Rook, King. GETing this URI MAY provide information about how this piece works within the game of chess.
- href: A URI identifying the actual piece on this board. GET requests to this URI MAY provide information about this particular piece.
Each piece MUST have a move
link.
An alternative design decision I could have made here was to provide an individual link for every valid move. There may be good reasons to do that, but I wanted to demonstrate that it isn't required. There are probably a handful of other resources that you would want to include to handle things like helping the client determine whose turn it is and whatnot.
For more complicated games, like Civilization, RTSs, FPSs, or MMOGs and what not, this may not be so practical IMO.