Tuesday, July 3, 2012

Creating Prototype High-Score Web Service

 Rather than reinventing the wheel with a custom network protocol and writing a high-speed server in C or C++, I decided to make a RESTful web service to store user names, high scores, and all the other information I needed for the social networking features of my games. I accomplished this quickly and easily by using Rails.

With Rails, I can create a prototype RESTful web service in a matter of minutes, and then incrementally polish and improve it to its final form. I know that it won't be just a throwaway, toy web service. That said, the focus of this book is on IPhone implementation details; this isn't a Rails book.

Here, I will cover only the basic steps necessary to create the web service. This simple high-score web service will just take the user's name and the current high score, and store that information in a big fat list. It won't concern itself with logins and other security trappings.

 =>Creating the High-Scores Rails App

              Rails and a supported database are all you need to get started developing Rails apps. If you are developing with the IPhone SDK, you must have Mac OS X 10.5.5 (Leopard) or later, and thank-fully, Leopard comes with Rails and SQLite 3 installed. With some basic knowledge of using the Unix command line from the Mac OS X Terminal app, you can get started with Rails.

#1. First, update RubyGems and Rails to the latest version. Open Terminal and type in the following commands( you will be prompted for your Mac OS X password ):
      sudo gem install rubygems-update
      sudo update_rubygames
      sudo gem install -y rails

     
 This will update RubyGems and Rails and their dependency package, if you don't already have them.

#2. Next, change directory to where you usually store your projects.  Enter the following to create a directory to store everything together and change current  directory there:

  mkdir iPhoneGamesProjects; cd iPhoneGamesProjects

#3. Then create a skeleton Rails app named simple_high_score_service, as follows:
   
   rails simple_high_score_service

#4. Change directory to your newly created Rails app:

    cd simple_high_score_service


#5.Now create a Rails controller, view,and model for handling the high score and the name of the user:

  script/generate scaffold highScore \
  score: integer full_name: string

#6. Finally, have Rails create the database where your Rails model will be stored:
            rake db: migrate

#7. Believe it or not, with just a few minutes' work, you have a prototype high-scores web service. Run the Rails app by typing the following:

            script/server

                  Now open your web browser to localhost: 300/high_scores to see the product of those strenuous minutes of hard work.

                  Your web browser should present you with a plain-looking web app where your users can store their name and their latest high score. It will store one record for each name and score pair. The default code generated by Rails lists records in the order in which they are saved in the database, and the navigation is a bit lacking, but it is a good, usable prototype. And the best part is that it implements full XML web services, with no extra work on your part.

                  High-score leaderboards are usually presented as a top- 100 list of scores in descending order. Let's make a few changes to the web service code to implement this. In your still-running Terminal session, enter the following command:

                             $ script/generate controller Top100Scores index

 This creates a skeleton controller called Top100Scores, with a single empty action and view called index.

Open your new controller file, simple_high_score_service/app/controllers/top100_scores_controller.rb,
in your favorite text editor, and then replace the entire contents with the code in Listing...