-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
executable file
·65 lines (55 loc) · 1.69 KB
/
app.rb
File metadata and controls
executable file
·65 lines (55 loc) · 1.69 KB
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
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'melora'
require 'sinatra'
require 'redis-store'
get '/' do
"What's an updog?"
end
helpers do
def redis
@redis ||= Redis::Store.new
end
def humans
%w(dm player)
end
def push_roll_to_redis(params, result)
game_id = params[:game].gsub(/[^\w]/, '')
# should probably do more validation on dice_string incase melora ever stops
redis.lpush("roll:#{game_id}",
"#{@player_name}: #{params[:dice_string]}: #{result.join(', ')}")
result.join ', '
end
def protected!
return if authorized?
headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
halt 401, "Not authorized\n"
end
def authorized?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
halt 401 unless @auth.provided? && @auth.basic? && @auth.credentials
halt 401 unless humans.include? @auth.credentials.last
@player_name = @auth.credentials.first
end
end
get '/roll/:game/:dice_string' do
pool = Melora::DicePool.new(Melora::String.parse_d_notation_string(params[:dice_string]))
result = pool.roll
push_roll_to_redis params, result
end
get '/roll-without-explosions/:game/:dice_string' do
protected!
d_notation_hash = Melora::String.parse_d_notation_string(params[:dice_string])
pool = Melora::DicePool.new(d_notation_hash.merge(exploding: false))
result = pool.roll
push_roll_to_redis params, result
end
get '/view_rolls/:game' do
game_id = params[:game].gsub(/[^\w]/, '')
@roll_list = redis.lrange("roll:#{game_id}", 0, -1)
erb :view_rolls
end
get '/roll/:dice_string' do
pool = Melora::DicePool.new(Melora::String.parse_d_notation_string(params[:dice_string]))
pool.roll.join ', '
end