feat(blackjack): add split functionality to the game
All checks were successful
CI / Get Changed Files (pull_request) Successful in 6s
CI / prettier (pull_request) Successful in 19s
CI / Checkstyle Main (pull_request) Successful in 50s
CI / eslint (pull_request) Successful in 53s
CI / test-build (pull_request) Successful in 1m12s

This commit is contained in:
Jan-Marlon Leibl 2025-04-02 11:25:03 +02:00
parent 4a7c54eab8
commit 3ef5530e77
Signed by: jleibl
GPG key ID: 300B2F906DC6F1D5
7 changed files with 410 additions and 276 deletions

View file

@ -6,9 +6,11 @@ import {
OnChanges,
Output,
SimpleChanges,
signal,
} from '@angular/core';
import { CommonModule, CurrencyPipe } from '@angular/common';
import { FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { FormGroup, ReactiveFormsModule } from '@angular/forms';
import { BettingService } from '@blackjack/services/betting.service';
@Component({
selector: 'app-game-info',
@ -101,7 +103,14 @@ import { FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angula
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class GameInfoComponent implements OnChanges {
@Input() balance = 0;
@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;
@ -109,24 +118,19 @@ export class GameInfoComponent implements OnChanges {
betForm: FormGroup;
constructor(private fb: FormBuilder) {
this.betForm = this.fb.group({
bet: ['', [Validators.required, Validators.min(1)]],
});
constructor(private bettingService: BettingService) {
this.betForm = this.bettingService.createBetForm();
}
ngOnChanges(changes: SimpleChanges): void {
if (changes['balance']) {
this.betForm
.get('bet')
?.setValidators([Validators.required, Validators.min(1), Validators.max(this.balance)]);
this.betForm.get('bet')?.updateValueAndValidity();
this.bettingService.updateBetFormValidators(this.betForm, this.balance);
}
}
setBetAmount(percentage: number) {
const betAmount = Math.floor(this.balance * percentage * 100) / 100;
if (betAmount >= 1) {
const betAmount = this.bettingService.calculateBetAmount(this.balance, percentage);
if (this.bettingService.isValidBet(betAmount, this.balance)) {
this.betForm.patchValue({ bet: betAmount });
}
}
@ -134,7 +138,7 @@ export class GameInfoComponent implements OnChanges {
onSubmit() {
if (this.betForm.valid) {
const betAmount = parseFloat(this.betForm.value.bet);
if (betAmount <= this.balance) {
if (this.bettingService.isValidBet(betAmount, this.balance)) {
this.newGame.emit(betAmount);
this.betForm.reset();
}