From 9b127058e5a6ddd0eca33c4dcdc1b6680b398efb Mon Sep 17 00:00:00 2001 From: ellenyuX Date: Tue, 15 Oct 2024 08:45:47 +0200 Subject: [PATCH 1/2] fixed so that it does not fetch stats if fields are empty --- .../statistics/statistics.component.html | 12 ++-- .../admin/statistics/statistics.component.ts | 57 +++++++++++-------- 2 files changed, 40 insertions(+), 29 deletions(-) diff --git a/src/app/admin/statistics/statistics.component.html b/src/app/admin/statistics/statistics.component.html index 79111a9f..fba7cbcb 100644 --- a/src/app/admin/statistics/statistics.component.html +++ b/src/app/admin/statistics/statistics.component.html @@ -13,12 +13,15 @@ } - + - +
@if (errorMessage) { +
+

{{errorMessage}}

+ }
- + @if (dataFetched()) { @@ -39,8 +42,5 @@

Statistikk for {{ selectedYear }}

} - - - diff --git a/src/app/admin/statistics/statistics.component.ts b/src/app/admin/statistics/statistics.component.ts index 9aebb082..e37a41d2 100644 --- a/src/app/admin/statistics/statistics.component.ts +++ b/src/app/admin/statistics/statistics.component.ts @@ -33,6 +33,7 @@ export class StatisticsComponent implements OnDestroy { year= ""; selected = "month"; selectedYear = ""; + errorMessage = ""; constructor( public dialogRef: MatDialogRef, @@ -40,31 +41,40 @@ export class StatisticsComponent implements OnDestroy { @Inject(MAT_DIALOG_DATA) public data: string) { } - confirmSelection() { - if (this.selected === "month") { - this.loginService.getStatisticsPerMonth(this.month, this.year).subscribe({ - next: (res) => { - this.dataFetched.set(true); - this.scoreCount = res; + confirmSelection() { + this.errorMessage = ""; // Reset error message - }, - error: (err) => { - console.error('Failed to fetch statistics', err); - } - }); - } else { - this.loginService.getStatisticsPerYear(this.selectedYear).subscribe({ - next: (res) => { - this.dataFetched.set(true); - this.scoreCount = res; - - }, - error: (err) => { - console.error('Failed to fetch statistics', err); + if (this.selected === "month" && this.month !== "") { + this.loginService.getStatisticsPerMonth(this.month, this.year).subscribe({ + next: (res) => { + this.dataFetched.set(true); + this.scoreCount = res; + this.month = ""; + this.year = ""; + }, + error: (err) => { + console.error('Failed to fetch statistics', err); + } + }); + + } else if (this.selected === "year" && this.selectedYear !== "") { + this.loginService.getStatisticsPerYear(this.selectedYear).subscribe({ + next: (res) => { + this.dataFetched.set(true); + this.scoreCount = res; + }, + error: (err) => { + console.error('Failed to fetch statistics', err); + } + }); + } else { + if (this.selected === "month") { + this.errorMessage = "Vennligst velg en dato"; + } else if (this.selected === "year") { + this.errorMessage = "Vennligst velg et år"; } - }); + } } - } closeDialog(): void { this.dialogRef.close(); @@ -73,6 +83,7 @@ export class StatisticsComponent implements OnDestroy { onToggleChanged(value: string) { this.selected = value; this.dataFetched.set(false); + this.errorMessage = ""; } onYearSelected(year: string) { @@ -84,7 +95,7 @@ export class StatisticsComponent implements OnDestroy { this.month = date[0]; this.display_month = date[1] this.year = date[2]; - } + } ngOnDestroy(): void { this.dialogRef.close(); From 4dacf6fdc9c104ef6a3aca1217d62f2ac3202e84 Mon Sep 17 00:00:00 2001 From: ivaj Date: Tue, 15 Oct 2024 14:55:54 +0200 Subject: [PATCH 2/2] Fix: return gamer to home screen if backend fails --- src/app/game/game-draw/game-draw.component.ts | 88 +++++++++++++------ 1 file changed, 60 insertions(+), 28 deletions(-) diff --git a/src/app/game/game-draw/game-draw.component.ts b/src/app/game/game-draw/game-draw.component.ts index 405e7be6..adc12a75 100644 --- a/src/app/game/game-draw/game-draw.component.ts +++ b/src/app/game/game-draw/game-draw.component.ts @@ -26,6 +26,9 @@ import { SpeechBubbleComponent } from '../shared-components/speech-bubble/speech import { OAvatarComponent } from '@/assets/avatars/o-avatar/o-avatar.component'; import { IAvatarComponent } from '@/assets/avatars/i-avatar/i-avatar.component'; import { ViewChild } from '@angular/core'; +import { Router } from '@angular/router'; +import { routes } from '../../shared/models/routes'; +import { MatSnackBar } from '@angular/material/snack-bar'; @Component({ selector: 'app-drawing', @@ -105,7 +108,9 @@ export class GameDrawComponent implements OnInit, OnDestroy { private drawingService: DrawingService, private imageService: ImageService, private soundService: SoundService, - private translationService: TranslationService + private translationService: TranslationService, + private router: Router, + private snackBar: MatSnackBar ) {} ngOnInit(): void { @@ -319,6 +324,10 @@ export class GameDrawComponent implements OnInit, OnDestroy { }); } + goHome() { + this.router.navigate([routes.LANDING]); + } + addTimeUsed() { this.secondsUsed++; this.drawingService.setSecondsUsed(this.secondsUsed); @@ -350,34 +359,57 @@ export class GameDrawComponent implements OnInit, OnDestroy { handleSinglePlayerClassification(dataUrl: string, croppedCoordinates: number[]) { const formData: FormData = this.createFormData(dataUrl); - this.drawingService.classify(formData).subscribe((res) => { - const sortedCertaintyArr = this.sortOnCertainty(res); - this.updateAiGuess(sortedCertaintyArr); - if (this.drawingService.roundIsDone(res.hasWon, res.gameState)) { - this.gameStateService.goToPage(GAMESTATE.intermediateResult); - this.drawingService.sortedCertainty = sortedCertaintyArr; - this.soundService.playResultSound(res.hasWon); - const score = this.score > 0 ? this.score : 0; - this.drawingService.lastResult.score = Math.round(score); - this.imageService - .resize(this.canvas.nativeElement.toDataURL('image/png'), croppedCoordinates, this.resultImageSize) - .subscribe({ - next: (dataUrlHighRes) => { - this.drawingService.lastResult.imageData = dataUrlHighRes; - }, - }); - } else { - this.imageService - .resize(this.canvas.nativeElement.toDataURL('image/png'), croppedCoordinates, this.resultImageSize) - .subscribe({ - next: (dataUrlHighRes) => { - if (this.result) { - this.result.imageData = dataUrlHighRes; - } - }, - }); - } + this.drawingService.classify(formData).subscribe({ + next: (res) => { + const sortedCertaintyArr = this.sortOnCertainty(res); + this.updateAiGuess(sortedCertaintyArr); + + if (this.drawingService.roundIsDone(res.hasWon, res.gameState)) { + this.gameStateService.goToPage(GAMESTATE.intermediateResult); + this.drawingService.sortedCertainty = sortedCertaintyArr; + this.soundService.playResultSound(res.hasWon); + const score = this.score > 0 ? this.score : 0; + this.drawingService.lastResult.score = Math.round(score); + this.imageService + .resize(this.canvas.nativeElement.toDataURL('image/png'), croppedCoordinates, this.resultImageSize) + .subscribe({ + next: (dataUrlHighRes) => { + this.drawingService.lastResult.imageData = dataUrlHighRes; + }, + }); + } else { + this.imageService + .resize(this.canvas.nativeElement.toDataURL('image/png'), croppedCoordinates, this.resultImageSize) + .subscribe({ + next: (dataUrlHighRes) => { + if (this.result) { + this.result.imageData = dataUrlHighRes; + } + }, + error: (error) => { + console.log("error"); + console.error("An error occurred while classifying the image:", error); + this.snackBar.open('Oops, noe gikk galt. Vennligst prøv igjen senere.', 'Close', { + duration: 3000, + }); + setTimeout(() => { + this.goHome(); + }, 5000); + }, + }); + } + }, + error: (err) => { + console.log("Error subscribing to classify:", err); // Log the error when subscribing fails + this.snackBar.open('Oops, noe gikk galt. Vennligst prøv igjen senere.', 'Close', { + duration: 3000, + }); + setTimeout(() => { + this.goHome(); + }, 5000); + }, }); + } classify(isMultiplayer = false) {