Rewriting My Competitors «
»


Games: , , , , , , ,
7 comments

It’s really clear that the polls So Play We All are measuring progress. When we have a quiet week, there’s a lot fewer votes. This week, there were 4, evenly split between Luke and I. The SPWA site doesn’t have code to handle ties so it highligted me as winning, which I guess means the bugfix is not my problem. :)

Luke laid down code for cards. I know I’m helping the enemy, but I’ve got to tweak it. His code is:

class CardController < ApplicationController
  def play
    play_sym = "play_#{@card}".intern # PH: this should be .to_sym
    self.send(play_sym)
    render :action => play_sym and return
  end
 
  def pocket
    pocket_sym = "pocket_#{@card}".intern
    self.send(pocket_sym)
  end # PH: no explicit render here? Feels like a paste error
 
  protected
 
  # plays
  def play_roland
    # Play the game to see what happens when you play the Roland card!
  end
 
  def play_water
    # Play the game to see what happens when you play the Water card!
  end
 
  # pockets
  def pocket_roland
    # Play the game to see what happens when you pocket the Roland card!
  end
 
  def pocket_water
    # Play the game to see what happens when you pocket the Water card!
  end
 
end
I’d write this code as:
class CardController < ApplicationController
  # I'm guessing from usage above @card is just the name for a card as a string
  # and that it's loaded from the url by a filter, note that I use it differently.
  before_filter :load_card
  after_filter :default_render_card
 
  def play
    @card.play
  end
 
  def pocket
    @card.pocket
  end
 
  protected
 
  def load_card
    @name = params[:card]
    @card = "card/#{@name}".camelize.constantize
  end
 
  def default_render_card
    render :template => "cards/#{request.action}/#{@name}" unless performed?
  end
end
 
# add in config/application.rb:
module Oaqn
  class Application < Rails::Application
    config.autoload_paths += %W(#{config.root}/app/cards)
  end
end
 
# and then create app/cards/roland.rb:
module Card
  module Roland
    def pocket
      # code for pocketing
    end
 
    def play
      # code for playing
    end
  end
end

So now the cards each get a source file to themselves, templates have their own per-action dirs (better swapped to per-card dirs, if there are more actions), there’s less duplicated code, and it’s far easier to test these smaller pieces. The only thing missing from this example is the fact that Luke may have to pass some game state into the methods. As long as there’s not too much it’s probably worth being explicit about.

2011-07-09: Luke actually used this code and found it didn’t work as written. I found an explanation; either add ‘app’ to autoload_paths instead of ‘app/cards’, or drop the Card wrapper.

Meanwhile, in the past, Jim wrote some PHP, and I’m not touching that language.

No, in seriousness, Jim talked about why he has some identifiers surrounded by __ (which I’d called python poisoning). I haven’t dug into his code (again, PHP), but it looks like it might be an InBandSignal to reuse Events as framework steps.

And then he talks about session fixation attacks, which are have been protected against out-of-the-box on PHP with the session.use_only_cookies setting for a while. I was a bit confused, I’m pretty sure he’s actually describing session capture attacks. Oh, and there was some other stuff about writing code to store sessions in the database. If you’re curious, Jim, here’s the code for a Rails app to do that, which appears commented-out in the stock config file for your editing convenience:

  Oaqn::Application.config.session_store :active_record_store

It includes support out of the box for keeping sessions in cookies (encrypted, of course), your SQL database via ActiveRecord, or Memcached. I’m curious, how much of your budget did you spend storing sessions?


Comments

  1. As I’ve discussed in other places in the past, I code with the notion of independence from php.ini settings. I’d be leaving a gaping security hole if my server turns out NOT to be sufficient and I go to some hosted method and the have it set to off and don’t allow me to tweak their ini settings.

    I spent about 3 hours on the session stuff. And it’s cool that Ruby allows you to do it much faster. However, how much of my budget would have been spent LEARNING A WHOLE NEW LANGUAGE? I’m willing to bet considerably MUCH more than we’ve spent up to this point in the competition. ;)

  2. The only reason to do that is if you’re writing code for the public rather than your own game: and there’s nothing wrong with telling the public, “You need these PHP config settings to use this free thing I’m giving you” (if ini_set can’t cover it). This is a concrete example of slowing down your game’s development for uncertain future goals. Note the dependency in a doc and move on, you can recode later if it’s a problem.

    And, yeah, I know it’d be a big painful jump to Ruby. Just wanted to show off how much nicer the grass is over here. :)

  3. Hey, thanks Peter! That code will make my cards a lot easier to write (and test).

    I’m about to start my 4 hours, and this will give me a good jump start on some of the things I have planned – thanks again!

  4. Heh. Yeah, PHP isn’t the greatest language in the world (I like C++ for that… D is also good but too immature). However, I dislike Ruby’s syntax (I prefer a C-style syntax… Ruby appears to be more like Basic to me) as I just don’t think it reads as clearly.

    I chose PHP as my first “web language” years ago purely as a career move. At the time, EVERY job listing I saw listed was requesting PHP, MySQL, Linux, Apache. So, I figured that’d be a good place to start learning. At the time, I didn’t realize I was going to end up being so consumed by work that I would find it difficult to find the time to learn a new language! lol

    I would like to learn Ruby eventually but I think I’m going to pick up new languages in this order (when I eventually get around to it):
    C# (and pick up .NET at the same time)
    Java
    Ruby

    Again, the order is purely derived from the job market.

  5. Oh, and I forgot to mention that the Events aren’t InBandSignal (I’ve been Exception-happy for at least a decade now) they’re just generic events… which allows the system to load the interested controller objects so they can update their models.

    Now that I’ve explained it, I probably miscoded it a bit. It still works but probably not as cleanly as it should. Something I’ll have to iterate over at some point. Maybe soon if it screws my display logic. That’s what happens when you code by the seat of your pants instead of formulating an overall plan first. lol

Leave a Reply

Your email address will not be published.