App.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 './App.css';
  7. const GameInfo = ({game}) => {
  8. const {
  9. gameId,
  10. creator,
  11. timer,
  12. coords,
  13. players
  14. } = game;
  15. return (
  16. <div className="GameInfo">
  17. <p>Game ID: {gameId}</p>
  18. <p>Creator: {creator}</p>
  19. <p>Time Limit: {timer}</p>
  20. <p>Coordinates: {JSON.stringify(coords)}</p>
  21. <p>Players: {JSON.stringify(players)}</p>
  22. </div>
  23. );
  24. }
  25. class GuessComp extends React.Component {
  26. constructor(props) {
  27. super(props);
  28. this.state = {
  29. loading: true,
  30. guesses: null,
  31. }
  32. }
  33. async componentDidMount() {
  34. const { gameId, name } = this.props;
  35. const guesses = await getGuesses(gameId, name);
  36. this.setState({loading: false, guesses});
  37. }
  38. render() {
  39. const { loading, guesses } = this.state;
  40. if (loading) {
  41. return <p>Loading...</p>
  42. }
  43. const { name } = this.props;
  44. return <p>Guesses for {name}: {JSON.stringify(guesses)}</p>
  45. }
  46. }
  47. class GameMakerComp extends React.Component {
  48. constructor(props) {
  49. super(props);
  50. this.state = {
  51. loading: false,
  52. game: null,
  53. }
  54. }
  55. async handleGameJoin() {
  56. this.setState({loading: true});
  57. const { gameId } = this.state.game
  58. await joinGame(gameId, "testing2");
  59. const game = await gameInfo(gameId);
  60. this.setState({loading: false, game});
  61. }
  62. async handleSendGuess() {
  63. this.setState({loading: true});
  64. const { gameId, coords } = this.state.game
  65. const { currentRound } = await getGuesses(gameId, "testing");
  66. const { lat, lng } = coords[currentRound];
  67. await sendGuess(gameId, "testing", currentRound, lat, lng);
  68. const game = await gameInfo(gameId);
  69. this.setState({loading: false, game});
  70. }
  71. render() {
  72. const { loading, game } = this.state;
  73. if (loading) {
  74. return <div><p>Loading...</p></div>
  75. }
  76. if (game) {
  77. return (
  78. <div>
  79. <GameInfo game={game}/>
  80. <p>Your Guesses:</p>
  81. <GuessComp gameId={game.gameId} name="testing"/>
  82. <button onClick={() => this.handleGameJoin()}>Add second user!</button>
  83. <button onClick={() => this.handleSendGuess()}>Send perfect guess!</button>
  84. </div>
  85. );
  86. }
  87. // return <div><button onClick={() => this.handleGameCreate()}>Create Game!</button></div>
  88. return <div><GamePanel /></div>
  89. }
  90. }
  91. class Game extends React.Component {
  92. constructor(props) {
  93. super(props);
  94. this.state = {
  95. playerName: "testing",
  96. loading: false,
  97. gameId: null,
  98. currentRound: null,
  99. targetPoint: null,
  100. selectedPoint: null,
  101. playerScores: null,
  102. }
  103. }
  104. async handleGameCreate() {
  105. this.setState({loading: true});
  106. const { playerName } = this.state;
  107. const gameId = await createGame(playerName, 300);
  108. this.setState({ gameId });
  109. await this.updateRoundState();
  110. }
  111. async updateRoundState() {
  112. const { gameId, playerName } = this.state;
  113. const { currentRound } = await getGuesses(gameId, playerName);
  114. const { coords, players } = await gameInfo(gameId);
  115. if (currentRound) {
  116. const targetPoint = coords[currentRound];
  117. this.setState({
  118. loading: false,
  119. currentRound,
  120. targetPoint
  121. });
  122. } else {
  123. this.setState({
  124. loading: false,
  125. gameId: null,
  126. currentRound: null,
  127. targetPoint: null,
  128. playerScores: players,
  129. });
  130. }
  131. }
  132. async handleSubmitGuess() {
  133. const { gameId, playerName, currentRound, selectedPoint } = this.state;
  134. this.setState({ loading: true, selectedPoint: null });
  135. const score = await sendGuess(gameId, playerName, currentRound, selectedPoint);
  136. console.log(`Score: ${score}`);
  137. await this.updateRoundState();
  138. }
  139. render() {
  140. const { loading, selectedPoint, targetPoint, playerScores } = this.state;
  141. if (loading) {
  142. return <p>Loading...</p>
  143. }
  144. if (!targetPoint) {
  145. return (<div>
  146. {(playerScores ? [<PlayerScores scores={playerScores}/>, <hr/>] : null)}
  147. <p>No game in progress!</p>
  148. <button onClick={() => this.handleGameCreate()}>Create New Game</button>
  149. </div>);
  150. }
  151. return <GamePanel
  152. onSelectPoint={latLng => this.setState({selectedPoint: latLng})}
  153. onSubmitGuess={() => this.handleSubmitGuess()}
  154. streetViewPoint={targetPoint}
  155. selectedPoint={selectedPoint}
  156. />
  157. }
  158. }
  159. const App = () => {
  160. return (
  161. <div className="App" style={{
  162. display: "flex",
  163. flexDirection: "column",
  164. justifyContent: "space-between",
  165. height: "100vh",
  166. }}>
  167. <div>
  168. <p>TerrAssumptions!</p>
  169. <hr/>
  170. </div>
  171. <Game style={{flex: 1}}/>
  172. <div>
  173. <hr/>
  174. <ApiInfo/>
  175. </div>
  176. </div>
  177. );
  178. }
  179. export default App;