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