[ui] DaySentence: Her folded petal, the words and their source; the library bundled after validation
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
<script lang="ts">
|
||||
import { careEvent, localDay, pour, roseAt, ROSE, type Mood, type RoseNow, type Stage } from '@rosea/engine';
|
||||
import type { CareEvent } from '@rosea/schema';
|
||||
import {
|
||||
careEvent, goodMoment, localDay, petalAt, pour, roseAt, ROSE, sentenceOpened,
|
||||
type Mood, type PetalNow, type Place, type RoseNow, type Stage
|
||||
} from '@rosea/engine';
|
||||
import type { CareEvent, SentenceOpened } from '@rosea/schema';
|
||||
import { en } from '../copy/en.js';
|
||||
import KeepSafe from './KeepSafe.svelte';
|
||||
import Petal from './Petal.svelte';
|
||||
import type { RoseWiring } from './types.js';
|
||||
// The art board's stand-ins until real art is accepted (rose.md R10, docs/ART.md).
|
||||
import bud from '../../../../art/placeholders/rose-bud.svg?url';
|
||||
@@ -19,7 +23,7 @@
|
||||
* The rose. Everything she is comes from the engine's replay of her care log
|
||||
* (rose.md); this renders it and turns the player's hands into care events.
|
||||
*/
|
||||
let { wiring, timeZone }: { wiring: RoseWiring; timeZone: string } = $props();
|
||||
let { wiring, timeZone, place = null }: { wiring: RoseWiring; timeZone: string; place?: Place | null } = $props();
|
||||
|
||||
const STAGE_PICTURE: Record<Stage, string> = { bud, opening, 'half-bloom': halfBloom, 'full-bloom': fullBloom };
|
||||
const MOOD_PICTURE: Record<Mood, string> = { content: fullBloom, thirsty, 'too-full': overwatered, windblown, blushing, waiting };
|
||||
@@ -29,18 +33,23 @@
|
||||
let pastKeys = $state.raw<readonly string[]>([]);
|
||||
let showSafe = $state(false);
|
||||
let events = $state.raw<readonly CareEvent[]>([]);
|
||||
/** Her opened petals (day-sentence.md). */
|
||||
let opened = $state.raw<readonly SentenceOpened[]>([]);
|
||||
let now = $state(0);
|
||||
let kept = $state(true);
|
||||
let showNumbers = $state(false);
|
||||
/** The petal was opened at the good moment in this visit: she sends the player out to the sky. */
|
||||
let lookUp = $state(false);
|
||||
|
||||
$effect(() => {
|
||||
const w = wiring;
|
||||
now = w.now();
|
||||
Promise.all([w.identity(), w.loadCare(), w.pastKeys()]).then(
|
||||
([id, log, past]) => {
|
||||
Promise.all([w.identity(), w.loadCare(), w.pastKeys(), w.loadOpened()]).then(
|
||||
([id, log, past, petals]) => {
|
||||
seed = id.publicKey;
|
||||
events = log;
|
||||
pastKeys = past;
|
||||
opened = petals;
|
||||
if (!id.kept) kept = false;
|
||||
},
|
||||
(error: unknown) => {
|
||||
@@ -54,13 +63,28 @@
|
||||
});
|
||||
|
||||
const rose = $derived<RoseNow | null>(seed === null ? null : roseAt(events, [seed, ...pastKeys], now, timeZone));
|
||||
const petal = $derived<PetalNow>(seed === null ? { kind: 'none' } : petalAt(wiring.sentences, opened, [seed, ...pastKeys], now, timeZone));
|
||||
const sunIsLow = $derived(place !== null && now > 0 && goodMoment(now, place));
|
||||
|
||||
function openPetal(): void {
|
||||
if (seed === null || petal.kind !== 'folded') return;
|
||||
now = wiring.now();
|
||||
const event = sentenceOpened({ now, timeZone, from: seed, messageId: petal.id, clientVersion: wiring.clientVersion });
|
||||
lookUp = place !== null && goodMoment(now, place);
|
||||
opened = [...opened, event];
|
||||
wiring.appendOpened(event).catch((error: unknown) => {
|
||||
console.error('rose: the device could not keep the opened petal', error);
|
||||
kept = false;
|
||||
});
|
||||
}
|
||||
|
||||
/** After she is brought back from a file: read her log and keys again. */
|
||||
function restored(): void {
|
||||
Promise.all([wiring.loadCare(), wiring.pastKeys()]).then(
|
||||
([log, past]) => {
|
||||
Promise.all([wiring.loadCare(), wiring.pastKeys(), wiring.loadOpened()]).then(
|
||||
([log, past, petals]) => {
|
||||
events = log;
|
||||
pastKeys = past;
|
||||
opened = petals;
|
||||
now = wiring.now();
|
||||
},
|
||||
(error: unknown) => console.error('rose: could not read her back after restoring', error)
|
||||
@@ -116,7 +140,10 @@
|
||||
: rose.needs.weeds ? s.weeds
|
||||
: rose.needs.petals ? s.petals
|
||||
: '';
|
||||
return [rose.drift > 0 ? s.back : '', need].filter(Boolean).join(' ');
|
||||
// The petal only when she needs nothing else: one line at a time (day-sentence.md §1).
|
||||
const p = en.rose.petal;
|
||||
const offer = need !== '' ? '' : petal.kind === 'folded' ? (sunIsLow ? p.goodMoment : p.waiting) : petal.kind === 'open' && lookUp ? p.lookUp : '';
|
||||
return [rose.drift > 0 ? s.back : '', need || offer].filter(Boolean).join(' ');
|
||||
});
|
||||
|
||||
const picture = $derived(rose ? (rose.stage === 'full-bloom' ? MOOD_PICTURE[rose.mood] : STAGE_PICTURE[rose.stage]) : bud);
|
||||
@@ -175,6 +202,8 @@
|
||||
{/if}
|
||||
<p class="said" role="status" aria-live="assertive">{pouring && can ? (can.tooFull ? en.rose.plenty : can.leavesLift ? en.rose.leavesLift : '') : ''}</p>
|
||||
|
||||
<Petal {petal} sentences={wiring.sentences} onopen={openPetal} />
|
||||
|
||||
{#if !kept}
|
||||
<p class="hint">{en.rose.notKept}</p>
|
||||
{/if}
|
||||
|
||||
Reference in New Issue
Block a user