Skip to content

Commit

Permalink
Store game locator instantly to not forget it
Browse files Browse the repository at this point in the history
  • Loading branch information
lindlof committed Dec 10, 2020
1 parent 3188c5e commit 2af8e09
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
15 changes: 4 additions & 11 deletions web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,13 @@ import Wallet from './wallet/Wallet';
import useAccount from './wallet/useAccount';
import Banner from './Banner';
import Grid from '@material-ui/core/Grid';
import CircularProgress from '@material-ui/core/CircularProgress';
import GameTicker from './components/GameTicker';

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,
Game.defaults,
);
const [game, setGame] = useLocalStorage<Game.Game | undefined>('game', undefined, Game.defaults);
const account = useAccount(client, game);
const lowBalance = account && account.balance < 11;
const { enqueueSnackbar } = useSnackbar();
Expand Down Expand Up @@ -59,7 +54,6 @@ export const App: React.FC = () => {
/>
</GameTicker>
)}
{game === null && <CircularProgress />}
{game === undefined && client && (
<Grid container direction="column" justify="center" alignItems="center" spacing={1}>
<Grid item xs={12}>
Expand Down Expand Up @@ -110,17 +104,17 @@ const playGame = async (
enqueueSnackbar: Function,
locator?: string,
) => {
setGame(null, false);

const game = Game.create(contract, privateGame, locator);
setGame(game);
const method = privateGame ? 'private_game' : 'join_game';
try {
await client.execute(contract, { [method]: { locator: game.locator } }, undefined, [
const res = await client.execute(contract, { [method]: { locator: game.locator } }, undefined, [
{
amount: '10000000',
denom: 'uscrt',
},
]);
console.log('txn', res.transactionHash);
} catch (e) {
if (e.message !== 'ciphertext not set') {
setGame(undefined);
Expand All @@ -129,7 +123,6 @@ const playGame = async (
return;
}
}
setGame(game);
};

const routeUrl = (
Expand Down
5 changes: 5 additions & 0 deletions web/src/GamePlaying.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ const GamePlaying = (props: Props) => {
}
};

if (game.stage === Game.Stage.Creating) {
return <CircularProgress />;
// TODO: Leaving game is not getting created
}

let displayContent: DisplayContent = DisplayContent.PickHandsign;
if (pickedRound === game.round) {
displayContent = DisplayContent.Loading;
Expand Down
12 changes: 8 additions & 4 deletions web/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ interface TickUpdate {
}

enum Stage {
Creating = 'CREATING',
Lobby = 'LOBBY',
GameOn = 'GAME_ON',
Over = 'OVER',
Expand All @@ -56,7 +57,7 @@ const defaults = Object.freeze({
privateGame: false,
locator: '',
playerNumber: undefined,
stage: Stage.Lobby,
stage: Stage.Creating,
round: 1,
won: false,
wins: 0,
Expand All @@ -72,7 +73,7 @@ const defaults = Object.freeze({
const create = (contract: string, privateGame: boolean, joinLocator?: string): Game => {
let locator = joinLocator;
let playerNumber: number | undefined;
let stage = Stage.Lobby;
let stage = Stage.Creating;
if (!locator) {
const randomLocator = new Uint8Array(32);
crypto.getRandomValues(randomLocator);
Expand Down Expand Up @@ -114,11 +115,14 @@ const tick = async (
winDeadlineSeconds: game.winDeadlineSeconds,
lossDeadlineSeconds: game.lossDeadlineSeconds,
};
if (game.stage === Stage.Lobby) {
if (game.stage === Stage.Creating || game.stage === Stage.Lobby) {
const lobby = await client.queryContractSmart(game.contract, {
game_lobby: { locator: game.locator },
});
if (!lobby.game_started) return;
if (!lobby.game_started) {
if (game.stage === Stage.Creating) return { ...game, stage: Stage.Lobby };
return;
}

let playerNumber = update.playerNumber;
if (playerNumber === undefined) {
Expand Down
2 changes: 1 addition & 1 deletion web/src/wallet/Wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const Wallet = (props: Props) => {
Get funds
</Button>
)}
<Typography variant="body1" color="textPrimary" component="p">
<Typography variant="body1" color="textPrimary" component="div">
{!account?.loading && (
<CircularProgress color="secondary" size="1rem" className={classes.progress} />
)}
Expand Down

0 comments on commit 2af8e09

Please sign in to comment.