Module: RaceHelper

Included in:
RaceController
Defined in:
lib/helpers/race_helper.rb

Instance Method Summary collapse

Instance Method Details

#clock(race, race_started) ⇒ Object


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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/helpers/race_helper.rb', line 35

def clock(race, race_started)
  @center.remove
  @left.style :width => WIDTH
  @left.clear do
    @clock = stack do
      fill gray(0.2)
      stroke black
      strokewidth 3

      center_x = @center.width/2
      center_y = @center.height/2
      oval :left => center_x, :top => center_y, :width => center_y * 2, :center => true

      fill white
      oval :left => center_x, :top => center_y, :width => center_y * 1.75, :center => true
      oval :left => center_x, :top => center_y, :width => center_y * 1.65, :center => true
       = image("media/big-logo.png", :attach => Window, :top => center_y, :left => center_x - 100)

      # hashmarks
      8.times do |i|
        move_to(center_x,center_y)
        nofill
        stroke black
        big_hashes = ((2*Shoes::PI) * i/8)
        small_hashes = ((2*Shoes::PI) * i/8) + (Shoes::PI * 1/8)

        shape do
          strokewidth 40
          arc center_x,center_y,(center_y*1.65),(center_y*1.65),big_hashes + -0.01,big_hashes + 0.01
        end
        shape do
          strokewidth 12
          arc center_x,center_y,(center_y*1.70),(center_y*1.70),small_hashes + -0.005,small_hashes + 0.005
        end 
      end

      # clock-arms
      race.race_participations.each do |racer|
        strokewidth 5
        stroke eval(racer.color)
        progress_angle = ((racer.percent_complete * 2 * Shoes::PI) - 0.5 * Shoes::PI)
        opposite = progress_angle + Shoes::PI
        shape do
          move_to(center_x,center_y)
          arc_to(center_x,center_y,(center_y*2 - 20),(center_y*2 - 20),progress_angle,progress_angle)
          arc_to(center_x,center_y,(center_y - 40),(center_y - 40),opposite,opposite)
        end
      end

      fill black
      stroke black
      oval :left => center_x, :top => center_y, :width => 20, :center => true

      unless race_started && race.next_race
        on_deck(race)
      end
    end

    # racer info
    @racers = stack do
      race.race_participations.each_with_index do |bike,index|
        stack(:attach => Window, :width => (WIDTH * 0.3).to_i, :left => (WIDTH * 0.7).to_i, :top => 100 + 160*index, :margin => [4,4,4,4]) do
          background eval(bike.color)
          stack do
            background gray(1.0,0.3)
            name = if bike.racer.name.length > 10 then bike.racer.name[0..10].concat('..') else bike.racer.name end
            title(name, :margin => [4,2,2,0], :stroke => white)
          end
          stack(:margin => [4,0,0,0]) do
            if bike.finished?
              subtitle("Finished: #{sprintf("%.2f", bike.finish_time)}", :margin => [0]*4, :stroke => white)
            else
              bike_speed = if race_started then bike.speed(bike.finish_time||SENSOR.time||0) else 0 end
              subtitle("#{sprintf('%.3d', bike_speed.to_i).rjust(5)} mph", :margin => [0]*4, :stroke => white)
              subtitle("#{sprintf('%.3d', [bike.distance.to_i, $RACE_DISTANCE].min).rjust(5)} m", :margin => [0]*4, :stroke => white)
            end
          end
        end
      end
    end
  end #left
end

#count_text(text) ⇒ Object


2
3
4
# File 'lib/helpers/race_helper.rb', line 2

def count_text(text)
  banner text, :font => "140px", :align => 'center'
end

#left_names(race) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/helpers/race_helper.rb', line 6

def left_names(race)
  @left.clear { 
    stack do # start progress_bars
      race.race_participations.each_with_index do |racer,i|
        stack(:width => 1.0, :height => 81) {
          background(eval(racer.color), :width => 1.0, :height => 70)
          tagline(racer.racer.name, :margin => [10,10,0,0]).displace(0,0)
        }
      end
    end.displace(0,162) # end progress_bars
  }
end

#on_deck(race) ⇒ Object


189
190
191
192
193
194
195
# File 'lib/helpers/race_helper.rb', line 189

def on_deck(race)
  stack(:attach => Window, :top => (window_height - 60), :left => 10, :width => (window_width), :height => 200) do
    if next_race = race.next_race
      tagline("up next: ", next_race.racers.join(", "), :stroke => white)
    end
  end
end

#progress_bars(race, speed = false) ⇒ Object


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/helpers/race_helper.rb', line 118

def progress_bars(race,speed=false)
  left_names(race)
  right_bars(race)
  stack do # start progress_bars
    flow do
      race.race_participations.each_with_index do |racer,i|
        if speed
          flow(:width => 0.5){
            flow(:width => 0.5){
              title(racer.speed(racer.finish_time||SENSOR.time), :align => 'right',:margin => [0]*4,
                :stroke => eval(racer.color))
            }
            flow(:width => 0.5){
              title("mph", :margin => [0]*4, :stroke => eval(racer.color))
            }
          }
        else
          flow(:width => 0.5){
            title(" ", :margin => [20]*4)
          }
        end
      end
    end
    race.race_participations.each_with_index do |racer,i|
      fill gradient(rgb(230,230,230),rgb(167,167,167), :angle => -90)
      stroke(black(0.0))
      rect(-1,81*i+162,@center.width+1,69)
      fill eval(racer.color)
      rect(-1,81*i+162,@center.width*racer.percent_complete+1,69)
      #oval(:radius => 34, :left => @center.width*racer.percent_complete+10, :top => 81*i+162)
    end
    
  end.displace(0,0) # end progress_bars
end

#race_track(race, race_started = false) ⇒ Object


31
32
33
# File 'lib/helpers/race_helper.rb', line 31

def race_track(race, race_started=false)
  self.send(RACE_TRACK, race, race_started)
end

#render_racer_name_and_color(race_participation, id) ⇒ Object


173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/helpers/race_helper.rb', line 173

def render_racer_name_and_color(race_participation,id)
  flow(:height => 0.3) {
    background(eval(race_participation.color), :width=> 0.9)
  }
  flow(:height => 0.3) {
    caption race_participation.racer.name
    delete_button {
      race_participation.destroy
      visit "/races/#{id}/edit"
    }
  }
  flow(:height => 0.3) {
    background(eval(race_participation.color), :width=> 0.9)
  }
end

#right_bars(race) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/helpers/race_helper.rb', line 19

def right_bars(race)
  @right.clear { 
    stack do # start progress_bars
      race.race_participations.each_with_index do |racer,i|
        fill right_bar_color||gradient(rgb(230,230,230),rgb(167,167,167), :angle => -90)
        stroke(black(0.0))
        rect(4,81*i+162,@right.width,69)
      end
    end.displace(0,162) # end progress_bars
  }
end

#stop_save_and_show_winner(race, id) ⇒ Object


153
154
155
156
157
158
159
160
# File 'lib/helpers/race_helper.rb', line 153

def stop_save_and_show_winner(race, id)
  @race_animation.stop
  SENSOR.stop
  race.raced = true
  race.save
  race.race_participations.map(&:save)
  visit "/races/#{id}/winner"
end

#swap_button(race, rerender_url) ⇒ Object


162
163
164
165
166
167
168
169
170
171
# File 'lib/helpers/race_helper.rb', line 162

def swap_button(race,rerender_url)
  button($i18n.swap) {
    racers = race.racers.reverse
    race.race_participations.each{|rp|rp.destroy}
    racers.map {|r|
      RaceParticipation.create(:racer => r, :race => race)
    }
    visit rerender_url
  } 
end