Advertisement
MichaelNeville

Wix Velo Curio Timer

Apr 30th, 2024
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Winbatch 8.69 KB | Source Code | 0 0
  1. import wixData from 'wix-data';
  2. import wixWindow from 'wix-window';
  3.  
  4. let startTime;
  5. let lapTime;
  6. let lapTimes = [];
  7. let lapCount = 0;
  8. let tally = 0;
  9. let originalTallyButtonLabel = $w("#tallyButton").label;
  10. let tallyHistory = []; // New array to store the tally history
  11. let soundPlayed = false; // Initialize the flag
  12.  
  13. //let lapNames = ["SLIDE MIDDLE BOARD", "UNDER BROKEN LAKE", "CASH REGISTER"]; // Add your custom lap names here
  14. //const LAP_TIME_LIMITS = [[0.2, 0.3],[1, 2],[20, 30]]; // Change these values to the time limits for each lap in minutes
  15.  
  16.   let lapNames = [];
  17.   let LAP_TIME_LIMITS = [];
  18.  
  19. let lapDetails = [];
  20. let previousLaps = [];
  21. const LAP_TIME_LIMITS_IN_SECONDS = LAP_TIME_LIMITS.map(lap => lap.map(limit => limit * 60)); // Convert the time limits to seconds
  22.  
  23. function formatTime(seconds, format = 'HH:MM:SS') {
  24.     const hours = Math.floor(seconds / 3600);
  25.     const minutes = Math.floor((seconds % 3600) / 60);
  26.     seconds = Math.floor(seconds % 60);
  27.     if (format === 'HH:MM:SS') {
  28.         return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
  29.     } else if (format === 'X MINUTES') {
  30.         return `TOTAL TIME: ${minutes} MIN`;
  31.     }
  32. }
  33.  
  34. $w.onReady(function () {
  35.     $w("#timer").text = "TOTAL TIME: 0 MIN";
  36.     $w("#lapTimer").text = "00:00:00";
  37.     $w("#lapTimerYellow").hide();
  38.     $w("#lapTimerRed").hide();
  39.  
  40. fetchLapData()
  41.  
  42.     $w("#startButton").onClick(() => {
  43.         startTime = Date.now();
  44.         lapTime = Date.now();
  45.         lapTimes = [];
  46.         lapCount = 0;
  47.         $w("#stopButton").label = "FINISH";
  48.         $w("#lapTimesText").text = "";
  49.         $w("#lapTimesTextReversed").text = ""; // Reset the lapTimesText
  50.     $w("#overallTallyText").text = "TOTAL CLUES: 0"; // Reset the overallTallyText
  51.     $w("#timer").text = "TOTAL TIME: 0 MIN";
  52.             $w("#lapTimesText").hide();
  53.         $w("#lapTimesTextReversed").show();
  54.     });
  55.  
  56.     $w("#tallyButton").onClick(() => {
  57.         tally++; // Increment the tally
  58.         tallyHistory[lapCount] = tally; // Update the tally history for the current lap
  59.         $w("#tallyButton").label = "✓"; // Change the button label to show the confirmation message
  60.        
  61.         $w("#overallTallyText").text = `TOTAL CLUES: ${tallyHistory.reduce((a, b) => a + b, 0)}`;
  62.         // Change the button label back to the original label after 1 second
  63.         setTimeout(() => {
  64.             $w("#tallyButton").label = originalTallyButtonLabel;
  65.         }, 1000);
  66.     });
  67.  
  68.     $w("#lapButton").onClick(() => {
  69.         const now = Date.now();
  70.         const lapTimeElapsed = (now - lapTime) / 1000;
  71.         lapTimes.unshift(lapTimeElapsed); // Add the new lap time at the beginning of the array for reversed display
  72.         lapTime = now;
  73.         lapCount++;
  74.         $w("#lapTimer").text = formatTime(lapTimeElapsed);
  75.  
  76.         // Original lapTimesText
  77.         $w("#lapTimesText").text = lapTimes.slice().reverse().map((time, index) => {
  78.             const minutes = Math.floor(time / 60);
  79.             const seconds = Math.floor(time % 60);
  80.             return `${lapNames[index] || `Lap ${index + 1}`}\n${minutes} Min, ${seconds} Sec • Clues: ${tallyHistory[index] || 0}\n`;
  81.         }).join('\n');
  82.  
  83.         // New reversed lapTimesText
  84.         $w("#lapTimesTextReversed").text = lapTimes.map((time, index) => {
  85.             const minutes = Math.floor(time / 60);
  86.             const seconds = Math.floor(time % 60);
  87.             return `${lapNames[lapCount - index - 1] || `Lap ${lapCount - index}`}\n${minutes} Min, ${seconds} Sec • Clues: ${tallyHistory[lapCount - index - 1] || 0}\n`;
  88.         }).join('\n');
  89.  
  90.         tally = 0; // Reset the tally
  91.         $w("#myAudioPlayer").pause(); // Stop playing the sound
  92.         $w("#myAudioPlayer").seek(0) // Reset the audio player
  93.     });
  94.  
  95.     $w("#prevLapButton").onClick(() => {
  96.         if (lapTimes.length > 0) {
  97.             lapTimes.shift(); // Remove the first lap time for reversed display
  98.             lapCount--;
  99.             lapTime = startTime + lapTimes.slice().reverse().reduce((a, b) => a + b, 0) * 1000;
  100.  
  101.             // Original lapTimesText
  102.             $w("#lapTimesText").text = lapTimes.slice().reverse().map((time, index) => {
  103.                 const minutes = Math.floor(time / 60);
  104.                 const seconds = Math.floor(time % 60);
  105.                 return `${lapNames[index] || `Lap ${index + 1}`}\n${minutes} Min, ${seconds} Sec • Clues: ${tallyHistory[index] || 0}\n`;
  106.             }).join('\n');
  107.  
  108.             // New reversed lapTimesText
  109.             $w("#lapTimesTextReversed").text = lapTimes.map((time, index) => {
  110.                 const minutes = Math.floor(time / 60);
  111.                 const seconds = Math.floor(time % 60);
  112.                 return `${lapNames[lapCount - index - 1] || `Lap ${lapCount - index}`}\n${minutes} Min, ${seconds} Sec • Clues: ${tallyHistory[lapCount - index - 1] || 0}\n`;
  113.             }).join('\n');
  114.  
  115.             tally = tallyHistory[lapCount] || 0; // Update the tally for the current lap
  116.             $w("#myAudioPlayer").pause(); // Stop playing the sound
  117.             $w("#myAudioPlayer").seek(0) // Reset the audio player
  118.         }
  119.     });
  120.  
  121.  
  122.  $w("#stopButton").onClick(() => {
  123.     saveToDataset();
  124.     // Stop and reset the timer
  125.     startTime = null;
  126.     lapTime = null;
  127.     lapTimes = [];
  128.     lapCount = 0;
  129.     tally = 0; // Reset the tally
  130.     tallyHistory = []; // Reset the tally history
  131.     $w("#lapTimesText").show();
  132.     $w("#lapTimesTextReversed").hide();
  133.     $w("#lapTimer").text = "00:00:00";
  134.     $w("#lapTimerYellow").hide();
  135.     $w("#lapTimerRed").hide();
  136.     $w("#stopButton").label = "✓"; // Update the button label
  137.         $w("#myAudioPlayer").pause(); // Stop playing the sound
  138.         $w("#myAudioPlayer").seek(0) // Reset the audio player
  139.     $w("#dataset2").setCurrentItemIndex(0)
  140.       .then(() => {
  141.         console.log("Dataset navigated to the first item");
  142.       })
  143.       .catch((err) => {
  144.         console.log("Failed to navigate to the first item", err);
  145.       });
  146.       setTimeout(() => {
  147.           $w("#stopButton").label = "FINISH";
  148.       }, 1000);
  149.   });
  150.      
  151.  
  152.  
  153.     setInterval(() => {
  154.     if (startTime && lapTime) {
  155.         const now = Date.now();
  156.         const totalElapsed = (now - startTime) / 1000;
  157.         const lapElapsed = (now - lapTime) / 1000;
  158.         $w("#timer").text = formatTime(totalElapsed, 'X MINUTES');
  159.         $w("#lapTimer").text = formatTime(lapElapsed, 'HH:MM:SS');
  160.         let currentLapLimits = LAP_TIME_LIMITS_IN_SECONDS[lapCount] || LAP_TIME_LIMITS_IN_SECONDS[LAP_TIME_LIMITS_IN_SECONDS.length - 1];
  161.         if (lapElapsed > currentLapLimits[1]) {
  162.             $w("#lapTimerYellow").hide();
  163.             $w("#lapTimerRed").show();
  164.             if (!soundPlayed) {
  165.                 $w("#myAudioPlayer").play(); // Play the sound
  166.                 soundPlayed = true; // Set the flag to true
  167.             }
  168.         } else if (lapElapsed > currentLapLimits[0]) {
  169.             $w("#lapTimerYellow").show();
  170.         } else {
  171.             $w("#lapTimerYellow").hide();
  172.             $w("#lapTimerRed").hide();
  173.             $w("#lapTimer").show();
  174.             $w("#lapTimer").text = formatTime(lapElapsed);
  175.             soundPlayed = false; // Reset the flag
  176.         }
  177.     }
  178. }, 1000);
  179.  
  180.  
  181.     function saveToDataset() {
  182.         const totalElapsed = Math.round((Date.now() - startTime) / 1000 / 60); // Round to nearest minute
  183.         let newEntry = {
  184.             total_time: totalElapsed,
  185.             start_time: new Date(startTime),
  186.             title: "Curio"
  187.         };
  188.         lapTimes.forEach((lapTime, index) => {
  189.             newEntry[`lap_${index + 1}`] = Math.round(lapTime / 60); // Round to nearest minute
  190.             newEntry[`clue_${index + 1}`] = tallyHistory[index] || 0;// Save the clues
  191.         });
  192.         wixData.insert('CurioLapTimes', newEntry)
  193.             .then(() => {
  194.                 console.log('Data saved to CurioLapTimes');
  195.             })
  196.             .catch((err) => {
  197.                 console.log(err);
  198.             });
  199.     }
  200.  
  201.  
  202. async function fetchLapData() {
  203.  
  204.  
  205.   try {
  206.     let results = await wixData.query('CurioDatabase').find();
  207.     results.items.forEach((item, index) => {
  208.       let lapName = item.title; // replace 'yourLapNameColumn' with the actual column name
  209.       let lapTime = item.averageTime; // replace 'yourTimeColumn' with the actual column name
  210.       lapNames[index] = lapName;
  211.       LAP_TIME_LIMITS[index] = [lapTime, lapTime * 2];
  212.     });
  213.   } catch (err) {
  214.     console.log(err);
  215.   }
  216.  
  217.   return { lapNames, LAP_TIME_LIMITS };
  218. }
  219.  
  220. // Usage:
  221. fetchLapData().then(data => {
  222.   console.log(data.lapNames);
  223.   console.log(data.LAP_TIME_LIMITS);
  224. });
  225.  
  226.  
  227. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement