From deac1289351f8c302a87157cf5f6eb9f27f1eb5e Mon Sep 17 00:00:00 2001 From: Jan-Marlon Leibl Date: Thu, 27 Mar 2025 15:47:16 +0100 Subject: [PATCH] style(blackjack): format code for better readability --- .../game/blackjack/blackjack.component.html | 12 ++- .../game/blackjack/blackjack.component.ts | 33 ++++--- .../dealer-hand/dealer-hand.component.ts | 14 +-- .../game-controls/game-controls.component.ts | 32 ++++--- .../game-info/game-info.component.ts | 4 +- .../game-result/game-result.component.spec.ts | 5 +- .../game-result/game-result.component.ts | 88 +++++++++---------- .../player-hand/player-hand.component.ts | 14 +-- .../playing-card/playing-card.component.ts | 58 +++++++----- 9 files changed, 139 insertions(+), 121 deletions(-) diff --git a/frontend/src/app/feature/game/blackjack/blackjack.component.html b/frontend/src/app/feature/game/blackjack/blackjack.component.html index 62d5489..1a6b8d3 100644 --- a/frontend/src/app/feature/game/blackjack/blackjack.component.html +++ b/frontend/src/app/feature/game/blackjack/blackjack.component.html @@ -5,17 +5,21 @@
- + @if (isActionInProgress()) {
-
-
+
+
{{ currentAction() }}
} - + @if (gameInProgress()) { ('IN_PROGRESS'); showGameResult = signal(false); - + // Add loading state trackers isActionInProgress = signal(false); currentAction = signal(''); @@ -66,7 +66,7 @@ export default class BlackjackComponent { // When game ends, make sure all dealer cards are visible const isGameOver = game.state !== 'IN_PROGRESS'; - + this.dealerCards.set( game.dealerCards.map((card, index) => ({ ...card, @@ -85,7 +85,7 @@ export default class BlackjackComponent { if (isGameOver) { console.log('Game is over, state:', game.state); this.refreshUserBalance(); - + // Show result immediately without resetting first this.showGameResult.set(true); console.log('Game result dialog should be shown now'); @@ -95,7 +95,7 @@ export default class BlackjackComponent { onNewGame(bet: number): void { this.isActionInProgress.set(true); this.currentAction.set('Spiel wird gestartet...'); - + this.blackjackService.startGame(bet).subscribe({ next: (game) => { this.updateGameState(game); @@ -114,7 +114,7 @@ export default class BlackjackComponent { this.isActionInProgress.set(true); this.currentAction.set('Karte wird gezogen...'); - + this.blackjackService.hit(this.currentGameId()!).subscribe({ next: (game) => { this.updateGameState(game); @@ -130,7 +130,7 @@ export default class BlackjackComponent { onStand(): void { if (!this.currentGameId() || this.isActionInProgress()) return; - + if (this.gameState() !== 'IN_PROGRESS') { console.log('Cannot stand: game is not in progress'); return; @@ -138,7 +138,7 @@ export default class BlackjackComponent { this.isActionInProgress.set(true); this.currentAction.set('Dealer zieht Karten...'); - + this.blackjackService.stand(this.currentGameId()!).subscribe({ next: (game) => { this.updateGameState(game); @@ -154,7 +154,7 @@ export default class BlackjackComponent { onDoubleDown(): void { if (!this.currentGameId() || this.isActionInProgress()) return; - + if (this.gameState() !== 'IN_PROGRESS' || this.playerCards().length !== 2) { console.log('Cannot double down: game is not in progress or more than 2 cards'); return; @@ -162,7 +162,7 @@ export default class BlackjackComponent { this.isActionInProgress.set(true); this.currentAction.set('Einsatz wird verdoppelt...'); - + this.blackjackService.doubleDown(this.currentGameId()!).subscribe({ next: (game) => { this.updateGameState(game); @@ -181,20 +181,19 @@ export default class BlackjackComponent { this.showGameResult.set(false); } - private handleGameError(error: any): void { + private handleGameError(error: HttpErrorResponse): void { if (error instanceof HttpErrorResponse) { if (error.status === 400 && error.error?.error === 'Invalid state') { this.gameInProgress.set(false); - + this.refreshUserBalance(); - } - else if (error.status === 500) { + } 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(); - + if (this.currentGameId()) { this.refreshGameState(this.currentGameId()!); } @@ -209,7 +208,7 @@ export default class BlackjackComponent { }, error: (err) => { console.error('Failed to refresh game state:', err); - } + }, }); } diff --git a/frontend/src/app/feature/game/blackjack/components/dealer-hand/dealer-hand.component.ts b/frontend/src/app/feature/game/blackjack/components/dealer-hand/dealer-hand.component.ts index 990b3c8..6d77f60 100644 --- a/frontend/src/app/feature/game/blackjack/components/dealer-hand/dealer-hand.component.ts +++ b/frontend/src/app/feature/game/blackjack/components/dealer-hand/dealer-hand.component.ts @@ -35,30 +35,30 @@ import { Card } from '../../models/blackjack.model'; export class DealerHandComponent implements OnChanges { @Input() cards: Card[] = []; cardsWithState: (Card & { isNew: boolean; id: string })[] = []; - + private lastCardCount = 0; - + ngOnChanges(changes: SimpleChanges): void { if (changes['cards']) { this.updateCardsWithState(); } } - + private updateCardsWithState(): void { const newCards = this.cards.length > this.lastCardCount; - + this.cardsWithState = this.cards.map((card, index) => { // Consider a card new if it's added after the initial state and is the latest card const isNew = newCards && index >= this.lastCardCount; - + return { ...card, isNew, // Generate a unique ID to help Angular track the cards - id: `${card.suit}-${card.rank}-${index}` + id: `${card.suit}-${card.rank}-${index}`, }; }); - + this.lastCardCount = this.cards.length; } } 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 899417f..9922309 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 @@ -10,7 +10,9 @@ import { Card } from '../../models/blackjack.model';
-
Deine Punkte: {{ calculateHandValue(playerCards) }}
+
+ Deine Punkte: {{ calculateHandValue(playerCards) }} +
Status: {{ getStatusText(gameState) }}
@@ -26,7 +28,9 @@ import { Card } from '../../models/blackjack.model'; Ziehen @if (isActionInProgress) {
-
+
} @@ -39,7 +43,9 @@ import { Card } from '../../models/blackjack.model'; Halten @if (isActionInProgress) {
-
+
} @@ -52,7 +58,9 @@ import { Card } from '../../models/blackjack.model'; Verdoppeln @if (isActionInProgress) {
-
+
} @@ -71,9 +79,9 @@ import { Card } from '../../models/blackjack.model'; }) export class GameControlsComponent { @Input() playerCards: Card[] = []; - @Input() gameState: string = 'IN_PROGRESS'; - @Input() isActionInProgress: boolean = false; - + @Input() gameState = 'IN_PROGRESS'; + @Input() isActionInProgress = false; + @Output() hit = new EventEmitter(); @Output() stand = new EventEmitter(); @Output() doubleDown = new EventEmitter(); @@ -82,7 +90,7 @@ export class GameControlsComponent { calculateHandValue(cards: Card[]): number { let sum = 0; let aceCount = 0; - + const rankValues: Record = { TWO: 2, THREE: 3, @@ -96,9 +104,9 @@ export class GameControlsComponent { JACK: 10, QUEEN: 10, KING: 10, - ACE: 11 + ACE: 11, }; - + for (const card of cards) { if (!card.hidden) { const value = rankValues[card.rank] || 0; @@ -108,12 +116,12 @@ export class GameControlsComponent { } } } - + while (sum > 21 && aceCount > 0) { sum -= 10; aceCount--; } - + return sum; } diff --git a/frontend/src/app/feature/game/blackjack/components/game-info/game-info.component.ts b/frontend/src/app/feature/game/blackjack/components/game-info/game-info.component.ts index f6b6091..6219b98 100644 --- a/frontend/src/app/feature/game/blackjack/components/game-info/game-info.component.ts +++ b/frontend/src/app/feature/game/blackjack/components/game-info/game-info.component.ts @@ -92,7 +92,9 @@ import { FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angula Neues Spiel @if (isActionInProgress) {
-
+
} diff --git a/frontend/src/app/feature/game/blackjack/components/game-result/game-result.component.spec.ts b/frontend/src/app/feature/game/blackjack/components/game-result/game-result.component.spec.ts index fbc1d3d..81ac70d 100644 --- a/frontend/src/app/feature/game/blackjack/components/game-result/game-result.component.spec.ts +++ b/frontend/src/app/feature/game/blackjack/components/game-result/game-result.component.spec.ts @@ -8,9 +8,8 @@ describe('GameResultComponent', () => { beforeEach(async () => { await TestBed.configureTestingModule({ - imports: [GameResultComponent] - }) - .compileComponents(); + imports: [GameResultComponent], + }).compileComponents(); fixture = TestBed.createComponent(GameResultComponent); component = fixture.componentInstance; diff --git a/frontend/src/app/feature/game/blackjack/components/game-result/game-result.component.ts b/frontend/src/app/feature/game/blackjack/components/game-result/game-result.component.ts index 48021c7..e84a7ce 100644 --- a/frontend/src/app/feature/game/blackjack/components/game-result/game-result.component.ts +++ b/frontend/src/app/feature/game/blackjack/components/game-result/game-result.component.ts @@ -7,55 +7,49 @@ import { animate, style, transition, trigger } from '@angular/animations'; standalone: true, imports: [CommonModule, CurrencyPipe], template: ` -