MediaWiki:Gadget-fightcaverotations-core.js: Difference between revisions

Jump to navigation Jump to search
no edit summary
(Created page with "// This gadget updates the predicted rotation times on TzHaar Fight Cave/Rotations. Made by Gau Cho, but it was Fjara's idea const ROTATION_CYCLE = 16 function mod(a, n) { return a - (n * Math.floor(a/n)); } function getMinutesFromMidnight(time) { // Gets minutes from UTC midnight return time.getUTCHours()*60 + time.getUTCMinutes() } function formatTime(time) { // Formats time (using the local timezone) return time.getHours() + ':' + ('0' + ti...")
 
No edit summary
 
Line 1:
"use strict";
 
// This gadget updates the predicted rotation times on [[TzHaar Fight Cave/Rotations]]. Made by Gau Cho, but it was Fjara's idea
 
constvar ROTATION_CYCLE = 16;
 
function mod(a, n) {
return a - (n * Math.floor(a / n));
}
 
function getMinutesFromMidnight(time) {
// Gets minutes from UTC midnight
return time.getUTCHours() * 60 + time.getUTCMinutes();
}
 
function formatTime(time) {
// Formats time (using the local timezone)
return time.getHours() + ':' + ('0' + time.getMinutes()).slice(-2);
}
 
function getNextRotationTime(rotationID, time, nthCycle) {
// Gets the time of the nth next cycle for the desired rotation, where the 0th cycle is the next time the rotation occurs
if if(rotationID < 0 || rotationID >= ROTATION_CYCLE) throw RangeError('rotationID invalid');
constvar minutesFromMidnight = getMinutesFromMidnight(time);
constvar offsetInMinutes = mod(rotationID - minutesFromMidnight, ROTATION_CYCLE);
return new Date(time.getTime() + offsetInMinutes * 60000 + nthCycle * ROTATION_CYCLE * 60000);
}
 
function updateTimes() {
constvar curTime = new Date();
// Unhighlight the old row that was highlighted
$('#rotation-table .table-yes').removeClass('table-yes')
 
// UpdateUnhighlight the timesold ofrow allthat thewas rowshighlighted
$('#rotation-table .table-yes').removeClass('table-yes');
for(var rotationID = 0; rotationID < ROTATION_CYCLE; rotationID++) {
// Calculated the next two desired times
var nextRotationTime = getNextRotationTime(rotationID, curTime, 0)
var thereafterRotationTime = getNextRotationTime(rotationID, curTime, 1)
 
// Update the times of all the rows
// rotationID 0 and 15 refer to the same rotation - this is the special case of the rotation that lasts for 2 minutes
for for(var rotationID = 0; rotationID < ROTATION_CYCLE; rotationID++) {
// We highlight if the time matches either rotationID 0 or 15, but display the time for rotationID 15 as it is 1 minute earlier (so we do rotationID 0 and then after rotationID 15)
// Calculated the next two desired times
const rotationIDSelector = rotationID === 15 ? 0 : rotationID
var nextRotationTime = ifgetNextRotationTime(rotationID, ===curTime, 150) {;
var nextRotationTimethereafterRotationTime = getNextRotationTime(rotationID, curTime, 01);
// We need to handle the edge case where we are in the second minute, in which case the rotation time will display to be 16 minutes too late
if(nextRotationTime.getTime() === curTime.getTime() + 15*60000) {
nextRotationTime = new Date(nextRotationTime.getTime() - 16*60000)
thereafterRotationTime = new Date(thereafterRotationTime.getTime() - 16*60000)
}
}
 
// rotationID 0 and 15 refer to the same rotation - this is the special case of the rotation that lasts for 2 minutes
// Highlight the new current row
// We highlight if the time matches either rotationID 0 or 15, but display the time for rotationID 15 as it is 1 minute earlier (so we do rotationID 0 and then after rotationID 15)
if(nextRotationTime.getTime() === curTime.getTime()) {
constvar rotationIDSelector = rotationID === 15 ? 0 : rotationID;
$('#rotation-' + rotationIDSelector).addClass('table-yes')
if (rotationID === 15) }{
// We need to handle the edge case where we are in the second minute, in which case the rotation time will display to be 16 minutes too late
// Update the times
if if(nextRotationTime.getTime() === curTime.getTime() + 15 * 60000) {
$('#rotation-' + rotationIDSelector + ' > .rotation-time-first').text(formatTime(nextRotationTime))
nextRotationTime = new Date(nextRotationTime.getTime() - 16 * 60000);
$('#rotation-' + rotationIDSelector + ' > .rotation-time-second').text(formatTime(thereafterRotationTime))
thereafterRotationTime = new Date(thereafterRotationTime.getTime() - 16 * 60000);
}
}
 
// UpdateHighlight the new current time in the proserow
$if ('#rotation-time')nextRotationTime.text(formatTimegetTime() === curTime.getTime()) {
$('#rotation-' + rotationIDSelector).addClass('table-yes');
}
// Update the times
$('#rotation-' + rotationIDSelector + ' > .rotation-time-first').text(formatTime(nextRotationTime));
$('#rotation-' + rotationIDSelector + ' > .rotation-time-second').text(formatTime(thereafterRotationTime));
}
 
// RunUpdate the scriptcurrent attime the exact moment thatin the minutes number changesprose
$('#rotation-time').text(formatTime(curTime));
const timeToNextMinute = 60000 - (curTime.getTime() % 60000)
setTimeout(updateTimes, timeToNextMinute)
 
// Run the script at the exact moment that the minutes number changes
$(updateTimes)
constvar timeToNextMinute = 60000 - (curTime.getTime() % 60000);
setTimeout(updateTimes, timeToNextMinute);
$(updateTimes);

Navigation menu