-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rb
208 lines (178 loc) · 5.38 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
require 'sinatra'
require 'json'
require 'securerandom'
require 'mongo'
require './master.rb'
include Mongo
MONGO_LOCK = {};
# Arbitrary score calculation
# def calculate_score(guesses, time)
# return guesses*10 + time < 300
# end
conn = Mongo::Client.new('mongodb://az-mastermind:[email protected]:59888/heroku_dwbnqjb5')
collection = conn[:games]
# Format post params when content_type is application/json or text/plain.
before do
headers 'Access-Control-Allow-Origin' => '*'
if request.content_type == "application/json"
request.body.rewind
params.merge!(JSON.parse(request.body.read))
end
end
options('/*') do
status 200
return
end
get('/') do
content_type :json
{ :ping => "pong" }.to_json
end
post('/new_game') do
content_type :json
new_game_key = SecureRandom.urlsafe_base64(50)
new_code = Game.new.answer.code
unless params['user']
status 400
body "Please provide a user parameter in your post request"
return
end
collection.insert_one({
:user => params[:user],
:game_key => new_game_key,
:num_guesses => 0,
:answer_code => new_code,
:start_time => Time.now,
:solved => 'false',
:colors => Code.colors,
:code_length => Code.num_pegs,
:past_results => []
})
return {
:game_key => new_game_key,
:num_guesses => 0,
:solved => 'false',
:colors => Code.colors,
:code_length => Code.num_pegs,
:past_results => []
}.to_json
end
post('/guess') do
content_type :json
game_key = params['game_key']
while MONGO_LOCK[game_key]
sleep 0.5
end
MONGO_LOCK[game_key] = true
unless game_key
status 400
MONGO_LOCK[game_key] = false
body "Could not find game key. Please post with proper key!"
return
end
player_guess = Code.sanitize(params['code'])
unless params['code'] && player_guess
status 400
MONGO_LOCK[game_key] = false
body "Invalid code submission. Please post with code parameter consisting of 8 letters of RBGYOPCM"
return
end
game = collection.find({ 'game_key' => game_key }).first()
if (Time.now - game['start_time']) > 60*30 # 30 minute time limit.
MONGO_LOCK.delete(game_key)
return {
:user => game['user'],
:game_key => game_key,
:num_guesses => game['num_guesses'],
:past_results => game['past_results'],
:start_time => game['start_time'],
:solved => 'false',
:colors => Code.colors,
:code_length => Code.num_pegs,
:result => "This game has expired (30 minute window). Please start a new game."
}.to_json
end
unless game
status 400
MONGO_LOCK[game_key] = false
body "Could not find game corresponding to provided game_key!"
return
end
if game['solved'] == 'true'
MONGO_LOCK.delete(game_key)
return {
:user => game['user'],
:game_key => game_key,
:num_guesses => game['num_guesses'],
:past_results => game['past_results'],
:start_time => game['start_time'],
:end_time => game['end_time'],
:time_taken => game['time_taken'],
:solved => 'true',
:colors => Code.colors,
:code_length => Code.num_pegs,
:result => "This game has already been solved.",
:further_instructions => "Awesome work! Looking for a job or just want to say hi? Email your game_key and solution to [email protected] and we'll get in touch with you."
}.to_json
end
answer_code = game['answer_code']
game_object = Game.new(answer_code)
result = game_object.display_matches(player_guess)
past_results = game['past_results'] << { :guess => player_guess, :exact => result[0], :near => result[1] }
collection.find(:game_key => game_key).find_one_and_update({
'$set' => {
:user => game['user'],
:game_key => game_key,
:answer_code => answer_code,
:num_guesses => game['num_guesses'] + 1,
:start_time => game['start_time'],
:solved => 'false',
:colors => Code.colors,
:code_length => Code.num_pegs,
:past_results => past_results
}
})
if game_object.win?(player_guess)
time_taken = Time.now - game['start_time']
collection.find(:game_key => game_key).update_one({
:user => game['user'],
:game_key => game_key,
:answer_code => answer_code,
:num_guesses => game['num_guesses'],
:past_results => past_results,
:start_time => game['start_time'],
:end_time => Time.now,
:time_taken => time_taken,
:colors => Code.colors,
:code_length => Code.num_pegs,
:solved => 'true'
})
MONGO_LOCK.delete(game_key)
return {
:user => game['user'],
:game_key => game_key,
:num_guesses => game['num_guesses'],
:past_results => past_results,
:guess => player_guess,
:time_taken => time_taken,
:solved => 'true',
:colors => Code.colors,
:code_length => Code.num_pegs,
:result => "You win!",
:further_instructions => "Awesome work! Looking for a job or just want to say hi? Email your solution to [email protected] and we'll get in touch with you."
}.to_json
end
MONGO_LOCK[game_key] = false
return {
:game_key => game_key,
:num_guesses => game['num_guesses'],
:past_results => past_results,
:guess => player_guess,
:solved => 'false',
:colors => Code.colors,
:code_length => Code.num_pegs,
:result => {
:exact => result[0],
:near => result[1]
}
}.to_json
end