Private
Public Access
1
0
Files
MineSeeker/assets/js/mine-seeker/hooks/useStepTimer.jsx

47 lines
1.1 KiB
JavaScript

/**
* 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 = () => {
const turnStartTimeRef = useRef(null);
const turnStartedRef = useRef(false);
const getStepElapsed = (currentActivePlayer, isGameRunning) => {
if (!isGameRunning) return 0;
if (!turnStartedRef.current) {
turnStartTimeRef.current = Date.now();
turnStartedRef.current = true;
return 0;
}
if (turnStartTimeRef.current) {
return Math.floor((Date.now() - turnStartTimeRef.current) / 1000);
}
return 0;
};
const resetStepTimer = () => {
turnStartTimeRef.current = null;
turnStartedRef.current = false;
};
const startNewTurn = () => {
turnStartTimeRef.current = Date.now();
turnStartedRef.current = true;
};
return { getStepElapsed, resetStepTimer, startNewTurn };
};
export default useStepTimer;