public
Fork of evanfarrar/opensprints
Description: Bringing GoldSprints to the masses.
Homepage: http://www.opensprints.com/
Clone URL: git://github.com/toothrot/opensprints.git
opensprints / main.rb
100755 129 lines (111 sloc) 3.102 kb
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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env shoes
require 'yaml'
 
begin
  options = YAML::load(File.read('conf.yml'))
rescue
  alert "You must write a conf.yml. See sample in conf-sample.yml"
end
Infinity = 1/0.0
require 'lib/units/base'
require 'lib/units/standard'
 
 
RACE_DISTANCE = options['race_distance']
$ROLLER_CIRCUMFERENCE = options['roller_circumference'].mm.to_km
TITLE = options['title']
require 'lib/racer'
require 'lib/race'
require 'lib/interface_widgets'
require 'lib/tournament'
require "lib/sensors/#{options['sensor']['type']}_sensor"
require 'lib/race_presenter'
 
queue = Queue.new
SENSOR = Sensor.new(queue, options['sensor']['device'])
 
UNIT_SYSTEM = (options['units'] == 'standard') ? :mph : :kmph
 
Shoes.app :title => TITLE, :width => 800, :height => 600 do
  background white
  extend InterfaceWidgets
  extend RacePresenterMod
  @tournament = Tournament.new(RACE_DISTANCE)
 
  def list_racers
    flow do
      flow(:width => 115) { para 'Name' }
      flow(:width => 50) { para 'Wins' }
      flow(:width => 25) { para 'Best' }
    end
    @tournament.racers.each do |racer|
      flow do
        border black
        flow(:width => 115) { para racer.name }
        flow(:width => 50) { para racer.wins, " / ", racer.races }
        flow(:width => 25) { para racer.best_time, "s" unless racer.best_time == Infinity }
        add_to_race racer
        delete_racer racer
      end
    end
  end
 
  def post_race
    relist_tournament
  end
 
  def tournament_record(race)
    @tournament.record(race)
  end
 
  def list_matches
    border black
    title "Matches"
    @tournament.matches.each do |match|
      flow(:margin => 5) do
        background lightgrey
        border black
        flow(:width => 180) do
          if match.racers.length == 1
            para match.racers.first.name
          else
            para span(match.blue_racer.name, :stroke => blue),
                 " vs ",
                 span(match.red_racer.name, :stroke => red)
          end
        end
        button("race")do
          race_window(match, RACE_DISTANCE, SENSOR, TITLE)
        end
        redblue(match)
        delete_race(match)
      end
    end
  end
 
  def add_racer(name)
    duped = @tournament.racers.any? do |racer|
      racer.name == name
    end
    if !duped && name!='enter name'
      @tournament.racers << Racer.new(:name => name, :units => UNIT_SYSTEM)
      relist_tournament
    end
  end
 
  def relist_tournament
    @matches.clear {list_matches}
    @racer_list.clear {list_racers}
  end
 
  stack(:width => 380, :margin => 5) do
    border black
    title "Racers"
    @racer_list = stack { list_racers }
    flow do
      @racer_name = edit_line "enter name", :width => 110
      create_racer
    end
  end
 
  @matches = stack(:width => 290, :margin => 5) do
    list_matches
  end
 
  button "autofill matches" do
    @tournament.autofill_matches
    relist_tournament
  end
 
  button "save" do
    File.open(ask_save_file, 'w+') { |f| f << @tournament.to_yaml }
  end
 
  button "open" do
    @tournament = YAML::load(File.open(ask_open_file))
    relist_tournament
  end
end