86 lines
3.2 KiB
TypeScript
86 lines
3.2 KiB
TypeScript
/**
|
|
* The towns a player can choose for the sunset (.agents/features/sunset-place.md).
|
|
* Data: places.json, built by scripts/build-places.ts from Natural Earth and the
|
|
* IANA time zone database, both public domain. Everything here runs on the device;
|
|
* a search never leaves it.
|
|
*
|
|
* Large (about 400 KB): the app imports this module only when the search opens.
|
|
* The garden's guess lives in ./guess.ts, which does not load it.
|
|
*/
|
|
import data from './places.json' with { type: 'json' };
|
|
|
|
/** A town a player may choose. The only location Rosea ever knows (CLAUDE.md §5.6 rule 6). */
|
|
export interface Town {
|
|
readonly name: string;
|
|
/** The province, state or region, or '' when it adds nothing. */
|
|
readonly region: string;
|
|
readonly country: string;
|
|
readonly lat: number;
|
|
readonly lon: number;
|
|
readonly timeZone: string;
|
|
}
|
|
|
|
type Row = readonly [string, string, string, number, number, number, number, number];
|
|
const rows = data.places as unknown as readonly Row[];
|
|
|
|
const town = (r: Row): Town => ({
|
|
name: r[0],
|
|
region: r[2],
|
|
country: data.countries[r[3]] ?? '',
|
|
lat: r[4],
|
|
lon: r[5],
|
|
timeZone: data.zones[r[6]] ?? 'UTC'
|
|
});
|
|
|
|
/** Lower case, no accents, words separated by single spaces: "Plzeň" and "plzen" meet. */
|
|
export function normalize(s: string): string {
|
|
return s.normalize('NFD').replace(/\p{M}/gu, '').toLowerCase().replace(/[^\p{L}\p{N}]+/gu, ' ').trim();
|
|
}
|
|
|
|
let index: { names: string[]; others: string[]; where: string[] } | undefined;
|
|
function searchIndex(): NonNullable<typeof index> {
|
|
index ??= {
|
|
names: rows.map((r) => normalize(r[0])),
|
|
others: rows.map((r) => normalize(r[1].replace(/\|/g, ' | '))),
|
|
where: rows.map((r) => normalize(`${r[2]} ${data.countries[r[3]] ?? ''}`))
|
|
};
|
|
return index;
|
|
}
|
|
|
|
/** How well a town's names match: lower is better, Infinity is no match. */
|
|
function score(q: string, name: string, others: string): number {
|
|
if (name === q) return 0;
|
|
if (name.startsWith(q)) return 1;
|
|
if (name.includes(` ${q}`)) return 2;
|
|
if (others.split(' | ').some((o) => o === q)) return 3;
|
|
if (others.split(' | ').some((o) => o.startsWith(q) || o.includes(` ${q}`))) return 4;
|
|
return Infinity;
|
|
}
|
|
|
|
/**
|
|
* Towns matching what the player typed, best first; among equals, the larger town
|
|
* first. "Sydney, Canada" narrows by region or country after a comma.
|
|
*/
|
|
export function searchTowns(query: string, limit = 8): Town[] {
|
|
const [namePart = '', wherePart = ''] = query.split(',');
|
|
const q = normalize(namePart);
|
|
const w = normalize(wherePart);
|
|
if (q === '') return [];
|
|
const { names, others, where } = searchIndex();
|
|
const hits: { i: number; s: number }[] = [];
|
|
for (let i = 0; i < rows.length; i++) {
|
|
const s = score(q, names[i]!, others[i]!);
|
|
if (s === Infinity) continue;
|
|
if (w && !where[i]!.split(' ').some((word) => word.startsWith(w.split(' ')[0]!)) && !where[i]!.includes(w)) continue;
|
|
hits.push({ i, s });
|
|
}
|
|
// Rows are stored largest first, so sorting by score alone keeps size order within a score.
|
|
hits.sort((a, b) => a.s - b.s || a.i - b.i);
|
|
return hits.slice(0, limit).map((h) => town(rows[h.i]!));
|
|
}
|
|
|
|
/** Every town, for checks over the whole list. */
|
|
export function allTowns(): Town[] {
|
|
return rows.map(town);
|
|
}
|