import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core'; import { CommonModule } from '@angular/common'; import { Card } from '../../models/blackjack.model'; @Component({ selector: 'app-game-controls', standalone: true, imports: [CommonModule], template: `
Deine Punkte: {{ calculateHandValue(playerCards) }}
Status: {{ getStatusText(gameState) }}
`, changeDetection: ChangeDetectionStrategy.OnPush, }) export class GameControlsComponent { @Input() playerCards: Card[] = []; @Input() gameState: string = 'IN_PROGRESS'; @Output() hit = new EventEmitter(); @Output() stand = new EventEmitter(); @Output() doubleDown = new EventEmitter(); @Output() leave = new EventEmitter(); calculateHandValue(cards: Card[]): number { let sum = 0; let aceCount = 0; const rankValues: Record = { TWO: 2, THREE: 3, FOUR: 4, FIVE: 5, SIX: 6, SEVEN: 7, EIGHT: 8, NINE: 9, TEN: 10, JACK: 10, QUEEN: 10, KING: 10, ACE: 11 }; for (const card of cards) { if (!card.hidden) { const value = rankValues[card.rank] || 0; sum += value; if (card.rank === 'ACE') { aceCount++; } } } while (sum > 21 && aceCount > 0) { sum -= 10; aceCount--; } return sum; } getStatusText(state: string): string { switch (state) { case 'IN_PROGRESS': return 'Spiel läuft'; case 'PLAYER_WON': return 'Gewonnen!'; case 'PLAYER_LOST': return 'Verloren!'; case 'DRAW': return 'Unentschieden!'; default: return state; } } getStatusClass(state: string): string { switch (state) { case 'PLAYER_WON': return 'text-emerald'; case 'PLAYER_LOST': return 'text-accent-red'; case 'DRAW': return 'text-yellow-400'; default: return 'text-white'; } } }