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:
@@ -20,5 +20,7 @@ export type ScoreManager = {
|
|||||||
distToKey: number;
|
distToKey: number;
|
||||||
distToExit: number;
|
distToExit: number;
|
||||||
keystrokes: number;
|
keystrokes: number;
|
||||||
|
optimalSteps: number;
|
||||||
|
efficiency: number;
|
||||||
finalScore: number | null;
|
finalScore: number | null;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
import { useTimer } from "./useTimer";
|
import { useTimer } from "./useTimer";
|
||||||
import { useScoreTime } from "./useScoreTime";
|
import { useScoreTime } from "./useScoreTime";
|
||||||
import { useScorePaths } from "./useScorePaths";
|
import { useScorePaths } from "./useScorePaths";
|
||||||
@@ -32,10 +32,28 @@ export function useScore({ board, hero, gameStatusManager, keyManager, rows, col
|
|||||||
const distToExit = entranceToExit || heroToExit;
|
const distToExit = entranceToExit || heroToExit;
|
||||||
|
|
||||||
const [finalScore, setFinalScore] = useState<number | null>(null);
|
const [finalScore, setFinalScore] = useState<number | null>(null);
|
||||||
|
const optimalRef = useRef<number>(0);
|
||||||
const keystrokes = keyManager ? keyManager.keyLog.length : 0;
|
const keystrokes = keyManager ? keyManager.keyLog.length : 0;
|
||||||
|
|
||||||
useScoreTime({ gameStatus, timer });
|
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(() => {
|
useEffect(() => {
|
||||||
if (gameStatus !== "started" && gameStatus !== "has-key") return;
|
if (gameStatus !== "started" && gameStatus !== "has-key") return;
|
||||||
|
|
||||||
@@ -70,6 +88,8 @@ export function useScore({ board, hero, gameStatusManager, keyManager, rows, col
|
|||||||
distToKey,
|
distToKey,
|
||||||
distToExit,
|
distToExit,
|
||||||
keystrokes,
|
keystrokes,
|
||||||
|
optimalSteps,
|
||||||
|
efficiency,
|
||||||
finalScore,
|
finalScore,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user