App.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import React from 'react';
  2. import { createGame, gameInfo, joinGame, getGuesses, sendGuess } from "./services/ggsh.service";
  3. import GamePanel from "./components/game-panel.component";
  4. import ApiInfo from "./components/api-info.component";
  5. import PlayerScores from "./components/player-scores.component";
  6. import RoundSummary from './components/round-summary.component';
  7. import './App.css';
  8. import PreGame from './components/pre-game.component';
  9. const LOADING = "LOADING";
  10. const PRE_GAME = "PREGAME";
  11. const IN_ROUND = "INROUND";
  12. const POST_ROUND = "POSTROUND";
  13. const POST_GAME = "POSTGAME";
  14. class Game extends React.Component {
  15. constructor(props) {
  16. super(props);
  17. this.state = {
  18. gameState: PRE_GAME,
  19. playerName: "testing", // TODO mechanism for setting this
  20. loading: false,
  21. gameId: null, // TODO mechanism for joining game
  22. currentRound: null,
  23. targetPoint: null,
  24. selectedPoint: null,
  25. lastScore: null,
  26. totalScore: null,
  27. players: null,
  28. }
  29. }
  30. async handleGameCreate() {
  31. this.setState({ gameState: LOADING });
  32. const { playerName } = this.state;
  33. const gameId = await createGame(playerName, 300);
  34. this.setState({ gameId });
  35. await this.updateRoundState();
  36. }
  37. async updateRoundState() {
  38. this.setState({ gameState: LOADING })
  39. const { gameId, playerName } = this.state;
  40. const { currentRound } = await getGuesses(gameId, playerName);
  41. const { coords, players } = await gameInfo(gameId);
  42. if (currentRound) {
  43. const targetPoint = coords[currentRound];
  44. this.setState({
  45. gameState: IN_ROUND,
  46. currentRound,
  47. targetPoint,
  48. selectedPoint: null,
  49. players,
  50. });
  51. } else {
  52. this.setState({ gameState: POST_GAME, players });
  53. }
  54. }
  55. async handleSubmitGuess() {
  56. const {
  57. gameId,
  58. playerName,
  59. currentRound,
  60. selectedPoint
  61. } = this.state;
  62. this.setState({ gameState: LOADING });
  63. const { score, totalScore } = await sendGuess(gameId, playerName, currentRound, selectedPoint);
  64. this.setState({
  65. gameState: POST_ROUND,
  66. lastScore: score,
  67. totalScore
  68. });
  69. }
  70. render() {
  71. const { gameState } = this.state;
  72. switch (gameState) {
  73. case LOADING:
  74. return <p>Loading...</p>
  75. case PRE_GAME:
  76. return <PreGame
  77. onGameCreate={() => this.handleGameCreate()}
  78. />
  79. case IN_ROUND:
  80. const { targetPoint, selectedPoint } = this.state;
  81. return <GamePanel
  82. onSelectPoint={latLng => this.setState({selectedPoint: latLng})}
  83. onSubmitGuess={() => this.handleSubmitGuess()}
  84. streetViewPoint={targetPoint}
  85. selectedPoint={selectedPoint}
  86. />
  87. case POST_ROUND:
  88. const { currentRound, lastScore, totalScore } = this.state;
  89. return <RoundSummary
  90. roundNum={currentRound}
  91. score={lastScore}
  92. totalScore={totalScore}
  93. onAdvanceState={() => this.updateRoundState()}
  94. buttonText={currentRound === "5" ? "View Summary" : "Next Round"}
  95. />
  96. case POST_GAME:
  97. const { players } = this.state;
  98. return <PlayerScores
  99. players={players}
  100. onReturnToStart={() => this.setState({ gameState: PRE_GAME })}
  101. />
  102. default:
  103. return <p>Application state is inconsistent, please refresh and rejoin your previous game.</p>
  104. }
  105. }
  106. }
  107. const App = () => {
  108. return (
  109. <div className="App" style={{
  110. display: "flex",
  111. flexDirection: "column",
  112. justifyContent: "space-between",
  113. height: "100vh",
  114. }}>
  115. <div>
  116. <p>TerrAssumptions!</p>
  117. <hr/>
  118. </div>
  119. <Game style={{flex: 1}}/>
  120. <div>
  121. <hr/>
  122. <ApiInfo/>
  123. </div>
  124. </div>
  125. );
  126. }
  127. export default App;