mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
Grey = cant click Red = current Green = complete blue = skipped? Need to add Clue page Need to fix Leaderboard.
101 lines
2.9 KiB
JavaScript
101 lines
2.9 KiB
JavaScript
/**
|
|
* Centralized timer service to ensure consistent timing across the application
|
|
*/
|
|
|
|
// Store the hunt timer information in localStorage for persistence
|
|
export const setHuntTimer = (startTime, endTime = null, isActive = true) => {
|
|
const timerData = {
|
|
startTime,
|
|
endTime,
|
|
isActive,
|
|
lastUpdated: new Date().toISOString(),
|
|
};
|
|
localStorage.setItem("huntTimerData", JSON.stringify(timerData));
|
|
};
|
|
|
|
// Clear the hunt timer completely
|
|
export const clearHuntTimer = () => {
|
|
localStorage.removeItem("huntTimerData");
|
|
localStorage.removeItem("teamHuntStartTime"); // Remove legacy timer as well
|
|
};
|
|
|
|
// Get the current hunt timer data with improved error handling
|
|
export const getHuntTimer = () => {
|
|
try {
|
|
// First try to get timer data from localStorage
|
|
const timerData = localStorage.getItem("huntTimerData");
|
|
|
|
if (!timerData) {
|
|
// Try to migrate from old format if exists
|
|
const legacyStartTime = localStorage.getItem("teamHuntStartTime");
|
|
if (legacyStartTime) {
|
|
console.log("Using legacy team hunt start time");
|
|
const migratedData = {
|
|
startTime: legacyStartTime,
|
|
endTime: null,
|
|
isActive: true,
|
|
lastUpdated: new Date().toISOString(),
|
|
};
|
|
|
|
// Save the migrated data for future use
|
|
localStorage.setItem("huntTimerData", JSON.stringify(migratedData));
|
|
return migratedData;
|
|
}
|
|
console.log("No hunt timer data found");
|
|
return null;
|
|
}
|
|
|
|
// Parse the timer data
|
|
const parsedData = JSON.parse(timerData);
|
|
|
|
// Validate the data has required fields
|
|
if (!parsedData || !parsedData.startTime) {
|
|
console.log("Invalid hunt timer data", parsedData);
|
|
return null;
|
|
}
|
|
|
|
return parsedData;
|
|
} catch (error) {
|
|
console.error("Error parsing hunt timer data:", error);
|
|
|
|
// Try to recover by clearing corrupt data
|
|
try {
|
|
localStorage.removeItem("huntTimerData");
|
|
} catch (e) {
|
|
// Ignore errors when trying to clean up
|
|
}
|
|
|
|
return null;
|
|
}
|
|
};
|
|
|
|
// Calculate and format elapsed time from a given start time
|
|
export const formatElapsedTime = (startTimeStr) => {
|
|
if (!startTimeStr) return "00:00:00";
|
|
|
|
const startTime = new Date(startTimeStr).getTime();
|
|
const now = Date.now();
|
|
const elapsed = Math.floor((now - startTime) / 1000);
|
|
|
|
// Handle negative time (shouldn't happen in normal usage)
|
|
if (elapsed < 0) return "00:00:00";
|
|
|
|
const hours = Math.floor(elapsed / 3600)
|
|
.toString()
|
|
.padStart(2, "0");
|
|
const minutes = Math.floor((elapsed % 3600) / 60)
|
|
.toString()
|
|
.padStart(2, "0");
|
|
const seconds = Math.floor(elapsed % 60)
|
|
.toString()
|
|
.padStart(2, "0");
|
|
|
|
return `${hours}:${minutes}:${seconds}`;
|
|
};
|
|
|
|
// Hook function to use in components to get formatted elapsed time
|
|
export const useElapsedTime = (startTime, isActive = true) => {
|
|
if (!startTime || !isActive) return "00:00:00";
|
|
return formatElapsedTime(startTime);
|
|
};
|