2024-02-14 16:43:11 +00:00
|
|
|
import {Component, OnDestroy, OnInit} from '@angular/core';
|
|
|
|
|
import {Observable, Subscription} from "rxjs";
|
|
|
|
|
import {environment} from "../../environments/environment";
|
2024-02-18 13:25:14 +00:00
|
|
|
import {LocalCacheService} from "../../corelib/service/local-cache.service";
|
2024-02-14 16:43:11 +00:00
|
|
|
import {RxStompService} from "../../corelib/stomp/stomp.service";
|
|
|
|
|
import {Message} from "@stomp/stompjs/esm6";
|
|
|
|
|
import {ReplayData} from "../replays";
|
|
|
|
|
|
|
|
|
|
interface Statistics {
|
|
|
|
|
total_beatmaps: number;
|
|
|
|
|
total_users: number;
|
|
|
|
|
total_scores: number;
|
|
|
|
|
total_replay_scores: number;
|
|
|
|
|
total_replay_similarity: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'app-home',
|
|
|
|
|
templateUrl: './home.component.html',
|
|
|
|
|
styleUrls: ['./home.component.css']
|
|
|
|
|
})
|
|
|
|
|
export class HomeComponent implements OnInit, OnDestroy {
|
|
|
|
|
|
|
|
|
|
liveScores: ReplayData[] = [];
|
|
|
|
|
|
|
|
|
|
liveScoresSub: Subscription | undefined;
|
|
|
|
|
statistics: Statistics | null = null;
|
|
|
|
|
wantsConnection: boolean = true;
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
private localCacheService: LocalCacheService,
|
|
|
|
|
private rxStompService: RxStompService,
|
|
|
|
|
) { }
|
|
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
|
const storedWantsConnection = localStorage.getItem('wantsConnection');
|
|
|
|
|
if (storedWantsConnection !== null) {
|
|
|
|
|
this.wantsConnection = JSON.parse(storedWantsConnection);
|
|
|
|
|
if (this.wantsConnection) {
|
|
|
|
|
this.subscribe();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.subscribe();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.getStatistics().subscribe((response: Statistics) => {
|
|
|
|
|
this.statistics = response;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private subscribe() {
|
|
|
|
|
this.liveScoresSub = this.rxStompService
|
|
|
|
|
.watch("/topic/live-scores/")
|
|
|
|
|
.subscribe((message: Message) => {
|
|
|
|
|
const data: ReplayData = JSON.parse(message.body);
|
|
|
|
|
this.liveScores.unshift(data);
|
|
|
|
|
if (this.liveScores.length > 50) {
|
|
|
|
|
this.liveScores = this.liveScores.slice(0, 50);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy(): void {
|
|
|
|
|
this.liveScoresSub?.unsubscribe();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toggleConnection(): void {
|
|
|
|
|
this.wantsConnection = !this.wantsConnection;
|
|
|
|
|
localStorage.setItem('wantsConnection', JSON.stringify(this.wantsConnection));
|
|
|
|
|
|
|
|
|
|
if(!this.wantsConnection) {
|
|
|
|
|
this.liveScoresSub?.unsubscribe()
|
|
|
|
|
} else {
|
|
|
|
|
this.subscribe();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getStatistics(): Observable<Statistics> {
|
|
|
|
|
return this.localCacheService.fetchData<Statistics>(
|
|
|
|
|
'statistics',
|
|
|
|
|
`${environment.apiUrl}/stats`,
|
|
|
|
|
60
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|