forked from saasbook/hw-sinatra-saas-wordguesser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
79 lines (66 loc) · 1.98 KB
/
app.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
require 'sinatra/base'
require 'sinatra/flash'
require './lib/hangperson_game.rb'
class HangpersonApp < Sinatra::Base
enable :sessions
register Sinatra::Flash
before do
@game = session[:game] || HangpersonGame.new('')
end
after do
session[:game] = @game
end
# These two routes are good examples of Sinatra syntax
# to help you with the rest of the assignment
get '/' do
redirect '/new'
end
get '/new' do
erb :new
end
post '/create' do
# NOTE: don't change next line - it's needed by autograder!
word = params[:word] || HangpersonGame.get_random_word
# NOTE: don't change previous line - it's needed by autograder!
@game = HangpersonGame.new(word)
redirect '/show'
end
# Use existing methods in HangpersonGame to process a guess.
# If a guess is repeated, set flash[:message] to "You have already used that letter."
# If a guess is invalid, set flash[:message] to "Invalid guess."
post '/guess' do
letter = params[:guess].to_s[0]
begin
if [email protected](letter)
flash[:message] = "You have already used that letter."
end
rescue ArgumentError
flash[:message] = "Invalid guess."
end
case @game.check_win_or_lose
when :win
redirect '/win'
when :lose
redirect '/lose'
else
redirect '/show'
end
end
# Everytime a guess is made, we should eventually end up at this route.
# Use existing methods in HangpersonGame to check if player has
# won, lost, or neither, and take the appropriate action.
# Notice that the show.erb template expects to use the instance variables
# wrong_guesses and word_with_guesses from @game.
get '/show' do
redirect '/new' unless [email protected]?
erb :show
end
get '/win' do
redirect '/show' unless [email protected]? && @game.check_win_or_lose == :win
erb :win
end
get '/lose' do
redirect '/show' unless [email protected]? && @game.check_win_or_lose == :lose
erb :lose
end
end