From 732d475a8452ea043dce02da30af56a83c17776e Mon Sep 17 00:00:00 2001 From: Jan-Marlon Leibl Date: Wed, 2 Apr 2025 09:06:44 +0200 Subject: [PATCH] feat: add game state enum and refactor game components --- .../casino/blackjack/BlackJackGameEntity.java | 4 - .../de/szut/casino/blackjack/CardEntity.java | 4 - frontend/src/app/enum/gameState.ts | 6 ++ .../game/blackjack/blackjack.component.html | 2 +- .../game/blackjack/blackjack.component.ts | 15 ++-- .../game-controls/game-controls.component.ts | 82 +++---------------- .../game-result/game-result.component.css | 1 - .../game-result/game-result.component.spec.ts | 22 ----- .../game-result/game-result.component.ts | 10 +-- .../services/game-controls.service.ts | 74 +++++++++++++++++ 10 files changed, 105 insertions(+), 115 deletions(-) create mode 100644 frontend/src/app/enum/gameState.ts delete mode 100644 frontend/src/app/feature/game/blackjack/components/game-result/game-result.component.css delete mode 100644 frontend/src/app/feature/game/blackjack/components/game-result/game-result.component.spec.ts create mode 100644 frontend/src/app/feature/game/blackjack/services/game-controls.service.ts diff --git a/backend/src/main/java/de/szut/casino/blackjack/BlackJackGameEntity.java b/backend/src/main/java/de/szut/casino/blackjack/BlackJackGameEntity.java index 0dbfcc0..4f22c9d 100644 --- a/backend/src/main/java/de/szut/casino/blackjack/BlackJackGameEntity.java +++ b/backend/src/main/java/de/szut/casino/blackjack/BlackJackGameEntity.java @@ -24,10 +24,6 @@ public class BlackJackGameEntity { @GeneratedValue private Long id; - @Version - @JsonIgnore - private Long version; - @ManyToOne @JoinColumn(name = "user_id", nullable = false) @JsonIgnore diff --git a/backend/src/main/java/de/szut/casino/blackjack/CardEntity.java b/backend/src/main/java/de/szut/casino/blackjack/CardEntity.java index 27d09d6..5520b58 100644 --- a/backend/src/main/java/de/szut/casino/blackjack/CardEntity.java +++ b/backend/src/main/java/de/szut/casino/blackjack/CardEntity.java @@ -19,10 +19,6 @@ public class CardEntity { @JsonIgnore private Long id; - @Version - @JsonIgnore - private Long version; - @ManyToOne @JoinColumn(name = "game_id", nullable = false) @JsonBackReference diff --git a/frontend/src/app/enum/gameState.ts b/frontend/src/app/enum/gameState.ts new file mode 100644 index 0000000..b07bf85 --- /dev/null +++ b/frontend/src/app/enum/gameState.ts @@ -0,0 +1,6 @@ +export enum GameState { + PLAYER_WON = 'PLAYER_WON', + IN_PROGRESS = 'IN_PROGRESS', + PLAYER_LOST = 'PLAYER_LOST', + DRAW = 'DRAW', +} \ No newline at end of file diff --git a/frontend/src/app/feature/game/blackjack/blackjack.component.html b/frontend/src/app/feature/game/blackjack/blackjack.component.html index c47d2f6..887aeeb 100644 --- a/frontend/src/app/feature/game/blackjack/blackjack.component.html +++ b/frontend/src/app/feature/game/blackjack/blackjack.component.html @@ -49,5 +49,5 @@ [gameState]="gameState()" [amount]="currentBet()" [show]="showGameResult()" - (close)="onCloseGameResult()" + (gameResultClosed)="onCloseGameResult()" > diff --git a/frontend/src/app/feature/game/blackjack/blackjack.component.ts b/frontend/src/app/feature/game/blackjack/blackjack.component.ts index 5b5ad42..621cbf5 100644 --- a/frontend/src/app/feature/game/blackjack/blackjack.component.ts +++ b/frontend/src/app/feature/game/blackjack/blackjack.component.ts @@ -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(undefined); gameInProgress = signal(false); - gameState = signal('IN_PROGRESS'); + gameState = signal(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; } diff --git a/frontend/src/app/feature/game/blackjack/components/game-controls/game-controls.component.ts b/frontend/src/app/feature/game/blackjack/components/game-controls/game-controls.component.ts index 9922309..00a8eca 100644 --- a/frontend/src/app/feature/game/blackjack/components/game-controls/game-controls.component.ts +++ b/frontend/src/app/feature/game/blackjack/components/game-controls/game-controls.component.ts @@ -1,6 +1,8 @@ import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core'; import { CommonModule } from '@angular/common'; import { Card } from '../../models/blackjack.model'; +import { GameState } from '../../../../../enum/gameState'; +import { GameControlsService } from '../../services/game-controls.service'; @Component({ selector: 'app-game-controls', @@ -11,10 +13,10 @@ import { Card } from '../../models/blackjack.model';
- Deine Punkte: {{ calculateHandValue(playerCards) }} + Deine Punkte: {{ gameControlsService.calculateHandValue(playerCards) }}
- Status: {{ getStatusText(gameState) }} + Status: {{ gameControlsService.getStatusText(gameState) }}
@@ -22,7 +24,7 @@ import { Card } from '../../models/blackjack.model';