-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathtemplates.html
More file actions
421 lines (376 loc) · 15.7 KB
/
Copy pathtemplates.html
File metadata and controls
421 lines (376 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="Browse remixable template worlds and spend Earned GOLD to start with a great foundation." />
<title>Template Marketplace - Tiny World Builder</title>
<link rel="icon" href="assets/twlogo.png" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@300..700&display=swap" />
<link rel="stylesheet" href="styles/landing.css" />
<script>
(function landingAuthBootstrap() {
window.__tinyworldAuthReady = new Promise(function (resolve) {
window.__resolveTinyWorldAuthReady = resolve;
setTimeout(function () { resolve(false); }, 2500);
});
})();
</script>
<script id="tinyworld-auth-importmap" type="importmap">
{
"imports": {
"@netlify/identity": "./vendor/netlify/identity.js",
"gotrue-js": "./vendor/netlify/gotrue.js"
}
}
</script>
<script type="module" src="vendor/tinyworld-auth.js"></script>
</head>
<body>
<header class="site-header" aria-label="Tiny World Builder">
<a class="site-logo" href="/" aria-label="Tiny World Builder home">
<img src="assets/twlogo-wordmark.png" alt="Tiny World Builder" width="1064" height="403" />
</a>
<nav class="site-nav" aria-label="Primary">
<a href="/">Home</a>
<a href="/#features">Features</a>
<a href="/#workflow">How it works</a>
<a href="/worlds">Worlds</a>
<a href="/badges">Badges</a>
<a href="/docs">Docs</a>
<a href="/features">Suggest</a>
<a href="/news">News</a>
<a href="https://github.com/jasonkneen/tiny-world-builder" rel="noopener noreferrer">GitHub</a>
<a class="nav-cta" href="/tiny-world-builder">Start building</a>
</nav>
</header>
<main class="templates-page">
<div class="beta-banner" role="status">The Tinyverse economy is in <strong>Beta Preview</strong> — GOLD, balances, and tokens are preview data while we finish building.</div>
<div class="templates-hero">
<h1>Template Marketplace <span class="beta-pill" title="The Tinyverse economy is in beta. Balances are preview data.">Beta Preview</span></h1>
<p>Browse remixable worlds built by the community. Spend Earned GOLD to get your own editable copy.</p>
<div id="tmpl-balance-wrap" aria-live="polite">
<!-- Populated by JS: balance pill or sign-in nudge -->
</div>
</div>
<!-- Template card grid — populated by JS -->
<div class="templates-grid" id="tmpl-grid" role="list" aria-label="Template worlds">
<!-- cards injected here -->
</div>
<!-- Empty state (shown when no templates returned) -->
<div class="templates-empty" id="tmpl-empty" hidden>
No templates listed yet.
</div>
</main>
<!-- Confirm dialog (single shared instance, hidden by default) -->
<div class="tmpl-confirm-overlay" id="tmpl-confirm" hidden role="dialog" aria-modal="true" aria-labelledby="tmpl-confirm-title">
<div class="tmpl-confirm-box">
<h2 class="tmpl-confirm-title" id="tmpl-confirm-title">Confirm Remix</h2>
<p class="tmpl-confirm-copy" id="tmpl-confirm-copy"></p>
<div class="tmpl-confirm-actions">
<button class="tmpl-confirm-cancel" id="tmpl-confirm-cancel" type="button">Cancel</button>
<button class="tmpl-confirm-ok" id="tmpl-confirm-ok" type="button">Remix</button>
</div>
</div>
</div>
<script src="scripts/world-preview.js"></script>
<script>
(function () {
'use strict';
// ---- SVG glyphs (static markup — innerHTML is fine here) ----
var COIN_SVG = '<svg viewBox="0 0 20 20" aria-hidden="true" focusable="false"><circle cx="10" cy="10" r="9" fill="#f5c842" stroke="#c9a000" stroke-width="1.5"/><text x="10" y="14.5" font-size="11" font-weight="bold" text-anchor="middle" fill="#7a5a00" font-family="ui-monospace,monospace">G</text></svg>';
var REMIX_ICON_SVG = '<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M17 1l4 4-4 4"/><path d="M3 11V9a4 4 0 0 1 4-4h14"/><path d="M7 23l-4-4 4-4"/><path d="M21 13v2a4 4 0 0 1-4 4H3"/></svg>';
// ---- State ----
var _templates = [];
var _balance = null; // number or null (not signed in)
var _signedIn = false;
// Per-card idempotency keys: Map<templateId, string>
var _idempotencyKeys = new Map();
// ---- Helpers ----
function genIdempotencyKey() {
// crypto.randomUUID() produces 36-char UUID which matches /^[A-Za-z0-9._:-]{8,128}$/
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
return crypto.randomUUID();
}
// Fallback for environments without crypto.randomUUID
return 'tmpl-' + Date.now().toString(36) + '-' + Math.random().toString(36).slice(2, 12);
}
function getOrCreateKey(templateId) {
if (!_idempotencyKeys.has(templateId)) {
_idempotencyKeys.set(templateId, genIdempotencyKey());
}
return _idempotencyKeys.get(templateId);
}
function resetKey(templateId) {
_idempotencyKeys.delete(templateId);
}
// ---- Fetch balance ----
function fetchBalance() {
return fetch('/api/me/coins', { credentials: 'same-origin', headers: { Accept: 'application/json' } })
.then(function (r) {
if (r.status === 401) { _signedIn = false; return null; }
_signedIn = true;
return r.json();
})
.then(function (data) {
_balance = (data && typeof data.balance === 'number') ? data.balance : null;
return _balance;
})
.catch(function () { return null; });
}
// ---- Fetch templates ----
function fetchTemplates() {
return fetch('/api/worlds/featured?templates=1', { headers: { Accept: 'application/json' } })
.then(function (r) { return r.json(); })
.then(function (data) { return Array.isArray(data.templates) ? data.templates : []; })
.catch(function () { return []; });
}
// ---- Render balance pill ----
function renderBalance() {
var wrap = document.getElementById('tmpl-balance-wrap');
if (!wrap) return;
if (_signedIn && _balance !== null) {
var pill = document.createElement('div');
pill.className = 'tmpl-balance';
pill.innerHTML = COIN_SVG;
var span = document.createElement('span');
span.textContent = _balance.toLocaleString() + ' GOLD';
pill.appendChild(span);
wrap.innerHTML = '';
wrap.appendChild(pill);
} else if (!_signedIn) {
var nudge = document.createElement('p');
nudge.className = 'tmpl-balance-sign-in';
nudge.innerHTML = '';
var link = document.createElement('a');
link.href = '/tiny-world-builder';
link.textContent = 'Sign in';
nudge.appendChild(link);
var after = document.createTextNode(' to remix templates and spend Earned GOLD.');
nudge.appendChild(after);
wrap.innerHTML = '';
wrap.appendChild(nudge);
} else {
wrap.innerHTML = '';
}
}
// ---- Render card ----
function renderCard(tmpl) {
var card = document.createElement('div');
card.className = 'tmpl-card';
card.setAttribute('role', 'listitem');
// Canvas for isometric preview
var canvas = document.createElement('canvas');
canvas.className = 'tmpl-canvas';
canvas.width = 400;
canvas.height = 300;
canvas.setAttribute('aria-label', 'Preview of ' + tmpl.name);
card.appendChild(canvas);
// Body
var body = document.createElement('div');
body.className = 'tmpl-body';
// Name
var nameEl = document.createElement('h3');
nameEl.className = 'tmpl-name';
nameEl.textContent = tmpl.name;
body.appendChild(nameEl);
// Meta: author + remix count
var meta = document.createElement('div');
meta.className = 'tmpl-meta';
var authorEl = document.createElement('span');
authorEl.className = 'tmpl-author';
authorEl.textContent = 'by ' + tmpl.authorName;
var countEl = document.createElement('span');
countEl.className = 'tmpl-remix-count';
countEl.textContent = tmpl.remixCount + (tmpl.remixCount === 1 ? ' remix' : ' remixes');
meta.appendChild(authorEl);
meta.appendChild(countEl);
body.appendChild(meta);
// Price row
var priceRow = document.createElement('div');
priceRow.className = 'tmpl-price-row';
if (tmpl.templatePrice === 0) {
var freeEl = document.createElement('span');
freeEl.className = 'tmpl-price-free';
freeEl.textContent = 'Free';
priceRow.appendChild(freeEl);
} else {
priceRow.innerHTML = COIN_SVG;
var priceEl = document.createElement('span');
priceEl.textContent = tmpl.templatePrice.toLocaleString() + ' GOLD';
priceRow.appendChild(priceEl);
}
body.appendChild(priceRow);
// Status message area
var statusEl = document.createElement('div');
statusEl.className = 'tmpl-status';
statusEl.hidden = true;
body.appendChild(statusEl);
// Remix button
var btn = document.createElement('button');
btn.className = 'tmpl-remix-btn';
btn.type = 'button';
btn.innerHTML = REMIX_ICON_SVG;
var btnLabel = document.createElement('span');
btnLabel.textContent = tmpl.templatePrice === 0
? 'Remix for free'
: 'Remix for ' + tmpl.templatePrice.toLocaleString() + ' GOLD';
btn.appendChild(btnLabel);
if (!_signedIn) {
btn.setAttribute('aria-label', 'Sign in to remix');
btn.disabled = false;
btn.addEventListener('click', function () {
window.location.href = '/tiny-world-builder';
});
} else {
btn.addEventListener('click', function () {
openConfirm(tmpl, btn, statusEl, btnLabel);
});
}
body.appendChild(btn);
card.appendChild(body);
// Draw preview synchronously after DOM insertion
requestAnimationFrame(function () {
if (window.TinyWorldPreview && tmpl.preview) {
try { window.TinyWorldPreview.renderPreview(canvas, tmpl.preview); } catch (_) {}
}
});
return card;
}
// ---- Render grid ----
function renderGrid() {
var grid = document.getElementById('tmpl-grid');
var empty = document.getElementById('tmpl-empty');
if (!grid) return;
grid.innerHTML = '';
if (!_templates.length) {
if (empty) empty.hidden = false;
return;
}
if (empty) empty.hidden = true;
_templates.forEach(function (tmpl) {
grid.appendChild(renderCard(tmpl));
});
}
// ---- Confirm dialog ----
var _confirmCallback = null;
function openConfirm(tmpl, btn, statusEl, btnLabel) {
var overlay = document.getElementById('tmpl-confirm');
var title = document.getElementById('tmpl-confirm-title');
var copy = document.getElementById('tmpl-confirm-copy');
var okBtn = document.getElementById('tmpl-confirm-ok');
var cancelBtn = document.getElementById('tmpl-confirm-cancel');
if (!overlay || !title || !copy || !okBtn || !cancelBtn) return;
// Set text via textContent for XSS safety
title.textContent = 'Confirm Remix';
copy.textContent = tmpl.templatePrice === 0
? 'Remix "' + tmpl.name + '" for free?'
: 'Remix "' + tmpl.name + '" for ' + tmpl.templatePrice.toLocaleString() + ' GOLD?';
overlay.hidden = false;
okBtn.focus();
_confirmCallback = function () {
overlay.hidden = true;
doRemix(tmpl, btn, statusEl, btnLabel);
};
okBtn.onclick = function () {
if (_confirmCallback) _confirmCallback();
_confirmCallback = null;
};
cancelBtn.onclick = function () {
overlay.hidden = true;
_confirmCallback = null;
};
overlay.onclick = function (e) {
if (e.target === overlay) {
overlay.hidden = true;
_confirmCallback = null;
}
};
}
// ---- Perform remix ----
function doRemix(tmpl, btn, statusEl, btnLabel) {
var key = getOrCreateKey(tmpl.id);
btn.disabled = true;
statusEl.hidden = true;
statusEl.className = 'tmpl-status';
statusEl.innerHTML = '';
fetch('/api/worlds/remix', {
method: 'POST',
credentials: 'same-origin',
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
body: JSON.stringify({ shareId: tmpl.id, idempotencyKey: key }),
})
.then(function (r) { return r.json().then(function (d) { return { status: r.status, data: d }; }); })
.then(function (res) {
if (res.data && res.data.ok) {
// Success — show link to original share; reset idempotency key for next attempt
resetKey(tmpl.id);
statusEl.hidden = false;
statusEl.className = 'tmpl-status tmpl-status-ok';
var msg = document.createTextNode('Remixed! Opening your copy… ');
statusEl.appendChild(msg);
var link = document.createElement('a');
link.href = '/tiny-world-builder?share=' + encodeURIComponent(tmpl.id);
link.textContent = 'Open builder';
statusEl.appendChild(link);
// Refresh balance
fetchBalance().then(renderBalance);
} else if (res.status === 402 || (res.data && res.data.reason === 'insufficient-coins')) {
// Not enough GOLD — keep key for retry (no charge happened)
statusEl.hidden = false;
statusEl.className = 'tmpl-status tmpl-status-warn';
var notEnough = document.createTextNode('Not enough GOLD — earn more in ');
statusEl.appendChild(notEnough);
var rewardsLink = document.createElement('a');
rewardsLink.href = '/rewards';
rewardsLink.textContent = 'Rewards';
statusEl.appendChild(rewardsLink);
btn.disabled = false;
} else {
// Other error — keep key safe, show friendly message
statusEl.hidden = false;
statusEl.className = 'tmpl-status tmpl-status-err';
var errText = (res.data && res.data.reason) ? res.data.reason : 'Something went wrong. Please try again.';
statusEl.textContent = errText;
btn.disabled = false;
}
})
.catch(function () {
statusEl.hidden = false;
statusEl.className = 'tmpl-status tmpl-status-err';
statusEl.textContent = 'Network error. Please try again.';
btn.disabled = false;
});
}
// ---- Main load ----
function load() {
return Promise.all([fetchTemplates(), fetchBalance()])
.then(function (results) {
_templates = results[0];
renderBalance();
renderGrid();
// Draw previews synchronously now that canvas nodes exist
// (renderCard schedules via rAF; re-run to catch any that resolved late)
var canvases = document.querySelectorAll('.tmpl-canvas');
canvases.forEach(function (c, i) {
var tmpl = _templates[i];
if (tmpl && window.TinyWorldPreview && tmpl.preview) {
try { window.TinyWorldPreview.renderPreview(c, tmpl.preview); } catch (_) {}
}
});
});
}
// Test seam
window._templatesLoad = load;
// Auto-boot on DOMContentLoaded
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', load);
} else {
load();
}
}());
</script>
</body>
</html>