Skip to content

Commit

Permalink
Fallback to defaults when loading game
Browse files Browse the repository at this point in the history
  • Loading branch information
lindlof committed Dec 5, 2020
1 parent cf45986 commit fba1fec
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
6 changes: 5 additions & 1 deletion web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ const config = envConfig();

export const App: React.FC = () => {
const [client, setClient] = useState<SecretJS.SigningCosmWasmClient | undefined>();
const [game, setGame] = useLocalStorage<Game.Game | null | undefined>('game', undefined);
const [game, setGame] = useLocalStorage<Game.Game | null | undefined>(
'game',
undefined,
Game.defaults,
);
const { enqueueSnackbar } = useSnackbar();
routeUrl(client, setGame, enqueueSnackbar);

Expand Down
31 changes: 20 additions & 11 deletions web/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ interface Round {
readonly handsign: Msg.Handsign | undefined;
}

const defaults = Object.freeze({
contract: '',
privateGame: false,
locator: '',
playerNumber: 1,
stage: Stage.Lobby,
round: 1,
won: false,
wins: 0,
losses: 0,
played: false,
opponentPlayed: false,
lastHandsign: undefined,
rounds: new Array<Round | undefined>(),
winDeadlineSeconds: undefined,
lossDeadlineSeconds: undefined,
});

const create = (contract: string, privateGame: boolean, joinLocator?: string): Game => {
let locator = joinLocator;
let playerNumber: number | undefined;
Expand All @@ -70,21 +88,12 @@ const create = (contract: string, privateGame: boolean, joinLocator?: string): G
}
}
return {
...defaults,
contract,
privateGame,
locator,
playerNumber,
stage,
round: 1,
won: false,
wins: 0,
losses: 0,
played: false,
opponentPlayed: false,
lastHandsign: undefined,
rounds: new Array<Round | undefined>(),
winDeadlineSeconds: undefined,
lossDeadlineSeconds: undefined,
};
};

Expand Down Expand Up @@ -197,4 +206,4 @@ const claimInactivity = async (client: SecretJS.SigningCosmWasmClient, game: Gam
};

export type { Game, Round };
export { Stage, create, tick, playHandsign, claimInactivity, Result };
export { Stage, create, tick, playHandsign, claimInactivity, Result, defaults };
11 changes: 8 additions & 3 deletions web/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { useState } from 'react';

export const useLocalStorage = <T>(key: string, initialValue: T) => {
export const useLocalStorage = <T>(key: string, initialValue: T, defaults?: T) => {
const [storedValue, setStoredValue] = useState(() => {
try {
const item = window.localStorage.getItem(key);
return item ? JSON.parse(item) : initialValue;
const itemStr = window.localStorage.getItem(key);
if (!itemStr) return initialValue;
const item = JSON.parse(itemStr);
if (typeof item === 'object') {
return { ...defaults, ...item };
}
return item;
} catch {
return initialValue;
}
Expand Down

0 comments on commit fba1fec

Please sign in to comment.