fix: 1999 calendar not working properly #777

Merged
Sainan merged 2 commits from 1999-calendar into main 2025-01-12 19:17:06 -08:00

View File

@ -19,9 +19,10 @@ export const worldStateController: RequestHandler = (req, res) => {
...staticWorldState ...staticWorldState
}; };
const day = Math.trunc(new Date().getTime() / 86400000); const EPOCH = 1734307200 * 1000; // Monday, Dec 16, 2024 @ 00:00 UTC+0; should logically be winter in 1999 iteration 0
const week = Math.trunc((day + 3) / 7); // week begins on mondays const day = Math.trunc((new Date().getTime() - EPOCH) / 86400000);
const weekStart = week * 604800000; const week = Math.trunc(day / 7);
const weekStart = EPOCH + week * 604800000;
const weekEnd = weekStart + 604800000; const weekEnd = weekStart + 604800000;
// Elite Sanctuary Onslaught cycling every week // Elite Sanctuary Onslaught cycling every week
coderabbitai[bot] commented 2025-01-12 18:42:26 -08:00 (Migrated from github.com)
Review

⚠️ Potential issue

Critical: EPOCH set in future will cause negative values

Setting EPOCH to December 2024 is problematic as it will cause negative day/week values for all dates before then, potentially breaking the 1999 calendar functionality.

Consider these changes:

  1. Set EPOCH to a date in or before 1999 to ensure positive day/week values
  2. Add validation for dates before EPOCH
  3. Extract magic numbers into named constants
-    const EPOCH = 1734307200 * 1000; // Monday, Dec 16, 2024 @ 00:00 UTC+0; should logically be winter in 1999 iteration 0
+    const MS_PER_DAY = 86400000;
+    const MS_PER_WEEK = 604800000;
+    const EPOCH = 915148800 * 1000; // January 1, 1999 @ 00:00 UTC+0
+    const currentTime = new Date().getTime();
+    if (currentTime < EPOCH) {
+        throw new Error('Current time cannot be before EPOCH');
+    }
-    const day = Math.trunc((new Date().getTime() - EPOCH) / 86400000);
-    const week = Math.trunc(day / 7);
-    const weekStart = EPOCH + week * 604800000;
-    const weekEnd = weekStart + 604800000;
+    const day = Math.trunc((currentTime - EPOCH) / MS_PER_DAY);
+    const week = Math.trunc(day / 7);
+    const weekStart = EPOCH + week * MS_PER_WEEK;
+    const weekEnd = weekStart + MS_PER_WEEK;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

    const MS_PER_DAY = 86400000;
    const MS_PER_WEEK = 604800000;
    const EPOCH = 915148800 * 1000; // January 1, 1999 @ 00:00 UTC+0
    const currentTime = new Date().getTime();
    if (currentTime < EPOCH) {
        throw new Error('Current time cannot be before EPOCH');
    }
    const day = Math.trunc((currentTime - EPOCH) / MS_PER_DAY);
    const week = Math.trunc(day / 7);
    const weekStart = EPOCH + week * MS_PER_WEEK;
    const weekEnd = weekStart + MS_PER_WEEK;
_:warning: Potential issue_ **Critical: EPOCH set in future will cause negative values** Setting EPOCH to December 2024 is problematic as it will cause negative day/week values for all dates before then, potentially breaking the 1999 calendar functionality. Consider these changes: 1. Set EPOCH to a date in or before 1999 to ensure positive day/week values 2. Add validation for dates before EPOCH 3. Extract magic numbers into named constants ```diff - const EPOCH = 1734307200 * 1000; // Monday, Dec 16, 2024 @ 00:00 UTC+0; should logically be winter in 1999 iteration 0 + const MS_PER_DAY = 86400000; + const MS_PER_WEEK = 604800000; + const EPOCH = 915148800 * 1000; // January 1, 1999 @ 00:00 UTC+0 + const currentTime = new Date().getTime(); + if (currentTime < EPOCH) { + throw new Error('Current time cannot be before EPOCH'); + } - const day = Math.trunc((new Date().getTime() - EPOCH) / 86400000); - const week = Math.trunc(day / 7); - const weekStart = EPOCH + week * 604800000; - const weekEnd = weekStart + 604800000; + const day = Math.trunc((currentTime - EPOCH) / MS_PER_DAY); + const week = Math.trunc(day / 7); + const weekStart = EPOCH + week * MS_PER_WEEK; + const weekEnd = weekStart + MS_PER_WEEK; ``` <!-- suggestion_start --> <details> <summary>📝 Committable suggestion</summary> > ‼️ **IMPORTANT** > Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements. `````suggestion const MS_PER_DAY = 86400000; const MS_PER_WEEK = 604800000; const EPOCH = 915148800 * 1000; // January 1, 1999 @ 00:00 UTC+0 const currentTime = new Date().getTime(); if (currentTime < EPOCH) { throw new Error('Current time cannot be before EPOCH'); } const day = Math.trunc((currentTime - EPOCH) / MS_PER_DAY); const week = Math.trunc(day / 7); const weekStart = EPOCH + week * MS_PER_WEEK; const weekEnd = weekStart + MS_PER_WEEK; ````` </details> <!-- suggestion_end --> <!-- This is an auto-generated comment by CodeRabbit -->
@ -87,7 +88,7 @@ export const worldStateController: RequestHandler = (req, res) => {
][week % 8] ][week % 8]
}); });
// 1999 Calendar Season cycling every week // 1999 Calendar Season cycling every week + YearIteration every 4 weeks
worldState.KnownCalendarSeasons[0].Activation = { $date: { $numberLong: weekStart.toString() } }; worldState.KnownCalendarSeasons[0].Activation = { $date: { $numberLong: weekStart.toString() } };
worldState.KnownCalendarSeasons[0].Expiry = { $date: { $numberLong: weekEnd.toString() } }; worldState.KnownCalendarSeasons[0].Expiry = { $date: { $numberLong: weekEnd.toString() } };
worldState.KnownCalendarSeasons[0].Season = ["CST_WINTER", "CST_SPRING", "CST_SUMMER", "CST_FALL"][week % 4]; worldState.KnownCalendarSeasons[0].Season = ["CST_WINTER", "CST_SPRING", "CST_SUMMER", "CST_FALL"][week % 4];
@ -97,6 +98,7 @@ export const worldStateController: RequestHandler = (req, res) => {
static1999SummerDays, static1999SummerDays,
static1999FallDays static1999FallDays
][week % 4]; ][week % 4];
worldState.KnownCalendarSeasons[0].YearIteration = Math.trunc(week / 4);
// Sentient Anomaly cycling every 30 minutes // Sentient Anomaly cycling every 30 minutes
const halfHour = Math.trunc(new Date().getTime() / (unixTimesInMs.hour / 2)); const halfHour = Math.trunc(new Date().getTime() / (unixTimesInMs.hour / 2));
@ -174,4 +176,5 @@ interface ICalendarSeason {
Days: { Days: {
day: number; day: number;
}[]; }[];
YearIteration: number;
} }