style: format code

This commit is contained in:
Phan Huy Tran 2025-05-21 10:42:36 +02:00 committed by Phan Huy Tran
commit f2aa81b6d2
5 changed files with 274 additions and 221 deletions

View file

@ -1,118 +1,124 @@
import { Component, signal, inject, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormBuilder, FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { DiceService } from './dice.service';
import { DiceDto, DiceResult } from './dice.model';
import { debounceTime, tap } from 'rxjs/operators';
import {UserService} from "@service/user.service";
type DiceFormGroup = FormGroup<{
betAmount: FormControl<number | null>;
rollOver: FormControl<boolean>;
targetValue: FormControl<number | null>;
}>;
@Component({
selector: 'app-dice',
standalone: true,
imports: [CommonModule, ReactiveFormsModule],
templateUrl: './dice.component.html',
})
export class DiceComponent implements OnInit {
private readonly formBuilder = inject(FormBuilder);
private readonly diceService = inject(DiceService);
private readonly userService = inject(UserService);
rolledValue = signal<number | null>(null);
win = signal<boolean | null>(null);
payout = signal<number | null>(null);
winChance = signal(0);
potentialWin = signal(0);
readonly diceForm: DiceFormGroup = this.createDiceForm();
private readonly MAX_DICE_VALUE = 100;
constructor() {
}
ngOnInit(): void {
this.diceForm.valueChanges.pipe(
debounceTime(100),
tap(() => this.calculateWinChanceAndPotentialWin())
).subscribe();
this.calculateWinChanceAndPotentialWin();
}
createDiceForm(): DiceFormGroup {
return this.formBuilder.group({
betAmount: new FormControl<number | null>(1.00, {
validators: [Validators.required, Validators.min(0.01)],
nonNullable: true,
}),
rollOver: new FormControl<boolean>(true, {
validators: [Validators.required],
nonNullable: true,
}),
targetValue: new FormControl<number | null>(50.50, {
validators: [Validators.required, Validators.min(1), Validators.max(100)],
nonNullable: true,
}),
});
}
toggleRollMode(): void {
const currentMode = this.diceForm.get('rollOver')?.value;
this.diceForm.get('rollOver')?.setValue(!currentMode);
}
calculateWinChanceAndPotentialWin(): void {
const formValues = this.diceForm.value;
const target = formValues.targetValue ?? 0;
const bet = formValues.betAmount ?? 0;
const isOver = formValues.rollOver ?? true;
const calculatedWinChance = isOver ? this.MAX_DICE_VALUE - target : target - 1;
this.winChance.set(Math.max(0, calculatedWinChance));
let multiplier = 0;
if (calculatedWinChance > 0) {
multiplier = (this.MAX_DICE_VALUE - 1) / calculatedWinChance;
}
this.potentialWin.set(bet * multiplier);
}
roll(): void {
if (this.diceForm.invalid) {
this.diceForm.markAllAsTouched();
return;
}
const diceDto: DiceDto = this.diceForm.getRawValue() as DiceDto;
this.rolledValue.set(null);
this.win.set(null);
this.payout.set(null);
this.diceService.rollDice(diceDto).subscribe({
next: (result: DiceResult) => {
this.rolledValue.set(result.rolledValue);
this.win.set(result.win);
this.payout.set(result.payout);
this.userService.refreshCurrentUser();
},
error: (error) => {
console.error('Dice roll failed:', error);
}
});
}
hasError(controlName: string, errorName: string): boolean {
const control = this.diceForm.get(controlName);
return control !== null && control.touched && control.hasError(errorName);
}
}
import { Component, signal, inject, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import {
FormBuilder,
FormControl,
FormGroup,
ReactiveFormsModule,
Validators,
} from '@angular/forms';
import { DiceService } from './dice.service';
import { DiceDto, DiceResult } from './dice.model';
import { debounceTime, tap } from 'rxjs/operators';
import { UserService } from '@service/user.service';
type DiceFormGroup = FormGroup<{
betAmount: FormControl<number | null>;
rollOver: FormControl<boolean>;
targetValue: FormControl<number | null>;
}>;
@Component({
selector: 'app-dice',
standalone: true,
imports: [CommonModule, ReactiveFormsModule],
templateUrl: './dice.component.html',
})
export class DiceComponent implements OnInit {
private readonly formBuilder = inject(FormBuilder);
private readonly diceService = inject(DiceService);
private readonly userService = inject(UserService);
rolledValue = signal<number | null>(null);
win = signal<boolean | null>(null);
payout = signal<number | null>(null);
winChance = signal(0);
potentialWin = signal(0);
readonly diceForm: DiceFormGroup = this.createDiceForm();
private readonly MAX_DICE_VALUE = 100;
constructor() {}
ngOnInit(): void {
this.diceForm.valueChanges
.pipe(
debounceTime(100),
tap(() => this.calculateWinChanceAndPotentialWin())
)
.subscribe();
this.calculateWinChanceAndPotentialWin();
}
createDiceForm(): DiceFormGroup {
return this.formBuilder.group({
betAmount: new FormControl<number | null>(1.0, {
validators: [Validators.required, Validators.min(0.01)],
nonNullable: true,
}),
rollOver: new FormControl<boolean>(true, {
validators: [Validators.required],
nonNullable: true,
}),
targetValue: new FormControl<number | null>(50.5, {
validators: [Validators.required, Validators.min(1), Validators.max(100)],
nonNullable: true,
}),
});
}
toggleRollMode(): void {
const currentMode = this.diceForm.get('rollOver')?.value;
this.diceForm.get('rollOver')?.setValue(!currentMode);
}
calculateWinChanceAndPotentialWin(): void {
const formValues = this.diceForm.value;
const target = formValues.targetValue ?? 0;
const bet = formValues.betAmount ?? 0;
const isOver = formValues.rollOver ?? true;
const calculatedWinChance = isOver ? this.MAX_DICE_VALUE - target : target - 1;
this.winChance.set(Math.max(0, calculatedWinChance));
let multiplier = 0;
if (calculatedWinChance > 0) {
multiplier = (this.MAX_DICE_VALUE - 1) / calculatedWinChance;
}
this.potentialWin.set(bet * multiplier);
}
roll(): void {
if (this.diceForm.invalid) {
this.diceForm.markAllAsTouched();
return;
}
const diceDto: DiceDto = this.diceForm.getRawValue() as DiceDto;
this.rolledValue.set(null);
this.win.set(null);
this.payout.set(null);
this.diceService.rollDice(diceDto).subscribe({
next: (result: DiceResult) => {
this.rolledValue.set(result.rolledValue);
this.win.set(result.win);
this.payout.set(result.payout);
this.userService.refreshCurrentUser();
},
error: (error) => {
console.error('Dice roll failed:', error);
},
});
}
hasError(controlName: string, errorName: string): boolean {
const control = this.diceForm.get(controlName);
return control !== null && control.touched && control.hasError(errorName);
}
}