feat: add stand and get game features to blackjack game
This commit is contained in:
parent
4e8530c861
commit
d90fcdcf1e
13 changed files with 446 additions and 32 deletions
|
@ -10,6 +10,8 @@ import { GameControlsComponent } from './components/game-controls/game-controls.
|
|||
import { GameInfoComponent } from './components/game-info/game-info.component';
|
||||
import { Card, BlackjackGame } from './models/blackjack.model';
|
||||
import { BlackjackService } from './services/blackjack.service';
|
||||
import { HttpErrorResponse } from '@angular/common/http';
|
||||
import { GameResultComponent } from './components/game-result/game-result.component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-blackjack',
|
||||
|
@ -22,6 +24,7 @@ import { BlackjackService } from './services/blackjack.service';
|
|||
PlayerHandComponent,
|
||||
GameControlsComponent,
|
||||
GameInfoComponent,
|
||||
GameResultComponent,
|
||||
],
|
||||
templateUrl: './blackjack.component.html',
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
|
@ -37,8 +40,14 @@ export default class BlackjackComponent {
|
|||
balance = signal(0);
|
||||
currentGameId = signal<number | undefined>(undefined);
|
||||
gameInProgress = signal(false);
|
||||
gameState = signal<string>('IN_PROGRESS');
|
||||
showGameResult = signal(false);
|
||||
|
||||
constructor() {
|
||||
this.refreshUserBalance();
|
||||
}
|
||||
|
||||
private refreshUserBalance(): void {
|
||||
this.userService.getCurrentUser().subscribe((user) => {
|
||||
this.balance.set(user?.balance ?? 0);
|
||||
});
|
||||
|
@ -49,6 +58,7 @@ export default class BlackjackComponent {
|
|||
this.currentGameId.set(game.id);
|
||||
this.currentBet.set(game.bet);
|
||||
this.gameInProgress.set(game.state === 'IN_PROGRESS');
|
||||
this.gameState.set(game.state);
|
||||
|
||||
this.dealerCards.set(
|
||||
game.dealerCards.map((card, index) => ({
|
||||
|
@ -63,12 +73,20 @@ export default class BlackjackComponent {
|
|||
hidden: false,
|
||||
}))
|
||||
);
|
||||
|
||||
if (game.state !== 'IN_PROGRESS') {
|
||||
this.refreshUserBalance();
|
||||
setTimeout(() => {
|
||||
this.showGameResult.set(true);
|
||||
}, 1000);
|
||||
}
|
||||
}
|
||||
|
||||
onNewGame(bet: number): void {
|
||||
this.blackjackService.startGame(bet).subscribe({
|
||||
next: (game) => {
|
||||
this.updateGameState(game);
|
||||
this.refreshUserBalance();
|
||||
},
|
||||
error: (error) => {
|
||||
console.error('Failed to start game:', error);
|
||||
|
@ -85,12 +103,18 @@ export default class BlackjackComponent {
|
|||
},
|
||||
error: (error) => {
|
||||
console.error('Failed to hit:', error);
|
||||
this.handleGameError(error);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
onStand(): void {
|
||||
if (!this.currentGameId()) return;
|
||||
|
||||
if (this.gameState() !== 'IN_PROGRESS') {
|
||||
console.log('Cannot stand: game is not in progress');
|
||||
return;
|
||||
}
|
||||
|
||||
this.blackjackService.stand(this.currentGameId()!).subscribe({
|
||||
next: (game) => {
|
||||
|
@ -98,10 +122,47 @@ export default class BlackjackComponent {
|
|||
},
|
||||
error: (error) => {
|
||||
console.error('Failed to stand:', error);
|
||||
this.handleGameError(error);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
onCloseGameResult(): void {
|
||||
this.showGameResult.set(false);
|
||||
}
|
||||
|
||||
private handleGameError(error: any): 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) {
|
||||
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()!);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private refreshGameState(gameId: number): void {
|
||||
this.blackjackService.getGame(gameId).subscribe({
|
||||
next: (game) => {
|
||||
this.updateGameState(game);
|
||||
},
|
||||
error: (err) => {
|
||||
console.error('Failed to refresh game state:', err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
leaveGame(): void {
|
||||
this.router.navigate(['/home']);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue