-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rb
58 lines (48 loc) · 1.17 KB
/
server.rb
1
2
3
4
5
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
require "sinatra"
require "sinatra/reloader"
require "sinatra/content_for"
require "sinatra/json"
require_relative "classes/bowling.rb"
configure do
enable :sessions
set :session_secret, "big secret"
end
set :port, 5000
before do
session[:inputs] ||= []
end
helpers do
INPUT_NAMES = { "X" => "strike", "/" => "spare", "-" => "miss" }
def reset_game_from_state(inputs)
game = Bowling.new
inputs.each { |input| game.play(INPUT_NAMES[input] || input) }
game
end
def format_state(game)
state = {
frame_scores: game.frame_scores,
inputs: game.inputs,
frame: game.frame,
roll_left: game.rolls_left,
next_set_of_pins: game.next_set_of_pins
}
end
end
# reconstruct game from session data
get "/api/game" do
game = reset_game_from_state(session[:inputs])
json format_state(game)
end
# reset game
get "/api/new_game" do
game = Bowling.new
session[:inputs] = []
json format_state(game)
end
# reconstruct game from session data and add new roll
post "/api/play/:input" do
game = reset_game_from_state(session[:inputs])
game.play(params[:input])
session[:inputs] = game.inputs.flatten
json format_state(game)
end