/** * This file is part of the SplendidBear Websites' projects. * * Copyright (c) 2026 @ www.splendidbear.org * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ import { useRef } from 'react'; const useStepTimer = () => { // Record when the current turn started (timestamp) const turnStartTimeRef = useRef(null); // Flag to track if we've already recorded a turn start const turnStartedRef = useRef(false); const getStepElapsed = (currentActivePlayer, isGameRunning) => { // If game not running, return 0 if (!isGameRunning) return 0; // Only initialize the turn timer ONCE per call to getStepElapsed // This prevents resetting on multiple calls if (!turnStartedRef.current) { turnStartTimeRef.current = Date.now(); turnStartedRef.current = true; return 0; } // After initialization, just calculate elapsed time if (turnStartTimeRef.current) { return Math.floor((Date.now() - turnStartTimeRef.current) / 1000); } return 0; }; const resetStepTimer = () => { turnStartTimeRef.current = null; turnStartedRef.current = false; }; // Call this when we know a turn has actually changed (from server response) const startNewTurn = () => { turnStartTimeRef.current = Date.now(); turnStartedRef.current = true; }; return { getStepElapsed, resetStepTimer, startNewTurn }; }; export default useStepTimer;