MediaWiki:Gadget-fightcaverotations-core.js: Difference between revisions
Jump to navigation
Jump to search
Content added Content deleted
(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: | 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 |
// This gadget updates the predicted rotation times on [[TzHaar Fight Cave/Rotations]]. Made by Gau Cho, but it was Fjara's idea |
||
var ROTATION_CYCLE = 16; |
|||
function mod(a, n) { |
function mod(a, n) { |
||
return a - n * Math.floor(a / n); |
|||
} |
} |
||
function getMinutesFromMidnight(time) { |
function getMinutesFromMidnight(time) { |
||
// Gets minutes from UTC midnight |
|||
return time.getUTCHours() * 60 + time.getUTCMinutes(); |
|||
} |
} |
||
function formatTime(time) { |
function formatTime(time) { |
||
// Formats time (using the local timezone) |
|||
return time.getHours() + ':' + ('0' + time.getMinutes()).slice(-2); |
|||
} |
} |
||
function getNextRotationTime(rotationID, time, nthCycle) { |
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 (rotationID < 0 || rotationID >= ROTATION_CYCLE) throw RangeError('rotationID invalid'); |
|||
var minutesFromMidnight = getMinutesFromMidnight(time); |
|||
var offsetInMinutes = mod(rotationID - minutesFromMidnight, ROTATION_CYCLE); |
|||
return new Date(time.getTime() + offsetInMinutes * 60000 + nthCycle * ROTATION_CYCLE * 60000); |
|||
} |
} |
||
function updateTimes() { |
function updateTimes() { |
||
var curTime = new Date(); |
|||
⚫ | |||
// Unhighlight the old row that was highlighted |
|||
⚫ | |||
// Unhighlight the old row that was highlighted |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
var thereafterRotationTime = getNextRotationTime(rotationID, curTime, 1) |
|||
// Update the times of all the rows |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
var nextRotationTime = getNextRotationTime(rotationID, curTime, 0); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
⚫ | |||
// Highlight the new current row |
|||
⚫ | |||
if(nextRotationTime.getTime() === curTime.getTime()) { |
|||
⚫ | |||
⚫ | |||
if (rotationID === 15) { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
// |
// Highlight the new current row |
||
if (nextRotationTime.getTime() === curTime.getTime()) { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
// Update the current time in the prose |
|||
$('#rotation-time').text(formatTime(curTime)); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
// Run the script at the exact moment that the minutes number changes |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ |
Latest revision as of 12:06, 20 October 2024
"use strict";
// This gadget updates the predicted rotation times on [[TzHaar Fight Cave/Rotations]]. Made by Gau Cho, but it was Fjara's idea
var 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 (rotationID < 0 || rotationID >= ROTATION_CYCLE) throw RangeError('rotationID invalid');
var minutesFromMidnight = getMinutesFromMidnight(time);
var offsetInMinutes = mod(rotationID - minutesFromMidnight, ROTATION_CYCLE);
return new Date(time.getTime() + offsetInMinutes * 60000 + nthCycle * ROTATION_CYCLE * 60000);
}
function updateTimes() {
var curTime = new Date();
// Unhighlight the old row that was highlighted
$('#rotation-table .table-yes').removeClass('table-yes');
// Update the times of all the rows
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);
// rotationID 0 and 15 refer to the same rotation - this is the special case of the rotation that lasts for 2 minutes
// 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)
var rotationIDSelector = rotationID === 15 ? 0 : rotationID;
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
if (nextRotationTime.getTime() === curTime.getTime() + 15 * 60000) {
nextRotationTime = new Date(nextRotationTime.getTime() - 16 * 60000);
thereafterRotationTime = new Date(thereafterRotationTime.getTime() - 16 * 60000);
}
}
// Highlight the new current row
if (nextRotationTime.getTime() === 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));
}
// Update the current time in the prose
$('#rotation-time').text(formatTime(curTime));
// Run the script at the exact moment that the minutes number changes
var timeToNextMinute = 60000 - curTime.getTime() % 60000;
setTimeout(updateTimes, timeToNextMinute);
}
$(updateTimes);