From b2e460537776f9b9de508c28b8e769cf8e80c30d Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Tue, 7 Dec 2010 00:02:17 +0100 Subject: [PATCH] Best lap handling. --- SCX/Car.pm | 9 ++++++++- SCX/GUI.pm | 24 ++++++++++++++++++------ SCX/Reader.pm | 5 ----- SCX/Track.pm | 22 +++++++++++++++++++++- 4 files changed, 47 insertions(+), 13 deletions(-) diff --git a/SCX/Car.pm b/SCX/Car.pm index d48cbd3..f4a38a0 100644 --- a/SCX/Car.pm +++ b/SCX/Car.pm @@ -12,6 +12,7 @@ sub new { throttle => -1, fuel => -1, order => $args->{order}, + track => $args->{track}, }; bless $self, $class; @@ -24,6 +25,8 @@ sub new { sub gui { return shift->{gui}; } +sub track { return shift->{track}; } + sub set_throttle { my ($self, $val) = @_; @@ -92,8 +95,12 @@ sub set_laptime { || $self->{best_lap} > $lap_time )) { - $self->gui->set_best_lap($self->{order}, $lap_time); $self->{best_lap} = $lap_time; + my $global = $self->{track}->check_best_lap($lap_time, + $self->{driver} + ); + $self->gui->set_best_lap($self->{order}, $lap_time, + $global); } } else { diff --git a/SCX/GUI.pm b/SCX/GUI.pm index e782ba7..2148f93 100755 --- a/SCX/GUI.pm +++ b/SCX/GUI.pm @@ -154,12 +154,20 @@ sub set_laptime { } sub set_best_lap { - my ($self, $row, $text) = @_; + my ($self, $row, $text, $global) = @_; ++$row; my $label = $self->get_object("label_best_lap$row") or die "Can't get label_best_lap$row"; - $label->set_markup(''.$text.''); + + if ($global) { + $global = ' foreground="#e00000"'; + } else { + $global = ''; + } + + $label->set_markup('' + .$text.''); } sub set_driver { @@ -200,7 +208,7 @@ sub rounds { my $label = $self->get_object("label_lap_global") or die "Can't get label_lap_global"; - $label->set_markup('Lap '.$text.''); + $label->set_markup('Lap: '.$text.''); } sub time { @@ -208,15 +216,19 @@ sub time { my $label = $self->get_object("label_time_global") or die "Can't get label_time_global"; - $label->set_markup('Time '.$text.''); + $label->set_markup('Time: '.$text.''); } sub best_lap { - my ($self, $text) = @_; + my ($self, $time, $who) = @_; + + my $text = $time; + $text .= ' by ' . $who + if defined $who; my $label = $self->get_object("label_best_lap_global") or die "Can't get label_best_lap_global"; - $label->set_markup('Best lap '.$text.''); + $label->set_markup('Best lap: '.$text.''); } 1; diff --git a/SCX/Reader.pm b/SCX/Reader.pm index 565af0f..db88e34 100644 --- a/SCX/Reader.pm +++ b/SCX/Reader.pm @@ -264,11 +264,6 @@ sub race_setup_packet { || $bytes[4] != 0xFF || $bytes[5] != 0xFF; - for my $car (0..5) { - $self->track->car($car)->set_lap(0); - $self->track->car($car)->set_laptime(undef); - } - $self->track->race_setup($bytes[0] == 0x00 ? 0 : $bytes[1] & 0x0F * 256 diff --git a/SCX/Track.pm b/SCX/Track.pm index 32f03f4..bd6ceea 100644 --- a/SCX/Track.pm +++ b/SCX/Track.pm @@ -5,6 +5,7 @@ package SCX::Track; use strict; use Carp; +use Time::HiRes qw(gettimeofday); use Glib qw(TRUE FALSE); use SCX::Car; @@ -19,14 +20,17 @@ sub new { $self->{race_running} = 0; $self->{lap_counting_up} = 1; + bless $self, $class; + for my $i (0..5) { $self->{cars}->[$i] = SCX::Car->new({ gui => $self->{gui}, order => $i, + track => $self, }); } - bless $self, $class; + return $self; } sub car { return shift->{cars}->[shift]; } @@ -95,7 +99,23 @@ sub race_setup { for my $car (0..5) { $self->car($car)->set_order($car); + $self->car($car)->set_lap(0); + $self->car($car)->set_laptime(undef); + } +} + +sub check_best_lap { + my ($self, $time, $who) = @_; + + return if !defined $time || $time == 0; + + if (!defined $self->{best_lap} + || $time < $self->{best_lap}) { + $self->{best_lap} = $time; + $self->{gui}->best_lap(sprintf("%.2f", $time), $who); + return 1; } + return 0; } 1; -- 2.43.5