gameStore.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { PRE_GAME, PRE_ROUND, IN_ROUND, POST_ROUND, POST_GAME } from "./gameStates";
  2. import { createStore, consoleMonitor } from "../store";
  3. import { joinGame, sendGuess, getCurrentRound, sendTimeout } from "./apiMethods";
  4. import {
  5. saveGameInfoToLocalStorage,
  6. clearGameInfoFromLocalStorage,
  7. saveTimerToLocalStorage,
  8. clearRoundInfoFromLocalStorage,
  9. getInfoFromLocalStorage,
  10. } from "./localStorageMethods";
  11. const [ hooks, set, get ] = createStore({
  12. gameId: null,
  13. playerName: null,
  14. lastRound: {
  15. roundNum: -1,
  16. targetPoint: {
  17. lat: 0,
  18. lng: 0,
  19. },
  20. score: -1,
  21. totalScore: -1,
  22. },
  23. playerId: null,
  24. gameState: PRE_GAME,
  25. currentRound: null,
  26. targetPoint: null,
  27. roundSeconds: 0,
  28. panoStartPosition: null,
  29. panoStartPov: null,
  30. }, process.env.REACT_APP_MONITOR_STORE === "true" ? consoleMonitor : null);
  31. export const {
  32. useGameId,
  33. usePlayerName,
  34. useLastRound,
  35. usePlayerId,
  36. useGameState,
  37. useCurrentRound,
  38. useTargetPoint,
  39. useRoundSeconds,
  40. usePanoStartPosition,
  41. usePanoStartPov,
  42. } = hooks;
  43. const setPlayerName = playerName => set({ playerName });
  44. const goToLobby = gameId => set({
  45. gameId,
  46. playerId: null,
  47. gameState: PRE_ROUND,
  48. });
  49. const updateCurrentRound = async () => {
  50. const { currentRound, coord, timer } = await getCurrentRound(
  51. get.gameId(),
  52. get.playerId()
  53. );
  54. set({
  55. currentRound,
  56. targetPoint: coord,
  57. panoStartPosition: coord,
  58. panoStartPov: { heading: 0, pitch: 0 },
  59. roundSeconds: timer,
  60. });
  61. };
  62. const joinGameAction = async () => {
  63. const gameId = get.gameId();
  64. const name = get.playerName();
  65. const { playerId } = await joinGame(gameId, name);
  66. set({ playerId });
  67. await updateCurrentRound();
  68. saveGameInfoToLocalStorage(gameId, name, playerId);
  69. };
  70. const rejoinGame = async () => {
  71. const { gameId, playerName, playerId, timer, position, pov } = getInfoFromLocalStorage();
  72. set({ gameId, playerName, playerId });
  73. await updateCurrentRound();
  74. set({
  75. roundSeconds: timer ?? get.roundSeconds(),
  76. panoStartPosition: position ?? get.panoStartPosition(),
  77. panoStartPov: pov ?? get.panoStartPov(),
  78. gameState: IN_ROUND,
  79. });
  80. };
  81. const startRound = () => set({ gameState: IN_ROUND });
  82. const submitGuess = async selectedPoint => {
  83. clearRoundInfoFromLocalStorage();
  84. const gameId = get.gameId();
  85. const playerId = get.playerId();
  86. const roundNum = get.currentRound();
  87. const targetPoint = get.targetPoint();
  88. const roundSeconds = get.roundSeconds();
  89. const { score, totalScore } = selectedPoint
  90. ? await sendGuess(
  91. gameId,
  92. playerId,
  93. roundNum,
  94. selectedPoint || { timeout: true },
  95. roundSeconds
  96. )
  97. : await sendTimeout(gameId, playerId, roundNum);
  98. set({
  99. lastRound: {
  100. roundNum,
  101. targetPoint,
  102. score,
  103. totalScore,
  104. },
  105. gameState: POST_ROUND,
  106. });
  107. await updateCurrentRound();
  108. };
  109. const goToSummary = (gameId, clearSavedGame = true) => {
  110. if (gameId) {
  111. set({ gameId });
  112. }
  113. set({ gameState: POST_GAME });
  114. if (clearSavedGame) {
  115. clearRoundInfoFromLocalStorage();
  116. clearGameInfoFromLocalStorage();
  117. }
  118. };
  119. const updateRoundSeconds = update => {
  120. const roundSeconds = update(get.roundSeconds());
  121. set({ roundSeconds });
  122. saveTimerToLocalStorage(roundSeconds);
  123. };
  124. export const dispatch = {
  125. setPlayerName,
  126. goToLobby,
  127. joinGame: joinGameAction,
  128. rejoinGame,
  129. startRound,
  130. submitGuess,
  131. goToSummary,
  132. updateRoundSeconds,
  133. };