feat: added bot, light and dark mode
This commit was merged in pull request #1.
This commit is contained in:
+162
-121
@@ -1,11 +1,13 @@
|
||||
import { DestroyRef, Injectable, OnDestroy, inject } from '@angular/core';
|
||||
import { Router } from '@angular/router';
|
||||
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
||||
import { interval, startWith, Subscription, switchMap } from 'rxjs';
|
||||
import { getPieceAtSquare, isPieceColor } from '../../core/chess/fen.utils';
|
||||
import { getErrorMessage } from '../../core/http/error-message.util';
|
||||
import { GameFull, GameState, GameStreamEvent, LegalMove } from '../../models/game.models';
|
||||
import { GameApiService } from '../../services/game-api.service';
|
||||
import { BotMoveService } from '../../services/bot-move.service';
|
||||
import { GameCompletionService } from '../../services/game-completion.service';
|
||||
import { GameImportService } from '../../services/game-import.service';
|
||||
import { BoardSelectionService, BoardSelection } from '../../services/board-selection.service';
|
||||
import { GameStreamService } from '../../services/game-stream.service';
|
||||
|
||||
@Injectable()
|
||||
export class GameFacade implements OnDestroy {
|
||||
@@ -16,25 +18,42 @@ export class GameFacade implements OnDestroy {
|
||||
fenInput = '';
|
||||
pgnInput = '';
|
||||
loading = true;
|
||||
selectedSquare: string | null = null;
|
||||
highlightedSquares: string[] = [];
|
||||
gameCompletionMessage = '';
|
||||
isGameFinished = false;
|
||||
isPromotionDialogOpen = false;
|
||||
|
||||
private boardSelection: BoardSelection = {
|
||||
selectedSquare: null,
|
||||
highlightedSquares: [],
|
||||
selectedSquareMoves: []
|
||||
};
|
||||
private pendingPromotionMoves: LegalMove[] = [];
|
||||
|
||||
private selectedSquareMoves: LegalMove[] = [];
|
||||
private readonly router = inject(Router);
|
||||
private readonly gameApi = inject(GameApiService);
|
||||
private readonly destroyRef = inject(DestroyRef);
|
||||
private streamSubscription: Subscription | null = null;
|
||||
private pollSubscription: Subscription | null = null;
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.streamSubscription?.unsubscribe();
|
||||
this.pollSubscription?.unsubscribe();
|
||||
}
|
||||
private readonly botMoveService = inject(BotMoveService);
|
||||
private readonly completionService = inject(GameCompletionService);
|
||||
private readonly importService = inject(GameImportService);
|
||||
private readonly boardSelectionService = inject(BoardSelectionService);
|
||||
private readonly streamService = inject(GameStreamService);
|
||||
|
||||
get state(): GameState | null {
|
||||
return this.game?.state ?? null;
|
||||
}
|
||||
|
||||
get selectedSquare(): string | null {
|
||||
return this.boardSelection.selectedSquare;
|
||||
}
|
||||
|
||||
get highlightedSquares(): string[] {
|
||||
return this.boardSelection.highlightedSquares;
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.streamService.cleanup();
|
||||
this.botMoveService.cleanup();
|
||||
}
|
||||
|
||||
setGameId(gameId: string): void {
|
||||
this.gameId = gameId;
|
||||
this.loadGame();
|
||||
@@ -45,36 +64,45 @@ export class GameFacade implements OnDestroy {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.selectedSquare && this.highlightedSquares.includes(square)) {
|
||||
const selectedMove = this.selectedSquareMoves.find((move) => move.to === square);
|
||||
// Handle move selection
|
||||
if (this.boardSelection.selectedSquare && this.boardSelection.highlightedSquares.includes(square)) {
|
||||
const selectedMove = this.boardSelection.selectedSquareMoves.find((move) => move.to === square);
|
||||
if (selectedMove) {
|
||||
// If multiple promotion outcomes exist for the target, ask player to choose one.
|
||||
const promotionMoves = this.boardSelection.selectedSquareMoves.filter(
|
||||
(move) => move.to === square && !!move.promotion
|
||||
);
|
||||
if (promotionMoves.length > 0) {
|
||||
this.pendingPromotionMoves = promotionMoves;
|
||||
this.isPromotionDialogOpen = true;
|
||||
return;
|
||||
}
|
||||
this.moveInput = selectedMove.uci;
|
||||
this.submitMove();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const piece = getPieceAtSquare(this.state.fen, square);
|
||||
if (!piece || !isPieceColor(piece, this.state.turn)) {
|
||||
this.clearSelection();
|
||||
return;
|
||||
}
|
||||
|
||||
// Load moves for selected square
|
||||
this.errorMessage = '';
|
||||
this.gameApi
|
||||
.getLegalMoves(this.gameId, square)
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe({
|
||||
next: (response) => {
|
||||
this.selectedSquare = square;
|
||||
this.selectedSquareMoves = response.moves;
|
||||
this.highlightedSquares = response.moves.map((move) => move.to);
|
||||
},
|
||||
error: () => {
|
||||
this.clearSelection();
|
||||
this.errorMessage = 'Could not load legal moves for selected square.';
|
||||
}
|
||||
});
|
||||
const newSelection = this.boardSelectionService.handleSquareSelection(
|
||||
square,
|
||||
this.gameId,
|
||||
this.state,
|
||||
this.boardSelection,
|
||||
(moves) => {
|
||||
this.boardSelection = {
|
||||
selectedSquare: square,
|
||||
highlightedSquares: moves.map((move) => move.to),
|
||||
selectedSquareMoves: moves
|
||||
};
|
||||
},
|
||||
(error) => {
|
||||
this.errorMessage = error;
|
||||
this.boardSelection = this.boardSelectionService.clearSelection();
|
||||
}
|
||||
);
|
||||
this.boardSelection = newSelection;
|
||||
}
|
||||
|
||||
submitMove(): void {
|
||||
@@ -91,9 +119,25 @@ export class GameFacade implements OnDestroy {
|
||||
next: (state) => {
|
||||
if (this.game) {
|
||||
this.game = { ...this.game, state };
|
||||
this.updateGameCompletion();
|
||||
}
|
||||
this.moveInput = '';
|
||||
this.clearSelection();
|
||||
this.boardSelection = this.boardSelectionService.clearSelection();
|
||||
this.botMoveService.tryMakeBotMove(
|
||||
this.gameId,
|
||||
this.game,
|
||||
this.state,
|
||||
(updatedState) => {
|
||||
if (this.game) {
|
||||
this.game = { ...this.game, state: updatedState };
|
||||
this.updateGameCompletion();
|
||||
}
|
||||
this.boardSelection = this.boardSelectionService.clearSelection();
|
||||
},
|
||||
(error) => {
|
||||
this.errorMessage = error;
|
||||
}
|
||||
);
|
||||
},
|
||||
error: (error) => {
|
||||
this.errorMessage = getErrorMessage(error, 'Move rejected.');
|
||||
@@ -101,59 +145,61 @@ export class GameFacade implements OnDestroy {
|
||||
});
|
||||
}
|
||||
|
||||
importFen(): void {
|
||||
const fen = this.fenInput.trim();
|
||||
if (!fen) {
|
||||
this.errorMessage = 'Please provide a FEN string.';
|
||||
onPromotionSelected(promotionPiece: 'queen' | 'rook' | 'bishop' | 'knight'): void {
|
||||
const selectedPromotionMove = this.pendingPromotionMoves.find((move) => move.promotion === promotionPiece);
|
||||
if (!selectedPromotionMove) {
|
||||
this.errorMessage = 'Selected promotion move is unavailable.';
|
||||
this.isPromotionDialogOpen = false;
|
||||
this.pendingPromotionMoves = [];
|
||||
return;
|
||||
}
|
||||
|
||||
this.moveInput = selectedPromotionMove.uci;
|
||||
this.isPromotionDialogOpen = false;
|
||||
this.boardSelection = this.boardSelectionService.clearSelection();
|
||||
this.pendingPromotionMoves = [];
|
||||
this.submitMove();
|
||||
}
|
||||
|
||||
onPromotionClosed(): void {
|
||||
this.isPromotionDialogOpen = false;
|
||||
this.boardSelection = this.boardSelectionService.clearSelection();
|
||||
this.pendingPromotionMoves = [];
|
||||
}
|
||||
|
||||
importFen(): void {
|
||||
this.errorMessage = '';
|
||||
this.gameApi
|
||||
.importFen(fen)
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe({
|
||||
next: (game) => {
|
||||
this.fenInput = '';
|
||||
this.pgnInput = '';
|
||||
void this.router.navigate(['/game', game.gameId]);
|
||||
},
|
||||
error: (error) => {
|
||||
this.errorMessage = getErrorMessage(error, 'FEN import failed.');
|
||||
}
|
||||
});
|
||||
this.importService.importFen(
|
||||
this.fenInput,
|
||||
() => {
|
||||
this.fenInput = '';
|
||||
this.pgnInput = '';
|
||||
},
|
||||
(error) => {
|
||||
this.errorMessage = error;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
importPgn(): void {
|
||||
const pgn = this.pgnInput.trim();
|
||||
if (!pgn) {
|
||||
this.errorMessage = 'Please provide a PGN string.';
|
||||
return;
|
||||
}
|
||||
|
||||
this.errorMessage = '';
|
||||
this.gameApi
|
||||
.importPgn(pgn)
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe({
|
||||
next: (game) => {
|
||||
this.pgnInput = '';
|
||||
this.fenInput = '';
|
||||
void this.router.navigate(['/game', game.gameId]);
|
||||
},
|
||||
error: (error) => {
|
||||
this.errorMessage = getErrorMessage(error, 'PGN import failed.');
|
||||
}
|
||||
});
|
||||
this.importService.importPgn(
|
||||
this.pgnInput,
|
||||
() => {
|
||||
this.pgnInput = '';
|
||||
this.fenInput = '';
|
||||
},
|
||||
(error) => {
|
||||
this.errorMessage = error;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private loadGame(): void {
|
||||
this.loading = true;
|
||||
this.errorMessage = '';
|
||||
this.clearSelection();
|
||||
this.streamSubscription?.unsubscribe();
|
||||
this.pollSubscription?.unsubscribe();
|
||||
this.pollSubscription = null;
|
||||
this.boardSelection = this.boardSelectionService.clearSelection();
|
||||
this.streamService.cleanup();
|
||||
|
||||
this.gameApi
|
||||
.getGame(this.gameId)
|
||||
@@ -162,7 +208,9 @@ export class GameFacade implements OnDestroy {
|
||||
next: (game) => {
|
||||
this.game = game;
|
||||
this.loading = false;
|
||||
this.startStream();
|
||||
this.updateGameCompletion();
|
||||
this.startStreaming();
|
||||
this.tryMakeBotMove();
|
||||
},
|
||||
error: (error) => {
|
||||
this.errorMessage = getErrorMessage(error, `Could not load game ${this.gameId}.`);
|
||||
@@ -171,57 +219,32 @@ export class GameFacade implements OnDestroy {
|
||||
});
|
||||
}
|
||||
|
||||
private startStream(): void {
|
||||
this.streamSubscription = this.gameApi
|
||||
.streamGame(this.gameId)
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe({
|
||||
next: (event) => this.applyStreamEvent(event),
|
||||
error: () => {
|
||||
this.errorMessage = 'Live stream disconnected. Falling back to polling.';
|
||||
this.startPolling();
|
||||
},
|
||||
complete: () => {
|
||||
this.errorMessage = 'Live stream ended. Falling back to polling.';
|
||||
this.startPolling();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private startPolling(): void {
|
||||
if (this.pollSubscription) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.pollSubscription = interval(1500)
|
||||
.pipe(
|
||||
startWith(0),
|
||||
switchMap(() => this.gameApi.getGame(this.gameId)),
|
||||
takeUntilDestroyed(this.destroyRef)
|
||||
)
|
||||
.subscribe({
|
||||
next: (game) => {
|
||||
const previousMoves = this.game?.state.moves.join(',') ?? '';
|
||||
this.game = game;
|
||||
if (previousMoves !== game.state.moves.join(',')) {
|
||||
this.clearSelection();
|
||||
}
|
||||
}
|
||||
});
|
||||
private startStreaming(): void {
|
||||
this.streamService.startStreaming(
|
||||
this.gameId,
|
||||
(event) => this.applyStreamEvent(event),
|
||||
() => {
|
||||
this.errorMessage = 'Live stream disconnected. Falling back to polling.';
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private applyStreamEvent(event: GameStreamEvent): void {
|
||||
if (event.type === 'gameFull') {
|
||||
this.game = event.game;
|
||||
this.clearSelection();
|
||||
this.boardSelection = this.boardSelectionService.clearSelection();
|
||||
this.updateGameCompletion();
|
||||
this.tryMakeBotMove();
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.type === 'gameState' && this.game) {
|
||||
const moveCountBefore = this.game.state.moves.length;
|
||||
this.game = { ...this.game, state: event.state };
|
||||
this.updateGameCompletion();
|
||||
if (event.state.moves.length !== moveCountBefore) {
|
||||
this.clearSelection();
|
||||
this.boardSelection = this.boardSelectionService.clearSelection();
|
||||
this.tryMakeBotMove();
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -231,9 +254,27 @@ export class GameFacade implements OnDestroy {
|
||||
}
|
||||
}
|
||||
|
||||
private clearSelection(): void {
|
||||
this.selectedSquare = null;
|
||||
this.selectedSquareMoves = [];
|
||||
this.highlightedSquares = [];
|
||||
private tryMakeBotMove(): void {
|
||||
this.botMoveService.tryMakeBotMove(
|
||||
this.gameId,
|
||||
this.game,
|
||||
this.state,
|
||||
(updatedState) => {
|
||||
if (this.game) {
|
||||
this.game = { ...this.game, state: updatedState };
|
||||
this.updateGameCompletion();
|
||||
}
|
||||
this.boardSelection = this.boardSelectionService.clearSelection();
|
||||
},
|
||||
(error) => {
|
||||
this.errorMessage = error;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private updateGameCompletion(): void {
|
||||
const completion = this.completionService.getGameCompletion(this.game, this.state);
|
||||
this.gameCompletionMessage = completion.message;
|
||||
this.isGameFinished = completion.isFinished;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user