From 2c27cd1b56f9f26636fb69f8c53220da7c5c6abd Mon Sep 17 00:00:00 2001 From: AMelonInsideLemon <166175391+AMelonInsideLemon@users.noreply.github.com> Date: Mon, 7 Apr 2025 10:43:17 +0200 Subject: [PATCH] set sortie timestamps using getSortieTime Co-Authored-By: Sainan <63328889+Sainan@users.noreply.github.com> --- .../dynamic/worldStateController.ts | 26 ++++++++++---- src/helpers/worlstateHelper.ts | 36 +++++++------------ 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/src/controllers/dynamic/worldStateController.ts b/src/controllers/dynamic/worldStateController.ts index 70b29b36..2be65e3a 100644 --- a/src/controllers/dynamic/worldStateController.ts +++ b/src/controllers/dynamic/worldStateController.ts @@ -11,7 +11,8 @@ import { config } from "@/src/services/configService"; import { CRng } from "@/src/services/rngService"; import { ExportNightwave, ExportRegions } from "warframe-public-export-plus"; import { - getTorontoTimeAtHour, + EPOCH, + getSortieTime, missionTags, sortieBosses, sortieBossNode, @@ -20,8 +21,6 @@ import { sortieFactionToSystemIndexes } from "@/src/helpers/worlstateHelper"; -const EPOCH = 1734307200 * 1000; // Monday, Dec 16, 2024 @ 00:00 UTC+0; should logically be winter in 1999 iteration 0 - export const worldStateController: RequestHandler = (req, res) => { const day = Math.trunc((Date.now() - EPOCH) / 86400000); const week = Math.trunc(day / 7); @@ -166,10 +165,23 @@ export const worldStateController: RequestHandler = (req, res) => { // Sortie cycling every day { - const dayStart = getTorontoTimeAtHour(EPOCH + day * 86400000, 12); - const dayEnd = getTorontoTimeAtHour(EPOCH + (day + 1) * 86400000, 12); + let genDay; + let dayStart; + let dayEnd; + const sortieRolloverToday = getSortieTime(day); + if (Date.now() < sortieRolloverToday) { + // Early in the day, generate sortie for `day - 1`, expiring at `sortieRolloverToday`. + genDay = day - 1; + dayStart = getSortieTime(genDay); + dayEnd = sortieRolloverToday; + } else { + // Late in the day, generate sortie for `day`, expiring at `getSortieTime(day + 1)`. + genDay = day; + dayStart = getSortieTime(genDay); + dayEnd = getSortieTime(day + 1); + } - const rng = new CRng(day); + const rng = new CRng(genDay); const boss = rng.randomElement(sortieBosses); @@ -281,7 +293,7 @@ export const worldStateController: RequestHandler = (req, res) => { Activation: { $date: { $numberLong: dayStart.toString() } }, Expiry: { $date: { $numberLong: dayEnd.toString() } }, Reward: "/Lotus/Types/Game/MissionDecks/SortieRewards", - Seed: day, + Seed: genDay, Boss: boss, Variants: selectedNodes }); diff --git a/src/helpers/worlstateHelper.ts b/src/helpers/worlstateHelper.ts index e6d83b74..4501d4d1 100644 --- a/src/helpers/worlstateHelper.ts +++ b/src/helpers/worlstateHelper.ts @@ -113,28 +113,18 @@ export const sortieBossNode: Record = { SORTIE_BOSS_INFALAD: "SolNode705" }; -// ChatGPT generated that. And seems it works fine -export const getTorontoTimeAtHour = (date: number, hour: number): number => { - const formatter = new Intl.DateTimeFormat("en-US", { +export const EPOCH = 1734307200 * 1000; // Monday, Dec 16, 2024 @ 00:00 UTC+0; should logically be winter in 1999 iteration 0 + +export const getSortieTime = (day: number): number => { + const dayStart = EPOCH + day * 86400000; + const date = new Date(dayStart); + date.setUTCHours(12); + const isDst = new Intl.DateTimeFormat("en-US", { timeZone: "America/Toronto", - year: "numeric", - month: "2-digit", - day: "2-digit" - }); - - const parts = formatter.formatToParts(date).reduce>((acc, { type, value }) => { - if (["year", "month", "day"].includes(type)) { - acc[type] = value; - } - return acc; - }, {}); - - const assumedLocalDate = new Date( - `${parts.year}-${parts.month}-${parts.day}T${hour.toString().padStart(2, "0")}:00:00` - ); - - const localDate = new Date(assumedLocalDate.toLocaleString("en-US", { timeZone: "America/Toronto" })); - const offsetMs = assumedLocalDate.getTime() - localDate.getTime(); - - return assumedLocalDate.getTime() + offsetMs; + timeZoneName: "short" + }) + .formatToParts(date) + .find(part => part.type === "timeZoneName")! + .value.includes("DT"); + return dayStart + (isDst ? 16 : 17) * 3600000; };