import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnChanges, Output, SimpleChanges, signal, } from '@angular/core'; import { CommonModule, CurrencyPipe } from '@angular/common'; import { FormGroup, ReactiveFormsModule } from '@angular/forms'; import { BettingService } from '@blackjack/services/betting.service'; import { AnimatedNumberComponent } from '../animated-number/animated-number.component'; @Component({ selector: 'app-game-info', standalone: true, imports: [CommonModule, CurrencyPipe, ReactiveFormsModule, AnimatedNumberComponent], template: `

Spiel Informationen

Aktuelle Wette:
@if (!gameInProgress) {
}
@if (betForm.get('bet')?.errors?.['required'] && betForm.get('bet')?.touched) { Bitte geben Sie einen Einsatz ein } @if (betForm.get('bet')?.errors?.['min'] && betForm.get('bet')?.touched) { Mindestens 1€ setzen } @if (betForm.get('bet')?.errors?.['max'] && betForm.get('bet')?.touched) { Nicht genügend Guthaben }
`, changeDetection: ChangeDetectionStrategy.OnPush, }) export class GameInfoComponent implements OnChanges { @Input() set balance(value: number) { this._balance.set(value); } get balance() { return this._balance(); } private _balance = signal(0); @Input() currentBet = 0; @Input() gameInProgress = false; @Input() isActionInProgress = false; @Output() newGame = new EventEmitter(); betForm: FormGroup; constructor(private bettingService: BettingService) { this.betForm = this.bettingService.createBetForm(); } ngOnChanges(changes: SimpleChanges): void { if (changes['balance']) { this.bettingService.updateBetFormValidators(this.betForm, this.balance); } } setBetAmount(percentage: number) { const betAmount = this.bettingService.calculateBetAmount(this.balance, percentage); if (this.bettingService.isValidBet(betAmount, this.balance)) { this.betForm.patchValue({ bet: betAmount }); } } onSubmit() { if (this.betForm.valid) { const betAmount = parseFloat(this.betForm.value.bet); if (this.bettingService.isValidBet(betAmount, this.balance)) { this.newGame.emit(betAmount); this.betForm.reset(); } } } }