RoundSummary.jsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import React, { useRef, useEffect } from "react";
  2. import styled from "styled-components";
  3. import { useLastRound, usePlayerName, dispatch } from "../../../domain/gameStore";
  4. import useMap from "../../../hooks/useMap";
  5. import usePlayerScores from "../../../hooks/usePlayerScores";
  6. import useRoundInfo from "../../../hooks/useRoundInfo";
  7. import DelayedButton from "../../util/DelayedButton";
  8. import Button from "../../util/Button";
  9. /* global google */
  10. const Container = styled.div`
  11. `
  12. const SummaryDiv = styled.div`
  13. position: absolute;
  14. display: flex;
  15. flex-flow: column nowrap;
  16. background-color: #333;
  17. z-index: 1;
  18. bottom: 16px;
  19. right: 8px;
  20. padding: 8px;
  21. `
  22. const ScoreSpan = styled.span`
  23. padding-bottom: 2px;
  24. `;
  25. const MapDiv = styled.div`
  26. position: absolute;
  27. height: 100%;
  28. width: 100%;
  29. `
  30. const FinishedButton = styled(Button)`
  31. margin-top: 5px;
  32. padding: 1em;
  33. `
  34. const NextButton = styled(DelayedButton)`
  35. margin-top: 5px;
  36. padding: 1em;
  37. `
  38. const flagIcon = {
  39. path: "M466.515 66.928C487.731 57.074 512 72.551 512 95.944v243.1c0 10.526-5.161 20.407-13.843 26.358-35.837 24.564-74.335 40.858-122.505 40.858-67.373 0-111.63-34.783-165.217-34.783-50.853 0-86.124 10.058-114.435 22.122V488c0 13.255-10.745 24-24 24H56c-13.255 0-24-10.745-24-24V101.945C17.497 91.825 8 75.026 8 56 8 24.296 34.345-1.254 66.338.048c28.468 1.158 51.779 23.968 53.551 52.404.52 8.342-.81 16.31-3.586 23.562C137.039 68.384 159.393 64 184.348 64c67.373 0 111.63 34.783 165.217 34.783 40.496 0 82.612-15.906 116.95-31.855zM96 134.63v70.49c29-10.67 51.18-17.83 73.6-20.91v-71.57c-23.5 2.17-40.44 9.79-73.6 21.99zm220.8 9.19c-26.417-4.672-49.886-13.979-73.6-21.34v67.42c24.175 6.706 47.566 16.444 73.6 22.31v-68.39zm-147.2 40.39v70.04c32.796-2.978 53.91-.635 73.6 3.8V189.9c-25.247-7.035-46.581-9.423-73.6-5.69zm73.6 142.23c26.338 4.652 49.732 13.927 73.6 21.34v-67.41c-24.277-6.746-47.54-16.45-73.6-22.32v68.39zM96 342.1c23.62-8.39 47.79-13.84 73.6-16.56v-71.29c-26.11 2.35-47.36 8.04-73.6 17.36v70.49zm368-221.6c-21.3 8.85-46.59 17.64-73.6 22.47v71.91c27.31-4.36 50.03-14.1 73.6-23.89V120.5zm0 209.96v-70.49c-22.19 14.2-48.78 22.61-73.6 26.02v71.58c25.07-2.38 48.49-11.04 73.6-27.11zM316.8 212.21v68.16c25.664 7.134 46.616 9.342 73.6 5.62v-71.11c-25.999 4.187-49.943 2.676-73.6-2.67z",
  40. fillOpacity: 1.0,
  41. fillColor: "#000000",
  42. scale: 0.075,
  43. anchor: new google.maps.Point(16, 512),
  44. };
  45. const questionSymbol = color => ({
  46. path: "M29.898 26.5722l-4.3921 0c-0.0118,-0.635 -0.0177,-1.0172 -0.0177,-1.1583 0,-1.4229 0.2352,-2.5929 0.7056,-3.5102 0.4704,-0.9231 1.417,-1.952 2.8281,-3.1044 1.4111,-1.1465 2.2578,-1.8991 2.5282,-2.2578 0.4292,-0.5585 0.6409,-1.1818 0.6409,-1.8579 0,-0.9408 -0.3763,-1.7463 -1.1289,-2.4224 -0.7526,-0.6703 -1.7639,-1.0054 -3.0397,-1.0054 -1.2289,0 -2.2578,0.3527 -3.0868,1.0524 -0.8232,0.6997 -1.3935,1.7698 -1.7051,3.2044l-4.4391 -0.5527c0.1234,-2.0578 0.9995,-3.8041 2.6223,-5.2387 1.6286,-1.4346 3.757,-2.152 6.4029,-2.152 2.7752,0 4.9859,0.7291 6.6322,2.1814 1.6404,1.4522 2.4635,3.1397 2.4635,5.0741 0,1.0642 -0.3057,2.0755 -0.9054,3.028 -0.6056,0.9525 -1.8933,2.2519 -3.8688,3.8923 -1.0231,0.8525 -1.6581,1.5346 -1.905,2.052 -0.2469,0.5174 -0.3587,1.4405 -0.3351,2.7752zm-4.3921 6.5087l0 -4.8389 4.8389 0 0 4.8389 -4.8389 0z",
  47. fillOpacity: 1.0,
  48. fillColor: color,
  49. scale: 1,
  50. anchor: new google.maps.Point(32, 40),
  51. })
  52. const lineSettings = color => ({
  53. strokeColor: color,
  54. strokeOpacity: 0,
  55. icons: [{
  56. icon: {
  57. path: 'M 0,-1 0,1',
  58. strokeOpacity: 1,
  59. scale: 4
  60. },
  61. offset: '0',
  62. repeat: '20px'
  63. }],
  64. });
  65. const makeMarker = (map, position, title, icon) => {
  66. const marker = new google.maps.Marker({
  67. clickable: true,
  68. map,
  69. position,
  70. title,
  71. icon,
  72. });
  73. const { lat, lng } = position;
  74. marker.addListener("click", () => {
  75. window.open(`https://www.google.com/maps?hl=en&q=+${lat},+${lng}`, "_blank");
  76. });
  77. return marker;
  78. }
  79. // logic adapted from How to Generate Random Colors Programmatically by Martin Ankerl
  80. // https://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/
  81. const goldenRatioConj = 0.618033988749895;
  82. let h = 0;
  83. const nextColor = () => {
  84. h += goldenRatioConj;
  85. h %= 1;
  86. const h6 = h * 6;
  87. const h_i = Math.floor(h6);
  88. const f = 0.45 * (h6 - h_i);
  89. const q = 0.9 - f;
  90. const t = f + 0.45;
  91. return "#" + ([
  92. [0.9, t, 0.45],
  93. [q, 0.9, 0.45],
  94. [0.45, 0.9, t],
  95. [0.45, q, 0.9],
  96. [t, 0.45, 0.9],
  97. [0.9, 0.45, q],
  98. ])[h_i].map(component => {
  99. const converted = Math.floor(component * 256).toString(16);
  100. if (converted.length === 1) {
  101. return "0" + converted;
  102. }
  103. return converted;
  104. }).reduce((x, y) => x + y);
  105. }
  106. const playerColors = {};
  107. export default () => {
  108. // get the info about the last round
  109. const { roundNum, targetPoint, score, totalScore } = useLastRound();
  110. // draw the map
  111. // TODO dynamically determine this zoom level?
  112. const mapDivRef = useRef(null);
  113. const mapRef = useMap(mapDivRef, targetPoint.lat, targetPoint.lng, 4);
  114. // set up the flag at the target point
  115. useEffect(() => {
  116. const targetMarker = makeMarker(mapRef.current, targetPoint, "Goal", flagIcon);
  117. return () => targetMarker.setMap(null);
  118. }, [mapRef, targetPoint]);
  119. // get the player's name
  120. const playerName = usePlayerName();
  121. // live update the player scores
  122. const players = usePlayerScores()
  123. ?.filter(({ guesses }) => guesses[roundNum] && guesses[roundNum].score);
  124. useEffect(() => {
  125. if (!players) return;
  126. const drawings = [];
  127. players.forEach(({ name, guesses }) => {
  128. const { lat, lng, score } = guesses[roundNum]
  129. const color = playerName === name ? "#000000" : playerColors[name] ?? nextColor();
  130. playerColors[name] = color;
  131. const selectedPoint = { lat, lng };
  132. const marker = makeMarker(mapRef.current, selectedPoint, `${name} - ${score} Points`, questionSymbol(color))
  133. drawings.push(marker);
  134. const line = new google.maps.Polyline({
  135. path: [ selectedPoint, targetPoint ],
  136. map: mapRef.current,
  137. ...lineSettings(color),
  138. });
  139. drawings.push(line);
  140. });
  141. return () => drawings.forEach((drawing) => drawing.setMap(null));
  142. }, [players, mapRef, targetPoint, roundNum, playerName]);
  143. // whether or not the game is done
  144. const [gameFinished] = useRoundInfo()
  145. return (
  146. <Container>
  147. <MapDiv ref={mapDivRef} />
  148. <SummaryDiv>
  149. <ScoreSpan>Score for Round {roundNum}: {score}</ScoreSpan>
  150. <ScoreSpan>Running Total: {totalScore}</ScoreSpan>
  151. {
  152. gameFinished
  153. ? <FinishedButton onClick={dispatch.startRound}>
  154. View Summary
  155. </FinishedButton>
  156. : <NextButton onEnd={dispatch.startRound} countDownFormatter={rem => `Click to cancel, ${rem}s...`}>
  157. Next Round
  158. </NextButton>
  159. }
  160. </SummaryDiv>
  161. </Container>
  162. );
  163. };