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
This commit is contained in:
parent
b1dbf5569c
commit
45c626bbd2
2 changed files with 80 additions and 4 deletions
|
|
@ -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<T = undefined> {
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -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 <input type="date"> 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<string>(
|
||||
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 && (
|
||||
<p className="text-xs text-muted-foreground">{dateSubtitle}</p>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex flex-col gap-1.5">
|
||||
<Label>Related mission</Label>
|
||||
|
|
|
|||
Loading…
Reference in a new issue