district-politics/projects/speech-statistics/src/app/auth/edit/time-tracking/time-tracking.component.ts

186 lines
6.4 KiB
TypeScript

import {Component, OnDestroy, OnInit} from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {AngularFireDatabase, SnapshotAction} from '@angular/fire/compat/database';
import {Observable, Subscription} from 'rxjs';
import {Faction} from '../../administration/factions/faction';
import {RecordingState} from './recording-state';
import {SpeechType} from '../../../shared/speech-type';
import {SpeechTimeService} from '../../shared/speech-time.service';
@Component({
selector: 'app-time-tracking',
templateUrl: './time-tracking.component.html',
styleUrls: ['./time-tracking.component.scss']
})
export class TimeTrackingComponent implements OnInit, OnDestroy {
public factions: Observable<SnapshotAction<Faction>[]> = new Observable<SnapshotAction<Faction>[]>();
public currentSpeechFaction: string | undefined;
public currentCommentaryFaction: string | undefined;
private statePerFaction: Map<string, RecordingState> = new Map<string, RecordingState>();
private interruptedPerFaction: Map<string, boolean> = new Map<string, boolean>();
private speechTime: number = 0;
private commentaryTime: number = 0;
private speechStart: number = 0;
private commentaryStart: number = 0;
private sessionKey: string | undefined;
private subscription: Subscription | undefined;
constructor(private database: AngularFireDatabase,
private route: ActivatedRoute,
private speechTimeService: SpeechTimeService) {
}
ngOnInit(): void {
const factionRef = this.database.list<Faction>('factions');
this.factions = factionRef.snapshotChanges();
this.factions.subscribe(factions => {
this.statePerFaction = new Map<string, RecordingState>();
this.interruptedPerFaction = new Map<string, boolean>();
for (const faction of factions) {
const key = faction.key;
if (key == null) {
continue;
}
this.statePerFaction.set(key, RecordingState.NOT_RECORDING);
this.interruptedPerFaction.set(key, false);
}
});
this.subscription = this.route.params.subscribe(params => {
this.sessionKey = params['sessionKey'];
});
}
public ngOnDestroy(): void {
this.subscription?.unsubscribe();
}
public isNotRecording(factionKey: string | null): boolean {
if (factionKey == null) {
return false;
}
return this.statePerFaction.get(factionKey) == RecordingState.NOT_RECORDING;
}
public isRecordingSpeech(factionKey: string | null): boolean {
if (factionKey == null) {
return false;
}
return this.statePerFaction.get(factionKey) == RecordingState.RECORDING_SPEECH;
}
public isRecordingCommentary(factionKey: string | null): boolean {
if (factionKey == null) {
return false;
}
return this.statePerFaction.get(factionKey) == RecordingState.RECORDING_COMMENTARY;
}
public isInterrupted(factionKey: string | null): boolean {
if (factionKey == null) {
return false;
}
return this.interruptedPerFaction.get(factionKey) === true;
}
public startSpeech(factionKey: string | null): void {
if (factionKey == null) {
return;
}
this.statePerFaction.set(factionKey, RecordingState.RECORDING_SPEECH);
this.currentSpeechFaction = factionKey;
const date = new Date();
this.speechStart = date.getTime();
}
public continueSpeech(factionKey: string | null): void {
if (factionKey == null) {
return;
}
const date = new Date();
this.speechStart = date.getTime();
this.interruptedPerFaction.set(factionKey, false);
if (this.currentCommentaryFaction != null) {
this.interruptedPerFaction.set(this.currentCommentaryFaction, true);
this.commentaryTime += date.getTime() - this.commentaryStart;
}
}
public stopSpeech(factionKey: string | null): void {
if (factionKey == null) {
return;
}
this.statePerFaction.set(factionKey, RecordingState.NOT_RECORDING);
this.currentSpeechFaction = undefined;
const date = new Date();
this.speechTime += date.getTime() - this.speechStart;
this.speechTimeService.recordTime(this.sessionKey, factionKey, this.speechTime, SpeechType.SPEECH);
this.speechTime = 0;
if (this.currentCommentaryFaction != null) {
this.statePerFaction.set(this.currentCommentaryFaction, RecordingState.NOT_RECORDING);
this.commentaryTime += date.getTime() - this.commentaryStart;
this.speechTimeService.recordTime(this.sessionKey, this.currentCommentaryFaction, this.commentaryTime, SpeechType.COMMENTARY);
this.currentCommentaryFaction = undefined;
this.commentaryTime = 0;
}
}
public startComment(factionKey: string | null): void {
if (factionKey == null) {
return;
}
const date = new Date();
if (this.currentCommentaryFaction != null) {
this.statePerFaction.set(this.currentCommentaryFaction, RecordingState.NOT_RECORDING);
this.commentaryTime += date.getTime() - this.commentaryStart;
this.speechTimeService.recordTime(this.sessionKey, this.currentCommentaryFaction, this.commentaryTime, SpeechType.COMMENTARY);
}
this.commentaryStart = date.getTime();
this.statePerFaction.set(factionKey, RecordingState.RECORDING_COMMENTARY);
this.currentCommentaryFaction = factionKey;
if (this.currentSpeechFaction != null) {
this.interruptedPerFaction.set(this.currentSpeechFaction, true);
this.speechTime += date.getTime() - this.speechStart;
}
}
public continueComment(factionKey: string | null): void {
if (factionKey == null) {
return;
}
const date = new Date();
this.commentaryStart = date.getTime();
this.interruptedPerFaction.set(factionKey, false);
if (this.currentSpeechFaction != null) {
this.interruptedPerFaction.set(this.currentSpeechFaction, true);
this.speechTime += date.getTime() - this.speechStart;
}
}
public stopComment(factionKey: string | null): void {
if (factionKey == null) {
return;
}
this.statePerFaction.set(factionKey, RecordingState.NOT_RECORDING);
this.currentCommentaryFaction = undefined;
const date = new Date();
this.commentaryTime += date.getTime() - this.commentaryStart;
this.speechTimeService.recordTime(this.sessionKey, factionKey, this.commentaryTime, SpeechType.COMMENTARY);
this.commentaryTime = 0;
if (this.currentSpeechFaction != null) {
this.interruptedPerFaction.set(this.currentSpeechFaction, false);
this.speechTime += date.getTime() - this.speechStart;
}
}
}