blowmage A Mike Moore joint

Binary Lottery 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:

My MWRC Badge. I'm number 11111111 because I made the badges. Its my right as an organizer! :)

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:

 1 require 'yaml'
 2 
 3 # The source of much evil...
 4 def update_digit(p, cnt, dig, wnr)
 5   if cnt < dig
 6     p.replace rand(2)
 7   else
 8     p.replace wnr[8 - dig].chr
 9     p.style[:stroke] = red
10   end
11 end
12 
13 # Use the full path because either Shoes is easily confused, or I am
14 users = YAML::load(open('/Users/blowmage/Lottery/users.yaml'))
15 winner = users[rand(users.size)] until (winner and winner[:eligible])
16 count = 0
17 
18 Shoes.app :width => 900, :height => 700 do
19   keypress do |k|
20     count += 1
21   end
22   stack do
23     para "MountainWest RubyConf 2008 Binary Lottery\n",
24       "And your winner is...", :font => 'Helvetica 48px'
25     lbl_name = para '', :font => 'Helvetica 128px'
26     flow do
27       a = para rand(2), :font => 'Helvetica 192px'
28       b = para rand(2), :font => 'Helvetica 192px'
29       c = para rand(2), :font => 'Helvetica 192px'
30       d = para rand(2), :font => 'Helvetica 192px'
31       e = para rand(2), :font => 'Helvetica 192px'
32       f = para rand(2), :font => 'Helvetica 192px'
33       g = para rand(2), :font => 'Helvetica 192px'
34       h = para rand(2), :font => 'Helvetica 192px'
35 
36       animate(60) do
37         update_digit(a, count, 8, winner[:number])
38         update_digit(b, count, 7, winner[:number])
39         update_digit(c, count, 6, winner[:number])
40         update_digit(d, count, 5, winner[:number])
41         update_digit(e, count, 4, winner[:number])
42         update_digit(f, count, 3, winner[:number])
43         update_digit(g, count, 2, winner[:number])
44         update_digit(h, count, 1, winner[:number])
45 
46         if count >= 8
47           lbl_name.replace winner[:name]
48         end
49         if count > 8
50           # Click one more time to flag the user so they won't win again.
51           lbl_name.style[:stroke] = red
52           winner[:eligible] = false
53           open('/Users/blowmage/Lottery/users.yaml', 'w') do |out|
54             YAML.dump users, out
55           end
56         end
57       end
58     end
59   end
60 end

Here is a sample of what the users.yaml file looked like:

 1 ---
 2 - :number: "00000000"
 3   :name: Jonathan Younger
 4   :eligible: true
 5 - :number: "00000001"
 6   :name: Brian Cooke
 7   :eligible: false
 8 - :number: "00000010"
 9   :name: Justin Kay
10   :eligible: false

Here is what the app looked like while running:

Binary Lottery written in Shoes

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:

Binary Lottery written in JRuby with MonkeyBars