timpei / hanabi

An implementation of Hanabi, a popular board game. Starts with a web client, but can be integrated with multiple platforms through the server API.
8 stars 3 forks source link

An implementation of Hanabi, a popular board game. Starts with a web client, but can be integrated with multiple platforms through the server API.

Requirements

Running Locally

To run on your local machine:

  1. Create a virtual environment and start it:

    cd hanabi-isotrophic
    virtualenv hanabi-env
    source hanabi-env/bin/activate
  2. Install python dependencies:

    pip install -r requirements.txt
  3. Setup environment:

    echo "export DATABASE_URL=postgres:///$(whoami)" > env.sh
    chmod +x env.sh
    . env.sh

Server Response

The server will always return socket payloads in a consistent structure:

{
    'event': event String,
    'game': game Object,
    'message': message Object
}

Game Object

API calls will often return a game object as the response. Here is one for example:

{
   "currentPlayer": "Tim",
   "discarded": [],
   "hasEnded": false,
   "hasStarted": true,
   "id": 4,
   "isRainbow": false,
   "numCardsRemaining": 34,
   "numHints": 7,
   "numLives": 3,
   "order": [
       "Curtis",
       "Jackie",
       "Tim"
   ],
   "played": [
       {"number": 1, "suit": "BLUE", "knownSuit": [], "knowNumber": true}
   ],
   "players": [
       {
           "hand": [
               {"number": 5, "suit": "YELLOW", "knownSuit": [], "knowNumber": false},
               {"number": 3, "suit": "BLUE", "knownSuit": [], "knowNumber": false},
               {"number": 4, "suit": "YELLOW", "knownSuit": [], "knowNumber": false},
               {"number": 1, "suit": "WHITE", "knownSuit": [], "knowNumber": true},
               {"number": 1, "suit": "BLUE", "knownSuit": [], "knowNumber": true}
           ],
           "name": "Curtis"
       },
       {
           "hand": [
               {"number": 2, "suit": "WHITE", "knownSuit": [], "knowNumber": false},
               {"number": 1, "suit": "GREEN", "knownSuit": [], "knowNumber": false},
               {"number": 1, "suit": "YELLOW", "knownSuit": [], "knowNumber": false},
               {"number": 3, "suit": "GREEN", "knownSuit": [], "knowNumber": false},
               {"number": 3, "suit": "WHITE", "knownSuit": [], "knowNumber": false}
           ],
           "name": "Tim"
       },
       {
           "hand": [
               {"number": 2, "suit": "YELLOW", "knownSuit": [], "knowNumber": false},
               {"number": 5, "suit": "BLUE", "knownSuit": [], "knowNumber": false},
               {"number": 5, "suit": "WHITE", "knownSuit": [], "knowNumber": false},
               {"number": 2, "suit": "BLUE", "knownSuit": [], "knowNumber": false},
               {"number": 2, "suit": "RED", "knownSuit": [], "knowNumber": false}
           ],
           "name": "Jackie"
       }
   ],
   "score": 1,
   "spectators": [
       {"name": "Tony"},
       {"name": "Meng"}
   ],
   "turnsLeft": -1
}

Some of the JSON fields are further discussed below:

Message Object

A message object contains information about the broadcased event:

{
    'name': sender name String,
    'type': message type,
    'time': timestamp,
    'message': pre-constructed message string,
    'elements': supplimentary data object
}

API Calls

REST

Get Game

Get current game object.

Socket.IO

In order to receive websocket payloads from SocketIO, the client must connect to the socket once he/she creates or enters the game. The socket namespace is the global namespace: http://<domain_name>. All emits from the server will be sent with the 'message' event.

Create Game

Create and join a new game.

Enter Game

Enter a game room. Player will become a spectator.

Resume Game

Resume or re-join a game. Player must be already a player or spectator prior to the call.

Join Game

Join a game. Player must be a spectator prior to the call. Will fail if number of joined players is at max (5).

Leave Game

Leave the game, i.e. not receiving future socket messages from the game room. Player can always resume game later.

Start Game

Start a game. Can only start a game that hasn't been started yet. This will create a deck and deal cards to players.

Send Message

Send a message to everyone in the game.

Give Hint

Gives a hint to another player. Must be the player's turn to play. hintType can either be "number" or "colour".

Discard Card

Play Card

End Game