Page MenuHomec4science

pilots.service.ts
No OneTemporary

File Metadata

Created
Mon, May 6, 09:51

pilots.service.ts

import {Injectable} from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {Observable} from 'rxjs';
import {WebSocketAPI} from '../_helpers/WebSocketAPI';
@Injectable({
providedIn: 'root'
})
export class PilotsService {
public currentPilots: Array<any>;
public passedPilots: Array<any>;
public setApiButton = false;
public API = '//' + location.hostname + ':8080';
public apiAdress = '';
public webSocketAPI: WebSocketAPI;
constructor(private http: HttpClient) {
this.webSocketAPI = new WebSocketAPI(this);
this.webSocketAPI._connect();
this.updatePilots();
}
updatePilots() {
this.updatePassedPilots();
this.updateCurrentPilots();
}
getCurrentPilots(): Observable<any> {
return this.http.get(this.API + '/getCurrentPilots');
}
updateCurrentPilots() {
this.getCurrentPilots().subscribe(data => {
this.currentPilots = data.sort((n1, n2) => {
if (n1.seat > n2.seat) {
return 1;
}
if (n1.seat < n2.seat) {
return -1;
}
return 0;
});
console.log('update current');
console.log(this.currentPilots);
});
}
getPassedPilots(): Observable<any> {
return this.http.get(this.API + '/getPassedPilots');
}
updatePassedPilots() {
this.getPassedPilots().subscribe(data => {
this.passedPilots = data;
console.log('update passed');
console.log(this.passedPilots);
});
}
save(pilot: any): Observable<any> {
return this.http.post(this.API + '/savePilot', pilot);
}
setApi(apiAdress: string) {
this.apiAdress = apiAdress;
}
updateNewPilots() {
if (this.apiAdress != '') {
this.setApiButton = false;
this.getNewPilots().subscribe(data => {
data[0].pilotResults.forEach(pilot => {
pilot.falseStart = false;
pilot.obstructedGate = 0;
pilot.collisionRecovery = 0;
pilot.circuitExit = 0;
pilot.crash = 0;
pilot.definitiveCrash = false;
pilot.videoProblems = 0;
pilot.celebratoryManoeuvre = 0;
pilot.dangerousPiloting = 0;
pilot.missedGate = 0;
pilot.circuitCut = 0;
pilot.failsafe = 0;
pilot.missedGateButton = false;
pilot.circuitCutButton = false;
pilot.current = 'current';
pilot.run = data[0].name;
pilot.race = data[0].sessionName;
});
let newRun = true;
if (this.currentPilots) {
this.currentPilots.forEach(pilot => {
if (pilot.run == data[0].name) {
newRun = false;
} else {
pilot.current = "passed";
this.save(pilot).subscribe(data => {
});
}
});
}
if (newRun) {
this.currentPilots = data[0].pilotResults.sort((n1, n2) => {
if (n1.seat > n2.seat) {
return 1;
}
if (n1.seat < n2.seat) {
return -1;
}
return 0;
});
console.log(this.currentPilots);
this.currentPilots.forEach(pilot => {
this.save(pilot).subscribe(data => {
this.webSocketAPI._send('updateCurrent');
this.webSocketAPI._send('updatePassed');
});
});
}
});
} else {
this.setApiButton = true;
}
}
getNewPilots(): Observable<any> {
return this.http.get(this.apiAdress);
}
}

Event Timeline