GamePanel.jsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import React, { useState, useEffect } from 'react';
  2. import { gameInfo, getGuesses, sendGuess } from "../../../domain/GGSHService";
  3. import Loading from '../../util/Loading';
  4. import GuessPane from "./GuessPane";
  5. import PositionedStreetView from "./PositionedStreetView";
  6. const GamePanelContainer = ({ gameId, playerName, onRoundEnd, onGameEnd }) => {
  7. const [{ currentRound, targetPoint, roundSeconds }, setRoundInfo] = useState({currentRound: null, targetPoint: null, roundSeconds: null});
  8. const [submitDisabled, setSubmitDisabled] = useState(false);
  9. const [selectedPoint, setSelectedPoint] = useState(null);
  10. useEffect(() => {
  11. const setup = async () => {
  12. const { currentRound } = await getGuesses(gameId, playerName);
  13. if (currentRound) {
  14. const { coords, timer } = await gameInfo(gameId);
  15. const targetPoint = coords[currentRound];
  16. setRoundInfo({ currentRound, targetPoint, roundSeconds: timer });
  17. } else {
  18. onGameEnd();
  19. }
  20. }
  21. setup();
  22. }, [gameId, playerName, onGameEnd]);
  23. if (!currentRound || !targetPoint || !roundSeconds) {
  24. return <Loading/>
  25. }
  26. const handleSubmitGuess = async () => {
  27. setSubmitDisabled(true);
  28. const { score, totalScore } = await sendGuess(gameId, playerName, currentRound, selectedPoint || { timeout: true });
  29. onRoundEnd({
  30. roundNum: currentRound,
  31. selectedPoint,
  32. targetPoint,
  33. score,
  34. totalScore,
  35. });
  36. }
  37. return (
  38. <div className="game-panel">
  39. <div className="game-panel__streetview">
  40. <PositionedStreetView position={targetPoint} />
  41. </div>
  42. <div className="game-panel__spacer"/>
  43. <GuessPane
  44. roundSeconds={roundSeconds}
  45. onTimeout={handleSubmitGuess}
  46. onSelectPoint={setSelectedPoint}
  47. onSubmitGuess={handleSubmitGuess}
  48. submitDisabled={submitDisabled || selectedPoint === null}
  49. />
  50. </div>
  51. );
  52. }
  53. export default GamePanelContainer;