Class: Gameworks::Server
- Inherits:
-
Object
- Object
- Gameworks::Server
- Defined in:
- lib/gameworks/server.rb
Instance Attribute Summary collapse
-
#game_registry ⇒ Object
readonly
Returns the value of attribute game_registry.
Instance Method Summary collapse
- #call(env) ⇒ Object
- #game_class ⇒ Object
-
#initialize ⇒ Server
constructor
A new instance of Server.
- #process_request(request = {}) ⇒ Object
Constructor Details
#initialize ⇒ Server
Returns a new instance of Server.
14 15 16 |
# File 'lib/gameworks/server.rb', line 14 def initialize @game_registry = Gameworks::GameRegistry.new end |
Instance Attribute Details
#game_registry ⇒ Object (readonly)
Returns the value of attribute game_registry.
12 13 14 |
# File 'lib/gameworks/server.rb', line 12 def game_registry @game_registry end |
Instance Method Details
#call(env) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/gameworks/server.rb', line 22 def call(env) tokens = {} env.each do |key,value| case key when "HTTP_X_TURN_TOKEN" tokens[:turn] = value when "HTTP_X_OBSERVER_TOKEN" tokens[:observer] = value end end body = env["rack.input"].read unless body.empty? payload = JSON.parse("[#{body}]").first end process_request( :method => env["REQUEST_METHOD"], :path => env["REQUEST_PATH"], :payload => payload, :tokens => tokens, :async_cb => env['async.callback']) end |
#game_class ⇒ Object
18 19 20 |
# File 'lib/gameworks/server.rb', line 18 def game_class raise NotImplementedError end |
#process_request(request = {}) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/gameworks/server.rb', line 44 def process_request(request={}) handler = case request[:path] when %r{^/$} then Gameworks::Servlet::GameList when %r{^/match$} then Gameworks::Servlet::MatchMaker when %r{^/[^/]+/?$} then Gameworks::Servlet::GameView when %r{^/[^/]+/players$} then Gameworks::Servlet::AddPlayer when %r{^/[^/]+/moves$} then Gameworks::Servlet::AddMove when %r{^/[^/]+/observers$} then Gameworks::Servlet::AddObserver end if handler begin handler.process(self, request) rescue Exception => e puts e.inspect puts e.backtrace [ 500, {}, [] ] end else [ 404, {}, [] ] end end |