Class: Gameworks::Servlet::AddPlayer

Inherits:
Base
  • Object
show all
Defined in:
lib/gameworks/servlet/add_player.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize, #method_not_allowed, process

Constructor Details

This class inherits a constructor from Gameworks::Servlet::Base

Instance Method Details

#POST(request) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/gameworks/servlet/add_player.rb', line 6

def POST(request)
  game_id = request[:path].split('/')[1]
  game = @server.game_registry.instance(game_id)
  return [404, {}, ["no such game"]] unless game

  if game.state == Gameworks::Game::STATE_INITIATING
    if player = game.add_player(request[:payload])
      game.start_if_ready
      player.wait_for_turn do |turn_token|
        request[:async_cb].call [ 200, {
          'Content-Type' => 'application/json',
          'X-Player-ID' => player.id,
          'X-Turn-Token' => turn_token
        }, [game.to_json(player)] ]
      end
      [-1, {}, []]
    else
      [403, {}, ["invalid player data"]]
    end
  else
    [410, {}, ["game already started"]]
  end
end