]> www.fi.muni.cz Git - slotcarman.git/commitdiff
Best lap handling.
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Mon, 6 Dec 2010 23:02:17 +0000 (00:02 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Mon, 6 Dec 2010 23:02:17 +0000 (00:02 +0100)
SCX/Car.pm
SCX/GUI.pm
SCX/Reader.pm
SCX/Track.pm

index d48cbd37bb8f6b44616df4b1bda4bdeb9823bc79..f4a38a0436b258016b5e38e68db07e05f9dde1dc 100644 (file)
@@ -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 {
index e782ba768e54e1f44477163759c65c85154da818..2148f93f77bfb4380a33cf2b977e435c8a035083 100755 (executable)
@@ -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('<span font_desc="18" weight="bold">'.$text.'</span>');
+
+       if ($global) {
+               $global = ' foreground="#e00000"';
+       } else {
+               $global = '';
+       }
+
+       $label->set_markup('<span font_desc="18" weight="bold"'.$global.'>'
+               .$text.'</span>');
 }
 
 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('<span font_desc="18" weight="bold">Lap '.$text.'</span>');
+       $label->set_markup('<span font_desc="18" weight="bold">Lap: '.$text.'</span>');
 }
 
 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('<span font_desc="18" weight="bold">Time '.$text.'</span>');
+       $label->set_markup('<span font_desc="18" weight="bold">Time: '.$text.'</span>');
 }
 
 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('<span font_desc="18" weight="bold">Best lap '.$text.'</span>');
+       $label->set_markup('<span font_desc="18" weight="bold">Best lap: '.$text.'</span>');
 }
 
 1;
index 565af0fe13d94b25d1a9cfbfcd5b6dff511c70ac..db88e3416519f969287032196f8f3b0e2a616b28 100644 (file)
@@ -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
index 32f03f40555bcf95fd8d7586e5c4f7efe7fd54ed..bd6ceea620e9d3626cba484c34f5f9f8c094944d 100644 (file)
@@ -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;