GameInterface API Reference (Server) - djungowski/node-battleship GitHub Wiki

GameInterface Methods

GameInterface::setPlayerName(name)

Sets your player name.

Argument type description
name String your new name

returns: void

GameInterface::getOpponentName()

Gets the name your opponent.

returns: String opponent name

GameInterface::place(ship, x, y, orientation)

Places a given ship on the playingfield.

Argument type description
ship String the type of the ship you would like to place, e.g. "carrier", "cruiser"...
x Integer horizontal coordinate component
y Integer  vertical coordinate component
orientation Integer  Ship orientation. Either 0 for horizontal or 1 for vertical

returns: void

GameInterface::getPlacements()

Get a list of current ship placements on the playingfield.

returns: Array array of placement objects.

A placement object typically looks like this:

{
  "ship":{
    "type":"cruiser",
    "hits":0,
    "size":3
  },
  "x":5,
  "y":5,
  "orientation":0
}

GameInterface::finishPlacement()

Tells the game that you are finished placing your ships and would like to start playing the game.

returns: void

GameInterface::shoot(x, y)

Shoot at your opponent's playing field, trying to hit one of his ships.

Argument type description
x Integer horizontal coordinate component
y Integer  vertical coordinate component

returns: object typical structure:

{
  "hit":false,
  "x":5,
  "y":6
}

if a ship was hit, the above object will additionally contain a ship offset containing a ship description object.

throws an error, if the game has not yet been started (placement phase) or has already ended (one of the players lost all of his ships).

GameInterface Events

The GameInterface will emit events under certain conditions to inform the player, most of the time as a result of opponent interaction.

The GameInterface extends the node.js EventEmitter class (see also http://nodejs.org/api/events.html#events_class_events_eventemitter), so basically all you have to do to receive these events is call the GameInterface::on(event, callback) method and pass in a corresponding callback function, which will then be called when the event is triggered.

In some cases, the callback will also receive arguments described along with the events below.

gameStart

...is triggered when both players have finished placing their ships on the playing field, and the game itself is beginning. The server will now choose one of the players randomly to start the game.

activate

...is triggered when your opponent's move is over, and your turn has begun. You should remove any protection from the user interface, and signal to the user that it is now his turn.

deactivate

...is triggered when your move is over, and your opponent's turn has begun. You should in some way show the user that he must now wait for his opponent to make his move.

opponentNameChanged(name)

...is triggered when your opponent has successfully entered his player name. This usually happens during game setup. You should update the user interface with the new player name.

Arguments: name : String the new opponent name

hit(strike)

...is triggered when your opponent has hit one of your ships.

Arguments: strike : Object describes what your opponent has hit. typically looks like:

{
  "hit":true,
  "x":5,
  "y":6,
  "ship":{
    "hits":1,
    "size":5,
    "type":"carrier"
  }
}

miss(shot)

...is triggered when your opponent has shot your playing field, but not hit any of your ships.

Arguments: shot : Object describes where your opponent has shot. typically looks like:

{
  "hit":false,
  "x":5,
  "y":6
}

sinking

...is triggered when your opponent has hit one of your ships, and the ship is now sinking.

win

...is triggered when you have successfully destroyed all of your opponent's ships. This also signals the end of the game.

lose

...is triggered when your opponent has destroyed all of your ships. Game Over.