From: Jan "Yenya" Kasprzak Date: Sun, 12 Dec 2010 17:55:36 +0000 (+0100) Subject: Init fixes, lap counting fixes. X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=commitdiff_plain;ds=sidebyside;h=47a074f4d544cc734a6c053025747ec08149370b;p=slotcarman.git Init fixes, lap counting fixes. --- diff --git a/SCX/Car.pm b/SCX/Car.pm index 91a244d..3f2b7a3 100644 --- a/SCX/Car.pm +++ b/SCX/Car.pm @@ -17,15 +17,15 @@ sub new { lap => 0, laptime => 0, avg_lap => 0, - state => 'greenflag', car_img => $args->{car_img}, id => $args->{id}, - order => $args->{id}, track => $args->{track}, }; bless $self, $class; + $self->set_order($self->{id}); + return $self; } @@ -130,7 +130,6 @@ sub set_laptime { if (defined $lap_time) { if ($lap_time > 1.0 && !$self->{in_pit_lane} - && $self->track->{race_running} && (!defined $self->{best_lap} || $self->{best_lap} > $lap_time )) { @@ -186,11 +185,8 @@ sub set_order { $self->gui->set_fuel($self->{order}, $self->{fuel}); $self->gui->set_car_id($self->{order}, $self->{id} + 1); $self->gui->set_distance($self->{order}, - $self->{time_diff}, $self->{lap_diff}); - $self->gui->set_state($self->{order}, $self->{state}); - if ($self->{in_pit_lane}) { - $self->gui->enter_pit_lane; - } + $self->{time_diff}, $self->{lap_diff}, $self->{grey_diff}); + $self->print_state; } sub print_state { @@ -221,6 +217,8 @@ sub reset { $self->{first_finish_time} = undef; $self->{time_diff} = undef; $self->{lap_diff} = undef; + $self->{total_time} = undef; + $self->{grey_diff} = undef; } sub finish_line { @@ -254,16 +252,19 @@ sub finish_line { } sub recalc_distance { - my ($self, $lap_first, $time_first) = @_; + my ($self, $lap_first, $time_first, $finishing) = @_; return if !defined $lap_first || !defined $self->{lap} || !defined $time_first || !defined $self->{first_finish_time}; - my $time; + $self->{total_time} = undef; + $self->{lap_diff} = undef; + $self->{time_diff} = undef; + $self->{grey_diff} = undef; if ($self->{lap} == $lap_first) { if ($self->{first_finish_time} == $time_first) { - $time = $self->{first_finish_time} + $self->{total_time} = $self->{first_finish_time} - $self->track->{race_running_since}; } else { $self->{time_diff} = $self->{first_finish_time} @@ -273,9 +274,22 @@ sub recalc_distance { $self->{lap_diff} = $lap_first - $self->{lap}; } - $self->gui->set_distance($self->{order}, $time, - $self->{lap_diff}, $self->{time_diff}); - $self->{grey_diff} = undef; + $self->gui->set_distance($self->{order}, $self->{total_time}, + $self->{lap_diff}, $self->{time_diff}, $self->{grey_diff}); + + if ($finishing) { + $self->{finished} = 1; + $self->print_state; + } +} + +sub greyout_distance { + my ($self) = @_; + + return if $self->{grey_diff}; + $self->{grey_diff} = 1; + $self->gui->set_distance($self->{order}, $self->{total_time}, + $self->{lap_diff}, $self->{time_diff}, $self->{grey_diff}); } 1; diff --git a/SCX/GUI.pm b/SCX/GUI.pm index 08b4ea9..6af8317 100755 --- a/SCX/GUI.pm +++ b/SCX/GUI.pm @@ -264,7 +264,7 @@ sub set_lap { } sub set_distance { - my ($self, $row, $time, $lap_diff, $time_diff) = @_; + my ($self, $row, $time, $lap_diff, $time_diff, $greyout) = @_; my $text = '--'; @@ -273,7 +273,12 @@ sub set_distance { } elsif (defined $time_diff) { $text = '−' . format_lap_time($time_diff); } elsif (defined $lap_diff) { - $text = '−' . $lap_diff . ' laps'; + $text = '−' . $lap_diff + . ($lap_diff == 1 ? ' lap' : ' laps'); + } + + if ($greyout) { + $text = '' . $text . ''; } $self->set_label('label_distance', $row, $text); diff --git a/SCX/Track.pm b/SCX/Track.pm index b71257e..fcf6b80 100644 --- a/SCX/Track.pm +++ b/SCX/Track.pm @@ -14,11 +14,12 @@ our $SEMAPHORE_STEP = 1000; sub new { my ($class, $args) = @_; - my $self; - - $self->{gui} = $args->{gui} or croak; - $self->{race_running} = 0; - $self->{lap_counting_up} = 1; + my $self = { + gui => $args->{gui}, + race_running => 0, + lap_counting_up => 1, + round => 0, + }; bless $self, $class; @@ -28,9 +29,12 @@ sub new { id => $i, track => $self, }); - $self->car($i)->set_order($i); } + $self->print_rounds; + $self->{gui}->time(undef); + $self->{gui}->best_lap(undef, undef); + return $self; } @@ -105,6 +109,7 @@ sub reset { $self->{race_running} = 0; $self->{start_in_progress} = 0; + $self->{race_finishing} = 0; $self->{best_lap} = undef; $self->{round} = 0; @@ -120,12 +125,16 @@ sub reset { sub print_rounds { my ($self) = @_; - $self->{gui}->rounds($self->{qualification_running} - ? 'Qualification' - : $self->{race_rounds} - ? $self->{round} . '/' . $self->{race_rounds} - : $self->{round} - ); + my $msg; + if ($self->{qualification_running}) { + $msg = 'Qualification'; + } elsif ($self->{race_rounds}) { + $msg = $self->{round} . '/' . $self->{race_rounds}; + } else { + $msg = $self->{round}; + } + + $self->{gui}->rounds($msg); } sub check_best_lap { @@ -189,20 +198,26 @@ sub recalc_order { } (0..5); my $lap_max = $laps[$new_order[0]]; - if (defined $lap_max && $self->{round} && $lap_max != $self->{round} - && (!$self->{race_rounds} - || $lap_max <= $self->{race_rounds})) { + my $lap_max_changed = 0; + if (defined $lap_max && defined $self->{round} + && $lap_max != $self->{round}) { $self->{round} = $lap_max; + $lap_max_changed = 1; $self->print_rounds; } + if ($self->{round} && $self->{race_rounds} + && $self->{round} > $self->{race_rounds}) { + $self->{race_finishing} = 1; + } + for my $id (0..5) { my $car = $new_order[$id]; if ($self->car($car)->{order} != $id) { $self->car($car)->set_order($id); } } - return ($new_order[0], $lap_max, $times[$new_order[0]]); + return ($lap_max_changed, $lap_max, $times[$new_order[0]]); } sub finish_line { @@ -218,19 +233,18 @@ sub finish_line { } } - - if ($was_processed) { - my ($first_car, $lap_max, $time_min) - = $self->recalc_order($time); - - for my $car (0..5) { - if ($processed{$car}) { - $self->car($car)->recalc_distance( - $lap_max, $time_min - ); - } else { - #$self->car($car)->greyout_distance; - } + return if !$was_processed; + + my ($lap_max_changed, $lap_max, $time_min) + = $self->recalc_order($time); + + for my $car (0..5) { + if ($processed{$car}) { + $self->car($car)->recalc_distance( + $lap_max, $time_min, $self->{race_finishing}, + ); + } elsif ($lap_max_changed) { + $self->car($car)->greyout_distance; } } }