The game sotre requires:
* state - inital state object.
* mutate - contains all state mutation logic.
* update - dispatches actions when game state passes certain conditions.
* game - game instance.
* onReady - calls when host is ready.
* onConnection - new client has connected.
* onDisconnect - client has disconnected.
* onDispatch - client dispatched an action.
// Create action for adding player to game state.
const action = addPlayer(playerID);
// Execute dispatch locally and send to other peer players.
host.game.dispatch(action);
host.socket.connections.forEach(connection => {
if (connection !== connectionID) {
host.socket.dispatch(connection, action);
}
});
// Get current game state and accept player connection.
const state = host.game.getState();
host.socket.connection(connectionID, { playerID, state });
},
* game - game instance.
* onReady - calls when host is ready.
* onConnection - new client has connected.
* onDisconnect - client has disconnected.
* onDispatch - client dispatched an action.