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

From RuneRealm Wiki
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


const ROTATION_CYCLE = 16
var ROTATION_CYCLE = 16;

function mod(a, n) {
function mod(a, n) {
return a - (n * Math.floor(a/n));
return a - n * Math.floor(a / n);
}
}

function getMinutesFromMidnight(time) {
function getMinutesFromMidnight(time) {
// Gets minutes from UTC midnight
// Gets minutes from UTC midnight
return time.getUTCHours()*60 + time.getUTCMinutes()
return time.getUTCHours() * 60 + time.getUTCMinutes();
}
}

function formatTime(time) {
function formatTime(time) {
// Formats time (using the local timezone)
// Formats time (using the local timezone)
return time.getHours() + ':' + ('0' + time.getMinutes()).slice(-2)
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
// 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')
if (rotationID < 0 || rotationID >= ROTATION_CYCLE) throw RangeError('rotationID invalid');
const minutesFromMidnight = getMinutesFromMidnight(time)
var minutesFromMidnight = getMinutesFromMidnight(time);
const offsetInMinutes = mod(rotationID - minutesFromMidnight, ROTATION_CYCLE)
var offsetInMinutes = mod(rotationID - minutesFromMidnight, ROTATION_CYCLE);
return new Date(time.getTime() + offsetInMinutes*60000 + nthCycle*ROTATION_CYCLE*60000)
return new Date(time.getTime() + offsetInMinutes * 60000 + nthCycle * ROTATION_CYCLE * 60000);
}
}

function updateTimes() {
function updateTimes() {
const curTime = new Date()
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
// Unhighlight the old row that was highlighted
$('#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 (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
if(rotationID === 15) {
var nextRotationTime = getNextRotationTime(rotationID, curTime, 0);
var thereafterRotationTime = getNextRotationTime(rotationID, curTime, 1);
// 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()) {
var 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 (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);
}
}
}


// Update the current time in the prose
// Highlight the new current row
$('#rotation-time').text(formatTime(curTime))
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));
}


// Run the script at the exact moment that the minutes number changes
// Update the current time in the prose
$('#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)
var timeToNextMinute = 60000 - curTime.getTime() % 60000;
setTimeout(updateTimes, timeToNextMinute);
}
$(updateTimes);

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);