Exercise 52: The Start Of Your Web Game
We're coming to the end of the book, and in this exercise I'm going to really challenge you. When you're done, you'll be a reasonably competent Ruby beginner. You'll still need to go through a few more books and write a couple more projects, but you'll have the skills to complete them. The only thing in your way will be time, motivation, and resources.
In this exercise, we won't make a complete game, but instead we'll make an "engine" that can run the game from Exercise 47 in the browser. This will involve refactoring Exercise 43, mixing in the structure from Exercise 47, adding automated tests, and finally creating a web engine that can run the games.
This exercise will be huge, and I predict you could spend anywhere from a week to months on it before moving on. It's best to attack it in little chunks and do a bit a night, taking your time to make everything work before moving on.
Refactoring the Exercise 43 Game
You've been altering the gothonweb project for two exercises and you'll do it one more time in this exercise. The skill you're learning is called "refactoring," or as I like to call it, "fixing stuff." Refactoring is a term programmers use to describe the process of taking old code, and changing it to have new features or just to clean it up. You've been doing this without even knowing it, as it's second nature to building software.
What you'll do in this part is take the ideas from Exercise 47 of a testable "map" of Rooms, and the game from Exercise 43, and combine them together to create a new game structure. It will have the same content, just "refactored" to have a better structure.
The first step is to grab the code from ex47/lib/ex47/game.rb and copy it to lib/gothonweb/map.rb and copy the ex47/tests/ex47_tests.rb file to tests/map_tests.rb and run rake test again to make sure it keeps working. You'll need to change the require in test_map.rb to match the new gothonweb/map.rb location.
I won't show you the output of a test run. Just assume that you should be doing it and it'll look like the above unless you have an error.
Once you have the code from Exercise 47 copied over, it's time to refactor it to have the Exercise 43 map in it. I'm going to start by laying down the basic structure, and then you'll have an assignment to make the map.rb file and the map_tests.rb file complete.
First you'll want to put the original Room class into a module so put this right at the top of map.rb:
Next you need to write up the rooms. Here's the complete room descriptions, but you can also put in dummy text for these and then fill in the full text later when the game is working. These go after the Room class you already have from Exercise 47:
After this you need to connect these rooms to create the map using Room.add_paths:
Lastly, we'll need a way to find rooms by their names, load them, and save them to a "session". I'll explain wha a session is in the next section, but enter this code for the last part of map.rb:
You'll notice that there are a couple of problems with our Room class and this map:
- There are parts in the original game where we ran code that determined things like the bomb's keypad code, or the right pod. In this game we just pick some defaults and go with it, but later you'll be given Study Drills to make this work again.
- I've made a generic_death ending for all of the bad decisions, which you'll have to finish for me. You'll need to go back through and add in all the original endings and make sure they work.
- I've got a new kind of transition labeled "*" that will be used for a "catch-all" action in the engine.
Once you've that written, here's the new automated test tests/test_map.rb that you should have to get yourself started:
Your task in this part of the exercise is to complete the map and make the automated test completely validate the whole map. This includes fixing all the generic_death objects to be real endings. Make sure this works really well and that your test is as complete as possible because we'll be changing this map later and you'll use the tests to make sure it keeps working.
Sessions and Tracking Users
At a certain point in your web application you'll need to keep track of some information and associate it with the user's browser. The web (because of HTTP) is what we like to call "stateless," which means each request you make is independent of any other requests being made. If you request page A, put in some data, and click a link to page B, all the data you sent to page A just disappears.
Put that line where you put your set calls, and then you can use the session like this:
# set a value in the session for later session[:room] = "central_corridor" # get the room in another handler with current_room = session[:room]
This is what we were doing in map.rb with the Map::load_room and Map::store_room to keep track of what room the player is currently in. You could also use this to keep track of choices they've made, if monsters have died, or how much damage the player has sustained.
You can read more about Sinatra's sessions at the project's README at http://www.sinatrarb.com/intro.html#Using%20Sessions.
Creating an Engine
You should have your game map working and a good unit test for it. I now want you to make a simple little game engine that will run the rooms, collect input from the player, and keep track of where a player is in the game. We'll be using the sessions you just learned to make a simple game engine that will:
- Start a new game for new users.
- Present the room to the user.
- Take input from the user.
- Run user input through the game.
- Display the results and keep going until the user dies.
If you did not install rerun in Exercise 50 then you'll want to install it now for doing the rest of this exercise.
To do this, you're going to take the trusty bin/app.rb you've been hacking on and create a fully working, session-based game engine. The catch is I'm going to make a very simple one with basic HTML files, and it'll be up to you to complete it. Here's the base engine:
There are even more new things in this script, but amazingly it's an entire web-based game engine in a small file. The biggest "hack" in the script are the lines that bring the sessions back, which is needed so that debug mode reloading works. Otherwise, each time you refresh the page the sessions will disappear and the game won't work.
You should next delete views/hello_form.erb and views/index.erb and create the two views mentioned in the above code. Here's a very simple views/show_room.erb:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
That is the template to show a room as you travel through the game. Next you need one to tell someone they died in the case that they got to the end of the map on accident, which is views/you_died.erb:
1 2 3 4
With those in place, you should now be able to do the following:
1. Get the test tests/test_app.rb working again so that you are testing the game. You won't be able to do much more than a few clicks in the game because of sessions, but you should be able to do some basics. 3. Run the rerun 'ruby bin/app.rb' script and test the game.
You should be able to refresh and fix the game like normal, and work with the game HTML and engine until it does all the things you want it to do.
Your Final Exam
Do you feel like this was a huge amount of information thrown at you all at once? Good, I want you to have something to tinker with while you build your skills. To complete this exercise, I'm going to give you a final set of exercises for you to complete on your own. You'll notice that what you've written so far isn't very well built; it is just a first version of the code. Your task now is to make the game more complete by doing these things:
- Fix all the bugs I mention in the code, and any that I didn't mention. If you find new bugs, let me know.
- Improve all of the automated tests so that you test more of the application and get to a point where you use a test rather than your browser to check the application while you work.
- Make the HTML look better.
- Research logins and create a signup system for the application, so people can have logins and high scores.
- Complete the game map, making it as large and feature-complete as possible.
- Give people a "help" system that lets them ask what they can do at each room in the game.
- Add any other features you can think of to the game.
- Create several "maps" and let people choose a game they want to run. Your bin/app.rb engine should be able to run any map of rooms you give it, so you can support multiple games.
- Finally, use what you learned in Exercises 48 and 49 to create a better input processor. You have most of the code necessary; you just need to improve the grammar and hook it up to your input form and the GameEngine.