[engine] Sun: nextInvitation checks yesterday, today and tomorrow; addDays steps the calendar; NamedPlace
This commit is contained in:
@@ -1,2 +1,2 @@
|
||||
export { isLocalDay, localDay, type LocalDay } from './time/localDay.js';
|
||||
export { invitation, sunDay, type Place, type SunDay } from './sun/sun.js';
|
||||
export { addDays, isLocalDay, localDay, type LocalDay } from './time/localDay.js';
|
||||
export { invitation, nextInvitation, sunDay, type NamedPlace, type Place, type SunDay } from './sun/sun.js';
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* (.agents/features/sun.md D3). The place is a city the player chose, or a guess
|
||||
* from the device's zone: never GPS (CLAUDE.md §5.6 rule 6).
|
||||
*/
|
||||
import { isLocalDay, localDay, type LocalDay } from '../time/localDay.js';
|
||||
import { addDays, isLocalDay, localDay, type LocalDay } from '../time/localDay.js';
|
||||
|
||||
export interface Place {
|
||||
/** Degrees, north positive. */
|
||||
@@ -22,6 +22,14 @@ export interface Place {
|
||||
readonly timeZone: string;
|
||||
}
|
||||
|
||||
/** A place with the name the player knows it by: a town they chose, or the guess. */
|
||||
export interface NamedPlace extends Place {
|
||||
readonly name: string;
|
||||
/** Province, state or region, or ''. */
|
||||
readonly region: string;
|
||||
readonly country: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* One local day of sun. Sunrise and sunset are the ones around this day's solar
|
||||
* noon, so tonight's sunset may fall after midnight, on the next local day (D2).
|
||||
@@ -134,6 +142,23 @@ export function sunDay(day: LocalDay, place: Place): SunDay {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The next invitation at or after `now`, and the sun's day it belongs to. Yesterday
|
||||
* is checked first: just after midnight in Reykjavík in June, last evening's sunset
|
||||
* is still a few minutes away (D2).
|
||||
*/
|
||||
export function nextInvitation(now: number, place: Place): { readonly sun: SunDay; readonly at: number } {
|
||||
if (!Number.isFinite(now)) throw new RangeError(`nextInvitation: now must be a finite instant, got ${String(now)}`);
|
||||
const today = localDay(now, place.timeZone);
|
||||
for (const offset of [-1, 0, 1, 2]) {
|
||||
const sun = sunDay(addDays(today, offset), place);
|
||||
const at = invitation(sun);
|
||||
if (at >= now) return { sun, at };
|
||||
}
|
||||
// Unreachable: tomorrow's invitation is always after now. Kept so a broken sun fails loudly.
|
||||
throw new Error(`nextInvitation: no invitation within two days of ${today}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* When the game invites the player outside: the sunset; in polar day the lowest
|
||||
* light; in polar night noon, when the twilight is brightest (D4; decided by the
|
||||
|
||||
@@ -61,6 +61,14 @@ export function localDay(epochMs: number, timeZone: string): LocalDay {
|
||||
return day;
|
||||
}
|
||||
|
||||
/** The calendar day `days` after `day` (before, if negative). A calendar step: no zone, no DST. */
|
||||
export function addDays(day: LocalDay, days: number): LocalDay {
|
||||
if (!isLocalDay(day)) throw new RangeError(`addDays: '${day}' is not a LocalDay`);
|
||||
if (!Number.isInteger(days)) throw new RangeError(`addDays: ${days} is not a whole number of days`);
|
||||
const [y, m, d] = day.split('-').map(Number) as [number, number, number];
|
||||
return localDay(Date.UTC(y, m - 1, d + days), 'UTC');
|
||||
}
|
||||
|
||||
/** Whether `value` has the shape of a LocalDay. Does not check that the date exists. */
|
||||
export function isLocalDay(value: string): value is LocalDay {
|
||||
return LOCAL_DAY.test(value);
|
||||
|
||||
Reference in New Issue
Block a user