add GameStore

This commit is contained in:
Halit Aksoy 2022-07-30 16:32:13 +03:00
parent 3947f28195
commit 644f839933
3 changed files with 50 additions and 19 deletions

View File

@ -34,9 +34,9 @@ const MancalaApp: FunctionComponent = () => {
const onConnectionError = (event: Event) => { const onConnectionError = (event: Event) => {
setConnetionState("error"); setConnetionState("error");
}; };
const connectToServer = (connectionState: ConnectionState) => { const connectToServer = async (connectionState: ConnectionState) => {
setConnetionState(connectionState); setConnetionState(connectionState);
context.userKeyStore.getUserKey((userKey: string) => { const userKey = await context.userKeyStore.getUserKey();
setUserKey(userKey); setUserKey(userKey);
const rtmtws = context.rtmt as RTMTWS; const rtmtws = context.rtmt as RTMTWS;
if (rtmtws) { if (rtmtws) {
@ -49,7 +49,6 @@ const MancalaApp: FunctionComponent = () => {
} else { } else {
console.error("context.rtmt is not RTMTWS"); console.error("context.rtmt is not RTMTWS");
} }
});
}; };
React.useEffect(() => { React.useEffect(() => {
connectToServer("connecting"); connectToServer("connecting");
@ -85,7 +84,7 @@ const MancalaApp: FunctionComponent = () => {
<Route index element={<Home context={context} theme={theme} userKey={userKey} />} /> <Route index element={<Home context={context} theme={theme} userKey={userKey} />} />
<Route path="/" > <Route path="/" >
<Route path="game" > <Route path="game" >
<Route path=":gameId" element={<GamePage context={context} theme={theme} userKey={userKey} />} ></Route> <Route path=":gameId" element={<GamePage context={context} theme={theme} userKey={userKey} connectionState={connectionState} />} ></Route>
</Route> </Route>
<Route path="loby" element={<LobyPage context={context} theme={theme} userKey={userKey} />}> <Route path="loby" element={<LobyPage context={context} theme={theme} userKey={userKey} />}>
</Route> </Route>

View File

@ -1,7 +1,10 @@
import { server } from "../const/config";
import { Texts, TrTr } from "../const/texts"; import { Texts, TrTr } from "../const/texts";
import { RTMT } from "../rtmt/rtmt"; import { RTMT } from "../rtmt/rtmt";
import { RTMTWS } from "../rtmt/rtmt_websocket"; 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"; import ThemeManager from "../theme/ThemeManager";
export type Context = { export type Context = {
@ -9,11 +12,14 @@ export type Context = {
userKeyStore: UserKeyStore; userKeyStore: UserKeyStore;
texts: Texts; texts: Texts;
themeManager: ThemeManager; themeManager: ThemeManager;
gameStore: GameStore;
}; };
export const initContext = (): Context => { export const initContext = (): Context => {
const rtmt = new RTMTWS(); 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 texts = TrTr;
const themeManager = new ThemeManager(); const themeManager = new ThemeManager();
return { return {
@ -21,5 +27,6 @@ export const initContext = () : Context => {
userKeyStore: userKeyStore, userKeyStore: userKeyStore,
texts: texts, texts: texts,
themeManager, themeManager,
gameStore
}; };
}; };

25
src/store/GameStore.ts Normal file
View File

@ -0,0 +1,25 @@
import { CommonMancalaGame, MancalaGame } from "mancala.js";
import { HttpService } from "../service/HttpService";
export interface GameStore {
get(id: string): Promise<MancalaGame | undefined>;
}
export class GameStoreImpl implements GameStore {
httpService: HttpService;
constructor(props: { httpService: HttpService }) {
this.httpService = props.httpService;
}
async get(id: string): Promise<MancalaGame | undefined> {
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);
}
}
}