Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import wixData from 'wix-data';
- import wixWindow from 'wix-window';
- let startTime;
- let lapTime;
- let lapTimes = [];
- let lapCount = 0;
- let tally = 0;
- let originalTallyButtonLabel = $w("#tallyButton").label;
- let tallyHistory = []; // New array to store the tally history
- let soundPlayed = false; // Initialize the flag
- //let lapNames = ["SLIDE MIDDLE BOARD", "UNDER BROKEN LAKE", "CASH REGISTER"]; // Add your custom lap names here
- //const LAP_TIME_LIMITS = [[0.2, 0.3],[1, 2],[20, 30]]; // Change these values to the time limits for each lap in minutes
- let lapNames = [];
- let LAP_TIME_LIMITS = [];
- let lapDetails = [];
- let previousLaps = [];
- const LAP_TIME_LIMITS_IN_SECONDS = LAP_TIME_LIMITS.map(lap => lap.map(limit => limit * 60)); // Convert the time limits to seconds
- function formatTime(seconds, format = 'HH:MM:SS') {
- const hours = Math.floor(seconds / 3600);
- const minutes = Math.floor((seconds % 3600) / 60);
- seconds = Math.floor(seconds % 60);
- if (format === 'HH:MM:SS') {
- return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
- } else if (format === 'X MINUTES') {
- return `TOTAL TIME: ${minutes} MIN`;
- }
- }
- $w.onReady(function () {
- $w("#timer").text = "TOTAL TIME: 0 MIN";
- $w("#lapTimer").text = "00:00:00";
- $w("#lapTimerYellow").hide();
- $w("#lapTimerRed").hide();
- fetchLapData()
- $w("#startButton").onClick(() => {
- startTime = Date.now();
- lapTime = Date.now();
- lapTimes = [];
- lapCount = 0;
- $w("#stopButton").label = "FINISH";
- $w("#lapTimesText").text = "";
- $w("#lapTimesTextReversed").text = ""; // Reset the lapTimesText
- $w("#overallTallyText").text = "TOTAL CLUES: 0"; // Reset the overallTallyText
- $w("#timer").text = "TOTAL TIME: 0 MIN";
- $w("#lapTimesText").hide();
- $w("#lapTimesTextReversed").show();
- });
- $w("#tallyButton").onClick(() => {
- tally++; // Increment the tally
- tallyHistory[lapCount] = tally; // Update the tally history for the current lap
- $w("#tallyButton").label = "✓"; // Change the button label to show the confirmation message
- $w("#overallTallyText").text = `TOTAL CLUES: ${tallyHistory.reduce((a, b) => a + b, 0)}`;
- // Change the button label back to the original label after 1 second
- setTimeout(() => {
- $w("#tallyButton").label = originalTallyButtonLabel;
- }, 1000);
- });
- $w("#lapButton").onClick(() => {
- const now = Date.now();
- const lapTimeElapsed = (now - lapTime) / 1000;
- lapTimes.unshift(lapTimeElapsed); // Add the new lap time at the beginning of the array for reversed display
- lapTime = now;
- lapCount++;
- $w("#lapTimer").text = formatTime(lapTimeElapsed);
- // Original lapTimesText
- $w("#lapTimesText").text = lapTimes.slice().reverse().map((time, index) => {
- const minutes = Math.floor(time / 60);
- const seconds = Math.floor(time % 60);
- return `${lapNames[index] || `Lap ${index + 1}`}\n${minutes} Min, ${seconds} Sec • Clues: ${tallyHistory[index] || 0}\n`;
- }).join('\n');
- // New reversed lapTimesText
- $w("#lapTimesTextReversed").text = lapTimes.map((time, index) => {
- const minutes = Math.floor(time / 60);
- const seconds = Math.floor(time % 60);
- return `${lapNames[lapCount - index - 1] || `Lap ${lapCount - index}`}\n${minutes} Min, ${seconds} Sec • Clues: ${tallyHistory[lapCount - index - 1] || 0}\n`;
- }).join('\n');
- tally = 0; // Reset the tally
- $w("#myAudioPlayer").pause(); // Stop playing the sound
- $w("#myAudioPlayer").seek(0) // Reset the audio player
- });
- $w("#prevLapButton").onClick(() => {
- if (lapTimes.length > 0) {
- lapTimes.shift(); // Remove the first lap time for reversed display
- lapCount--;
- lapTime = startTime + lapTimes.slice().reverse().reduce((a, b) => a + b, 0) * 1000;
- // Original lapTimesText
- $w("#lapTimesText").text = lapTimes.slice().reverse().map((time, index) => {
- const minutes = Math.floor(time / 60);
- const seconds = Math.floor(time % 60);
- return `${lapNames[index] || `Lap ${index + 1}`}\n${minutes} Min, ${seconds} Sec • Clues: ${tallyHistory[index] || 0}\n`;
- }).join('\n');
- // New reversed lapTimesText
- $w("#lapTimesTextReversed").text = lapTimes.map((time, index) => {
- const minutes = Math.floor(time / 60);
- const seconds = Math.floor(time % 60);
- return `${lapNames[lapCount - index - 1] || `Lap ${lapCount - index}`}\n${minutes} Min, ${seconds} Sec • Clues: ${tallyHistory[lapCount - index - 1] || 0}\n`;
- }).join('\n');
- tally = tallyHistory[lapCount] || 0; // Update the tally for the current lap
- $w("#myAudioPlayer").pause(); // Stop playing the sound
- $w("#myAudioPlayer").seek(0) // Reset the audio player
- }
- });
- $w("#stopButton").onClick(() => {
- saveToDataset();
- // Stop and reset the timer
- startTime = null;
- lapTime = null;
- lapTimes = [];
- lapCount = 0;
- tally = 0; // Reset the tally
- tallyHistory = []; // Reset the tally history
- $w("#lapTimesText").show();
- $w("#lapTimesTextReversed").hide();
- $w("#lapTimer").text = "00:00:00";
- $w("#lapTimerYellow").hide();
- $w("#lapTimerRed").hide();
- $w("#stopButton").label = "✓"; // Update the button label
- $w("#myAudioPlayer").pause(); // Stop playing the sound
- $w("#myAudioPlayer").seek(0) // Reset the audio player
- $w("#dataset2").setCurrentItemIndex(0)
- .then(() => {
- console.log("Dataset navigated to the first item");
- })
- .catch((err) => {
- console.log("Failed to navigate to the first item", err);
- });
- setTimeout(() => {
- $w("#stopButton").label = "FINISH";
- }, 1000);
- });
- setInterval(() => {
- if (startTime && lapTime) {
- const now = Date.now();
- const totalElapsed = (now - startTime) / 1000;
- const lapElapsed = (now - lapTime) / 1000;
- $w("#timer").text = formatTime(totalElapsed, 'X MINUTES');
- $w("#lapTimer").text = formatTime(lapElapsed, 'HH:MM:SS');
- let currentLapLimits = LAP_TIME_LIMITS_IN_SECONDS[lapCount] || LAP_TIME_LIMITS_IN_SECONDS[LAP_TIME_LIMITS_IN_SECONDS.length - 1];
- if (lapElapsed > currentLapLimits[1]) {
- $w("#lapTimerYellow").hide();
- $w("#lapTimerRed").show();
- if (!soundPlayed) {
- $w("#myAudioPlayer").play(); // Play the sound
- soundPlayed = true; // Set the flag to true
- }
- } else if (lapElapsed > currentLapLimits[0]) {
- $w("#lapTimerYellow").show();
- } else {
- $w("#lapTimerYellow").hide();
- $w("#lapTimerRed").hide();
- $w("#lapTimer").show();
- $w("#lapTimer").text = formatTime(lapElapsed);
- soundPlayed = false; // Reset the flag
- }
- }
- }, 1000);
- function saveToDataset() {
- const totalElapsed = Math.round((Date.now() - startTime) / 1000 / 60); // Round to nearest minute
- let newEntry = {
- total_time: totalElapsed,
- start_time: new Date(startTime),
- title: "Curio"
- };
- lapTimes.forEach((lapTime, index) => {
- newEntry[`lap_${index + 1}`] = Math.round(lapTime / 60); // Round to nearest minute
- newEntry[`clue_${index + 1}`] = tallyHistory[index] || 0;// Save the clues
- });
- wixData.insert('CurioLapTimes', newEntry)
- .then(() => {
- console.log('Data saved to CurioLapTimes');
- })
- .catch((err) => {
- console.log(err);
- });
- }
- async function fetchLapData() {
- try {
- let results = await wixData.query('CurioDatabase').find();
- results.items.forEach((item, index) => {
- let lapName = item.title; // replace 'yourLapNameColumn' with the actual column name
- let lapTime = item.averageTime; // replace 'yourTimeColumn' with the actual column name
- lapNames[index] = lapName;
- LAP_TIME_LIMITS[index] = [lapTime, lapTime * 2];
- });
- } catch (err) {
- console.log(err);
- }
- return { lapNames, LAP_TIME_LIMITS };
- }
- // Usage:
- fetchLapData().then(data => {
- console.log(data.lapNames);
- console.log(data.LAP_TIME_LIMITS);
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement