mancala/frontend/src/store/GameStore.ts

28 lines
882 B
TypeScript
Raw Normal View History

2022-07-30 16:32:13 +03:00
import { CommonMancalaGame, MancalaGame } from "mancala.js";
2022-08-01 22:24:48 +03:00
import { Game } from "../models/Game";
2022-07-30 16:32:13 +03:00
import { HttpService } from "../service/HttpService";
export interface GameStore {
2022-08-01 22:24:48 +03:00
get(id: string): Promise<Game | undefined>;
2022-07-30 16:32:13 +03:00
}
export class GameStoreImpl implements GameStore {
httpService: HttpService;
constructor(props: { httpService: HttpService }) {
this.httpService = props.httpService;
}
2022-08-01 22:24:48 +03:00
async get(id: string): Promise<Game | undefined> {
2022-07-30 16:32:13 +03:00
try {
const response = await this.httpService.get(`/game/${id}`);
const json = await response.json();
2022-08-01 22:24:48 +03:00
const game: Game = json as Game;
game.mancalaGame = MancalaGame.createFromMancalaGame(game.mancalaGame);
return game;
2022-07-30 16:32:13 +03:00
} catch (error) {
// todo check error
Promise.resolve(undefined);
}
}
}