get '/blackjack/*' do
if params[:splat] == "/hit" and defined? session[:bj_game]
erb :blackjack
elsif params[:splat] == "/fold" and defined? session[:bj_game]
session[:bj_hum].fold = true
erb :blackjack
else
if defined? session[:bj_game]
new_session_check
score_check
erb :blackjack
else
session[:bj_game] = false
session[:score] = 0
new_session_check
score_check
erb :blackjack
end
end
end
def new_session_check
if session[:bj_game] == false
session[:bj_hum] = Blackjack.new
session[:bj_com] = Blackjack.new
session[:bj_game] = true
end
end
def score_check
if session[:bj_hum].game_loop == false
if session[:bj_hum].score.to_i > 0
session[:score] += session[:bj_hum].score.to_i
check_save(session[:score])
else
session[:score] = 0
end
session[:bj_game] = false
session[:bj_hum] = Blackjack.new
session[:bj_com] = Blackjack.new
end
end
Whenever it gets to score check, I get NoMethodError - Undefined method 'game_loop' for NilClass.
However, if I start from the main page:
get '/' do
session[:bj_game] = false
session[:score] = 0
erb :home
end
And click on the link to /blackjack from the home erb, it will work as the new_session_check saw that the variable was false and then created new instances of the Blackjack class(which does have attr_accessor for game_loop).
Why does it not register in the get '/blackjack' version?
http://pastebin.com/6EFpp5gh - Here is a mockup version you can run to verify this. First go to localhost:4567/blackjack and you will get an internal service error. Restart the server then go to localhost:4567 first and then localhost:4567/blackjack afterward and you will see that it works.