World Cup pick’em: Team group standing possibilities

The past three years I’ve been running a rather successful, private NFL pick the winners site.  The first two years it was a custom cobbled together PHP application, using raw database commands and everything.  Considering I wrote it two days before the season started, and still managed to sign up 20+ people, I was happy with its success.

This past year, I rewrote the site using Rails and fell in love, mostly with ActiveRecord.  It made working with databases fun again instead of feeling like such a chore.  I also really got into Ruby; it reminds me a lot of my Perl days back in college.  All little utility scripts I write these days are in it.

Anyway, I have a little less than 91 days until the 2010 World Cup starts, and I’ll be running a similar site, hopefully getting a good amount of people involved.  I’ve been writing the models for a while now, and today I implemented a feature I wanted to have for it.

For those unfamiliar, the World Cup is divided into two stages; the group stage and the knockout stage.  There are 8 groups, each with 4 teams, and each team in a group plays every other team in the group once.  3 points for a win, 1 point for a draw.  The top two teams in the group progress to the knockout stage.

Tonight, I wrote a helper method on the Team model which, after playing 2 of its 3 group games, can show you the probabilities of the team ending up in which place in the group. It looks like this:

>> cpcts = teams.map { |t| { t.abbr => t.get_possible_pcts } }
=> [{"USA"=>[0.916666666666667, 0.0833333333333333, 0.0, 0.0]},
   {"ENG"=>[0.0833333333333333, 0.5, 0.416666666666667, 0.0]},
   {"SVN"=>[0.0, 0.416666666666667, 0.166666666666667, 0.416666666666667]},
   {"ALG"=>[0.0, 0.0, 0.416666666666667, 0.583333333333333]}]

These numbers of course entirely based on simulated results for each team’s first games.  I like what I see here though, the US with a 91% chance of being group winners…

My database setup is slightly complicated, and entering a result for a game involves creating 3 records: one to record the score and two to indicate the winner/loser/tie-er of each game.  This meant for each possibility of the final games, I had to create these records and roll them back again.  I used a transaction and simply threw an exception, 36 times per team!

Also, if teams are level on points, there is a list of tiebreakers, much of them having to do with goal differences and goals scored, so in order to get a somewhat meaningful simulation, I had to simulate the final two games with each team involves scoring anywhere between 0 and 5 goals – thus the 36 transactions.  It’s a bit heavy, but if I can’t find a faster way I can always cache these results, as they would only change after a game has been played.

Plenty more to come about the WC pickem site as it develops.

One Trackback

  1. [...] « World Cup pick’em: Team group standing possibilities Lame haxpact saturday [...]

Post a Comment

Your email is never shared. Required fields are marked *

*
*