From 644f839933bf3431f0b0cbd5361f45ae71d090fe Mon Sep 17 00:00:00 2001 From: Halit Aksoy Date: Sat, 30 Jul 2022 16:32:13 +0300 Subject: [PATCH] add GameStore --- src/MancalaApp.tsx | 31 +++++++++++++++---------------- src/context/context.tsx | 13 ++++++++++--- src/store/GameStore.ts | 25 +++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 src/store/GameStore.ts diff --git a/src/MancalaApp.tsx b/src/MancalaApp.tsx index 3d049c8..2329499 100644 --- a/src/MancalaApp.tsx +++ b/src/MancalaApp.tsx @@ -34,22 +34,21 @@ const MancalaApp: FunctionComponent = () => { const onConnectionError = (event: Event) => { setConnetionState("error"); }; - const connectToServer = (connectionState: ConnectionState) => { + const connectToServer = async (connectionState: ConnectionState) => { setConnetionState(connectionState); - context.userKeyStore.getUserKey((userKey: string) => { - setUserKey(userKey); - const rtmtws = context.rtmt as RTMTWS; - if (rtmtws) { - rtmtws.initWebSocket( - userKey, - onConnectionDone, - onConnectionLost, - onConnectionError - ); - } else { - console.error("context.rtmt is not RTMTWS"); - } - }); + const userKey = await context.userKeyStore.getUserKey(); + setUserKey(userKey); + const rtmtws = context.rtmt as RTMTWS; + if (rtmtws) { + rtmtws.initWebSocket( + userKey, + onConnectionDone, + onConnectionLost, + onConnectionError + ); + } else { + console.error("context.rtmt is not RTMTWS"); + } }; React.useEffect(() => { connectToServer("connecting"); @@ -85,7 +84,7 @@ const MancalaApp: FunctionComponent = () => { } /> - } > + } > }> diff --git a/src/context/context.tsx b/src/context/context.tsx index 4825b3c..ee35337 100644 --- a/src/context/context.tsx +++ b/src/context/context.tsx @@ -1,7 +1,10 @@ +import { server } from "../const/config"; import { Texts, TrTr } from "../const/texts"; import { RTMT } from "../rtmt/rtmt"; import { RTMTWS } from "../rtmt/rtmt_websocket"; -import { UserKeyStore, UserKeyStoreImpl } from "../store/key_store"; +import { HttpServiceImpl } from "../service/HttpService"; +import { GameStore, GameStoreImpl } from "../store/GameStore"; +import { UserKeyStore, UserKeyStoreImpl } from "../store/KeyStore"; import ThemeManager from "../theme/ThemeManager"; export type Context = { @@ -9,11 +12,14 @@ export type Context = { userKeyStore: UserKeyStore; texts: Texts; themeManager: ThemeManager; + gameStore: GameStore; }; -export const initContext = () : Context => { +export const initContext = (): Context => { const rtmt = new RTMTWS(); - const userKeyStore = new UserKeyStoreImpl(); + const httpService = new HttpServiceImpl(server.serverAdress); + const userKeyStore = new UserKeyStoreImpl({ httpService }); + const gameStore = new GameStoreImpl({ httpService }); const texts = TrTr; const themeManager = new ThemeManager(); return { @@ -21,5 +27,6 @@ export const initContext = () : Context => { userKeyStore: userKeyStore, texts: texts, themeManager, + gameStore }; }; diff --git a/src/store/GameStore.ts b/src/store/GameStore.ts new file mode 100644 index 0000000..66f4c6c --- /dev/null +++ b/src/store/GameStore.ts @@ -0,0 +1,25 @@ +import { CommonMancalaGame, MancalaGame } from "mancala.js"; +import { HttpService } from "../service/HttpService"; + +export interface GameStore { + get(id: string): Promise; +} + +export class GameStoreImpl implements GameStore { + httpService: HttpService; + constructor(props: { httpService: HttpService }) { + this.httpService = props.httpService; + } + + async get(id: string): Promise { + try { + const response = await this.httpService.get(`/game/${id}`); + const json = await response.json(); + return MancalaGame.createFromMancalaGame(json as CommonMancalaGame); + } catch (error) { + // todo check error + Promise.resolve(undefined); + } + } + +} \ No newline at end of file