import { Component, OnDestroy, OnInit, inject } from '@angular/core'; import { ChessBoardComponent } from '../chess-board/chess-board.component'; import { StockfishService } from './stockfish.service'; import { ChessBoardService } from '../chess-board/chess-board.service'; import { Subscription, firstValueFrom } from 'rxjs'; import { Color } from '../../chess-logic/models'; import { MoveListComponent } from '../move-list/move-list.component'; import { CommonModule } from '@angular/common'; @Component({ selector: 'app-computer-mode', imports: [MoveListComponent, CommonModule], templateUrl: '../chess-board/chess-board.component.html', styleUrls: ['../chess-board/chess-board.component.css'] }) export class ComputerModeComponent extends ChessBoardComponent implements OnInit, OnDestroy { private computerSubscriptions$ = new Subscription(); constructor(private stockfishService: StockfishService) { super(inject(ChessBoardService)); } public override ngOnInit(): void { super.ngOnInit(); const computerConfiSubscription$: Subscription = this.stockfishService.computerConfiguration$.subscribe({ next: (computerConfiguration) => { if (computerConfiguration.color === Color.White) this.flipBoard(); } }); const chessBoardStateSubscription$: Subscription = this.chessBoardService.chessBoardState$.subscribe({ next: async (FEN: string) => { if (this.chessBoard.isGameOver) { chessBoardStateSubscription$.unsubscribe(); return; } const player: Color = FEN.split(" ")[1] === "w" ? Color.White : Color.Black; console.log("FEN contains :%s", player === 0 ? "White" : "Black"); console.log("Computer Configuration :%s", this.stockfishService.computerConfiguration$.value.color === 0 ? "White" : "Black"); // if (this.stockfishService.computerConfiguration$.value.color === Color.White) { if ( player !== this.stockfishService.computerConfiguration$.value.color) { // Not the computer's turn console.log("Not the computer's turn", this.stockfishService.computerConfiguration$.value.color); return; } // } // else { // if (player === this.stockfishService.computerConfiguration$.value.color) { // // Not the computer's turn // console.log("Not the computer's turn", this.stockfishService.computerConfiguration$.value.color); // return; // } // } const { prevX, prevY, newX, newY, promotedPiece } = await firstValueFrom(this.stockfishService.getBestMove(FEN)); this.updateBoard({x: prevX,y: prevY}, {x: newX, y: newY}, promotedPiece); } }); this.computerSubscriptions$.add(chessBoardStateSubscription$); this.computerSubscriptions$.add(computerConfiSubscription$); } public override ngOnDestroy(): void { super.ngOnDestroy(); this.computerSubscriptions$.unsubscribe(); } }