import { inject, Injectable, signal } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { handleRequest } from '@/core/helpers/http'; import { HttpResponse } from '@/core/models/response.interface'; import { AnswerResponse, GameData, GameResult } from './game.models'; @Injectable({ providedIn: 'root', }) export class GameService { private http = inject(HttpClient); gameId = signal(null); async getGameResults() { const { data } = await handleRequest( this.http.get>('games/my_results') ); return data; } async startNewGame(): Promise { const { data } = await handleRequest( this.http.get>('games/new_question') ); this.gameId.set(data.game_id); return data; } async getNextQuestion(gameId: string): Promise { const { data } = await handleRequest( this.http.get>(`games/new_question/${gameId}`) ); return data; } async submitAnswer(gameId: string, answerId: number): Promise { const { data } = await handleRequest( this.http.post>(`games/answer/${gameId}`, { answer_id: answerId, }) ); return data; } async submitTimeout(gameId: string, timeoutAnswerId: number): Promise { const { data } = await handleRequest( this.http.post>(`games/answer/${gameId}`, { answer_id: timeoutAnswerId, }) ); return data; } // Utils resetGame() { this.gameId.set(null); } } export const injectGameService = () => inject(GameService);