feat: add game state enum and refactor game components

This commit is contained in:
Jan-Marlon Leibl 2025-04-02 09:06:44 +02:00
parent 349e4ce1ec
commit 4b569157aa
Signed by: jleibl
GPG key ID: 300B2F906DC6F1D5
10 changed files with 105 additions and 115 deletions

View file

@ -12,6 +12,7 @@ import { Card, BlackjackGame } from './models/blackjack.model';
import { BlackjackService } from './services/blackjack.service';
import { HttpErrorResponse } from '@angular/common/http';
import { GameResultComponent } from './components/game-result/game-result.component';
import { GameState } from '../../../enum/gameState';
@Component({
selector: 'app-blackjack',
@ -40,7 +41,7 @@ export default class BlackjackComponent {
balance = signal(0);
currentGameId = signal<number | undefined>(undefined);
gameInProgress = signal(false);
gameState = signal<string>('IN_PROGRESS');
gameState = signal<GameState>(GameState.IN_PROGRESS);
showGameResult = signal(false);
isActionInProgress = signal(false);
@ -60,15 +61,15 @@ export default class BlackjackComponent {
console.log('Game state update:', game);
this.currentGameId.set(game.id);
this.currentBet.set(game.bet);
this.gameInProgress.set(game.state === 'IN_PROGRESS');
this.gameState.set(game.state);
this.gameInProgress.set(game.state === GameState.IN_PROGRESS);
this.gameState.set(game.state as GameState);
const isGameOver = game.state !== 'IN_PROGRESS';
const isGameOver = game.state !== GameState.IN_PROGRESS;
this.dealerCards.set(
game.dealerCards.map((card, index) => ({
...card,
hidden: !isGameOver && index === 1 && game.state === 'IN_PROGRESS',
hidden: !isGameOver && index === 1 && game.state === GameState.IN_PROGRESS,
}))
);
@ -127,7 +128,7 @@ export default class BlackjackComponent {
onStand(): void {
if (!this.currentGameId() || this.isActionInProgress()) return;
if (this.gameState() !== 'IN_PROGRESS') {
if (this.gameState() !== GameState.IN_PROGRESS) {
console.log('Cannot stand: game is not in progress');
return;
}
@ -151,7 +152,7 @@ export default class BlackjackComponent {
onDoubleDown(): void {
if (!this.currentGameId() || this.isActionInProgress()) return;
if (this.gameState() !== 'IN_PROGRESS' || this.playerCards().length !== 2) {
if (this.gameState() !== GameState.IN_PROGRESS || this.playerCards().length !== 2) {
console.log('Cannot double down: game is not in progress or more than 2 cards');
return;
}