feat: new spec
This commit is contained in:
@@ -15,11 +15,12 @@ import {
|
||||
export class GameApiService {
|
||||
private readonly apiBase = environment.apiBaseUrl;
|
||||
private readonly wsBase = environment.wsBaseUrl;
|
||||
private readonly apiPath = environment.apiPath;
|
||||
|
||||
constructor(private readonly http: HttpClient) {}
|
||||
|
||||
createGame(): Observable<GameFull> {
|
||||
return this.http.post<GameFull>(`${this.apiBase}/api/board/game`, {});
|
||||
return this.http.post<GameFull>(`${this.apiBase}${this.apiPath}`, {});
|
||||
}
|
||||
|
||||
createGameVsBot(difficulty: 'easy' | 'medium' | 'hard' = 'medium'): Observable<GameFull> {
|
||||
@@ -38,15 +39,15 @@ export class GameApiService {
|
||||
? { white: playerInfo, black: botInfo }
|
||||
: { white: botInfo, black: playerInfo };
|
||||
|
||||
return this.http.post<GameFull>(`${this.apiBase}/api/board/game`, payload);
|
||||
return this.http.post<GameFull>(`${this.apiBase}${this.apiPath}`, payload);
|
||||
}
|
||||
|
||||
getGame(gameId: string): Observable<GameFull> {
|
||||
return this.http.get<GameFull>(`${this.apiBase}/api/board/game/${gameId}`);
|
||||
return this.http.get<GameFull>(`${this.apiBase}${this.apiPath}/${gameId}`);
|
||||
}
|
||||
|
||||
makeMove(gameId: string, uci: string): Observable<GameState> {
|
||||
return this.http.post<GameState>(`${this.apiBase}/api/board/game/${gameId}/move/${uci}`, {});
|
||||
return this.http.post<GameState>(`${this.apiBase}${this.apiPath}/${gameId}/move/${uci}`, {});
|
||||
}
|
||||
|
||||
getLegalMoves(gameId: string, square?: string): Observable<LegalMovesResponse> {
|
||||
@@ -54,21 +55,30 @@ export class GameApiService {
|
||||
if (square) {
|
||||
params = params.set('square', square);
|
||||
}
|
||||
return this.http.get<LegalMovesResponse>(`${this.apiBase}/api/board/game/${gameId}/moves`, { params });
|
||||
return this.http.get<LegalMovesResponse>(`${this.apiBase}${this.apiPath}/${gameId}/moves`, { params });
|
||||
}
|
||||
|
||||
importFen(fen: string): Observable<GameFull> {
|
||||
return this.http.post<GameFull>(`${this.apiBase}/api/board/game/import/fen`, { fen });
|
||||
return this.http.post<GameFull>(`${this.apiBase}${this.apiPath}/import/fen`, { fen });
|
||||
}
|
||||
|
||||
importPgn(pgn: string): Observable<GameFull> {
|
||||
return this.http.post<GameFull>(`${this.apiBase}/api/board/game/import/pgn`, { pgn });
|
||||
return this.http.post<GameFull>(`${this.apiBase}${this.apiPath}/import/pgn`, { pgn });
|
||||
}
|
||||
|
||||
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> {
|
||||
return new Observable<GameStreamEvent>((observer) => {
|
||||
const wsUrl = `${this.wsBase}/api/board/game/${gameId}/stream`;
|
||||
const streamUrl = `${this.apiBase}/api/board/game/${gameId}/stream`;
|
||||
const wsUrl = `${this.resolveWsBase()}${this.apiPath}/${gameId}/stream`;
|
||||
const streamUrl = `${this.apiBase}${this.apiPath}/${gameId}/stream`;
|
||||
const ws = new WebSocket(wsUrl);
|
||||
const abortController = new AbortController();
|
||||
let connected = false;
|
||||
@@ -101,6 +111,7 @@ export class GameApiService {
|
||||
}
|
||||
|
||||
fallbackActive = true;
|
||||
console.log(`[GameApiService] NDJSON fallback started for ${gameId}, URL:`, streamUrl);
|
||||
|
||||
try {
|
||||
const response = await fetch(streamUrl, {
|
||||
@@ -109,11 +120,13 @@ export class GameApiService {
|
||||
});
|
||||
|
||||
if (!response.ok || !response.body) {
|
||||
console.error(`[GameApiService] NDJSON fetch failed: HTTP ${response.status}`);
|
||||
emitErrorEvent(`Unable to open stream: HTTP ${response.status}`);
|
||||
observer.complete();
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`[GameApiService] NDJSON stream connected for ${gameId}`);
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let buffer = '';
|
||||
@@ -157,14 +170,17 @@ export class GameApiService {
|
||||
}
|
||||
};
|
||||
|
||||
ws.onerror = () => {
|
||||
ws.onerror = (error) => {
|
||||
console.warn(`[GameApiService] WebSocket error for ${gameId}, attempting NDJSON fallback:`, error);
|
||||
if (!connected) {
|
||||
void startNdjsonFallback();
|
||||
}
|
||||
};
|
||||
|
||||
ws.onclose = () => {
|
||||
console.warn(`[GameApiService] WebSocket closed for ${gameId}, connected=${connected}`);
|
||||
if (!connected) {
|
||||
console.log(`[GameApiService] Starting NDJSON fallback for ${gameId}`);
|
||||
void startNdjsonFallback();
|
||||
} else {
|
||||
observer.complete();
|
||||
|
||||
Reference in New Issue
Block a user