Created
January 29, 2012 01:58
-
-
Save anonymous/1696733 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative "map" | |
require "sinatra" | |
module Gothonweb | |
use Rack::Session::Pool unless test? | |
#enable :sessions unless test? | |
get '/' do | |
session[:room] = START | |
redirect "/game" | |
end | |
get '/game' do | |
if session[:room] | |
p "inside session room" | |
erb :show_room, :locals => {:room => session[:room]} | |
else | |
erb :you_died | |
end | |
end | |
post '/game' do | |
action = "#{params[:action] || nil}" | |
p "Session room: #{session[:room]}" | |
if session[:room] | |
puts "Got inside the session of the post!" | |
if session[:room].go(action) | |
session[:room] = session[:room].go(params[:action]) | |
else | |
session[:room] = session[:room].go('*') | |
end | |
end | |
redirect "/game" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Room | |
attr_accessor :name, :description, :paths | |
def initialize(name, description) | |
@name = name | |
@description = description | |
@paths = {} | |
end | |
def go(direction) | |
@paths[direction] | |
end | |
def add_paths(paths) | |
@paths.update(paths) | |
end | |
end | |
$central_corridor = Room.new("Central Corridor", | |
%q{ | |
The Gothons of planet Percal #25 have invaded your ship and destroyed | |
your entire crew. You are the last surviving member and your last | |
mission is to get the neutron destruct bomb from the Weapons armory, | |
put it in the bridge, and blow the ship up after getting into an | |
escape pod. | |
You are running down the central corridor to the Weapons Armory when | |
a Gothon jumps out, red scaly skin, dark grimly teeth, and evil clown costume | |
flowing around his hate filled body. He's blocking the door to the | |
Armory and about to pull a weapon to blast you. | |
}) | |
def generate_death | |
quips = [ "You died. You kinda suck at this.", | |
"Nice job, you died...jackass.", | |
"Such a luser.", | |
"I have a small puppy that's better at this."] | |
quips[rand(quips.length())] | |
end | |
$generic_death = Room.new("death", generate_death()) | |
$central_corridor.add_paths({ | |
'shoot!' => $generic_death, | |
'dodge!' => $generic_death, | |
'*' => $generic_death | |
}) | |
#use functions to make calling these classes as shown in the text | |
def generic_death | |
$generic_death | |
end | |
def central_corridor | |
$central_corridor | |
end | |
START = central_corridor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ENV['RACK_ENV'] = 'test' | |
require 'test/unit' | |
require 'rack/test' | |
require_relative 'gothonweb' | |
require_relative 'map' | |
class GothonWebTests < Test::Unit::TestCase | |
include Rack::Test::Methods | |
def assert_response(resp, contains=nil, matches=nil, headers=nil, status=200) | |
assert_equal(resp.status, status, "Expected response #{status} not in #{resp}") | |
if status == 200 | |
assert(resp.body, "Response data is empty.") | |
end | |
if contains | |
assert((resp.body.include? contains), "Response does not contain #{contains}") | |
end | |
if matches | |
reg = Regexp.new(matches) | |
assert reg.match(contains), "Response does not match #{matches}" | |
end | |
if headers | |
assert_equal(resp.headers, headers) | |
end | |
end | |
def app | |
Sinatra::Application | |
end | |
def test_get_game #passes | |
session = {} | |
session[:room] = central_corridor | |
get '/game',{},'rack.session' => session | |
assert_response(last_response, contains='Central Corridor') | |
end | |
def test_post_game #fails | |
action = 'shoot!' | |
room = central_corridor | |
session = {} | |
session[:room] = room | |
post '/game', :action => action, 'rack.session' => session | |
follow_redirect! | |
p "After follow_redirect!" | |
p last_response | |
assert_response(last_response, contains=generic_death.description) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment