|
@@ -1,6 +1,6 @@
|
|
|
import React, { useRef, useEffect } from "react";
|
|
|
import styled from "styled-components";
|
|
|
-import { useLastRound, usePlayerName, dispatch } from "../../../domain/gameStore";
|
|
|
+import { useLastRound, dispatch } from "../../../domain/gameStore";
|
|
|
import useMap from "../../../hooks/useMap";
|
|
|
import usePlayerScores from "../../../hooks/usePlayerScores";
|
|
|
import useRoundInfo from "../../../hooks/useRoundInfo";
|
|
@@ -44,36 +44,33 @@ const NextButton = styled(DelayedButton)`
|
|
|
padding: 1em;
|
|
|
`
|
|
|
|
|
|
-export default () => {
|
|
|
+const useMapWithMarkers = (mapDivRef) => {
|
|
|
// get the info about the last round
|
|
|
const { roundNum, targetPoint } = useLastRound();
|
|
|
|
|
|
- // draw the map
|
|
|
- const mapDivRef = useRef(null);
|
|
|
+ // create the map
|
|
|
const mapRef = useMap(mapDivRef, targetPoint.lat, targetPoint.lng, 4);
|
|
|
|
|
|
// set up the flag at the target point
|
|
|
useEffect(() => {
|
|
|
- const targetMarker = makeFlagMarker(mapRef.current, targetPoint, "Goal", "#000000");
|
|
|
+ const targetMarker = makeFlagMarker(mapRef.current, targetPoint);
|
|
|
return () => targetMarker.setMap(null);
|
|
|
}, [mapRef, targetPoint]);
|
|
|
|
|
|
- // get the player's name
|
|
|
- const playerName = usePlayerName();
|
|
|
-
|
|
|
- // live update the player scores
|
|
|
+ // live update with all the scores
|
|
|
const players = usePlayerScores()
|
|
|
- ?.filter(({ guesses }) => guesses[roundNum] && guesses[roundNum].score !== null);
|
|
|
+ ?.filter(({ guesses }) => guesses[roundNum] && guesses[roundNum].score !== null)
|
|
|
+ ?.map(({ name, totalScore, guesses }) => ({ name, totalScore, guess: guesses[roundNum] }));
|
|
|
|
|
|
// set up the marker colors
|
|
|
- const playerColors = usePlayerColors(players?.map(({ name }) => name))
|
|
|
+ const playerColors = usePlayerColors(players?.map(({ name }) => name));
|
|
|
|
|
|
+ // draw all the player markers and trails
|
|
|
useEffect(() => {
|
|
|
if (!players) return;
|
|
|
const drawings = [];
|
|
|
- players.forEach(({ name, guesses }) => {
|
|
|
- const { lat, lng, score } = guesses[roundNum]
|
|
|
- const color = playerName === name ? "#000000" : playerColors[name];
|
|
|
+ players.forEach(({ name, guess: { lat, lng, score } }) => {
|
|
|
+ const color = playerColors[name];
|
|
|
const selectedPoint = { lat, lng };
|
|
|
|
|
|
const marker = makeQuestionMarker(mapRef.current, selectedPoint, `${name} - ${score} Points`, color);
|
|
@@ -83,15 +80,19 @@ export default () => {
|
|
|
drawings.push(line);
|
|
|
});
|
|
|
return () => drawings.forEach((drawing) => drawing.setMap(null));
|
|
|
- }, [players, mapRef, targetPoint, roundNum, playerName, playerColors]);
|
|
|
+ }, [players, mapRef, targetPoint, playerColors]);
|
|
|
+}
|
|
|
|
|
|
- // whether or not the game is done
|
|
|
- const [gameFinished] = useRoundInfo()
|
|
|
+export default () => {
|
|
|
+ // get the info about the last round
|
|
|
+ const { roundNum, score, totalScore } = useLastRound();
|
|
|
|
|
|
- // look up the score for last round and the total score
|
|
|
- const playerInfo = players?.find(({ name }) => name === playerName);
|
|
|
- const score = playerInfo?.guesses?.[roundNum]?.score ?? 0;
|
|
|
- const totalScore = playerInfo?.totalScore ?? 0;
|
|
|
+ // draw the map
|
|
|
+ const mapDivRef = useRef(null);
|
|
|
+ useMapWithMarkers(mapDivRef);
|
|
|
+
|
|
|
+ // whether or not the game is done
|
|
|
+ const [gameFinished] = useRoundInfo();
|
|
|
|
|
|
return (
|
|
|
<Container>
|