Back to Find the Invisible Cow
SOURCE / PINNED RELEASE

Made of little things.

Find the Invisible Cow

Release
e48b3dced31c…
Author-recorded commit
cf79302f143d…
License
LICENSE
Author’s source reference
nostr://npub1p8rxwea4e0l4jml0q53n5mvh3n7pywq523jk3lm336v04r8ny9jqtq7ttf/wss%3A%2F%2Fgit.napplet.soy%2F/n-3b179f1d7df

Archive hash verified: f8e300eb672b5ee8…. The source-to-build association is the author’s claim; it has not been independently rebuilt.

src/napplet-settings.ts
import { config } from '@napplet/sdk';
import { runtimeHasDomain } from './domain-availability.js';
import settingsSchema from '../config.schema.json';

/** Player-facing assistance settings (NAP-CONFIG). Both have in-app defaults,
 * because config is an optional domain. */
export interface GameSettings {
  hintMode: 'off' | 'subtle' | 'full';
  catchSize: 'normal' | 'large';
}

export const defaultSettings: GameSettings = {
  hintMode: 'off',
  catchSize: 'normal',
};

const HINT_MODES = new Set(['off', 'subtle', 'full']);
const CATCH_SIZES = new Set(['normal', 'large']);

type Listener = (settings: GameSettings) => void;

const listeners = new Set<Listener>();
let current: GameSettings = { ...defaultSettings };

export function getSettings(): GameSettings {
  return current;
}

export function onSettingsChange(listener: Listener): () => void {
  listeners.add(listener);
  return () => listeners.delete(listener);
}

function apply(values: Record<string, unknown>): void {
  const next: GameSettings = { ...current };
  if (typeof values.hintMode === 'string' && HINT_MODES.has(values.hintMode)) {
    next.hintMode = values.hintMode as GameSettings['hintMode'];
  }
  if (typeof values.catchSize === 'string' && CATCH_SIZES.has(values.catchSize)) {
    next.catchSize = values.catchSize as GameSettings['catchSize'];
  }
  if (next.hintMode === current.hintMode && next.catchSize === current.catchSize) return;
  current = next;
  for (const listener of listeners) listener(current);
}

/** Wire the optional config domain. Returns immediately with defaults elsewhere. */
export function initSettings(): void {
  if (!runtimeHasDomain('config')) return;
  void (async () => {
    // Some hosts consume the embedded schema; others need standard registration.
    // Do not replace a schema that the host already supplied.
    if (!config.schema) {
      await config.registerSchema(settingsSchema as Parameters<typeof config.registerSchema>[0]);
    }
    const subscription = config.subscribe((values) => {
      if (values && typeof values === 'object') apply(values as Record<string, unknown>);
    });
    window.addEventListener('pagehide', () => subscription.close(), { once: true });
  })().catch(() => {
    // Optional operations may be unavailable even when a host exposes the domain.
    // Keep the in-app defaults so the game still works in a limited host.
  });
}