classRoomattr_accessor:name,:description,:pathsdefinitialize(name,description)@name=name@description=description@paths={}enddefgo(direction)@paths[direction]enddefadd_paths(paths)@paths.update(paths)endendcentral_corridor=Room.new("Central Corridor",%q{The Gothons of Planet Percal #25 have invaded your ship and destroyedyour entire crew. You are the last surviving member and your lastmission 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're running down the central corridor to the Weapons Armory whena Gothon jumps out, red scaly skin, dark grimy teeth, and evil clown costumeflowing around his hate filled body. He's blocking the door to theArmory and about to pull a weapon to blast you.})laser_weapon_armory=Room.new("Laser Weapon Armory",%q{Lucky for you they made you learn Gothon insults in the academy.You tell the one Gothon joke you know:Lbhe zbgure vf fb sng, jura fur fvgf nebhaq gur ubhfr, fur fvgf nebhaq gur ubhfr.The Gothon stops, tries not to laugh, then busts out laughing and can't move.While he's laughing you run up and shoot him square in the headputting him down, then jump through the Weapon Armory door.You do a dive roll into the Weapon Armory, crouch and scan the roomfor more Gothons that might be hiding. It's dead quiet, too quiet.You stand up and run to the far side of the room and find theneutron bomb in its container. There's a keypad lock on the boxand you need the code to get the bomb out. If you get the codewrong 10 times then the lock closes forever and you can'tget the bomb. The code is 3 digits.})the_bridge=Room.new("The Bridge",%q{The container clicks open and the seal breaks, letting gas out.You grab the neutron bomb and run as fast as you can to thebridge where you must place it in the right spot.You burst onto the Bridge with the netron destruct bombunder your arm and surprise 5 Gothons who are trying totake control of the ship. Each of them has an even uglierclown costume than the last. They haven't pulled theirweapons out yet, as they see the active bomb under yourarm and don't want to set it off.})escape_pod=Room.new("Escape Pod",%q{You point your blaster at the bomb under your armand the Gothons put their hands up and start to sweat.You inch backward to the door, open it, and then carefullyplace the bomb on the floor, pointing your blaster at it.You then jump back through the door, punch the close buttonand blast the lock so the Gothons can't get out.Now that the bomb is placed you run to the escape pod toget off this tin can.You rush through the ship desperately trying to make it tothe escape pod before the whole ship explodes. It seems likehardly any Gothons are on the ship, so your run is clear ofinterference. You get to the chamber with the escape pods, andnow need to pick one to take. Some of them could be damagedbut you don't have time to look. There's 5 pods, which onedo you take?})the_end_winner=Room.new("The End",%q{You jump into pod 2 and hit the eject button.The pod easily slides out into space heading tothe planet below. As it flies to the planet, you lookback and see your ship implode then explode like abright star, taking out the Gothon ship at the sametime. You won!})the_end_loser=Room.new("The End",%q{You jump into a random pod and hit the eject button.The pod escapes out into the void of space, thenimplodes as the hull ruptures, crushing your bodyinto jam jelly.})escape_pod.add_paths({'2'=>the_end_winner,'*'=>the_end_loser})generic_death=Room.new("death","You died.")the_bridge.add_paths({'throw the bomb'=>generic_death,'slowly place the bomb'=>escape_pod})laser_weapon_armory.add_paths({'0132'=>the_bridge,'*'=>generic_death})central_corridor.add_paths({'shoot!'=>generic_death,'dodge!'=>generic_death,'tell a joke'=>laser_weapon_armory})START=central_corridor
require'test/unit'require_relative'../lib/map'classMapTests<Test::Unit::TestCasedeftest_room()gold=Room.new("GoldRoom",%q{This room has gold in it you can grab. There's a door to the north.})assert_equal(gold.name,"GoldRoom")assert_equal(gold.paths,{})enddeftest_room_paths()center=Room.new("Center","Test room in the center.")north=Room.new("North","Test room in the north.")south=Room.new("South","Test room in the south.")center.add_paths({'north'=>north,'south'=>south})assert_equal(center.go('north'),north)assert_equal(center.go('south'),south)enddeftest_map()start=Room.new("Start","You can go west and down a hole.")west=Room.new("Trees","There are trees here, you can go east.")down=Room.new("Dungeon","It's dark down here, you can go up.")start.add_paths({'west'=>west,'down'=>down})west.add_paths({'east'=>start})down.add_paths({'up'=>start})assert_equal(start.go('west'),west)assert_equal(start.go('west').go('east'),start)assert_equal(start.go('down').go('up'),start)enddeftest_gothon_game_map()assert_equal(START.go('shoot!'),generic_death)assert_equal(START.go('dodge!'),generic_death)room=START.go('tell a joke')assert_equal(room,laser_weapon_armory)endend
在你的 web 程式運行的某個位置,你需要追踪一些信息,並將這些信息和用戶的瀏覽器關聯起來。在HTTP 協議的框架中,web 環境是「無狀態(stateless)」的,這意味著你的每一次請求和你其它的請求都是相互獨立的。如果你請求了頁面A,輸入了一些資料,然後點了一個頁面B 的鏈接,那你在頁面A 輸入的數據就全部消失了。
解決這個問題的方法是為 web 程式建立一個很小的資料儲存功能,給每個瀏覽器賦予一個獨一無二的數字,用來跟踪瀏覽器所作的事情。這個功能通常適用資料庫或者是存儲在磁碟上的檔案來實現。在 Sinatra 這個框架中實現這樣的功能是很容易的,以下就是一個這樣的例子(使用 Rack middleware):
123456789101112131415
require'rubygems'require'sinatra'useRack::Session::Poolget'/count'dosession[:count]||=0session[:count]+=1"Count: #{session[:count]}"endget'/reset'dosession.clear"Count reset to 0."end
require_relative"gothonweb/version"require_relative"map"require"sinatra"require"erb"moduleGothonwebuseRack::Session::Poolget'/'do# this is used to "setup" the session with starting valuespSTARTsession[:room]=STARTredirect("/game")endget'/game'doifsession[:room]erb:show_room,:locals=>{:room=>session[:room]}else# why is there here? do you need it?erb:you_diedendendpost'/game'doaction="#{params[:action]||nil}"# there is a bug here, can you fix it?ifsession[:room]session[:room]=session[:room].go(params[:action])endredirect("/game")endend