Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: return gamer to home screen if backend fails #286

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 60 additions & 28 deletions src/app/game/game-draw/game-draw.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -319,6 +324,10 @@ export class GameDrawComponent implements OnInit, OnDestroy {
});
}

goHome() {
this.router.navigate([routes.LANDING]);
}

addTimeUsed() {
this.secondsUsed++;
this.drawingService.setSecondsUsed(this.secondsUsed);
Expand Down Expand Up @@ -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) {
Expand Down
Loading