Skip to content

Commit

Permalink
Fix #120: Stop all cars when race/qualifying is finished.
Browse files Browse the repository at this point in the history
  • Loading branch information
tkem committed Dec 17, 2023
1 parent e3d28b0 commit 8b0bb5b
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 8 deletions.
7 changes: 5 additions & 2 deletions src/app/app-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Injectable, isDevMode } from '@angular/core';

import { SettingsService } from './services/settings.service';

import { Observable } from 'rxjs';

import { map } from 'rxjs/operators';

const DRIVERS = isDevMode() ? [
Expand Down Expand Up @@ -102,6 +104,7 @@ export class RaceOptions {
time: number;
pause = false;
slotmode = false;
stopfin = false;
drivers?: number;
auto = false;
pace = false;
Expand Down Expand Up @@ -171,7 +174,7 @@ export class AppSettings {
return this.settings.set('options', value);
}

getQualifyingSettings() {
getQualifyingSettings(): Observable<RaceOptions> {
return this.settings.observe('qualifying').pipe(
map(value => Object.assign(new RaceOptions('qualifying'), value))
);
Expand All @@ -181,7 +184,7 @@ export class AppSettings {
return this.settings.set('qualifying', value);
}

getRaceSettings() {
getRaceSettings(): Observable<RaceOptions> {
return this.settings.observe('race').pipe(
map(value => Object.assign(new RaceOptions('race'), value))
);
Expand Down
3 changes: 3 additions & 0 deletions src/app/rms/race-settings.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@
<ion-item *ngIf="mode == 'race'">
<ion-toggle #slotmode formControlName="slotmode" translate>Finish all laps</ion-toggle>
</ion-item>
<ion-item>
<ion-toggle #stopfin formControlName="stopfin" translate>Stop cars when finished</ion-toggle>
</ion-item>
</ion-list>
</form>
</ion-content>
5 changes: 4 additions & 1 deletion src/app/rms/race-settings.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function createQualifyingForm(fb: FormBuilder, params: NavParams) {
value: params.get('pause') || false,
disabled: !params.get('time')
}),
stopfin: new FormControl(params.get('stopfin') || false),
drivers: new FormControl(params.get('drivers') || ''),
auto: new FormControl(params.get('auto') || false),
pace: new FormControl(params.get('pace') || false)
Expand All @@ -63,6 +64,7 @@ function createRaceForm(fb: FormBuilder, params: NavParams) {
value: !!params.get('slotmode'),
disabled: !params.get('laps')
}),
stopfin: new FormControl(params.get('stopfin') || false),
drivers: new FormControl(params.get('drivers') || ''),
auto: new FormControl(params.get('auto') || false),
pace: new FormControl(params.get('pace') || false)
Expand Down Expand Up @@ -165,7 +167,8 @@ export class RaceSettingsComponent implements AfterViewInit {
drivers: options.drivers ? parseInt(options.drivers) : undefined,
auto: options.auto,
pace: options.pace,
slotmode: options.slotmode
slotmode: options.slotmode,
stopfin: options.stopfin
}));
}

Expand Down
21 changes: 19 additions & 2 deletions src/app/rms/rms.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,17 @@ export class RmsPage implements OnDestroy, OnInit {
skipWhile(value => !value),
map(value => [value ? 'yellowflag' : 'greenflag', null])
),
session.allFinished.pipe(
filter(v => v),
take(1),
map(() => ['alldone', null]) // TODO: add notification, qualifying vs. race?
),
this.lapcount.pipe(
filter(laps => {
return options.laps >= 10 && laps.count === options.laps - 4 && !session.finished.value;
}),
take(1),
map(() => ['fivelaps', null])
map(() => ['fivelaps', null]) // TODO: threelaps, too?
),
this.lapcount.pipe(
filter(laps => {
Expand Down Expand Up @@ -312,20 +317,32 @@ export class RmsPage implements OnDestroy, OnInit {
)
);

this.subscriptions.add(
events.pipe(
filter(([event]) => event == 'alldone'),
withLatestFrom(this.getRaceOptions(options.mode))
).subscribe(([[event], options]) => {
if (options.stopfin) {
cu.toggleStart(); // TODO: read state?
}
})
);

if (options.mode != 'practice') {
const start = cu.getStart();
start.pipe(take(1)).toPromise().then(value => {
if (value === 0) {
cu.toggleStart();
}
// wait until startlight goes off; TODO: subscribe/unsibscribe?
// wait until startlight goes off; TODO: subscribe/unsubscribe?
cu.getStart().pipe(pairwise(),filter(([prev, curr]) => {
return prev != 0 && curr == 0;
}),take(1),).toPromise().then(() => {
this.logger.info('Start ' + options.mode + ' mode');
session.start();
});
});

}

return session;
Expand Down
18 changes: 15 additions & 3 deletions src/app/rms/session.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EMPTY, BehaviorSubject, Observable, interval, merge } from 'rxjs';
import { combineLatest, distinctUntilChanged, filter, groupBy, map, mergeMap, publishReplay, refCount, scan, share, startWith, tap, withLatestFrom } from 'rxjs/operators';
import { combineLatestWith, distinctUntilChanged, filter, groupBy, map, mergeMap, publishReplay, refCount, scan, share, startWith, tap, withLatestFrom } from 'rxjs/operators';

import { RaceOptions } from '../app-settings';
import { ControlUnit } from '../carrera';
Expand Down Expand Up @@ -60,6 +60,7 @@ export class Session {
currentLap: Observable<number>;
finished = new BehaviorSubject(false);
yellowFlag = new BehaviorSubject(false);
allFinished: Observable<boolean>;
timer: Observable<number>;
started = false;
stopped = false;
Expand Down Expand Up @@ -108,7 +109,7 @@ export class Session {

this.ranking = reset.pipe(
startWith(null),
combineLatest(this.grid),
combineLatestWith(this.grid),
map(([_reset, grid]) => {
return grid; // for reset side effects only...
}),
Expand Down Expand Up @@ -144,6 +145,17 @@ export class Session {
distinctUntilChanged()
);

this.allFinished = this.ranking.pipe(
combineLatestWith(this.finished),
map(([cars, fini]) => {
return fini && cars.every(e => e.finished);
}),
startWith(false),
publishReplay(1),
refCount(),
distinctUntilChanged()
);

if (options.time) {
this.timer = interval(TIMER_INTERVAL).pipe(
withLatestFrom(
Expand Down Expand Up @@ -240,7 +252,7 @@ export class Session {

type PitInfo = [number, boolean];
return times.pipe(
combineLatest(
combineLatestWith(
pits.pipe(
map(mask => ((mask & ~this.mask) & (1 << group.key)) != 0),
distinctUntilChanged(),
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"Share": "Teilen",
"Speed": "Tempo",
"Start": "Start",
"Stop cars when finished": "Autos halten am Ende an",
"Stop time when paused": "Zeit bei Unterbrechung stoppen",
"Time": "Zeit",
"Timeout": "Zeit ist um",
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"Share": "Share",
"Speed": "Speed",
"Start": "Start",
"Stop cars when finished": "Stop cars when finished",
"Stop time when paused": "Stop time when paused",
"Time": "Time",
"Timeout": "Time's up",
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"Share": "Compartir",
"Speed": "Velocidad",
"Start": "Comenzar",
"Stop cars when finished": "Detener los autos cuando haya terminado",
"Stop time when paused": "Cronómetro detenido",
"Time": "Tiempo",
"Timeout": "Se acabó el tiempo",
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"Share": "Partager",
"Speed": "Vitesse",
"Start": "Départ",
"Stop cars when finished": "Arrêter les voitures une fois terminé",
"Stop time when paused": "Stopper le chrono sous pause",
"Time": "Temps",
"Timeout": "Le temps est écoulé",
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"Share": "Condividi",
"Speed": "Velocità",
"Start": "Partenza",
"Stop cars when finished": "Fermare le auto una volta finito",
"Stop time when paused": "Cronometro fermo in pausa",
"Time": "Durata",
"Timeout": "Tempo scaduto",
Expand Down

0 comments on commit 8b0bb5b

Please sign in to comment.