game_lobby_server
v1.4.0
Published
Server for Game Lobby practice project
Downloads
3
Readme
Game Lobby Server
A server for Interactive Frontend Development course project.
Usage
When imported as a library
game_lobby_server --help
game_lobby_server --delay=500 --port=8081 --failure-percentage=30
When used as a stand-alone
./bin/server.js --help
./bin/server.js --delay=500 --port=8081 --failure-percentage=30
or
yarn start
Endpoints
POST /games/
Request Body Parameters
- type:
String
(guess_number | guess_word | hangman | rps / metronome / mathemagician) - frequency:
Integer
- Required only when type is metronome - rounds:
Integer
- Required only when type is _mathemagician__
Response {id, type, status}
- id:
String
- type:
String
, echo of the submitted type - status:
String
(waiting_for_move)
OR
Response with status code 422 in case input parameters were invalid
Game type: mathemagician
Additional response fields:
- nextExpression:
{lhs, rhs, operator}
- lhs:
Integer
- rhs:
Integer
- operator: One of '+', '-', '*', '/' (integer division)
- correctAnswerLength:
Integer
. When expected answer is "-5" then answerLength is 2.
- lhs:
- skipsRemaining:
Integer
Game type: hangman
Additional response fields:
- won:
Boolean
, false - wrongGuessCount:
Integer
, 0 - letters:
Array[String]
, list of letters in the target word, all letters are denoted withnull
POST /games/:gameId/moves
Game type: mathemagician
Request Body parameters
- guess:
Integer
| "skip"
Response {move, game}
- move:
correct, timeSpentMillis, nextExpression
- timeSpentMillis:
Integer
- correct:
Boolean
- skipped:
Boolean
- timeSpentMillis:
- game: `id, type, status}
- id:
String
- type:
String
(mathemagician) - status:
String
(waiting_for_move | finished) - nextExpression:
null
if game finished or same structure asnextExpression
in POST /games/ endpoint response - skipsRemaining:
Integer
- id:
Game type: guess_number
Request Body Parameters
- guess:
Integer
Response {move, game}
- move:
{comparedToAnswer, guess}
- comparedToAnswer:
String
(LT | GT | EQ) - guess:
Integer
, echo of the submitted guess
- comparedToAnswer:
- game:
{id, type, status}
- id:
String
- type:
String
(guess_number) - status:
String
(waiting_for_move | finished)
- id:
Game type: guess_word
Request Body Parameters
- guess:
String
Response {move, game}
- move:
{correct, letterMatches, guess}
- correct:
Boolean
- letterMatches:
Array[Boolean]
, true if character at position was correct, false otherwise - guess:
String
, echo of the submitted guess
- correct:
- game:
{id, type, status}
- id:
String
- type:
String
(guess_word) - status:
String
(waiting_for_move | finished)
- id:
Game type: hangman
Request Body Parameters
- guess:
String
Response:
- id:
String
- type:
String
(hangman) - status:
String
(waiting_for_move | finished) - won:
Boolean
, whether the game has finished with a victory - wrongGuessCount:
Integer
, number of invalid guesses thus far - letters:
Array[String]
, list of letters that have been already guessed, letters which have not been guessed are denoted withnull
- move:
{matchedLetterCount, guess}
- matchedLetterCount:
Integer
, how many letters of the target word matched the input - guess:
String
, echo of the submitted guess
- matchedLetterCount:
Game type: rps
Request Body Parameters
- guess:
String
Response:
- id:
String
- type:
String
(rps) - status:
String
(waiting_for_move | finished) - won:
Boolean
, whether the game has finished with a victory - move:
{result, opposition, guess}
- result:
String
(WIN, TIE, LOSS), result of the Rock-Paper-Scissors round - opposition:
String
(ROCK, PAPER, SCISSORS), the opposition for the round - guess:
String
, echo of the submitted guess
- result:
Game type: metronome
Additional response fields:
- startTimeMillis:
Integer
- tries:
Array[Try]
, list of metronome tries - Try: {miss}`
- miss:
Integer
, number of milliseconds missed from desired frequency
- miss:
POST /games/:gameId/moves
No parameters required
WebSocket API
Required query parameters
Pass query parameters in the URL when connection to the server, e.g connect to the following URL:
ws://localhost:8081/?playerName=foo
Required parameters:
- playerName:
String
, the name the connecting player wants to use.
Error codes
If the connection cannot be accepted for any reason, the connection is closed with
code:4000, reason:'player-name-taken'
, if player is not allowed to connect due to the provided name not being available.
Messages
WebSocket API pushes messages as JSON objects encoded as UTF-8 strings.
The encoded JSON object has the following properties:
- eventName:
String
, name of the event - payload:
Object
, payload of the event
Example frame that might be sent to a connection:
'{"eventName":"connection:accepted","payload":{"playerId":"b00e6b69-ae49-431f-8dbd-12d7b5c95153"}}'
The following events might be sent
- connection:accepted:
{playerId}
, sent immediately after connection is opened.- playerId:
String
, an unique ID given to the connected player
- playerId:
- online-players:
Array[{id, name}]
, list of online players, sent whenever a player connects or disconnects.- id:
String
, unique identifier of the player - name:
String
, name of the player
- id: