-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rb
58 lines (50 loc) · 1.68 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 'pry'
enable :sessions
#############
# FUNCTIONS #
#############
use Rack::Session::Cookie, {
secret: "keep_it_secret_keep_it_safe"
}
def winner(params)
message = ""
case true
when params.has_value?("rock") && params.has_value?("paper")
message = "#{params.invert["rock"]} chose rock, #{params.invert["paper"]} chose paper. #{params.invert["paper"]} scores."
session["#{params.invert["paper"]}"] += 1
when params.has_value?("scissors") && params.has_value?("rock")
message = "#{params.invert["scissors"]} chose scissors, #{params.invert["rock"]} chose rock. #{params.invert["rock"]} scores."
session["#{params.invert["rock"]}"] += 1
when params.has_value?("paper") && params.has_value?("scissors")
message = "#{params.invert["paper"]} chose paper, #{params.invert["scissors"]} chose scissors. #{params.invert["scissors"]} scores."
session["#{params.invert["scissors"]}"] += 1
else message = "Human chose #{params["Human"]}, A.I. chose #{params["A.I."]}. Tie, no winner."
end
message
end
###############
# CONTROLLERS #
###############
get '/' do
redirect '/home'
end
get '/home' do
if params["message"]
erb :home, locals: { message: params["message"], score: { "Human" => session["Human"] , "A.I." => session["A.I."] } }
else
session["Human"] = 0
session["A.I."] = 0
erb :home, locals: { message: "" , score: { "Human" => session["Human"] , "A.I." => session["A.I."] } }
end
end
post '/home' do
if params["restart"]
redirect '/'
else
params["A.I."] = ["rock", "paper", "scissors"].sample
message = winner(params)
query = "message=#{message.gsub(' ', '%20')}"
redirect "/home?#{query}"
end
end