Back to Faultline
SOURCE / PINNED RELEASE

Made of little things.

Faultline

Release
da1ffaa5624c…
Author-recorded commit
60f5f03189db…
License
LICENSE
Author’s source reference
nostr://npub182jczunncwe0jn6frpqwq3e0qjws7yqqnc3auccqv9nte2dnd63scjm4rf/wss%3A%2F%2Fgit.napplet.soy%2F/n-6932b12c78c

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

src/player-settings.ts
import { identity, storage } from '@napplet/sdk';
import { runtimeHasDomain } from './domain-availability.js';

export const cleanName = (v: unknown) =>
  typeof v === 'string'
    ? Array.from(v.replace(/[\u0000-\u001f\u007f-\u009f\u202a-\u202e\u2066-\u2069]/gu, '').trim())
        .slice(0, 24)
        .join('')
        .trimEnd()
    : '';
export const validKey = (v: unknown): v is string => typeof v === 'string' && /^[a-f0-9]{64}$/.test(v);
export class PlayerSettings {
  alias = '';
  toggleCrouch = true;
  profileSelected = false;
  profile?: { key: string; name: string };
  loading = false;
  message = '';
  readonly canProfile = runtimeHasDomain('identity');
  private epoch = 0;
  private revision = 0;
  private scope = 0;
  private saved = Promise.resolve();
  private sub?: { close(): void };
  constructor(
    private changed: () => void,
    private scopeChanged: () => void,
  ) {
    void this.restore();
    if (this.canProfile) {
      try {
        this.sub = identity.onChanged(() => {
          this.epoch++;
          this.scope++;
          this.revision++;
          this.profile = undefined;
          this.profileSelected = false;
          this.loading = false;
          this.alias = '';
          this.toggleCrouch = true;
          this.message = '';
          this.scopeChanged();
          this.changed();
          void this.restore();
        });
      } catch {
        /* Reading a profile on demand still works without push notifications. */
      }
    }
    window.addEventListener(
      'pagehide',
      () => {
        this.epoch++;
        this.sub?.close();
      },
      { once: true },
    );
  }
  private async restore() {
    const revision = this.revision;
    if (!runtimeHasDomain('storage')) return;
    try {
      const raw = await storage.getItem('player-preferences');
      if (!raw || revision !== this.revision) return;
      const v = JSON.parse(raw);
      this.alias = cleanName(v?.alias);
      this.toggleCrouch = v?.crouch !== 'hold';
      if (v?.useNostr === true && this.canProfile) await this.useProfile();
      else this.changed();
    } catch {
      /* Keep the in-memory preferences when storage is unavailable. */
    }
  }
  save(alias: string, toggle: boolean) {
    this.revision++;
    this.alias = cleanName(alias);
    this.toggleCrouch = toggle;
    this.changed();
    if (runtimeHasDomain('storage')) {
      const value = JSON.stringify({ alias: this.alias, crouch: toggle ? 'toggle' : 'hold', useNostr: this.profileSelected }),
        scope = this.scope;
      this.saved = this.saved
        .then(async () => {
          if (scope === this.scope) await storage.setItem('player-preferences', value);
        })
        .catch(() => {});
    }
  }
  useAlias() {
    this.epoch++;
    this.revision++;
    this.profileSelected = false;
    this.profile = undefined;
    this.loading = false;
    this.message = '';
    this.changed();
  }
  async useProfile() {
    this.revision++;
    this.profileSelected = true;
    const token = ++this.epoch;
    this.loading = true;
    this.profile = undefined;
    this.message = 'Loading profile…';
    this.changed();
    try {
      const key = await identity.getPublicKey();
      if (token !== this.epoch) return;
      if (!validKey(key)) throw Error('Sign in through the host to use a Nostr profile.');
      this.profile = { key, name: 'Nostr ' + key.slice(0, 8) };
      this.changed();
      const data = await identity.getProfile().catch(() => null);
      if (token !== this.epoch) return;
      const name =
        cleanName(data?.displayName) ||
        cleanName((data as { display_name?: string } | null)?.display_name) ||
        cleanName(data?.name);
      this.profile = { key, name: name || 'Nostr ' + key.slice(0, 8) };
      this.message = name
        ? 'Using ' + name + '.'
        : 'No profile name found. Using your shortened public key.';
    } catch (e) {
      if (token !== this.epoch) return;
      this.message =
        e instanceof Error && e.message.startsWith('Sign in')
          ? e.message
          : 'Profile unavailable. You can play under an alias.';
    } finally {
      if (token === this.epoch) {
        this.loading = false;
        this.changed();
      }
    }
  }
}