123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- import React from 'react';
- import { createGame, gameInfo, joinGame, getGuesses, sendGuess } from "./services/ggsh.service";
- import GamePanel from "./components/game-panel.component";
- import ApiInfo from "./components/api-info.component";
- import PlayerScores from "./components/player-scores.component";
- import RoundSummary from './components/round-summary.component';
- import './App.css';
- import PreGame from './components/pre-game.component';
- const LOADING = "LOADING";
- const PRE_GAME = "PREGAME";
- const IN_ROUND = "INROUND";
- const POST_ROUND = "POSTROUND";
- const POST_GAME = "POSTGAME";
- class Game extends React.Component {
- constructor(props) {
- super(props);
- this.state = {
- gameState: PRE_GAME,
- playerName: "testing", // TODO mechanism for setting this
- loading: false,
- gameId: null, // TODO mechanism for joining game
- currentRound: null,
- targetPoint: null,
- selectedPoint: null,
- lastScore: null,
- totalScore: null,
- players: null,
- }
- }
- async handleGameCreate() {
- this.setState({ gameState: LOADING });
- const { playerName } = this.state;
- const gameId = await createGame(playerName, 300);
- this.setState({ gameId });
- await this.updateRoundState();
- }
- async updateRoundState() {
- this.setState({ gameState: LOADING })
- const { gameId, playerName } = this.state;
- const { currentRound } = await getGuesses(gameId, playerName);
- const { coords, players } = await gameInfo(gameId);
- if (currentRound) {
- const targetPoint = coords[currentRound];
- this.setState({
- gameState: IN_ROUND,
- currentRound,
- targetPoint,
- selectedPoint: null,
- players,
- });
- } else {
- this.setState({ gameState: POST_GAME, players });
- }
- }
- async handleSubmitGuess() {
- const {
- gameId,
- playerName,
- currentRound,
- selectedPoint
- } = this.state;
- this.setState({ gameState: LOADING });
- const { score, totalScore } = await sendGuess(gameId, playerName, currentRound, selectedPoint);
- this.setState({
- gameState: POST_ROUND,
- lastScore: score,
- totalScore
- });
- }
- render() {
- const { gameState } = this.state;
- switch (gameState) {
- case LOADING:
- return <p>Loading...</p>
- case PRE_GAME:
- return <PreGame
- onGameCreate={() => this.handleGameCreate()}
- />
- case IN_ROUND:
- const { targetPoint, selectedPoint } = this.state;
- return <GamePanel
- onSelectPoint={latLng => this.setState({selectedPoint: latLng})}
- onSubmitGuess={() => this.handleSubmitGuess()}
- streetViewPoint={targetPoint}
- selectedPoint={selectedPoint}
- />
- case POST_ROUND:
- const { currentRound, lastScore, totalScore } = this.state;
- return <RoundSummary
- roundNum={currentRound}
- score={lastScore}
- totalScore={totalScore}
- onAdvanceState={() => this.updateRoundState()}
- buttonText={currentRound === "5" ? "View Summary" : "Next Round"}
- />
- case POST_GAME:
- const { players } = this.state;
- return <PlayerScores
- players={players}
- onReturnToStart={() => this.setState({ gameState: PRE_GAME })}
- />
- default:
- return <p>Application state is inconsistent, please refresh and rejoin your previous game.</p>
- }
- }
- }
- const App = () => {
- return (
- <div className="App" style={{
- display: "flex",
- flexDirection: "column",
- justifyContent: "space-between",
- height: "100vh",
- }}>
- <div>
- <p>TerrAssumptions!</p>
- <hr/>
- </div>
- <Game style={{flex: 1}}/>
- <div>
- <hr/>
- <ApiInfo/>
- </div>
- </div>
- );
- }
- export default App;
|