304 lines
9.5 KiB
Svelte
304 lines
9.5 KiB
Svelte
<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 { en } from '../copy/en.js';
|
|
import KeepSafe from './KeepSafe.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';
|
|
import opening from '../../../../art/placeholders/rose-opening.svg?url';
|
|
import halfBloom from '../../../../art/placeholders/rose-half-bloom.svg?url';
|
|
import fullBloom from '../../../../art/placeholders/rose-full-bloom.svg?url';
|
|
import thirsty from '../../../../art/placeholders/rose-thirsty.svg?url';
|
|
import overwatered from '../../../../art/placeholders/rose-overwatered.svg?url';
|
|
import windblown from '../../../../art/placeholders/rose-windblown.svg?url';
|
|
import blushing from '../../../../art/placeholders/rose-blushing.svg?url';
|
|
import waiting from '../../../../art/placeholders/rose-waiting.svg?url';
|
|
|
|
/**
|
|
* 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();
|
|
|
|
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 };
|
|
|
|
let seed = $state<string | null>(null);
|
|
/** Keys she was named by on another device (keep-her-safe.md K2). */
|
|
let pastKeys = $state.raw<readonly string[]>([]);
|
|
let showSafe = $state(false);
|
|
let events = $state.raw<readonly CareEvent[]>([]);
|
|
let now = $state(0);
|
|
let kept = $state(true);
|
|
let showNumbers = $state(false);
|
|
|
|
$effect(() => {
|
|
const w = wiring;
|
|
now = w.now();
|
|
Promise.all([w.identity(), w.loadCare(), w.pastKeys()]).then(
|
|
([id, log, past]) => {
|
|
seed = id.publicKey;
|
|
events = log;
|
|
pastKeys = past;
|
|
if (!id.kept) kept = false;
|
|
},
|
|
(error: unknown) => {
|
|
console.error('rose: could not read her care log', error);
|
|
kept = false;
|
|
}
|
|
);
|
|
// Wind comes and goes, and a blush fades: look again now and then.
|
|
const timer = setInterval(() => (now = w.now()), 30_000);
|
|
return () => clearInterval(timer);
|
|
});
|
|
|
|
const rose = $derived<RoseNow | null>(seed === null ? null : roseAt(events, [seed, ...pastKeys], now, timeZone));
|
|
|
|
/** 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]) => {
|
|
events = log;
|
|
pastKeys = past;
|
|
now = wiring.now();
|
|
},
|
|
(error: unknown) => console.error('rose: could not read her back after restoring', error)
|
|
);
|
|
}
|
|
|
|
function care(act: CareEvent['act'], amount?: number): void {
|
|
if (seed === null) return;
|
|
now = wiring.now();
|
|
const event = careEvent({ now, timeZone, from: seed, to: seed, act, clientVersion: wiring.clientVersion, ...(amount === undefined ? {} : { amount }) });
|
|
events = [...events, event];
|
|
wiring.appendCare(event).catch((error: unknown) => {
|
|
console.error('rose: the device could not keep this care', error);
|
|
kept = false;
|
|
});
|
|
}
|
|
|
|
// --- Water by feel: hold to pour (GAMEPLAY §1) ---------------------------
|
|
/** A full can in this long, held. */
|
|
const FULL_CAN_MS = 3000;
|
|
const TICK_MS = 50;
|
|
let amount = $state(0);
|
|
let pouring = $state(false);
|
|
let pourTimer: ReturnType<typeof setInterval> | undefined;
|
|
const can = $derived(rose ? pour(rose.water, amount) : null);
|
|
|
|
function startPour(): void {
|
|
if (pouring || rose === null) return;
|
|
pouring = true;
|
|
amount = 0;
|
|
pourTimer = setInterval(() => (amount = Math.min(1, amount + TICK_MS / FULL_CAN_MS)), TICK_MS);
|
|
}
|
|
function stopPour(): void {
|
|
if (!pouring) return;
|
|
clearInterval(pourTimer);
|
|
pouring = false;
|
|
if (amount > 0.01) care('water', amount);
|
|
amount = 0;
|
|
}
|
|
$effect(() => () => clearInterval(pourTimer));
|
|
|
|
const isPourKey = (e: KeyboardEvent): boolean => e.key === ' ' || e.key === 'Enter';
|
|
|
|
// --- What she says: one line, or nothing --------------------------------
|
|
const says = $derived.by(() => {
|
|
if (!rose) return '';
|
|
const s = en.rose.says;
|
|
if (rose.mood === 'blushing') return s.thanks;
|
|
if (rose.mood === 'too-full') return s.tooFull;
|
|
const need = rose.needs.wind ? s.wind
|
|
: rose.water < ROSE.thirsty ? s.thirsty
|
|
: rose.needs.water ? s.dry
|
|
: rose.needs.weeds ? s.weeds
|
|
: rose.needs.petals ? s.petals
|
|
: '';
|
|
return [rose.drift > 0 ? s.back : '', need].filter(Boolean).join(' ');
|
|
});
|
|
|
|
const picture = $derived(rose ? (rose.stage === 'full-bloom' ? MOOD_PICTURE[rose.mood] : STAGE_PICTURE[rose.stage]) : bud);
|
|
const alt = $derived(rose ? en.rose.picture(en.rose.stageWords[rose.stage], en.rose.moodWords[rose.mood]) : '');
|
|
const percent = (x: number): string => `${Math.round(Math.max(0, Math.min(1, x)) * 100)} %`;
|
|
</script>
|
|
|
|
{#if rose}
|
|
<section class="rose">
|
|
<figure>
|
|
<img src={picture} {alt} width="120" height="320" class:turned={rose.drift > 0} />
|
|
{#if says}
|
|
<figcaption class="bubble" aria-live="polite">{says}</figcaption>
|
|
{/if}
|
|
</figure>
|
|
|
|
<div class="care" role="group" aria-label={en.rose.care}>
|
|
<button
|
|
type="button"
|
|
class="water"
|
|
class:pouring
|
|
aria-describedby="rose-water-hint"
|
|
onpointerdown={(e) => {
|
|
e.preventDefault();
|
|
(e.currentTarget as HTMLElement).setPointerCapture?.(e.pointerId);
|
|
startPour();
|
|
}}
|
|
onpointerup={stopPour}
|
|
onpointercancel={stopPour}
|
|
onkeydown={(e) => {
|
|
if (!isPourKey(e)) return;
|
|
e.preventDefault();
|
|
if (!e.repeat) startPour();
|
|
}}
|
|
onkeyup={(e) => {
|
|
if (isPourKey(e)) stopPour();
|
|
}}
|
|
onblur={stopPour}
|
|
>{en.rose.water}</button>
|
|
{#if rose.needs.wind}
|
|
<button type="button" onclick={() => care('shade')}>{en.rose.shade}</button>
|
|
{/if}
|
|
{#if rose.needs.weeds}
|
|
<button type="button" onclick={() => care('weed')}>{en.rose.weed}</button>
|
|
{/if}
|
|
{#if rose.needs.petals}
|
|
<button type="button" onclick={() => care('petals')}>{en.rose.petals}</button>
|
|
{/if}
|
|
</div>
|
|
<p id="rose-water-hint" class="hint">{en.rose.waterHint}</p>
|
|
|
|
{#if pouring && can}
|
|
<div class="can">
|
|
<meter min="0" max="1.2" low={ROSE.enough} high={ROSE.tooFull} optimum="0.7" value={Math.min(1.2, can.after)} aria-label={en.rose.waterLevel}></meter>
|
|
</div>
|
|
{/if}
|
|
<p class="said" role="status" aria-live="assertive">{pouring && can ? (can.tooFull ? en.rose.plenty : can.leavesLift ? en.rose.leavesLift : '') : ''}</p>
|
|
|
|
{#if !kept}
|
|
<p class="hint">{en.rose.notKept}</p>
|
|
{/if}
|
|
|
|
<div class="more">
|
|
<button type="button" class="quiet" aria-expanded={showNumbers} aria-controls="rose-numbers" onclick={() => (showNumbers = !showNumbers)}>{en.rose.numbers}</button>
|
|
<button type="button" class="quiet" aria-expanded={showSafe} onclick={() => (showSafe = !showSafe)}>{en.rose.safe.title}</button>
|
|
</div>
|
|
{#if showNumbers}
|
|
<dl id="rose-numbers" class="numbers">
|
|
<dt>{en.rose.waterLevel}</dt><dd>{percent(rose.water)}</dd>
|
|
<dt>{en.rose.daysOfCare}</dt><dd>{rose.daysOfCare}</dd>
|
|
<dt>{en.rose.warmth}</dt><dd>{en.rose.warmthDays(rose.warmth)}</dd>
|
|
<dt>{en.rose.trend}</dt><dd>{en.rose.trendWords[rose.trend]}</dd>
|
|
</dl>
|
|
{/if}
|
|
{#if showSafe && seed}
|
|
<KeepSafe {wiring} currentKey={seed} today={localDay(now, timeZone)} onrestored={restored} />
|
|
{/if}
|
|
</section>
|
|
{/if}
|
|
|
|
<style>
|
|
.rose {
|
|
display: grid;
|
|
justify-items: center;
|
|
gap: var(--space-2);
|
|
}
|
|
|
|
figure {
|
|
margin: 0;
|
|
display: grid;
|
|
justify-items: center;
|
|
gap: var(--space-2);
|
|
}
|
|
|
|
img {
|
|
height: 13rem;
|
|
width: auto;
|
|
transition: transform 0.6s ease;
|
|
}
|
|
|
|
/* Drift is drawn as waiting: turned a little away, never hurt (CLAUDE.md §8). */
|
|
img.turned {
|
|
transform: rotate(-4deg) scaleX(-1);
|
|
}
|
|
|
|
.bubble {
|
|
max-width: 22rem;
|
|
padding: var(--space-2) var(--space-3);
|
|
border-radius: 1rem;
|
|
background: color-mix(in srgb, var(--rose) 12%, var(--paper));
|
|
}
|
|
|
|
.care {
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
justify-content: center;
|
|
gap: var(--space-2);
|
|
}
|
|
|
|
.care button {
|
|
font: inherit;
|
|
padding: var(--space-2) var(--space-3);
|
|
border-radius: 999px;
|
|
border: 1px solid var(--rose);
|
|
background: var(--paper);
|
|
color: var(--ink);
|
|
cursor: pointer;
|
|
touch-action: none;
|
|
user-select: none;
|
|
}
|
|
|
|
.care button.pouring {
|
|
background: color-mix(in srgb, var(--rose) 18%, var(--paper));
|
|
}
|
|
|
|
.hint,
|
|
.said {
|
|
margin: 0;
|
|
color: var(--ink-soft);
|
|
font-size: 0.9rem;
|
|
}
|
|
|
|
.said:empty {
|
|
display: none;
|
|
}
|
|
|
|
meter {
|
|
width: 12rem;
|
|
}
|
|
|
|
.quiet {
|
|
font: inherit;
|
|
background: none;
|
|
border: none;
|
|
color: var(--rose);
|
|
text-decoration: underline;
|
|
cursor: pointer;
|
|
}
|
|
|
|
.more {
|
|
display: flex;
|
|
gap: var(--space-3);
|
|
}
|
|
|
|
.numbers {
|
|
display: grid;
|
|
grid-template-columns: auto auto;
|
|
gap: var(--space-1) var(--space-3);
|
|
margin: 0;
|
|
}
|
|
|
|
dt {
|
|
color: var(--ink-soft);
|
|
}
|
|
|
|
dd {
|
|
margin: 0;
|
|
}
|
|
|
|
button:focus-visible {
|
|
outline: 2px solid var(--rose);
|
|
outline-offset: 2px;
|
|
}
|
|
</style>
|