+sub qualification_start {
+ my ($self) = @_;
+
+ return if $self->{qualification_running};
+ for my $car (0..5) {
+ $self->car($car)->set_lap(undef);
+ $self->car($car)->set_laptime(undef);
+ }
+
+ $self->{qualification_running} = 1;
+ $self->{gui}->rounds('Qualification');
+ $self->{gui}->time(undef);
+ $self->{gui}->best_lap(undef);
+}
+
+sub packet_received {
+ my ($self, $time) = @_;
+
+ if ($self->{race_running}) {
+ $self->{gui}->time($time - $self->{race_running_since});
+ }
+}
+
+sub recalc_order {
+ my ($self, $now) = @_;
+
+ return if !$self->{race_running};
+
+ my @laps;
+ my @times;
+ for my $id (0..5) {
+ $laps[$id] = $self->car($id)->{lap} // -1;
+ $times[$id] = $self->car($id)->{first_finish_time} // $now;
+ }
+
+ my @new_order = sort {
+ $laps[$b] <=> $laps[$a]
+ ||
+ $times[$a] <=> $times[$b]
+ ||
+ $a <=> $b;
+ } (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})) {
+ $self->{round} = $lap_max;
+ $self->print_rounds;
+ }
+
+ 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]]);
+}
+
+sub finish_line {
+ my ($self, $time, $regular, @cars) = @_;
+
+ my @processed;
+ for my $car (@cars) {
+ push @processed, $car
+ if $self->car($car)->finish_line($time, $regular);
+ }
+
+ if (@processed) {
+ my ($first_car, $lap_max, $time_min)
+ = $self->recalc_order($time);
+
+ for my $car (@processed) {
+ $self->car($car)->recalc_distance($lap_max, $time_min);
+ }
+ }
+}
+