feat: resurrect optimalSteps and efficiency metrics

- Calculate optimal path from entrance to key to exit using maze.getDistance()
- Compute efficiency as keystroke ratio to optimal steps (percentage)
- Add optimalSteps and efficiency to ScoreManager return value
- No game-over trigger on efficiency threshold (preserved for future use)
- Metrics available for display and potential future scoring enhancements
This commit is contained in:
2025-11-02 16:54:43 +00:00
parent b6c3225c60
commit 9db77779b8
2 changed files with 23 additions and 1 deletions
+2
View File
@@ -20,5 +20,7 @@ export type ScoreManager = {
distToKey: number;
distToExit: number;
keystrokes: number;
optimalSteps: number;
efficiency: number;
finalScore: number | null;
};
+21 -1
View File
@@ -1,4 +1,4 @@
import { useEffect, useState } from "react";
import { useEffect, useRef, useState } from "react";
import { useTimer } from "./useTimer";
import { useScoreTime } from "./useScoreTime";
import { useScorePaths } from "./useScorePaths";
@@ -32,10 +32,28 @@ export function useScore({ board, hero, gameStatusManager, keyManager, rows, col
const distToExit = entranceToExit || heroToExit;
const [finalScore, setFinalScore] = useState<number | null>(null);
const optimalRef = useRef<number>(0);
const keystrokes = keyManager ? keyManager.keyLog.length : 0;
useScoreTime({ gameStatus, timer });
// Calculate optimal steps from entrance -> key -> exit
useEffect(() => {
const maze = board.mazeInstanceRef.current;
if (!maze) return;
const k = maze.getDistance("entrance", "key");
const e = maze.getDistance("key", "exit");
const total = k + e;
if (Number.isFinite(total) && total > 0) {
optimalRef.current = total;
}
}, [gameStatus, board.mazeInstanceRef]);
const optimalSteps = optimalRef.current;
const efficiency = optimalSteps > 0 ? Math.round((keystrokes / optimalSteps) * 100) : 0;
useEffect(() => {
if (gameStatus !== "started" && gameStatus !== "has-key") return;
@@ -70,6 +88,8 @@ export function useScore({ board, hero, gameStatusManager, keyManager, rows, col
distToKey,
distToExit,
keystrokes,
optimalSteps,
efficiency,
finalScore,
};
}