Advertisement
countrymac1977

Rocket League Ranked Tournament Countdown Timer

Mar 30th, 2021 (edited)
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. Rocket League Ranked Tournament Countdown Timer
  4. by t00much#8965 and Country Mac#3820 on Discord
  5.  
  6. This script is intended for use as a Nightbot Twitch chat command to display a countdown of the
  7. next scheduled Rocket League RankedTournament. This script only works for US tournaments with
  8. the time zone defaulted to  Pacific. If you need to change the time zone, change the `timeZone`
  9. variable in the Nightbot command to either 'East', 'Central', or 'Mountain'.
  10.  
  11. Nightbot command: !addcom !tt /me $(eval var timeZone='West', user=`$(1)`;$(urlfetch json
  12. https://pastebin.com/raw/fgEYW0m8))
  13.  
  14. */
  15. var username = (user === 'null') ? '' : user;
  16. var t = timeZone.toLowerCase();
  17.  
  18. Date.prototype.stdTimezoneOffset = function() {
  19.     var jan = new Date(this.getFullYear(), 0, 1);
  20.     var jul = new Date(this.getFullYear(), 6, 1);
  21.     return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
  22. };
  23. Date.prototype.isDstObserved = function() {
  24.     return this.getTimezoneOffset() < this.stdTimezoneOffset();
  25. };
  26. var today = new Date();
  27. var DT_or_ST = (today.isDstObserved()) ? 'DT' : 'ST';
  28.  
  29. if (t === 'east') {
  30.     var TZ = 12,
  31.         TZ_diff = 0,
  32.         TZ_Str = 'E' + DT_or_ST;
  33. } else if (t === 'central') {
  34.     var TZ = 13,
  35.         TZ_diff = 1,
  36.         TZ_Str = 'C' + DT_or_ST;
  37. } else if (t === 'mountain') {
  38.     var TZ = 14,
  39.         TZ_diff = 2,
  40.         TZ_Str = 'M' + DT_or_ST;
  41. } else if (t === 'west')
  42.     var TZ = 15,
  43.         TZ_diff = 3,
  44.         TZ_Str = 'P' + DT_or_ST;
  45. else {
  46.     throw new Error("Invalid US time zone. Nightbot command timeZone variable must be either:
  47.     'east', 'central', 'mountain', or 'pacific' NotLikeThis")
  48. }
  49.  
  50. const timeTil = h => {
  51.     let d = new Date();
  52.     d.setHours(h, 0, 0, 0);
  53.     return (d - Date.now());
  54. };
  55.  
  56. const formatTimeTil = q => {
  57.     let h, m, s;
  58.     h = Math.floor(q / 1000 / 60 / 60);
  59.     m = Math.floor((q / 1000 / 60 / 60 - h) * 60);
  60.     s = Math.floor(((q / 1000 / 60 / 60 - h) * 60 - m) * 60);
  61.     if (m == 0 && h == 0) return `${s} second${s>1 ? 's' : ''}`;
  62.     else if (h == 0) return `${m} minute${m>1 ? 's' : ''}, ${s} second${s>1 ? 's' : ''}`;
  63.     else if (m == 0) return `${h} hour${h>1 ? 's' : ''}, ${s} second${s>1 ? 's' : ''}`;
  64.     else if (s == 0) return `${h} hour${h>1 ? 's' : ''}, ${m} minute${m>1 ? 's' : ''}`;
  65.     else return `${h} hour${h>1 ? 's' : ''}, ${m} minute${m>1 ? 's' : ''}, ${s}
  66.          second${s>1 ? 's' : ''}`;
  67. };
  68.  
  69. const getTimes = _ => {
  70.     let times = [18, 21],
  71.         now = new Date();
  72.     if ([0, 6].includes(now.getDay)) times.push([12, 15]);
  73.     return times;
  74. };
  75.  
  76. const getClosestTime = _ => {
  77.     let closestTime = getTimes().map(t => ({
  78.         hour: t,
  79.         diff: timeTil(t)
  80.     })).reduce((acc, cur) => {
  81.         return (cur.diff < acc.diff && cur.diff > 0) ? cur : acc;
  82.     }, {
  83.         hour: 24,
  84.         diff: timeTil(24)
  85.     });
  86.     let today = new Date();
  87.     let AoP = (closestTime.hour > 11 && closestTime.hour != 0) ? 'pm' : 'am';
  88.     return `The next tournament starts in ${formatTimeTil(closestTime.diff)} (${(closestTime.hour
  89.     > 12) ? closestTime.hour - TZ : closestTime.hour - TZ_diff} ${AoP} ${TZ_Str}) ${username}`;
  90. };
  91. getClosestTime();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement