Class: TournamentController

Inherits:
Shoes::Main
  • Object
show all
Includes:
RacerHelper, TournamentHelper
Defined in:
lib/controllers/tournament_controller.rb

Instance Method Summary collapse

Methods included from RacerHelper

#cancel_button, #create_categorizations_for_checkboxes, #create_old_racer, #find_or_create_participation, #render_racer_form

Methods included from TournamentHelper

#stats_table

Instance Method Details

#category_stats(tournament_id, category_id, racers_offset = 0) ⇒ Object


279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/controllers/tournament_controller.rb', line 279

def category_stats(tournament_id,category_id,racers_offset=0)
  layout(:menu)
  racers_offset = racers_offset.to_i
  tournament = Tournament[tournament_id]
  category = Category[category_id]
  #TODO optimize
  racers = TournamentParticipation.filter(:tournament_id => tournament_id).all.select{|tp|tp.racer.categorizations.map(&:category).include? category}
  racers = racers.sort_by{|tp|[tp.best_time||Infinity]}
  racers.shift(9*racers_offset)

  @nav.clear {
    button($i18n.back_to_event) { visit "/tournaments/#{tournament_id}" }
    button($i18n.next) { visit "/tournaments/#{tournament_id}/stats/category/#{category_id}/#{racers_offset+1}" }
    pause = button($i18n.pause)
    play = button($i18n.play)
    pause.click { @t.toggle; play.toggle; pause.toggle }
    play.click  { @t.toggle; play.toggle; pause.toggle }
    play.hide
  }
  @center.clear {
    if racers.any?
      @stats = flow do
        stats_table(category.name,racers.shift(9))
        @t = timer(5) { visit "/tournaments/#{tournament_id}/stats/category/#{category_id}/#{racers_offset+1}" }
      end
    else# out of racers in category
      visit "/tournaments/#{tournament_id}/stats/category/#{Category.next_after(category)}/0" #try the next category
    end
  }
end

#edit(id) ⇒ Object


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/controllers/tournament_controller.rb', line 69

def edit(id)
  tournament = Tournament[id]
  # TODO: optimize
  Race.filter(:tournament_id => id).all.each { |r| r.destroy if(r.racers.length == 0) }
  # TODO: optimize
  tournament.tournament_participations.each { |tp| tp.destroy if(tp.racer.nil?||tp.racer.name.blank?) }
  tournament = Tournament[id]
  @title = tournament.name
  layout(:menu)
  
  session[:referrer] = []
  @nav.append {
    button("stats") { visit "/tournaments/#{tournament.pk}/stats" }
    button("export") { visit "/tournaments/#{tournament.pk}/stats/export" }
  }
  @center.clear {
    form(tournament)
  }

end

#export_stats(id) ⇒ Object


310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/controllers/tournament_controller.rb', line 310

def export_stats(id)
  layout(:menu)
  tournament = Tournament[id]
  racers = tournament.tournament_participations.sort_by{|tp|[(tp.best_time||Infinity)]}
  @center.clear {
    e = edit_box(:width => 400, :height => 400)
    export = "Overall\n"
    export << "Name,Losses,Best\n"
    racers.each {|racer| export << "#{racer.racer.name},#{racer.losses},#{racer.best_time}\n" }
    Category.all.each do |category|
      racers = TournamentParticipation.filter(:tournament_id => id).all.select{|tp|tp.racer.categorizations.map(&:category).include? category}
      export << "\n#{category.name}\n"
      export << "Name,Losses,Best\n"
      racers.each {|racer| export << "#{racer.racer.name},#{racer.losses},#{racer.best_time}\n" }
    end
    e.text = export
  }
end

#form(tournament) ⇒ Object


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
117
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/controllers/tournament_controller.rb', line 90

def form(tournament)
  if(session[:category] && (session[:category] != $i18n.all_categories))
    #TODO optimize and use Sequel
    tournament_participations = tournament.tournament_participations.select{ |tp|
      tp.racer.categorizations.map(&:category).include?(session[:category])
    }
  else
    tournament_participations = tournament.tournament_participations
  end

  case session[:order_by]
    when $i18n.best_time
      tournament_participations = tournament_participations.sort_by(&:rank)
    when $i18n.name
      tournament_participations = tournament_participations.sort_by{|tp|tp.racer.name.downcase}
  end
  races = tournament.races
  session[:hide_finished] = true if session[:hide_finished].nil?
  if session[:hide_finished]
    tournament_participations.reject! {|tp| tp.eliminated }
    races.reject! { |r| r.raced }
  end
      
  stack(:width => 0.4, :height => 1.0) {
    container
    #TODO i18n
    stack(:height => 0.12) { subtitle "racers:" }
    racers = stack(:height => 0.80, :scroll => true){ 
      tournament_participations.each do |tp|
        flow {
          flow(:width => 0.6) {
            tp.eliminated ? para(del(tp.racer.name)) : para(tp.racer.name) 
          }
          flow(:width => 0.3) {
            flow(:width => 0.8) {
              edit_button do
                session[:referrer].push(@center.app.location)
                visit "/racers/#{tp.racer.pk}/#{tp.tournament.pk}"
              end
            }
            flow(:width => 0.2) {
              delete_button do
                if tp.race_participations.any?
                  tp.eliminate
                else
                  tp.destroy
                end
                visit "/tournaments/#{tournament.pk}"
              end
            }
          }
        }
      end
    }
    stack(:width => 1.0, :height => 0.08) {
      button($i18n.add_a_new_racer) {
        session[:referrer].push(@center.app.location)
        visit("/racers/new/tournament/#{tournament.pk}")
      }
    }
  }
  stack(:width => 0.05)
  stack(:width => 0.55, :height => 1.0) {
    container
    #TODO i18n
    stack(:height => 0.12) { subtitle "races:" }
    stack(:height => 0.8, :scroll => true) {
      races.each{|race|
        flow {
          flow(:width => 0.6) {
            if race.unraced?
              para(race.racers.join(' vs '))
            else
              para(del(race.racers.join(' vs ')))
            end
          }
          flow(:width => 0.35) {
            flow(:width => 0.8){
              if race.raced
                button "RESULTS" do
                  visit "/races/#{race.pk}/winner"
                end
              else
                button $i18n.race do
                  visit "/races/#{race.pk}/ready"
                end
              end
            }
            flow(:width => 0.2){
              delete_button { 
                race.race_participations.each{|rp| rp.destroy }
                race.destroy; 
                visit "/tournaments/#{tournament.pk}"
              }
            }
          }
        }
      }
    }
    stack(:width => 1.0, :height => 0.08) {
      button($i18n.add_a_new_race) {
        session[:referrer].push(@center.app.location)
        visit "/races/new/tournament/#{tournament.pk}"
      }
    }
    
  }
  @left.clear do
    left_button $i18n.autofill do
      if(session[:category] && (session[:category] != $i18n.all_categories))
        #TODO optimize and use Sequel
        racers = tournament.tournament_participations.select{ |tp|
          !tp.eliminated && tp.racer.categorizations.map(&:category).include?(session[:category])
        }
        tournament.autofill(racers.map(&:racer)-tournament.matched_racers)
      else
        tournament.autofill
      end
      tournament.save
      visit "/tournaments/#{tournament.pk}"
    end
    left_button $i18n.new_round do
      n = ask($i18n.how_many_should_advance)
      unless n.nil?
        tournament_participations.sort_by{ |tp|
          tp.rank
        }[n.to_i..-1].each{ |tp| tp.update(:eliminated => true) }
        tournament.save
        visit "/tournaments/#{tournament.pk}"
      end
    end
    category = session[:category]
    order_by = session[:order_by]
    hide_finished = session[:hide_finished]
    categories = [$i18n.all_categories]+Category.all.to_a
    inscription "Filter by Category:", :margin => [0,30,0,0]
    list_box(:width => 1.0, :choose => category, :items => categories) do |list|
      session[:category] = list.text
      visit "/tournaments/#{tournament.pk}" if category != session[:category]
    end
    inscription "Order Racers:", :margin => [0,30,0,0]
    list_box(:width => 1.0, :choose => order_by, :items => [$i18n.name,$i18n.best_time]) do |list|
      session[:order_by] = list.text
      visit "/tournaments/#{tournament.pk}" if order_by != session[:order_by]
    end
    if session[:hide_finished]
      left_button $i18n.show_finished do
        session[:hide_finished] =  false
        visit "/tournaments/#{tournament.pk}" if hide_finished != session[:hide_finished]
      end
    else
      left_button $i18n.hide_finished do
        session[:hide_finished] =  true
        visit "/tournaments/#{tournament.pk}" if hide_finished != session[:hide_finished]
      end
    end
  end
end

#listObject


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/controllers/tournament_controller.rb', line 16

def list
  layout(:menu)
  @center.clear {
    stack(:width => 0.5) do
      container
      button($i18n.new_event) { visit "/tournaments/new" }
      Tournament.all.each {|tournament|
        flow(:width => 1.0, :margin_left => 20) {
          separator_line
        }
        flow(:width => 1.0, :margin_left => 20) {
          flow(:width => 0.6, :margin_top => 8) {
            para(link(tournament.name,:click => "/tournaments/#{tournament.pk}"))
          }
          flow(:width => 0.1) { }
          flow(:width => 0.3) {
            delete_button { tournament.destroy; visit "/tournaments" }
          }
        }
      }
    end
  }
end

#newObject


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
# File 'lib/controllers/tournament_controller.rb', line 40

def new
  layout(:menu)
  @center.clear {
    container
    tournament = Tournament.new
    flow {
      para $i18n.name
      edit_line(tournament.name) do |edit|
        tournament.name = edit.text
      end
    }
    button $i18n.save_and_continue do
      if tournament.name.blank?
        #FIXME: i18n 
        alert("Sorry, Tournament name can't be blank.")
      elsif Tournament.filter(:name => tournament.name).any?
        #FIXME: i18n 
        alert("Sorry, Tournament name is already taken.")
      else
        tournament.save
        visit "/tournaments/#{tournament.pk}"
      end
    end
    button $i18n.cancel do
      visit "/tournaments"
    end
  }
end

#overall_stats(id, racers_offset = 0) ⇒ Object


249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/controllers/tournament_controller.rb', line 249

def overall_stats(id, racers_offset=0)
  layout(:menu)
  racers_offset = racers_offset.to_i
  tournament = Tournament[id]
  racers = tournament.tournament_participations.sort_by{|tp|[(tp.best_time||Infinity)]}
  racers.shift(9*racers_offset)

  @nav.clear {
    if admin_window?
      button($i18n.back_to_event) { visit "/tournaments/#{id}" }
      button($i18n.next) { visit "/tournaments/#{id}/stats/#{racers_offset+1}" }
      pause = button($i18n.pause)
      play = button($i18n.play)
      pause.click { @t.toggle; play.toggle; pause.toggle }
      play.click  { @t.toggle; play.toggle; pause.toggle }
      play.hide
    end
  }
  @center.clear {
    if racers.any?
      @stats = flow(:height => 1.0) do
         stats_table($i18n.overall,racers.shift(9))
         @t = timer(5) { visit "/tournaments/#{id}/stats/#{racers_offset+1}" }
      end
    else# out of racers in overall
      visit "/tournaments/#{id}/stats/category/#{Category.next_after(nil)}/0" #try the next category
    end
  }
end