August 4, 2014 by Daniel P. Clark

Create a Game Instance in Rails

So you need to load a class instance in Rails for your visitors to play your game. But Rails generally is a static website with static database lookups. So for this we’re going to assign a class instance to a game room.

First let’s generate our project; we’re going to start with a prebuilt devise/bootstrap starter. Here’s a quick way to get started in Mac/Linux. Otherwise you can manually add at least devise to fully utilize this code.

Then lets use the rails generator to build the game room scaffolding:

This should get a lot of the work done for us, but let’s tidy up some of the generated output. Open the file app/assets/stylesheets/games.css.scss and drop this in.

Then open up your partial view for the form in app/views/games/_form.html.erb and change the form fields to this:

You’ll notice that we’ve removed the “instance” field, and we’ve changed 3 of the other fields to select. But don’t run it yet, because we need to define the model for Game.kinds. Here’s the model app/models/games.rb

Alright now we have the validations, and the game type enumerator built-in so your partial form view should work. (in this example we won’t be using gin or rummy)

Next let’s add our game loader. To do this we’ll create a helper method in our app/controllers/application_controller.rb file

Note that the purpose of the function is to reference the “current game” that the game room is running by the  Class::__id__ method. We will hand this a game class, such as HoldEm, and then it will be sure to return the correct running instance of the class HoldEm for us to use in Rails.  And if an instance isn’t running then the klassy = klass.new  part runs calling  HoldEm.new  and returns the instance of it. NOTE: you need to have the assignment here of  klassy =  or else the class instance WILL DISAPPEAR since it wasn’t assigned “within” the scope in which it was declared.

Now let’s look at how we’ll use this in our games controller. What we’re going to do is use the “show” page as the actual game room; so the game only needs to be running in that page. So we’ll modify the show method in app/controllers/games_controller.rb to this:

You should know that  @game  is already declared in

at the beginning of the file. Here  @card_game  gets assigned the actual game object we want to start up, or continue, and then makes sure the class id is saved in the database table, for this game room, under instance . As long as the game is running this game room will properly load the correct game class. And to perform any of your game functions you can just call them on @card_game .

Now let’s update the show view so you can see the class instance being used. Change your app/views/games/show.html.erb file to this:

If you’ve run into any problems yet it may be because you don’t have a HoldEm class defined. For now you can create a lib/holdem.rb file and put the bare-bones in it:

And then make sure it’s loaded from the lib folder. In my use case I feel it’s more at home in the application controller rather than in the config/application.rb file. So in the application controller I added:

Now one last change to make. In your visitors controller we need to add a redirect. So open app/controllers/visitors_controller.rb and add this:

Now everything should be ready. Run your rails server and create an account. Once you’re signed into devise it will redirect you to the list of game rooms. Create one, then on the show page click refresh as often as you like and you will see the class id right next to the game room name. This will run faithfully for you, if you want to prove a case where the game class itself disappears then try removing  klassy =  in the  application_controller  and refresh several times. You will now see the number change ever so often. Be sure to put it back!

Congratulations! You now have control over running your game class per game room and ensuring the class is properly loaded!

If you’re worried about the possibility of too many classes “some how” being created you can add this within the  game_controllers  destroy method

Although we’re not given too much control when it comes to garbage collection, this does seem to destroy any objects that are no longer assigned.

Well I hope this was very helpful for you to get your game room created in Rails with it’s own game instance! Now make the games, and have fun!

Please comment, share, subscribe to my RSS Feed,and follow me on twitter @6ftdan!

God Bless! – Daniel P. Clark

#game#howto#rails#ruby