mancala/frontend/src/store/GameStore.ts
2024-03-24 02:51:02 +03:00

28 lines
882 B
TypeScript

import { CommonMancalaGame, MancalaGame } from "mancala.js";
import { Game } from "../models/Game";
import { HttpService } from "../service/HttpService";
export interface GameStore {
get(id: string): Promise<Game | undefined>;
}
export class GameStoreImpl implements GameStore {
httpService: HttpService;
constructor(props: { httpService: HttpService }) {
this.httpService = props.httpService;
}
async get(id: string): Promise<Game | undefined> {
try {
const response = await this.httpService.get(`/game/${id}`);
const json = await response.json();
const game: Game = json as Game;
game.mancalaGame = MancalaGame.createFromMancalaGame(game.mancalaGame);
return game;
} catch (error) {
// todo check error
Promise.resolve(undefined);
}
}
}