feat(blackjack): add split functionality to the game
All checks were successful
CI / Get Changed Files (pull_request) Successful in 6s
CI / prettier (pull_request) Successful in 19s
CI / Checkstyle Main (pull_request) Successful in 50s
CI / eslint (pull_request) Successful in 53s
CI / test-build (pull_request) Successful in 1m12s

This commit is contained in:
Jan-Marlon Leibl 2025-04-02 11:25:03 +02:00
parent 4a7c54eab8
commit 3ef5530e77
Signed by: jleibl
GPG key ID: 300B2F906DC6F1D5
7 changed files with 410 additions and 276 deletions

View file

@ -1,4 +1,4 @@
import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';
import { ChangeDetectionStrategy, Component, inject, signal, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Router } from '@angular/router';
import { PlayingCardComponent } from './components/playing-card/playing-card.component';
@ -13,6 +13,7 @@ import { GameResultComponent } from '@blackjack/components/game-result/game-resu
import { GameState } from '@blackjack/enum/gameState';
import { NavbarComponent } from '@shared/components/navbar/navbar.component';
import { UserService } from '@service/user.service';
import { timer } from 'rxjs';
@Component({
selector: 'app-blackjack',
@ -30,7 +31,7 @@ import { UserService } from '@service/user.service';
templateUrl: './blackjack.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export default class BlackjackComponent {
export default class BlackjackComponent implements OnInit {
private router = inject(Router);
private userService = inject(UserService);
private blackjackService = inject(BlackjackService);
@ -47,13 +48,11 @@ export default class BlackjackComponent {
isActionInProgress = signal(false);
currentAction = signal<string>('');
constructor() {
this.refreshUserBalance();
}
private refreshUserBalance(): void {
this.userService.getCurrentUser().subscribe((user) => {
this.balance.set(user?.balance ?? 0);
ngOnInit(): void {
this.userService.currentUser$.subscribe((user) => {
if (user) {
this.balance.set(user.balance);
}
});
}
@ -83,8 +82,10 @@ export default class BlackjackComponent {
if (isGameOver) {
console.log('Game is over, state:', game.state);
this.userService.refreshCurrentUser();
this.showGameResult.set(true);
console.log('Game result dialog should be shown now');
timer(1500).subscribe(() => {
this.showGameResult.set(true);
console.log('Game result dialog shown after delay');
});
}
}
@ -180,21 +181,18 @@ export default class BlackjackComponent {
onCloseGameResult(): void {
console.log('Closing game result dialog');
this.showGameResult.set(false);
this.userService.refreshCurrentUser();
}
private handleGameError(error: HttpErrorResponse): void {
if (error instanceof HttpErrorResponse) {
if (error.status === 400 && error.error?.error === 'Invalid state') {
this.gameInProgress.set(false);
this.refreshUserBalance();
this.userService.refreshCurrentUser();
} else if (error.status === 500) {
console.log('Server error occurred. The game may have been updated in another session.');
this.gameInProgress.set(false);
this.refreshUserBalance();
this.userService.refreshCurrentUser();
if (this.currentGameId()) {
this.refreshGameState(this.currentGameId()!);
}