aboutsummaryrefslogtreecommitdiffstats
path: root/src/features/hats.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/features/hats.js')
-rw-r--r--src/features/hats.js97
1 files changed, 95 insertions, 2 deletions
diff --git a/src/features/hats.js b/src/features/hats.js
index a14fc2b..bf0ec31 100644
--- a/src/features/hats.js
+++ b/src/features/hats.js
@@ -81,6 +81,17 @@ const HATS = [
<path d="M41.5 26 l0.9 2 2 0.9 -2 0.9 -0.9 2 -0.9 -2 -2 -0.9 2 -0.9 Z" fill="#ffe08a"/>`),
},
{
+ // Free to everyone through December (the registrar says when: its
+ // /hats/<pk> answer lists the hats free right now). Kept after.
+ id: 'santa', nameKey: 'hatSanta', sat: null, seasonal: true, w: 104, b: 73, r: -8,
+ art: svg(`<path d="M11 33 Q12 14 30 8.5 Q45 4.5 53 12.5 Q56.5 16.5 56 22 Q53 17.5 47 16.5 Q50.5 24 51.5 33 Z" fill="#d62f35"/>
+ <path d="M30 8.5 Q45 4.5 53 12.5 Q56.5 16.5 56 22 Q53 17.5 47 16.5 Q44 13 37 12 Q34 10 30 8.5 Z" fill="#b5242a"/>
+ <path d="M17 31 Q18 18 29 12" stroke="#ec5a5f" stroke-width="2" fill="none" stroke-linecap="round"/>
+ <circle cx="56" cy="23.5" r="5.2" fill="#f7f5f0" stroke="#d9d4ca" stroke-width="0.9"/>
+ <rect x="7" y="29.5" width="50" height="10.5" rx="5.25" fill="#f7f5f0" stroke="#d9d4ca" stroke-width="0.9"/>
+ <path d="M14 33 q2 1.5 4 0 M24 34.5 q2 1.5 4 0 M35 33 q2 1.5 4 0 M45 34.5 q2 1.5 4 0" stroke="#e2ddd3" stroke-width="1.1" fill="none" stroke-linecap="round"/>`),
+ },
+ {
// Not for sale: gifted by the operator to beta testers (see the
// registrar's /hats/grant). Appears in the shop only once owned.
id: 'construction', nameKey: 'hatConstruction', sat: null, w: 108, b: 71, r: -6,
@@ -222,9 +233,52 @@ export function hatsFeature(ctx) {
}
}
+ // ---- the whole list ------------------------------------------------------
+ // Only a couple of dozen heads wear a hat, so the registrar hands over all
+ // of them (/hats/worn) and every avatar is answered from that, even one
+ // seen for the first time; per-head lookups were 400 ms + a round trip
+ // after the page had painted. Public data, so it's kept per device, not per
+ // account. Until it has arrived once, the per-head path above still runs.
+ const ALL_KEY = (net) => 'hat-worn-all:' + net;
+ const allWorn = {}; // net -> { hats: {pk: id}, t }
+ const readAll = (net) => {
+ if (!(net in allWorn)) {
+ try { allWorn[net] = JSON.parse(localStorage.getItem(ALL_KEY(net)) || 'null'); } catch { allWorn[net] = null; }
+ }
+ return allWorn[net];
+ };
+ const allBusy = {}, allFailedAt = {};
+ async function fetchAll(net) {
+ if (allBusy[net] || Date.now() - (allFailedAt[net] || 0) < 60_000) return;
+ allBusy[net] = true;
+ try {
+ const r = await fetch(`${REGISTRAR}/hats/worn?net=${net}`);
+ if (!r.ok) throw new Error('hats list failed');
+ const j = await r.json();
+ const hats = j.hats || {};
+ const prev = readAll(net);
+ allWorn[net] = { hats, t: Date.now() };
+ try { localStorage.setItem(ALL_KEY(net), JSON.stringify(allWorn[net])); } catch {}
+ if (!prev || JSON.stringify(prev.hats) !== JSON.stringify(hats)) render();
+ } catch {
+ allFailedAt[net] = Date.now();
+ } finally {
+ allBusy[net] = false;
+ }
+ }
+ const fromAll = (pk) => {
+ const net = hatNet();
+ const all = readAll(net);
+ if (!all || Date.now() - all.t > REVALIDATE) fetchAll(net);
+ return all ? { hat: all.hats[pk] || null } : null;
+ };
+ setTimeout(() => { try { fromAll(''); } catch {} }, 0); // warm before the first avatar
+
// Stale-while-revalidate: answer from cache immediately, refresh behind.
function hatFor(pk) {
if (!pk || !/^[0-9a-f]{64}$/.test(pk)) return null;
+ const known = fromAll(pk);
+ if (known) return known.hat;
loadStore();
const v = worn.get(hatNet() + ':' + pk);
if (!v || Date.now() - v.t > REVALIDATE) queueFetch(pk);
@@ -287,7 +341,7 @@ export function hatsFeature(ctx) {
if (!me) { ui.hatShopData = { owned: [], equipped: null }; render(); return; }
const attempt = (retriesLeft) => fetch(`${REGISTRAR}/hats/${me}?net=${hatNet()}`).then((r) => r.json()).then((j) => {
if (!ui.hatShop) return;
- ui.hatShopData = { owned: j.owned || [], equipped: j.equipped || null, prices: j.prices || null };
+ ui.hatShopData = { owned: j.owned || [], equipped: j.equipped || null, prices: j.prices || null, free: j.free || [] };
const now = Date.now();
worn.set(hatNet() + ':' + me, { hat: j.equipped || null, t: now });
saveStore();
@@ -339,6 +393,29 @@ export function hatsFeature(ctx) {
}
}
+ // A free seasonal hat: no invoice, the registrar just hands it over.
+ async function takeFree(hat) {
+ ui.hatBuying = hat.id;
+ render();
+ try {
+ const id = await identity();
+ if (!id) {
+ if (!hook('nostrReconnectPrompt')) toast(t('msgNoIdentity'));
+ return;
+ }
+ const rec = await post('/hats/free', { hat: hat.id, net: hatNet() }, id.signer);
+ ui.hatShopData = { ...ui.hatShopData, owned: rec.owned, equipped: rec.equipped };
+ worn.set(hatNet() + ':' + id.pubkey, { hat: rec.equipped, t: Date.now() });
+ saveStore();
+ toast(t('hatYours', { name: t(hat.nameKey) }));
+ } catch (e) {
+ toast(e.message || String(e));
+ } finally {
+ ui.hatBuying = null;
+ render();
+ }
+ }
+
async function equip(hatId) {
ui.hatBuying = hatId || 'none';
render();
@@ -366,6 +443,8 @@ export function hatsFeature(ctx) {
return (d && d.prices && d.prices[hat.id]) || hat.sat;
};
+ const isFree = (hat, d) => !!(d && (d.free || []).includes(hat.id));
+
const demo = (hat) => h('span', { class: 'hat-demo' },
h('span', { class: 'hat-demo-head' }),
h('span', { class: 'ava-hat', style: posStyle(hat), html: hat.art }));
@@ -383,6 +462,8 @@ export function hatsFeature(ctx) {
action = h('button', { class: 'btn-sm hat-btn wearing', onClick: () => equip(null) }, t('hatWearing'));
} else if (ownedHere) {
action = h('button', { class: 'btn-sm hat-btn', onClick: () => equip(hat.id) }, t('hatWear'));
+ } else if (isFree(hat, d)) {
+ action = h('button', { class: 'btn-sm hat-btn', disabled: !!ui.hatBuying, onClick: () => takeFree(hat) }, t('hatFree'));
} else if (ui.hatConfirm === hat.id) {
action = h('button', { class: 'btn-sm hat-btn confirm', onClick: () => buy(hat) }, t('hatConfirmTap'));
} else {
@@ -397,6 +478,7 @@ export function hatsFeature(ctx) {
h('div', { class: 'chat-name' }, t(hat.nameKey)),
ownedHere
? h('div', { class: 'muted small' }, t('hatOwned'))
+ : isFree(hat, d) ? h('div', { class: 'muted small' }, t('hatSantaWhen'))
: hat.id === 'crown' ? h('div', { class: 'muted small' }, t('hatCrownWho')) : null),
action);
}
@@ -415,7 +497,9 @@ export function hatsFeature(ctx) {
// unpriced hats (the crown, the beta hard hat) are not
// merchandise — they appear only in the collection of a head
// that owns them, never as a listing
- ...HATS.filter((hat) => (priceOf(hat) || 0) > 0 || ((d && d.owned) || []).includes(hat.id))
+ // a free seasonal hat leads the list while it's in season
+ ...HATS.filter((hat) => isFree(hat, d))
+ .concat(HATS.filter((hat) => !isFree(hat, d) && ((priceOf(hat) || 0) > 0 || ((d && d.owned) || []).includes(hat.id))))
.map((hat) => shopRow(hat, d, me))),
d && d.offline ? h('div', { class: 'small faint', style: 'text-align:center' }, t('hatShopOffline')) : null),
h('button', { class: 'btn-ghost btn-block', onClick: closeShop }, t('back')));
@@ -438,6 +522,15 @@ export function hatsFeature(ctx) {
node,
h('span', { class: 'ava-hat', style: posStyle(hat), html: hat.art }));
},
+ // A wallet just opened: the /hats link lands in the shop.
+ init() {
+ if (!ui.hatsAtBoot) return;
+ ui.hatsAtBoot = false;
+ ui.screen = 'wallet';
+ openShop();
+ },
+ // ...and the shop's address is /hats for as long as it is on screen
+ navPath(snap) { return snap && snap.screen === 'wallet' && snap.hatShop ? '/hats' : null; },
// The footer's "Show your support" link: straight into the shop.
openHatShop() { openShop(); try { window.scrollTo(0, 0); } catch {} return true; },
// Own-profile editor entry: the door to the shop.