Class: Gameworks::Servlet::AddMove

Inherits:
Base
  • Object
show all
Defined in:
lib/gameworks/servlet/add_move.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gameworks/servlet/add_move.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_IN_PLAY
    player = game.player_for_token(request[:tokens][:turn])
    if player
      snapshot = game.snapshot
      legal, error = game.add_move(request[:payload], player)
      if legal
        player.wait_for_turn do |turn_token|
          EventMachine.add_timer(game.delay_time) do
            request[:async_cb].call [ 200, {
              'Content-Type' => 'application/json',
              'X-Turn-Token' => turn_token
            }, [game.delta(snapshot, player).to_json] ]
          end
        end
        [-1, {}, []]
      else
        # invalid move!
        game.disqualify(player)
        [403, {}, ["invalid move: #{error}. disqualified!"]]
      end
    else
      # not your turn!
      [403, {}, ["invalid/missing turn token"]]
    end
  else
    if game.state == Gameworks::Game::STATE_INITIATING
      # not yet allowed
      [403, {}, ["game not yet started"]]
    else
      # no longer available
      [410, {}, ["game finished"]]
    end
  end
end