bullpen/app/src/services/BullpenSessionService.ts

94 lines
2.5 KiB
TypeScript

import BullpenPitch from "@/types/BullpenPitch";
import User from "@/types/User";
import PitchType from "@/types/PitchType";
export class BullpenSessionService {
private static instance: BullpenSessionService;
private static nullPitcher: User = {
id: 0,
firstName: "",
lastName: "",
email: "",
password: "",
dateOfBirth: new Date(0),
createdAt: new Date(0),
updatedAt: new Date(0),
};
private static nullPitchType: PitchType = {
id: 0,
name: "",
abbreviation: ""
}
private constructor() {
this.currentPitch = this.createPitch();
}
// Static method to get the instance of the service
public static getInstance(): BullpenSessionService {
if (!BullpenSessionService.instance) {
BullpenSessionService.instance = new BullpenSessionService();
}
return BullpenSessionService.instance;
}
public startSession(pitcher: User): void {
this.pitcher = pitcher;
this.pitches = [];
this.currentPitch = this.createPitch();
}
public clear(): void {
this.pitcher = BullpenSessionService.nullPitcher;
this.pitches = [];
}
public finishSession(): void {
// Todo: save to backend
}
public cancelSession(): void {
this.pitches = [];
this.pitcher = BullpenSessionService.nullPitcher;
}
public addBullpenPitch( bullpenPitch: BullpenPitch ): void {
this.pitches.push( bullpenPitch );
}
public getBullpenPitches(): BullpenPitch[] {
return this.pitches;
}
public currentBullpenPitch(): BullpenPitch {
return this.currentPitch;
}
public nextPitch(): void {
this.pitches.push(this.currentBullpenPitch());
this.currentPitch = this.createPitch();
}
public getPitcher(): User {
return this.pitcher;
}
public isSessionStarted(): boolean {
return this.pitcher.id === 0;
}
private createPitch(): BullpenPitch {
return {
id: 0,
date: new Date(),
pitchType: BullpenSessionService.nullPitchType,
plannedPitchArea: 0,
realPitchArea: 0,
realPitchSubArea: 0
}
}
private pitches: BullpenPitch[] = [];
private pitcher: User = BullpenSessionService.nullPitcher;
private currentPitch: BullpenPitch;
}
export const bullpenSessionService = BullpenSessionService.getInstance();