浏览代码

Wrapping submitGuess up as a single action

Kirk Trombley 5 年之前
父节点
当前提交
276abc2fa7

+ 1 - 9
client/src/components/screens/GamePanel/GamePanel.jsx

@@ -1,6 +1,5 @@
 import React, { useState } from 'react';
 import styled from 'styled-components';
-import { sendGuess } from "../../../domain/GGSHService";
 import Loading from '../../util/Loading';
 import GuessPane from "./GuessPane";
 import PositionedStreetView from "./PositionedStreetView";
@@ -47,14 +46,7 @@ const GamePanelContainer = () => {
 
   const handleSubmitGuess = async () => {
     setSubmitDisabled(true);
-    const { score, totalScore } = await sendGuess(currentRound, selectedPoint || { timeout: true });
-    dispatch.endRound({
-      roundNum: currentRound,
-      selectedPoint,
-      targetPoint,
-      score,
-      totalScore,
-    });
+    await dispatch.submitGuess(selectedPoint);
   }
 
   return (

+ 1 - 5
client/src/domain/GGSHService.js

@@ -1,5 +1,3 @@
-import { dispatch } from "./gameStore";
-
 const API_BASE = "https://kirkleon.ddns.net/terrassumptions/api";
 
 export const getStatus = async () => {
@@ -62,9 +60,7 @@ export const getCurrentRound = async (gameId, name) => {
     return await res.json();
 }
 
-export const sendGuess = async (round, point) => {
-    const gameId = dispatch.getGameId();
-    const name = dispatch.getPlayerName();
+export const sendGuess = async (gameId, name, round, point) => {
     const res = await fetch(`${API_BASE}/game/${gameId}/guesses/${round}`, {
         method: "POST",
         headers: {

+ 35 - 3
client/src/domain/gameStore.js

@@ -1,6 +1,6 @@
 import { PRE_GAME, PRE_ROUND, IN_ROUND, POST_ROUND, POST_GAME } from "./GameState";
 import { createStore } from "../store";
-import { createGame, joinGame } from "./GGSHService";
+import { createGame, joinGame, sendGuess, getCurrentRound } from "./GGSHService";
 
 export const [
   {
@@ -14,7 +14,19 @@ export const [
 ] = createStore({
   gameId: null,
   playerName: null,
-  lastRound: null,
+  lastRound: {
+    roundNum: -1,
+    selectedPoint: {
+      lat: 0,
+      lng: 0,
+    },
+    targetPoint: {
+      lat: 0,
+      lng: 0,
+    },
+    score: -1,
+    totalScore: -1,
+  },
   gameJoined: false,
   gameState: PRE_GAME,
 }, {
@@ -25,7 +37,6 @@ export const [
   resetGame: ([set]) => set({ gameJoined: false, gameState: PRE_GAME }),
   startGame: ([set]) => set({ gameState: PRE_ROUND }),
   startRound: ([set]) => set({ gameState: IN_ROUND }),
-  endRound: ([set], lastRound) => set({ lastRound, gameState: POST_ROUND }),
   endGame: ([set]) => set({ gameState: POST_GAME }),
   createGameAndStart: async ([set, get], timer) => {
     const name = get.playerName();
@@ -38,4 +49,25 @@ export const [
     await joinGame(gameId, name);
     set({ gameJoined: true });
   },
+  submitGuess: async ([set, get], selectedPoint) => {
+    const gameId = get.gameId();
+    const name = get.playerName();
+    const { currentRound, coord } = await getCurrentRound(gameId, name);
+    const { score, totalScore } = await sendGuess(
+      gameId,
+      name,
+      currentRound, 
+      selectedPoint || { timeout: true }
+    );
+    set({
+      lastRound: {
+        roundNum: currentRound,
+        selectedPoint,
+        targetPoint: coord,
+        score,
+        totalScore,
+      },
+      gameState: POST_ROUND,
+    });
+  }
 });