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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require 'yaml'

# The source of much evil...
def update_digit(p, cnt, dig, wnr)
  if cnt < dig
    p.replace rand(2)
  else
    p.replace wnr[8 - dig].chr
    p.style[:stroke] = red
  end
end

# Use the full path because either Shoes is easily confused, or I am
users = YAML::load(open('/Users/blowmage/Lottery/users.yaml'))
winner = users[rand(users.size)] until (winner and winner[:eligible])
count = 0

Shoes.app :width => 900, :height => 700 do
  keypress do |k|
    count += 1
  end
  stack do
    para "MountainWest RubyConf 2008 Binary Lottery\n",
      "And your winner is...", :font => 'Helvetica 48px'
    lbl_name = para '', :font => 'Helvetica 128px'
    flow do
      a = para rand(2), :font => 'Helvetica 192px'
      b = para rand(2), :font => 'Helvetica 192px'
      c = para rand(2), :font => 'Helvetica 192px'
      d = para rand(2), :font => 'Helvetica 192px'
      e = para rand(2), :font => 'Helvetica 192px'
      f = para rand(2), :font => 'Helvetica 192px'
      g = para rand(2), :font => 'Helvetica 192px'
      h = para rand(2), :font => 'Helvetica 192px'

      animate(60) do
        update_digit(a, count, 8, winner[:number])
        update_digit(b, count, 7, winner[:number])
        update_digit(c, count, 6, winner[:number])
        update_digit(d, count, 5, winner[:number])
        update_digit(e, count, 4, winner[:number])
        update_digit(f, count, 3, winner[:number])
        update_digit(g, count, 2, winner[:number])
        update_digit(h, count, 1, winner[:number])

        if count >= 8
          lbl_name.replace winner[:name]
        end
        if count > 8
          # Click one more time to flag the user so they won't win again.
          lbl_name.style[:stroke] = red
          winner[:eligible] = false
          open('/Users/blowmage/Lottery/users.yaml', 'w') do |out|
            YAML.dump users, out
          end
        end
      end
    end
  end
end

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

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