Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

102 lines (69 sloc) 1.832 kB
require "sinatra"
require "sequel"
Sequel::Model.db = Sequel.connect("sqlite://hangman.db")
$LOAD_PATH << "./models"
require "game"
require "guess"
require "match"
require "word"
enable :sessions
# The homepage. Allow the player to resume its current game if any.
get "/" do
game_token = session[:game_token]
@game = Game.find_running_by_token(game_token)
haml :home
end
# Archives.
get "/games" do
@games = Game.order(:id).reverse
haml :games
end
# Create a new game for the user.
# Save the token in the session to resume a started game.
post "/games" do
game = Game.start
session[:game_token] = game.token
redirect to("/games/#{game.token}")
end
# Display a game page. If the game is the current one of the
# player, show a page to play. Else show the archive page.
get "/games/:token" do
game_token = params[:token]
@game = Game.find_by_token(game_token)
@can_play = @game.token == session[:game_token]
@word = @game.word
haml @can_play ? :my_game : :game
end
# Submit a guess.
post "/games/:token/guesses" do
letter = params[:letter].strip.downcase
game_token = session[:game_token]
game = Game.find_running_by_token(game_token)
game.guess(letter)
redirect to("/games/#{game.token}")
end
# Display the words of the database.
get "/words" do
@words = Word.all
haml :words
end
# Form to add a new word in the database.
get "/words/new" do
haml :add_word
end
# Add the word in the database if it's no already in.
post "/words" do
content = params[:content].downcase
if content != ""
Word.find_or_create_by_content(content)
redirect to("/words")
else
haml :add_word
end
end
get "/application.css" do
scss :application
end
get "/application.js" do
coffee :application
end
Jump to Line
Something went wrong with that request. Please try again.