-
Notifications
You must be signed in to change notification settings - Fork 1
/
routes.rb
194 lines (168 loc) · 5.79 KB
/
routes.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
require 'sinatra'
require 'json'
require_relative 'app/controllers/game_controller.rb'
require_relative 'app/controllers/storage_controller.rb'
enable :sessions
set :session_secret, 'got-board-game'
set :raise_errors, false
set :show_exceptions, false
# Routes that don't require authentication
UNPROTECTED_ROUTES = [
['post', '/session'],
['post', '/users']
]
def validate_constants(class_strings, expected_class)
begin
class_strings.each do |class_string|
actual_class = class_string.constantize
unless actual_class < expected_class
raise NameError.new(actual_class.to_s + ' is not a ' + expected_class.to_s, actual_class.to_s)
end
end
rescue NameError => e
raise e.name + ' is not a valid ' + expected_class.to_s
end
end
before do
if request.post?
begin
request.body.rewind
@data = JSON.parse(request.body.read)
rescue JSON::ParserError => e
halt('JSON input expected')
end
end
route = [request.request_method.downcase, request.path_info]
unless UNPROTECTED_ROUTES.include?(route)
session_id = session['session_id']
@username = StorageController.get_user_for_session(session_id)
end
end
before '/games/:game/?:path?' do |game_id, path|
games = StorageController.list_games(@username)
game = games.find { |game| game[:game_id] == game_id.to_i }
if game.nil?
halt(@username.to_s + ' does not have access to ' + game_id.to_s)
end
@game = GameController.new(StorageController.get_game(game_id))
@house_class = game[:house]
end
after '/games/:game/:path?' do |game_id, path|
if request.post?
StorageController.save_game(game_id, @game.game)
end
end
error do
e = env['sinatra.error']
headers 'Content-Type' => 'text/plain'
body e.message
end
# Get information about your current session
get '/session' do
{ :username => @username, :session_id => session['session_id'] }.to_json
end
# Log in
# Body: {"username":"jdoe","password":"password"}
post '/session' do
if !@data['username'] || !@data['password']
raise 'Format: {"username":"jdoe","password":"password"}'
end
username = @data['username']
password = @data['password']
session_id = session['session_id']
if StorageController.correct_password?(username, password)
StorageController.create_session(username, session_id)
{ :username => username, :session_id => session_id }.to_json
else
raise 'Incorrect username or password'
end
end
# List existing users
get '/users' do
StorageController.list_users.to_json
end
# Create a user
# Body: {"username":"jdoe","password":"password","player_name":"John"}
post '/users' do
if !@data['username'] || !@data['password'] || !@data['player_name']
raise 'Format: {"username":"jdoe","password":"password","player_name":"John"}'
end
username = @data['username']
password = @data['password']
player_name = @data['player_name']
StorageController.create_user!(username, password, player_name)
{ :username => username, :player_name => player_name }.to_json
end
# See information about an existing user
get '/users/:username' do |username|
StorageController.get_user(username).to_json
end
# Delete a user
delete '/users/:username' do |username|
unless @username == username
raise 'Cannot delete another user'
end
StorageController.delete_user(username)
end
# List existing games
get '/games' do
StorageController.list_games(@username).to_json
end
# Start a new game
# Body: {"HouseStark":"jdoe","HouseLannister":"jsmith","HouseBaratheon":"jjones"} or ["jdoe","jsmith","jjames"]
post '/games' do
if @data.is_a?(Hash)
validate_constants(@data.keys, House)
house_classes_to_usernames = @data.map { |house_class_string, username| [house_class_string.constantize, username] }.to_h
elsif @data.is_a?(Array)
random_house_classes = Game.allowed_house_classes_for_players(@data.length).shuffle
house_classes_to_usernames = random_house_classes.map.with_index { |house_class, i| [house_class, @data[i]] }.to_h
end
unless house_classes_to_usernames.has_value?(@username)
raise 'Cannot create a game that does not include yourself: ' + @username.to_s + ', ' + @data.to_s
end
g = Game.create_new(house_classes_to_usernames.keys)
houses = [HouseStark, HouseLannister, HouseBaratheon, HouseGreyjoy, HouseTyrell, HouseMartell]
house_usernames = houses.map do |house_class|
username = house_classes_to_usernames.fetch(house_class, nil)
end
game_id = StorageController.create_game(g, *house_usernames)
{ :game_id => game_id }.to_json
end
# See information about an existing game
get '/games/:game' do |game_id|
@game.game_info.to_json
end
# Place orders, execute orders, replace orders with Messenger Raven token
# Body: {"CastleBlack":"WeakMarchOrder","DragonstonePortToShipbreakerBay":"SpecialRaidOrder"}
post '/games/:game/orders' do |game_id|
validate_constants(@data.keys, Area)
validate_constants(@data.values, OrderToken)
orders = @data.map { |area_class_string, order_class_string| [area_class_string.constantize, order_class_string.constantize] }.to_h
orders.each { |area_class, order_class| @game.place_order!(@house_class, area_class, order_class) }
{ :game_id => game_id }.to_json
end
# Muster troops
# Body: {"Winterfell":[{"muster":"Knight"}],"Seagard":[{"upgrade":"Ship"},{"muster":"Footman"}]}
post '/games/:game/muster' do |game_id|
end
# Bid on influence track or wildling attack
# Body: 4
post '/games/:game/bid' do |game_id|
unless @data.is_a?(Integer)
raise 'Format: 4'
end
@game.bid!(@house_class, @data)
end
# Use Valyrian Steel Blade token
# Body: none
post '/games/:game/combat' do |game_id|
end
# Use Messenger Raven token to see the top card of the wildling deck
get '/games/:game/wildling_deck' do |game_id|
end
# Use Messenger Raven token to replace card in wildling deck
# Body: top
# Body: bottom
post '/games/:game/wildling_deck' do |game_id|
end