Skip to content

Commit

Permalink
Adds Game Not Found functionality (#14)
Browse files Browse the repository at this point in the history
* Adds an action, reducer, and basic UI for when a game is not found

* The Fetch api is le suck. Also fixes a bug with games not loading correctly
  • Loading branch information
Brandon Brown authored Jul 6, 2020
1 parent 3fa0495 commit 1c7bafd
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 22 deletions.
28 changes: 18 additions & 10 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export const gameOver = () => ({
type: "GAME_OVER"
});

export const gameNotFound = () => ({
type: "GAME_NOT_FOUND"
});

export const requestFrames = () => ({
type: "REQUEST_FRAMES"
});
Expand Down Expand Up @@ -57,17 +61,21 @@ export const fetchFrames = () => {

dispatch(requestFrames());

await streamAllFrames(engineUrl, gameId, (game, frame) => {
// Workaround for bug where turn exluded on turn 0
frame.Turn = frame.Turn || 0;
dispatch(receiveFrame(game, frame));
try {
await streamAllFrames(engineUrl, gameId, (game, frame) => {
// Workaround for bug where turn exluded on turn 0
frame.Turn = frame.Turn || 0;
dispatch(receiveFrame(game, frame));

// Workaround to render the first frame into the board
if (frame.Turn === 0) {
const frame = getState().frames[0];
dispatch(setCurrentFrame(frame));
}
});
// Workaround to render the first frame into the board
if (frame.Turn === 0) {
const frame = getState().frames[0];
dispatch(setCurrentFrame(frame));
}
});
} catch (e) {
return dispatch(gameNotFound());
}

if (autoplay) {
const frame = getState().frames[0];
Expand Down
30 changes: 30 additions & 0 deletions src/components/game-not-found.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from "react";
import styled from "@emotion/styled";

import { colors } from "../theme";

const NotFoundWrapper = styled("div")({
display: "flex",
alignItems: "center",
justifyContent: "center",
height: "100%",
width: "100%"
});

class GameNotFound extends React.Component {
render() {
return (
<NotFoundWrapper>
<h2
style={{
color: colors.food
}}
>
Game no longer available, sorry!
</h2>
</NotFoundWrapper>
);
}
}

export default GameNotFound;
9 changes: 6 additions & 3 deletions src/components/game.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import styled from "@emotion/styled";

import BlankState from "./blank-state";
import LoadingIndicator from "./loading-indicator";
import GameNotFound from "./game-not-found";
import Board from "./board";
import Scoreboard from "./scoreboard";
import MediaControls from "./mediaControls";
Expand Down Expand Up @@ -122,13 +123,15 @@ class Game extends React.Component {
}

render() {
const { currentFrame } = this.props;

if (this.invalidArgs) {
return <BlankState />;
}

if (currentFrame) {
if (this.props.gameNotFound) {
return <GameNotFound />;
}

if (this.props.currentFrame) {
return this.renderGame();
}

Expand Down
6 changes: 2 additions & 4 deletions src/containers/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const mapStateToProps = state => {
return {
options: options,
grid: state.grid,
gameNotFound: state.gameNotFound,
paused: state.paused,
currentFrame: state.currentFrame,
frames: state.frames,
Expand All @@ -37,7 +38,4 @@ const mapDispatchToProps = dispatch => ({
highlightSnake: snakeId => dispatch(highlightSnake(snakeId))
});

export default connect(
mapStateToProps,
mapDispatchToProps
)(Game);
export default connect(mapStateToProps, mapDispatchToProps)(Game);
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const initialState = {
grid: [],
frames: [],
paused: true,
gameNotFound: false,
highlightedSnake: null,
theme: themes.light
};
Expand Down
7 changes: 5 additions & 2 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ export default (state = {}, action) => {
action.gameOptions.autoplay =
action.gameOptions.autoplay && action.gameOptions.autoplay === "true";
action.gameOptions.turn = parseInt(action.gameOptions.turn) || 0;
action.gameOptions.loop = action.gameOptions.loop && action.gameOptions.loop === "true";
action.gameOptions.loop =
action.gameOptions.loop && action.gameOptions.loop === "true";
return { ...state, gameOptions: action.gameOptions };
case "GAME_NOT_FOUND":
return { ...state, gameNotFound: true };
case "PAUSE_GAME":
return { ...state, paused: true };
case "GAME_OVER":
windowPostMessage({
action: action.type,
action: action.type
});
return { ...state, paused: true };
case "RESUME_GAME":
Expand Down
13 changes: 10 additions & 3 deletions src/utils/engine-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ const DEFAULT_SNAKE_HEAD = "default";
const DEFAULT_SNAKE_TAIL = "default";

async function get(url, query) {
const fetchResult = await fetch(url + makeQueryString(query));
return fetchResult.json();
const response = await fetch(url + makeQueryString(query));
if (response.status === 200) {
return Promise.resolve(response.json());
} else {
return Promise.resolve(response.json()).then(responseJson => {
console.error(responseJson.error);
return Promise.reject(responseJson.error);
});
}
}

export function delay(millis) {
Expand All @@ -29,7 +36,7 @@ export function getReadableCauseOfDeath(death) {
case "wall-collision":
return "Moved out of bounds";
case "team-eliminated":
return "Team was eliminated"
return "Team was eliminated";
default:
return death.cause;
}
Expand Down

0 comments on commit 1c7bafd

Please sign in to comment.