Skip to content
Drizzt1991 edited this page Jul 14, 2013 · 1 revision

Protocol

The protocol describes the methods and their purpose.

The idea

We use socksJS which emulates sockets. So the concept is about a duplex pipeline, but we will not use it exactly that way. We will separate the concept of SERVER and CLIENT.

  • SERVER is a notification service. It never actually forces the CLIENT for any action. It only performs NOTIFICATION and can ASK for some data.
  • CLIENT is the end, that interacts with the user. All CLIENT actions are ACTION's. Those require SERVER's ANSWER. If it does not provide one, then it is equal to disconnect. All ACTION's require ANSWER's.

Package structures

CLIENT ACTION examples

{'method':'CONNECT', data:null, id:18293}

SERVER ANSWER examples

{'status': 'OK', 'id':18293}

{'status': 'ERROR', 'message': 'You are BANNED. ^_^', 'id':18293}

SERVER NOTIFICATION example

{'method': 'BOARD_UPDATE', 'main_board': [...] 'sub_boards': [[...], ...] }

Entities

USER_ID is just an STR obj.

FIELD is an INT in range 0-9, where 1-9 means positions on the board (where 5 is center and 1 is upper left corner) and 0 means any field on the board.

BOARD is an 9 elements array of INT's in range 0-2 where 0 means free field and 1,2 stand for CROSS and NOUGHT respectively. Element indexes (by formula index+1) correspond to FIELD's.

GAME_STATUS is one of 'FINISHED', 'ABANDONED', 'IN_PROCESS'

USER

fields:
    remote_address: `STR`
    user_id: `USER_ID`
    user_name: `STR`      | READWRITE

GAME

fields:
    opponent: `USER`
    cur_user: `USER_ID`
    cur_board:`FIELD`
    board_data: `FULL_BOARD_DATA`
    status: `GAME_STATUS`

FULL_BOARD_DATA

fields:
    main_board: `BOARD`, 
    sub_boards: [9]`BOARD`

MOVE

fields:
    board: `FIELD`, # Which board to perform a move on.
    field: `FIELD`  # Field in this board.

GAME_RESULT

fields:
    winner: `USER_ID`, 
    status: `GAME_STATUS`,
    reason: `STR`  # Reason to abandon game if that status is set.

METHOD's Reference

CONNECT


type: 
    METHOD
description:
    Initializes a connection to the SERVER. If an active connection is present SERVER treats this as a reconnect method and performs disconnect and then another connect. 
answer:
    OK|ERROR

DISCONNECT


type:
    METHOD
description:
    Graceful close of connection. It free's server's running games and sends a notification (TODO: WHAT NOTIFICATION) to the other player
answer:
    OK|ERROR

START_GAME


type:
    METHOD
description:
    Ask SERVER to initialize a new GAME. If there is a current running game, then the SERVER will ignore it
and send `error` ANSWER to the client. To start a new game you must send END_GAME first, or perform DISCONNECT and wait for the game to end by the timeout.
answer:
    OK|ERROR

END_GAME


type:
    METHOD
description:
    Ask SERVER to close a new GAME. If there is no currently running game, then the SERVER will ignore it
and send `error` ANSWER to the client.
answer:
    OK|ERROR

GET_USER_INFO


type:
    METHOD
description:
    Ask SERVER to give information for the user.
answer:
    OK|ERROR + `USER` as data

UPDATE_USER_INFO


type:
    METHOD
description:
    Ask SERVER to update information for the user. 
data:
    updated fields of `USER`
answer:
    OK|ERROR

GET_GAME_INFO


type:
    METHOD
description:
    Ask SERVER to give information on the current running game. If no game running will return error.
answer:
    OK|ERROR + `GAME` as data

MOVE


type:
    METHOD
description:
    Perform a move on the board. There can be 3 error's in this case:
        - It is not your turn
        - Board is not allowed
        - Field is already set
data:
    A `MOVE` entity
answer:
    OK|ERROR

NOTIFICATION's reference

GAME_STARTED


type:
    NOTIFICATION
description:
    When issuing the START_GAME method CLIENT should expect GAME_STARTED after the opponent is found.
data:
    `GAME` entity

GAME_ENDED


type:
    NOTIFICATION
description:
    Issued when someone win's the game or when opponents leave the game (gracefully or game ended by disconnect).
data:
    `GAME_RESULT` entity

BOARD_UPDATE


type:
    NOTIFICATION
description:
    Issued when someone makes a move on the board, that is allowed.
data:
    `FULL_BOARD_DATA` entity