-
Notifications
You must be signed in to change notification settings - Fork 0
/
da_bomb.rb
58 lines (43 loc) · 1.21 KB
/
da_bomb.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
Encoding.default_external = Encoding::UTF_8
class DaBomb < Sinatra::Base
VERSION = '0.1.0'
before do
content_type 'application/json'
end
get '/' do
status 200
return "Hi... this is DaBomb server v. #{VERSION}"
end
post '/players' do
player = Player.new(:username => Player.next)
player.register
status 201
return Yajl::Encoder.encode({:username => player.username})
end
post '/players/:username/retire' do |username|
player = Player.new(:username => username)
player.retire
status 204
end
post '/play/:username' do |username|
player = Player.new({:username => username})
status 200
return Yajl::Encoder.encode({:code => player.pair})
end
post '/defuse/:code' do |code|
username = params[:username]
time = params[:time]
if username.blank? || time.blank?
status 400
return Yajl::Encoder.encode({:error => 'Please, provide player username and defuse time', :error_code => 'E.02'})
end
match = Match.from_code(code)
match.defuse(Player.new(:username => username), time)
status 204
end
get '/leaderboard' do
leaders = Leaderboard.top
status 200
Yajl::Encoder.encode(leaders)
end
end