Projects

Binary Lottery 2008

30 Mar 2008

This year at MountainWest RubyConf 2008 we had a slew of books and t-shirts to give away to attendees. Like last year we printed each attendee's unique number on their badge in base 2 (binary). We would then randomly select a winner from the attendee list. But we would use their binary number in the reveal, showing only one number at a time. It might be cheesy, but we really enjoy it so deal. :)

This is what the badges looked like:

Last year I wrote a command line app that used figlet to display the winner. You can see the video of last year's Lightning Talk where I show the code if you are interested. This year I wanted to mix it up a bit, and I decided about 10 hours before the conference to try my hand at using _why's Shoes to build a GUI version. So here it is:

1require'yaml' 2 3# The source of much evil... 4defupdate_digit(p,cnt,dig,wnr) 5ifcnt<dig 6p.replacerand(2) 7else 8p.replacewnr[8-dig].chr 9p.style[:stroke]=red10end11end1213# Use the full path because either Shoes is easily confused, or I am14users=YAML::load(open('/Users/blowmage/Lottery/users.yaml'))15winner=users[rand(users.size)]until(winnerandwinner[:eligible])16count=01718Shoes.app:width=>900,:height=>700do19keypressdo|k|20count+=121end22stackdo23para"MountainWest RubyConf 2008 Binary Lottery\n",24"And your winner is...",:font=>'Helvetica 48px'25lbl_name=para'',:font=>'Helvetica 128px'26flowdo27a=pararand(2),:font=>'Helvetica 192px'28b=pararand(2),:font=>'Helvetica 192px'29c=pararand(2),:font=>'Helvetica 192px'30d=pararand(2),:font=>'Helvetica 192px'31e=pararand(2),:font=>'Helvetica 192px'32f=pararand(2),:font=>'Helvetica 192px'33g=pararand(2),:font=>'Helvetica 192px'34h=pararand(2),:font=>'Helvetica 192px'3536animate(60)do37update_digit(a,count,8,winner[:number])38update_digit(b,count,7,winner[:number])39update_digit(c,count,6,winner[:number])40update_digit(d,count,5,winner[:number])41update_digit(e,count,4,winner[:number])42update_digit(f,count,3,winner[:number])43update_digit(g,count,2,winner[:number])44update_digit(h,count,1,winner[:number])4546ifcount>=847lbl_name.replacewinner[:name]48end49ifcount>850# Click one more time to flag the user so they won't win again.51lbl_name.style[:stroke]=red52winner[:eligible]=false53open('/Users/blowmage/Lottery/users.yaml','w')do|out|54YAML.dumpusers,out55end56end57end58end59end60end

Logan Barnett also wrote a GUI using JRuby and MonkeyBars. I really hope he releases his version as well. Here is what the app looked like on the third draw, sort of an easter egg I discovered during the conference: