Browse Source

Implement score check API changes to take method and round number

Kirk Trombley 3 years ago
parent
commit
c322bc4e3e

+ 5 - 1
client/src/components/screens/RoundSummary/useClickToCheckScore.jsx

@@ -1,10 +1,14 @@
 import useClickMarker from "../../../hooks/useClickMarker";
 import { checkScore } from "../../../domain/apiMethods";
+import { useGameConfig } from "../../../hooks/useGameInfo";
+import { useLastRound } from "../../../domain/gameStore";
 
 const useClickToCheckScore = (mapRef, point1) => {
+  const { scoreMethod } = useGameConfig();
+  const { roundNum } = useLastRound();
   // when the map is clicked, call the scoring API and update the marker's title
   useClickMarker(mapRef, async (point2, marker) => {
-    const { score } = await checkScore(point1, point2);
+    const { score } = await checkScore(point1, point2, scoreMethod, roundNum);
     marker.setLabel({
       fontWeight: "500",
       text: `Potential Score: ${score}`,

+ 2 - 2
client/src/domain/apiMethods.js

@@ -12,13 +12,13 @@ export const getStatus = async () => {
   }
 };
 
-export const checkScore = async (point1, point2) => {
+export const checkScore = async (point1, point2, scoreMethod, roundNumber) => {
   const res = await fetch(`${API_BASE}/score`, {
     method: "POST",
     headers: {
       "Content-Type": "application/json",
     },
-    body: JSON.stringify({ point1, point2 }),
+    body: JSON.stringify({ point1, point2, scoreMethod, roundNumber }),
   });
   if (!res.ok) {
     throw Error(res.statusText);

+ 23 - 4
server/app/api/other.py

@@ -1,4 +1,4 @@
-from typing import List
+from typing import List, Optional
 
 from fastapi import APIRouter, Depends
 from fastapi_camelcase import CamelModel
@@ -7,7 +7,7 @@ from pydantic import confloat
 from sqlalchemy.orm import Session
 
 from .. import scoring
-from ..schemas import CacheInfo, GeneratorInfo
+from ..schemas import CacheInfo, GameConfig, GeneratorInfo, ScoreMethodEnum
 from ..db import get_db, queries
 from ..point_gen import points, generator_info
 
@@ -22,6 +22,8 @@ class Point(CamelModel):
 class ScoreCheck(CamelModel):
     point1: Point
     point2: Point
+    score_method: Optional[ScoreMethodEnum] = None
+    round_number: Optional[int] = None
 
 
 class Score(CamelModel):
@@ -47,8 +49,25 @@ def health():
 
 
 @router.post("/score", response_model=Score)
-def check_score(points: ScoreCheck):
-    score, distance = scoring.score((points.point1.lat, points.point1.lng), (points.point2.lat, points.point2.lng))
+def check_score(check: ScoreCheck):
+    match check.score_method:
+        case ScoreMethodEnum.country_race:
+            # TODO is there a way to make this more well-defined?
+            return Score(distance=0, score=0)
+        case ScoreMethodEnum.hard:
+            score, distance = scoring.score_hard((check.point1.lat, check.point1.lng), (check.point2.lat, check.point2.lng))
+        case ScoreMethodEnum.nightmare:
+            score, distance = scoring.score_nightmare((check.point1.lat, check.point1.lng), (check.point2.lat, check.point2.lng))
+        case ScoreMethodEnum.ramp:
+            score, distance = scoring.score((check.point1.lat, check.point1.lng), (check.point2.lat, check.point2.lng))
+            if check.round_number is not None:
+                score *= 1 + ((check.round_number - 1) * 0.5)
+        case ScoreMethodEnum.ramp_hard:
+            score, distance = scoring.score_hard((check.point1.lat, check.point1.lng), (check.point2.lat, check.point2.lng))
+            if check.round_number is not None:
+                score *= 1 + ((check.round_number - 1) * 0.5)
+        case _:
+            score, distance = scoring.score((check.point1.lat, check.point1.lng), (check.point2.lat, check.point2.lng))
     return Score(distance=distance, score=score)