Files
NowChess-Frontend/src/app/services/game-api.service.ts
T
Janis Eccarius 8e2afb93f3 feat: NCWF-5/6/7/8/9 chess analysis page and engine integration
- NCWF-5: scaffold /analysis route with ChessBoard viewer and navigation
- NCWF-6: FEN / PGN / Game-ID input form with depth selector
- NCWF-7: extend GameApiService with analyzePosition(); add AnalysisService
  with game-wide annotation pipeline; proxy /api/analysis -> :8087
- NCWF-8: EvalTimelineComponent — SVG win-chance chart per ply
- NCWF-9: AnnotatedMoveListComponent — quality labels (!! ! ?! ? ??)
  derived from win-chance delta

Also fix pre-existing app.spec.ts failure (missing provideHttpClient).
Apply project-wide prettier formatting pass.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-10 15:55:14 +02:00

103 lines
3.4 KiB
TypeScript

import { Injectable, inject } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { environment } from '../../environments/environment';
import {
GameFull,
GameState,
GameStreamEvent,
LegalMovesResponse,
PlayerInfo,
} from '../models/game.models';
import { AnalysisRequest, AnalysisResponse } from '../models/analysis.models';
import { StreamHandlerService } from './stream-handler.service';
@Injectable({ providedIn: 'root' })
export class GameApiService {
private readonly apiBase = environment.apiBaseUrl;
private readonly wsBase = environment.wsBaseUrl;
private readonly apiPath = environment.apiPath;
private readonly streamHandler = inject(StreamHandlerService);
constructor(private readonly http: HttpClient) {}
createGame(): Observable<GameFull> {
return this.http.post<GameFull>(`${this.apiBase}${this.apiPath}`, {});
}
createGameVsBot(difficulty: 'easy' | 'medium' | 'hard' = 'medium'): Observable<GameFull> {
const playerColor = Math.random() > 0.5 ? 'white' : 'black';
const playerInfo: PlayerInfo = {
id: `player-${Date.now()}`,
displayName: 'You',
};
const botInfo: PlayerInfo = {
id: `bot-${difficulty}`,
displayName: `Bot (${difficulty})`,
};
const payload =
playerColor === 'white'
? { white: playerInfo, black: botInfo }
: { white: botInfo, black: playerInfo };
return this.http.post<GameFull>(`${this.apiBase}${this.apiPath}/vs-bot`, payload);
}
getGame(gameId: string): Observable<GameFull> {
return this.http.get<GameFull>(`${this.apiBase}${this.apiPath}/${gameId}`);
}
makeMove(gameId: string, uci: string): Observable<GameState> {
return this.http.post<GameState>(`${this.apiBase}${this.apiPath}/${gameId}/move/${uci}`, {});
}
getLegalMoves(gameId: string, square?: string): Observable<LegalMovesResponse> {
let params = new HttpParams();
if (square) {
params = params.set('square', square);
}
return this.http.get<LegalMovesResponse>(`${this.apiBase}${this.apiPath}/${gameId}/moves`, {
params,
});
}
importFen(fen: string): Observable<GameFull> {
return this.http.post<GameFull>(`${this.apiBase}${this.apiPath}/import/fen`, { fen });
}
importPgn(pgn: string): Observable<GameFull> {
return this.http.post<GameFull>(`${this.apiBase}${this.apiPath}/import/pgn`, { pgn });
}
resignGame(gameId: string): Observable<void> {
return this.http.post<void>(`${this.apiBase}${this.apiPath}/${gameId}/resign`, {});
}
offerDraw(gameId: string): Observable<void> {
return this.http.post<void>(`${this.apiBase}${this.apiPath}/${gameId}/draw/offer`, {});
}
analyzePosition(request: AnalysisRequest): Observable<AnalysisResponse> {
return this.http.post<AnalysisResponse>(`${this.apiBase}/api/analysis/position`, request);
}
private resolveWsBase(): string {
if (this.wsBase) {
return this.wsBase;
}
const wsProtocol = window.location.protocol === 'https:' ? 'wss' : 'ws';
return `${wsProtocol}://${window.location.host}`;
}
streamGame(gameId: string): Observable<GameStreamEvent> {
const token = localStorage.getItem('token');
let wsUrl = `${this.resolveWsBase()}${this.apiPath}/${gameId}/ws`;
if (token) {
wsUrl += `?token=${encodeURIComponent(token)}`;
}
return this.streamHandler.createGameStream(wsUrl, gameId);
}
}