8 use Time::HiRes qw(gettimeofday);
9 use Glib qw(TRUE FALSE);
12 our $SEMAPHORE_STEP = 1000;
15 my ($class, $args) = @_;
19 $self->{gui} = $args->{gui} or croak;
20 $self->{race_running} = 0;
21 $self->{lap_counting_up} = 1;
26 $self->{cars}->[$i] = SCX::Car->new({
31 $self->car($i)->set_order($i);
37 sub car { return shift->{cars}->[shift]; }
42 return if $self->{race_running} || $self->{start_in_progress};
43 $self->{race_running} = 0;
44 $self->{start_in_progress} = 1;
45 $self->{semaphore} = 0;
46 $self->{gui}->show_semaphore(0);
47 Glib::Timeout->add($SEMAPHORE_STEP, \&semaphore_step, $self);
53 return FALSE if !$self->{start_in_progress} && !$self->{race_running};
56 if ($self->{semaphore} <= 5) {
57 $self->{gui}->show_semaphore($self->{semaphore});
58 my $timeout = $SEMAPHORE_STEP;
59 $timeout += $SEMAPHORE_STEP * rand()
60 if $self->{semaphore} == 5;
61 Glib::Timeout->add($timeout, \&semaphore_step, $self);
62 } elsif ($self->{semaphore} == 6) {
63 $self->{race_running} = 1;
64 $self->{race_running_since} = gettimeofday;
65 $self->{start_in_progress} = undef;
66 $self->{gui}->show_semaphore(0);
67 Glib::Timeout->add(3*$SEMAPHORE_STEP, \&semaphore_step, $self);
69 $self->{gui}->show_semaphore(undef);
70 $self->{semaphore} = undef;
78 $self->{race_running} = 0;
82 my ($self, $rounds) = @_;
85 $self->{gui}->rounds('0/' . $rounds);
86 $self->{race_rounds} = $rounds;
88 $self->{gui}->rounds('0');
89 $self->{race_rounds} = 0;
91 $self->{race_time} = 0;
92 $self->{best_lap} = undef;
94 $self->{gui}->show_semaphore(undef);
95 $self->{race_running} = 0;
96 $self->{start_in_progress} = 0;
98 $self->{gui}->time('00:00');
99 $self->{gui}->best_lap('0.00');
102 $self->car($car)->set_order($car);
103 $self->car($car)->set_lap(0);
104 $self->car($car)->set_laptime(undef);
109 my ($self, $time, $who) = @_;
111 return if !defined $time || $time == 0;
113 if (!defined $self->{best_lap}
114 || $time < $self->{best_lap}) {
115 $self->{best_lap} = $time;
116 $self->{gui}->best_lap(sprintf("%.2f", $time), $who);
122 sub qualification_start {
125 return if $self->{qualification_running};
127 $self->car($car)->set_lap(undef);
128 $self->car($car)->set_laptime(undef);
131 $self->{qualification_running};
132 $self->{gui}->lap('Qualification');
133 $self->{gui}->time('00:00');
134 $self->{gui}->best_lap('0:00');