From 45c626bbd291a204456415e79dae9021ff93892c Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Tue, 13 Oct 2026 12:33:19 -0400 Subject: [PATCH] feat(journals): cap in-world date at present and add "days since awakening" subtitle - Reject in-world dates after the current in-game date (now + 47,326 days) - Default the date picker to the present in-universe date when composing - Show a subtitle with the formatted date and days since the Awakening --- src/app/(frontend)/journals/actions.ts | 21 ++++++- .../frontend/journals/JournalFormDialog.tsx | 63 ++++++++++++++++++- 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/src/app/(frontend)/journals/actions.ts b/src/app/(frontend)/journals/actions.ts index 9aa8e2fa..d1b4c1bb 100644 --- a/src/app/(frontend)/journals/actions.ts +++ b/src/app/(frontend)/journals/actions.ts @@ -16,6 +16,18 @@ import type { Journal } from "@/payload-types"; */ const MIN_OCCURRENCE_DATE = Date.parse("2156-03-19T00:00:00.000Z"); +/** + * Days the in-game clock runs ahead of real time. The latest allowed in-world + * journal date is exactly this many days from the real "now", so the picker + * never lets an author write about the in-game future. + */ +const MAX_OCCURRENCE_OFFSET_DAYS = 47_326; + +/** Latest allowed in-world date as an instant: real now + the game's offset. */ +function maxOccurrenceInstant(): number { + return Date.now() + MAX_OCCURRENCE_OFFSET_DAYS * 86_400_000; +} + export interface ActionResult { success: boolean; error?: string; @@ -54,8 +66,13 @@ function validate(input: JournalInput): string | null { } if (input.occurredAt) { const occurred = Date.parse(input.occurredAt); - if (!Number.isNaN(occurred) && occurred < MIN_OCCURRENCE_DATE) { - return "The in-world date cannot be before March 19, 2156."; + if (!Number.isNaN(occurred)) { + if (occurred < MIN_OCCURRENCE_DATE) { + return "The in-world date cannot be before March 19, 2156."; + } + if (occurred > maxOccurrenceInstant()) { + return "The in-world date cannot be after the current in-game date."; + } } } return null; diff --git a/src/components/frontend/journals/JournalFormDialog.tsx b/src/components/frontend/journals/JournalFormDialog.tsx index b9bda3df..0ba73987 100644 --- a/src/components/frontend/journals/JournalFormDialog.tsx +++ b/src/components/frontend/journals/JournalFormDialog.tsx @@ -29,6 +29,48 @@ import { } from "@/components/frontend/intelligence/LexicalRichTextEditor"; import { createJournal, updateJournal } from "@/app/(frontend)/journals/actions"; +/** + * Days the in-game clock runs ahead of real time. The in-world date picker is + * capped exactly this many days from "now", so it lines up with the game's + * present and never lets an author pick a date in the in-game future. + */ +export const MAX_OCCURRENCE_OFFSET_DAYS = 47_326; + +/** Format a Date as a local yyyy-MM-dd string (the value form). */ +function toYMD(date: Date): string { + const y = date.getFullYear(); + const m = String(date.getMonth() + 1).padStart(2, "0"); + const d = String(date.getDate()).padStart(2, "0"); + return `${y}-${m}-${d}`; +} + +/** + * The Awakening: the campaign's start date (2156-03-19). In-world dates are + * measured from here, so the subtitle can say how many days in-universe it has + * been since it all began. + */ +const AWAKENING_EPOCH = Date.parse("2156-03-19T00:00:00.000Z"); + +/** + * Format a yyyy-MM-dd in-universe date as a locale string (e.g. "May 10, 2156"). + * Built in local time so formatting never shifts the calendar day. + */ +function formatInWorldDate(ymd: string): string { + const [y, m, d] = ymd.split("-").map(Number); + return new Intl.DateTimeFormat("en-US", { + month: "long", + day: "numeric", + year: "numeric", + }).format(new Date(y, m - 1, d)); +} + +/** Whole calendar days between a yyyy-MM-dd date and the Awakening. */ +function daysSinceAwakening(ymd: string): number { + const [y, m, d] = ymd.split("-").map(Number); + const occurredUTC = Date.UTC(y, m - 1, d); + return Math.round((occurredUTC - AWAKENING_EPOCH) / 86_400_000); +} + export interface MissionOption { id: number; label: string; @@ -69,9 +111,12 @@ export function JournalFormDialog({ const router = useRouter(); const isEdit = typeof draft.id === "number"; + // Latest selectable in-world date: real now + the game's clock offset. + const maxAttr = toYMD(new Date(Date.now() + MAX_OCCURRENCE_OFFSET_DAYS * 86_400_000)); + const [title, setTitle] = React.useState(draft.title); const [status, setStatus] = React.useState<"draft" | "published">(draft.status); - const [occurredAt, setOccurredAt] = React.useState(draft.occurredAt); + const [occurredAt, setOccurredAt] = React.useState(draft.occurredAt || maxAttr); const [missionId, setMissionId] = React.useState( draft.missionId === null ? "none" : String(draft.missionId), ); @@ -83,11 +128,21 @@ export function JournalFormDialog({ if (!open) return; setTitle(draft.title); setStatus(draft.status); - setOccurredAt(draft.occurredAt); + setOccurredAt(draft.occurredAt || maxAttr); setMissionId(draft.missionId === null ? "none" : String(draft.missionId)); setBody(draft.body); }, [open, draft]); + // Subtitle under the date picker: locale date + days since the Awakening. + // Pre-Awakening dates (older seed data) can't be selected in the picker, so + // show just the locale date rather than a negative "days since" count. + const daysSince = occurredAt ? daysSinceAwakening(occurredAt) : null; + const sinceAwakening = + daysSince !== null && daysSince >= 0 + ? ` - ${daysSince} ${daysSince === 1 ? "day" : "days"} since awakening` + : ""; + const dateSubtitle = occurredAt ? `${formatInWorldDate(occurredAt)}${sinceAwakening}` : null; + const handleSubmit = async () => { setSaving(true); const input = { @@ -156,9 +211,13 @@ export function JournalFormDialog({ id="journal-date" type="date" min="2156-03-19" + max={maxAttr} value={occurredAt} onChange={(e) => setOccurredAt(e.target.value)} /> + {dateSubtitle && ( +

{dateSubtitle}

+ )}