Back to Supersonic RC Revive
SOURCE / PINNED RELEASE

Made of little things.

Supersonic RC Revive

Release
bb8b570f3600…
Author-recorded commit
073e2d2c0e63…
License
LICENSE
Author’s source reference
nostr://npub1ye5ptcxfyyxl5vjvdjar2ua3f0hynkjzpx552mu5snj3qmx5pzjscpknpr/wss%3A%2F%2Fgit.shakespeare.diy%2F/SuperSonicRCRevive

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

src/game/onlineNames.ts
import { common, identity } from '@napplet/sdk';
import { runtimeHasDomain } from '../domain-availability.js';
import { HASH } from './onlineProtocol.js';

export interface ProfileFields { displayName?: string; name?: string }
type Lookup = (pubkey: string) => Promise<ProfileFields | null | undefined>;

const lookupProfile: Lookup = async pubkey => runtimeHasDomain('common') ? (await common.getProfile(pubkey)).profile : null;

/** The signed-in user's Nostr pubkey to share for name tags, or null for guests and hosts without identity. */
export async function localProfilePubkey(): Promise<string | null> {
  if (!runtimeHasDomain('identity')) return null;
  try {
    const pubkey = await identity.getPublicKey();
    return HASH.test(pubkey) ? pubkey : null;
  } catch { return null; }
}

/** Kind 0 names are untrusted text: one line, no controls, short enough for a car marker. */
export function tagName(profile: ProfileFields | null | undefined): string | null {
  for (const raw of [profile?.displayName, profile?.name]) {
    if (typeof raw !== 'string') continue;
    const clean = raw.replace(/\p{Cf}/gu, '').replace(/[\p{Cc}\p{Zl}\p{Zp}\s]+/gu, ' ').trim();
    if (!clean) continue;
    const chars = [...clean];
    return chars.length > 16 ? `${chars.slice(0, 15).join('')}…` : clean;
  }
  return null;
}

/** One NAP-COMMON lookup per pubkey per session; failures fall back to the key tag. */
export class ProfileNames {
  private readonly names = new Map<string, string | null>();
  constructor(private readonly lookup: Lookup = lookupProfile) {}
  name(pubkey: string | undefined): string | null {
    if (!pubkey) return null;
    if (!this.names.has(pubkey)) {
      this.names.set(pubkey, null);
      void this.lookup(pubkey)
        .then(profile => { this.names.set(pubkey, tagName(profile)); })
        .catch(error => console.log('[online] profile lookup failed', error));
    }
    return this.names.get(pubkey) ?? null;
  }
}