Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
1760 lines (1674 loc) · 70.2 KB
/
Copy pathapp.js
File metadata and controls
1760 lines (1674 loc) · 70.2 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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// app.js — the Pump Rider UI. All rules live in engine.js; this file
// renders state to a canvas, forwards the one button, plays tiny synth
// sounds, and owns the theme system.
//
// Themes: CSS variables under [data-theme] dress the page; the canvas
// reads the matching palette from THEMES below. Resolution order:
// ?theme= in the URL, then localStorage, then prefers-color-scheme.
// The toggle button flips and persists. ?seed= pins the terrain seed
// (demos, screenshots, shared runs); ?play=1 hides the start card over
// the frozen menu scene.
import {
createGame, step, terrainHeightAt, PRESETS, payoutFactorFor,
WORLD_H, PLAYER_R, BASE_SPEED, MAX_SPEED,
} from './engine.js';
import {
createRunRecorder, buildCardData, chartModel, LAST_RUN_KEY, decimateSamples,
pickOverHeadline,
STAKE_LADDER, STAKE_KEY, DEFAULT_STAKE, stakeIndexOf, stepStakeIndex, fmtMoney,
TARGET_LADDER, TARGET_KEY, DEFAULT_TARGET, targetIndexOf, targetPoints,
CURRENCY_KEY, NEAR_LADDER, DEFAULT_NEAR_STAKE, sanitizeCurrency,
sanitizeNearStake, usdToNear, nearToUsd, stepNearStake, fmtAmount,
} from './recap.js';
// Build variant, stamped by build-single.mjs (the token below is
// replaced at bundle time). The default stamp keeps the NEAR
// affiliation layer; the 'product' stamp ships the identical game
// without it. The modular dev build keeps the token, which reads as
// the default flavour.
const VARIANT = '__VARIANT__';
const $ = (id) => document.getElementById(id);
const params = new URLSearchParams(location.search);
// ---------------------------------------------------------------- themes
const THEMES = {
dark: {
sky: ['#000000', '#000000', '#000000'],
grid: 'rgba(255,255,255,0.08)',
candleUp: 'rgba(0,255,163,0.10)',
candleDown: 'rgba(255,61,138,0.11)',
speckle: 'rgba(255,255,255,0.55)',
chart: '#00ffa3',
chartHi: '#8dffd9',
prevLine: 'rgba(255,120,120,0.45)',
barLine: '#9aa7d9',
fillTop: 'rgba(0,255,163,0.30)',
fillBot: 'rgba(0,255,163,0.02)',
glow: 'rgba(0,255,163,0.55)',
cliff: '#ff5252',
orb: '#ffd23d',
orbCore: '#fff3b0',
orbInk: '#6b4e00',
body: '#f5f7ff',
fin: '#ff3d8a',
win: '#45d0ff',
flameO: '#ff9a3d',
flameY: '#ffd23d',
floaterStroke: 'rgba(0,0,0,0.75)',
},
light: {
sky: ['#fff9ec', '#fdeed3', '#fbe0ac'],
grid: 'rgba(22,33,60,0.10)',
candleUp: 'rgba(0,150,90,0.13)',
candleDown: 'rgba(224,36,115,0.13)',
speckle: 'rgba(146,110,40,0.35)',
chart: '#009e60',
chartHi: '#00c978',
prevLine: 'rgba(190,60,60,0.42)',
barLine: '#5d6a8c',
fillTop: 'rgba(0,158,96,0.26)',
fillBot: 'rgba(0,158,96,0.03)',
glow: 'rgba(0,158,96,0.35)',
cliff: '#d92b2b',
orb: '#f59e0b',
orbCore: '#ffe066',
orbInk: '#6b4e00',
body: '#ffffff',
fin: '#e02473',
win: '#0e7fd8',
flameO: '#ff8a2a',
flameY: '#ffc93d',
floaterStroke: 'rgba(255,250,240,0.85)',
},
};
const THEME_KEY = 'pump-rider-theme';
const BEST_KEY = 'pump-rider-best';
const MUTE_KEY = 'pump-rider-muted';
function store(key, value) {
try {
if (value === undefined) return localStorage.getItem(key);
localStorage.setItem(key, value);
} catch { return null; }
}
function storeRemove(key) {
try { localStorage.removeItem(key); } catch { /* nothing persisted anyway */ }
}
function resolveTheme() {
const q = params.get('theme');
if (q === 'dark' || q === 'light') return q;
const saved = store(THEME_KEY);
if (saved === 'dark' || saved === 'light') return saved;
return window.matchMedia && window.matchMedia('(prefers-color-scheme: light)').matches
? 'light'
: 'dark';
}
let themeName = resolveTheme();
function applyTheme() {
document.documentElement.dataset.theme = themeName;
// The icon names the destination: a moon in light mode (tap for
// dark), a sun in dark mode (tap for light).
$('btn-theme').textContent = themeName === 'dark' ? '☀️' : '🌙';
$('btn-theme').setAttribute('aria-label', themeName === 'dark' ? 'Switch to light theme' : 'Switch to dark theme');
}
function toggleTheme() {
themeName = themeName === 'dark' ? 'light' : 'dark';
store(THEME_KEY, themeName);
applyTheme();
}
// ---------------------------------------------------------------- settings
// Difficulty: preset chips (Chill / Classic / Degen) + Speed / Pump /
// Gravity sliders. Values are multipliers handed to the engine at run
// start (engine PRESETS are the single source of truth for the chips).
// The shipped feel was too quick, so the default preset is Chill.
// Moving any slider flips the preset to Custom. All of it persists.
const SETTINGS_KEY = 'pump-rider-settings';
const SLIDER_KEYS = ['speed', 'pump', 'gravity'];
const SLIDER_RANGES = { speed: [0.5, 1.5], pump: [0.5, 1.8], gravity: [0.5, 1.8] };
function presetSettings(name) {
const p = PRESETS[name];
return { speed: p.speedScale, pump: p.thrust, gravity: p.gravity };
}
function clampSetting(key, v) {
const [lo, hi] = SLIDER_RANGES[key];
return Number.isFinite(v) ? Math.min(hi, Math.max(lo, v)) : null;
}
function loadSettings() {
const fallback = { preset: 'chill', ...presetSettings('chill') };
try {
const raw = store(SETTINGS_KEY);
if (!raw) return fallback;
const parsed = JSON.parse(raw);
const out = { preset: 'custom' };
for (const key of SLIDER_KEYS) {
const v = clampSetting(key, Number(parsed[key]));
if (v === null) return fallback;
out[key] = v;
}
if (parsed.preset && PRESETS[parsed.preset]) {
const p = presetSettings(parsed.preset);
if (SLIDER_KEYS.every((k) => Math.abs(p[k] - out[k]) < 1e-9)) out.preset = parsed.preset;
}
return out;
} catch { return fallback; }
}
let settings = loadSettings();
function persistSettings() { store(SETTINGS_KEY, JSON.stringify(settings)); }
function engineSettings() {
return { speedScale: settings.speed, thrust: settings.pump, gravity: settings.gravity };
}
function presetLabel() {
return settings.preset === 'custom'
? 'Custom'
: settings.preset[0].toUpperCase() + settings.preset.slice(1);
}
// The volatility readout: difficulty is market volatility, and the
// payout factor it implies (engine.js) is shown wherever the choice
// is made — both difficulty panels and the stake card — updating
// live as the sliders move, before any run starts.
function volatilityText() {
const f = payoutFactorFor(engineSettings());
const label = f < 0.9 ? 'LOW' : f > 1.05 ? 'HIGH' : 'MEDIUM';
return `VOLATILITY ${label} · PAYOUT x${f.toFixed(2)}`;
}
function syncVolUI() {
const text = volatilityText();
document.querySelectorAll('.vol-line').forEach((el) => { el.textContent = text; });
}
function syncSettingsUI() {
const label = presetLabel();
syncVolUI();
document.querySelectorAll('.settings-panel').forEach((panel) => {
panel.querySelector('.preset-name').textContent = label;
panel.querySelectorAll('.chip').forEach((chip) => {
chip.classList.toggle('active', chip.dataset.preset === settings.preset);
});
panel.querySelectorAll('.js-slider').forEach((input) => {
input.value = settings[input.dataset.setting];
});
panel.querySelectorAll('.slider-val').forEach((el) => {
el.textContent = `${settings[el.dataset.val].toFixed(2)}x`;
});
});
}
function applyPreset(name) {
settings = { preset: name, ...presetSettings(name) };
persistSettings();
syncSettingsUI();
}
function wireSettings() {
document.querySelectorAll('.chip').forEach((chip) => {
chip.addEventListener('click', () => applyPreset(chip.dataset.preset));
});
document.querySelectorAll('.js-slider').forEach((input) => {
input.addEventListener('input', () => {
settings[input.dataset.setting] = clampSetting(input.dataset.setting, parseFloat(input.value));
settings.preset = 'custom';
persistSettings();
syncSettingsUI();
});
});
syncSettingsUI();
}
// ---------------------------------------------------------------- stake
// The pre-game stake, picked on the stake card between the start card
// and the run, in dollars or in NEAR (the toggle on the card switches
// units; the choice and the amount persist across sessions). The run
// settles in the active unit — every 100 pts is 1x on the bag, and
// the money math lives in recap.js with the card model. Dollars ride
// a fixed ladder (chips + stepper walk it); NEAR has its own native
// ladder and a finer stepper (0.5 under 10, then 5s). Switching units
// converts the amount at the fixed reference rate in recap.js.
let currency = sanitizeCurrency(store(CURRENCY_KEY));
let stakeIndex = stakeIndexOf(Number(store(STAKE_KEY)));
let nearStake = sanitizeNearStake(Number(store(STAKE_KEY)));
function currentStake() { return currency === 'near' ? nearStake : STAKE_LADDER[stakeIndex]; }
function persistStake() { store(STAKE_KEY, String(currentStake())); }
function setStakeIndex(i) {
if (currency !== 'usd') return;
stakeIndex = Math.min(STAKE_LADDER.length - 1, Math.max(0, i));
persistStake();
syncStakeUI();
}
function setNearStake(v) {
if (currency !== 'near') return;
nearStake = sanitizeNearStake(v);
persistStake();
syncStakeUI();
}
function setCurrency(next) {
next = sanitizeCurrency(next);
if (next === currency) return;
const value = currentStake();
currency = next;
store(CURRENCY_KEY, currency);
if (currency === 'near') nearStake = usdToNear(value);
else stakeIndex = stakeIndexOf(nearToUsd(value));
persistStake();
buildStakeChips();
syncStakeUI();
}
function buildStakeChips() {
const ladder = currency === 'near' ? NEAR_LADDER : STAKE_LADDER;
const box = $('stake-chips');
box.textContent = '';
for (const v of ladder) {
const b = document.createElement('button');
b.type = 'button';
b.className = 'stake-chip';
b.dataset.stake = String(v);
b.textContent = fmtAmount(v, currency);
b.addEventListener('click', () => {
if (currency === 'near') setNearStake(v);
else setStakeIndex(stakeIndexOf(v));
});
box.append(b);
}
}
function syncStakeUI() {
const stake = currentStake();
document.querySelector('.stake-card').classList.toggle('near-mode', currency === 'near');
$('stake-amount').textContent = fmtAmount(stake, currency);
document.querySelectorAll('.stake-chip').forEach((chip) => {
chip.classList.toggle('active', Number(chip.dataset.stake) === stake);
});
$('btn-stake-minus').disabled = currency === 'near' ? stake <= 0.5 : stakeIndex === 0;
$('btn-stake-plus').disabled = currency === 'near' ? stake >= 1000 : stakeIndex === STAKE_LADDER.length - 1;
$('btn-ape').textContent = `APE IN ${fmtAmount(stake, currency)}`;
$('cur-usd').classList.toggle('active', currency === 'usd');
$('cur-near').classList.toggle('active', currency === 'near');
$('cur-rate').classList.toggle('hidden', currency !== 'near');
}
function wireStake() {
buildStakeChips();
$('btn-stake-minus').addEventListener('click', () => {
if (currency === 'near') setNearStake(stepNearStake(nearStake, -1));
else setStakeIndex(stepStakeIndex(stakeIndex, -1));
});
$('btn-stake-plus').addEventListener('click', () => {
if (currency === 'near') setNearStake(stepNearStake(nearStake, 1));
else setStakeIndex(stepStakeIndex(stakeIndex, 1));
});
$('cur-usd').addEventListener('click', () => setCurrency('usd'));
$('cur-near').addEventListener('click', () => setCurrency('near'));
syncStakeUI();
}
// ---------------------------------------------------------------- the bar
// The called target: a gain over the stake the player promises
// themselves on the same stake card. Clear it and the bag pays out;
// miss it and the bag is gone (the settlement lives in recap.js).
// The choice persists across sessions like the stake does.
let targetIndex = targetIndexOf(Number(store(TARGET_KEY)));
function currentTarget() { return TARGET_LADDER[targetIndex]; }
function setTargetIndex(i) {
targetIndex = Math.min(TARGET_LADDER.length - 1, Math.max(0, i));
store(TARGET_KEY, String(currentTarget()));
syncTargetUI();
}
function syncTargetUI() {
const target = currentTarget();
document.querySelectorAll('.target-chip').forEach((chip) => {
chip.classList.toggle('active', Number(chip.dataset.target) === target);
});
$('target-need').textContent = `you need ${targetPoints(target)} pts to clear the bar`;
}
function wireTarget() {
document.querySelectorAll('.target-chip').forEach((chip) => {
chip.addEventListener('click', () => setTargetIndex(targetIndexOf(Number(chip.dataset.target))));
});
syncTargetUI();
}
// ---------------------------------------------------------------- audio
// Tiny WebAudio synth. The context is created ONLY inside a user-gesture
// handler (first press), per browser autoplay rules.
let actx = null;
let muted = store(MUTE_KEY) === '1';
function ensureAudio() {
if (!actx) {
try { actx = new (window.AudioContext || window.webkitAudioContext)(); } catch { actx = null; }
}
if (actx && actx.state === 'suspended') actx.resume();
if (actx) startMusic(); // the soundtrack starts with the unlock, in the menu already
}
// The unlock must ride the FIRST gesture anywhere — a button tap, a key
// press, a touch on the stage — not only presses that land on the game
// surface: browsers only honour a resume issued inside a real gesture,
// so a context created suspended stays silent until one of these fires.
window.addEventListener('pointerdown', ensureAudio);
window.addEventListener('touchstart', ensureAudio, { passive: true });
window.addEventListener('keydown', ensureAudio);
function tone(freq, dur, { type = 'sine', vol = 0.12, slideTo = null, delay = 0 } = {}) {
if (muted || !actx) return;
if (actx.state === 'suspended') actx.resume(); // a play attempt is also a chance to unlock
const t0 = actx.currentTime + delay;
const o = actx.createOscillator();
const g = actx.createGain();
o.type = type;
o.frequency.setValueAtTime(freq, t0);
if (slideTo) o.frequency.exponentialRampToValueAtTime(Math.max(30, slideTo), t0 + dur);
g.gain.setValueAtTime(vol, t0);
g.gain.exponentialRampToValueAtTime(0.001, t0 + dur);
o.connect(g); g.connect(actx.destination);
o.start(t0); o.stop(t0 + dur + 0.03);
}
function noiseBurst(dur = 0.35, vol = 0.22) {
if (muted || !actx) return;
if (actx.state === 'suspended') actx.resume(); // a play attempt is also a chance to unlock
const n = Math.floor(actx.sampleRate * dur);
const buf = actx.createBuffer(1, n, actx.sampleRate);
const d = buf.getChannelData(0);
for (let i = 0; i < n; i++) d[i] = (Math.random() * 2 - 1) * (1 - i / n);
const src = actx.createBufferSource(); src.buffer = buf;
const f = actx.createBiquadFilter(); f.type = 'lowpass'; f.frequency.value = 900;
const g = actx.createGain(); g.gain.value = vol;
src.connect(f); f.connect(g); g.connect(actx.destination);
src.start();
}
const sfx = {
orb(combo) { tone(540 + combo * 45, 0.09, { type: 'square', vol: 0.05 }); tone(1080 + combo * 90, 0.08, { vol: 0.07, delay: 0.02 }); },
closecall() { tone(280, 0.13, { type: 'sawtooth', vol: 0.045, slideTo: 640 }); },
warn() { tone(880, 0.09, { type: 'square', vol: 0.06 }); tone(880, 0.09, { type: 'square', vol: 0.06, delay: 0.16 }); },
drop() { tone(720, 0.5, { type: 'sawtooth', vol: 0.07, slideTo: 55 }); },
survived() { tone(523, 0.1, { vol: 0.08 }); tone(784, 0.16, { vol: 0.08, delay: 0.09 }); },
comboLost() { tone(320, 0.14, { type: 'triangle', vol: 0.06, slideTo: 170 }); },
crash() { noiseBurst(); tone(130, 0.4, { vol: 0.22, slideTo: 38 }); },
};
// ---------------------------------------------------------------- music
// A procedural soundtrack under the SFX: one seamless synthwave
// loop in A minor — a driving bass line, a bright arp, kick and
// hats — composed entirely of WebAudio voices. No samples, nothing
// fetched, nothing copyrighted: the single file stays fully
// offline. A lookahead scheduler (25ms timer, notes placed 120ms
// ahead) keeps the loop tight for a handful of nodes per step.
// The music rides its own gain node at a modest level so the SFX
// stay on top; the mute silences the gain node itself, and while
// the game is paused the scheduler holds the loop's place instead
// of advancing it, so it resumes exactly where it stopped.
const MUSIC_VOL = 0.2;
const MUSIC_BPM = 112;
// Four bars of sixteenths: Am — F — C — G, the night-ride walk.
const MUSIC_CHORDS = [
{ bass: 55.0, arp: [440.0, 523.25, 659.25, 880.0] }, // A1 · A4 C5 E5 A5
{ bass: 43.65, arp: [349.23, 440.0, 523.25, 698.46] }, // F1 · F4 A4 C5 F5
{ bass: 65.41, arp: [261.63, 329.63, 392.0, 523.25] }, // C2 · C4 E4 G4 C5
{ bass: 49.0, arp: [392.0, 493.88, 587.33, 783.99] }, // G1 · G4 B4 D5 G5
];
const MUSIC_ARP_WALK = [0, 1, 2, 3, 3, 2, 1, 0];
const MUSIC_BASS_WALK = [1, 1, 2, 1, 1, 1, 2, 1];
let musicGain = null;
let musicTimer = null;
let musicStep = 0;
let musicNextTime = 0;
let musicNoiseBuf = null;
function musicToneAt(freq, t0, dur, { type = 'triangle', vol = 0.4 } = {}) {
const o = actx.createOscillator();
const g = actx.createGain();
o.type = type;
o.frequency.setValueAtTime(freq, t0);
g.gain.setValueAtTime(0, t0);
g.gain.linearRampToValueAtTime(vol, t0 + 0.008);
g.gain.exponentialRampToValueAtTime(0.001, t0 + dur);
o.connect(g); g.connect(musicGain);
o.start(t0); o.stop(t0 + dur + 0.03);
}
function musicKickAt(t0) {
const o = actx.createOscillator();
const g = actx.createGain();
o.type = 'sine';
o.frequency.setValueAtTime(128, t0);
o.frequency.exponentialRampToValueAtTime(41, t0 + 0.11);
g.gain.setValueAtTime(0.85, t0);
g.gain.exponentialRampToValueAtTime(0.001, t0 + 0.13);
o.connect(g); g.connect(musicGain);
o.start(t0); o.stop(t0 + 0.16);
}
function musicHatAt(t0, vol) {
if (!musicNoiseBuf) {
const n = Math.floor(actx.sampleRate * 0.5);
musicNoiseBuf = actx.createBuffer(1, n, actx.sampleRate);
const d = musicNoiseBuf.getChannelData(0);
for (let i = 0; i < n; i++) d[i] = Math.random() * 2 - 1;
}
const src = actx.createBufferSource(); src.buffer = musicNoiseBuf;
src.playbackRate.value = 0.9 + Math.random() * 0.2;
const f = actx.createBiquadFilter(); f.type = 'highpass'; f.frequency.value = 6800;
const g = actx.createGain();
g.gain.setValueAtTime(vol, t0);
g.gain.exponentialRampToValueAtTime(0.001, t0 + 0.045);
src.connect(f); f.connect(g); g.connect(musicGain);
src.start(t0, Math.random() * 0.3, 0.06); src.stop(t0 + 0.06);
}
function scheduleMusicStep(step, t0, stepDur) {
const chord = MUSIC_CHORDS[Math.floor(step / 16) % 4];
const s16 = step % 16;
if (s16 % 2 === 0) { // the driving bass, eighths with octave pops
const mult = MUSIC_BASS_WALK[s16 / 2];
musicToneAt(chord.bass * mult, t0, stepDur * 1.9, { type: 'triangle', vol: 0.75 });
}
const arpIdx = MUSIC_ARP_WALK[s16 % 8];
musicToneAt(chord.arp[arpIdx], t0, stepDur * 0.92, { type: 'square', vol: 0.11 });
if (s16 % 4 === 0) musicKickAt(t0);
if (s16 % 4 === 2) musicHatAt(t0, 0.2);
else if (s16 % 2 === 1) musicHatAt(t0, 0.06);
}
function musicTick() {
if (!actx || !musicGain) return;
if (paused) { // hold the loop's place; it resumes with the game
musicNextTime = Math.max(musicNextTime, actx.currentTime + 0.05);
return;
}
const stepDur = 60 / MUSIC_BPM / 4;
while (musicNextTime < actx.currentTime + 0.12) {
scheduleMusicStep(musicStep, musicNextTime, stepDur);
musicStep = (musicStep + 1) % 64;
musicNextTime += stepDur;
}
}
function startMusic() {
if (!actx || musicTimer) return;
if (!musicGain) {
musicGain = actx.createGain();
musicGain.gain.value = muted ? 0 : MUSIC_VOL;
musicGain.connect(actx.destination);
}
musicStep = 0;
musicNextTime = actx.currentTime + 0.06;
musicTimer = setInterval(musicTick, 25);
}
// ---------------------------------------------------------------- state
const canvas = $('game');
const ctx = canvas.getContext('2d');
const seedParam = params.get('seed');
let G = null; // engine state
let mode = 'demo'; // demo (frozen menu scene) | playing | over
let paused = false;
let thrustHeld = false;
let stakeOpen = false; // the pre-game stake card is showing
// Mirrors the UI state onto <body data-state> for tests and screenshots.
function syncState() {
document.body.dataset.state = stakeOpen ? 'stake' : mode;
}
let particles = [];
let floaters = [];
let shake = 0;
let crashAt = 0;
let best = Number(store(BEST_KEY) || 0);
let bestBeaten = false; // this run has crossed the stored best
let lastHud = {};
let recorder = null; // PnL recorder for the live run (recap.js)
let lastSummary = null; // the most recent finished run's summary
let lastCard = null; // the most recent finished run's PnL card model
let lastPrev = null; // the run before it (its summary, samples included)
function newRun(seed) {
G = createGame(seed, { settings: engineSettings() });
particles = [];
floaters = [];
shake = 0;
bestBeaten = false;
$('stat-best-box').classList.remove('beaten');
paused = false;
$('paused-banner').classList.add('hidden');
$('rug-banner').classList.add('hidden');
}
function startGame() {
ensureAudio();
// Whatever is physically held down at this instant (the Space press
// that aped in, say) cannot thrust until it is released and pressed
// again — see the input section. Pointer-started runs snapshot an
// empty set, because a click only fires after its pointerup, so the
// player's first real hold of the run just works.
ignoredPointers = new Set(heldPointers);
ignoredKeys = new Set(heldKeys);
newRun(seedParam !== null ? seedParam : (Math.random() * 1e9) | 0);
recorder = createRunRecorder();
recorder.begin(presetLabel(), currentStake(), currentTarget(), currency);
mode = 'playing';
$('overlay-start').classList.add('hidden');
$('overlay-over').classList.add('hidden');
syncState();
}
// Game-over headlines come from the shared card model (recap.js):
// a new best first, then the WIN set for a cleared bar, else the
// crash set. One short line, small type, never wraps.
function gameOver() {
mode = 'over';
syncState();
crashAt = performance.now();
const isBest = bestBeaten || G.score > best;
if (isBest) { best = G.score; store(BEST_KEY, String(best)); }
// PnL card: close the run's books, settle it against the called
// bar, remember it (score curve included, decimated) so the next
// card can draw it as the sub-line, and render the card.
if (recorder) {
lastSummary = recorder.finish(G);
const prev = loadLastRun();
lastPrev = prev;
lastCard = buildCardData(lastSummary, prev, isBest);
store(LAST_RUN_KEY, JSON.stringify({
score: lastSummary.score,
peakScore: lastSummary.peakScore,
orbs: lastSummary.orbs,
bestCombo: lastSummary.bestCombo,
duration: lastSummary.duration,
stake: lastSummary.stake,
target: lastSummary.target,
samples: decimateSamples(lastSummary.samples),
}));
renderPnlCard(lastCard, lastSummary, prev);
$('over-title').textContent = lastCard.headline;
}
if (!recorder) $('over-title').textContent = pickOverHeadline(isBest, false, G.score);
setOverSettings(false); // difficulty starts collapsed on this card
setTimeout(() => {
if (mode !== 'over') return;
$('overlay-over').classList.remove('hidden');
}, 650);
}
// ---------------------------------------------------------------- PnL card
function loadLastRun() {
try {
const raw = store(LAST_RUN_KEY);
const parsed = raw ? JSON.parse(raw) : null;
return parsed && Number.isFinite(parsed.score) ? parsed : null;
} catch { return null; }
}
const SVGNS = 'http://www.w3.org/2000/svg';
function svgEl(tag, attrs) {
const el = document.createElementNS(SVGNS, tag);
for (const [k, v] of Object.entries(attrs)) el.setAttribute(k, v);
return el;
}
// The card's chart, drawn into its SVG from chartModel's unit-square
// points. TWO runs share one common scale: the previous run as a thin
// muted sub-line, this run as the thick major line in the card's
// identity colour (win green, loss red — the money truth, not the
// vs-last-run grade). A neutral dashed line marks the called bar, a
// dashed line in the line's own colour marks this run's peak, gold
// squares sit where orbs were clipped, a pink flag where a rug was
// survived, and a red X where it ended.
function renderPnlChart(svg, m, card) {
const T = THEMES[themeName];
const line = card.win ? '#00ec97' : T.cliff;
const W = 320; const H = 110;
const X0 = 7; const X1 = W - 7; const YT = 11; const YB = H - 6;
const px = (u) => X0 + u * (X1 - X0);
const py = (u) => YB - u * (YB - YT);
svg.textContent = '';
if (m.line.length < 2) return;
// The bar the player called: a neutral dashed line the run had to
// cross, labelled at the left so a miss visibly dies below it.
svg.append(svgEl('line', {
x1: X0, y1: py(m.targetY), x2: X1, y2: py(m.targetY),
stroke: T.barLine, 'stroke-width': 1, 'stroke-dasharray': '2 3',
opacity: 0.8, 'vector-effect': 'non-scaling-stroke', class: 'target-line',
}));
const barLabel = svgEl('text', {
x: X0 + 2, y: Math.max(8, py(m.targetY) - 2.5), fill: T.barLine,
'font-size': 7.5, 'font-weight': 800, 'letter-spacing': '0.05em',
});
barLabel.textContent = `BAR ${m.needed}`;
svg.append(barLabel);
// The previous run, quiet underneath: thin, muted, unfilled.
if (m.hasPrev && m.prevLine.length >= 2) {
const prevPts = m.prevLine.map((p) => `${px(p.x).toFixed(1)},${py(p.y).toFixed(1)}`);
svg.append(svgEl('path', {
d: `M${prevPts.join(' L')}`, fill: 'none', stroke: T.prevLine,
'stroke-width': 1.2, 'stroke-linejoin': 'round', 'stroke-linecap': 'round',
'vector-effect': 'non-scaling-stroke', class: 'prev-line',
}));
const lastPrev = m.prevLine[m.prevLine.length - 1];
const prevLabel = svgEl('text', {
x: Math.max(X0 + 14, px(lastPrev.x) - 2), y: Math.min(YB - 2, py(lastPrev.y) + 8),
fill: T.prevLine, 'font-size': 7, 'font-weight': 700, 'text-anchor': 'end',
});
prevLabel.textContent = 'last run';
svg.append(prevLabel);
}
const pts = m.line.map((p) => `${px(p.x).toFixed(1)},${py(p.y).toFixed(1)}`);
// Dashed peak line at THIS run's high on the common scale, stopping
// short of the crash X so the HIGH chip and the marker never fight
// over the corner.
if (m.peakScore > 0) {
svg.append(svgEl('line', {
x1: X0, y1: py(m.peakY), x2: px(m.crash.x) - 12, y2: py(m.peakY),
stroke: line, 'stroke-width': 1, 'stroke-dasharray': '4 4',
opacity: 0.55, 'vector-effect': 'non-scaling-stroke',
}));
}
svg.append(svgEl('path', {
d: `M${pts.join(' L')} L${px(m.crash.x).toFixed(1)},${YB} L${X0},${YB} Z`,
fill: line, 'fill-opacity': 0.16, stroke: 'none',
}));
svg.append(svgEl('path', {
d: `M${pts.join(' L')}`, fill: 'none', stroke: line,
'stroke-width': 2, 'stroke-linejoin': 'round', 'stroke-linecap': 'round',
'vector-effect': 'non-scaling-stroke',
}));
for (const o of m.orbs) {
svg.append(svgEl('rect', {
x: px(o.x) - 2.6, y: py(o.y) - 2.6, width: 5.2, height: 5.2, fill: T.orb,
}));
}
for (const r of m.rugs) {
const cx = px(r.x); const cy = py(r.y);
svg.append(svgEl('path', {
d: `M${cx},${cy - 6} L${cx + 4.6},${cy + 3.4} L${cx - 4.6},${cy + 3.4} Z`, fill: T.fin,
}));
}
const cx = px(m.crash.x); const cy = py(m.crash.y);
svg.append(svgEl('path', {
d: `M${cx - 4},${cy - 4} L${cx + 4},${cy + 4} M${cx + 4},${cy - 4} L${cx - 4},${cy + 4}`,
stroke: T.cliff, 'stroke-width': 2.2, 'stroke-linecap': 'round',
'vector-effect': 'non-scaling-stroke',
}));
}
function renderPnlCard(card, summary, prev) {
$('pnl-preset').textContent = card.preset || 'CUSTOM';
// The card's identity is the bar settlement: win and it glows
// green with a gold hero, miss and it glows red with a red hero.
const pnlEl = $('pnl-card');
pnlEl.classList.toggle('win', card.win);
pnlEl.classList.toggle('loss', !card.win);
$('pnl-score').textContent = card.heroText;
const verdict = $('pnl-verdict');
verdict.textContent = card.verdictLabel;
verdict.className = `pnl-verdict ${card.tone}`;
$('pnl-time').textContent = card.timeText;
const gain = $('pnl-gain');
gain.textContent = card.gainText;
gain.className = `pnl-gain ${card.tone}`;
$('pnl-mult').textContent = `${card.multText} combo`;
const money = $('pnl-money');
money.textContent = card.moneyText;
money.className = `pnl-money ${card.moneyTone}`;
$('pnl-vs').textContent = card.vsPrevText;
$('pnl-peak').textContent = `HIGH ${card.peakScore}`;
for (const tile of card.tiles) {
$(`pnl-tile-${tile.key}`).textContent = tile.value;
}
$('pnl-foot').textContent = card.footer;
renderPnlChart($('pnl-chart'), chartModel(summary, prev), card);
}
// ---------------------------------------------------------------- PNG painter
// The Download button's card: the same layout as the on-screen PnL
// card, painted onto a 1080x1080 canvas from the same card model, so
// the file a player shares can never disagree with what they saw.
const PNL_PAINT = {
dark: { bg: '#000000', panel: '#0d0d0d', ink: '#eef2ff', dim: '#9aa7d9', line: '#232323', green: '#00ffa3', red: '#ff5252', gold: '#ffd23d', pink: '#ff3d8a', chart: '#00ffa3', score: '#ffc837', best: '#00ec97', prevLine: 'rgba(200,110,110,0.5)', barLine: '#9aa7d9' },
light: { bg: '#fffdf6', panel: '#fdf3dd', ink: '#16213c', dim: '#5d6a8c', line: '#e3d3ae', green: '#009e60', red: '#d92b2b', gold: '#d97706', pink: '#e02473', chart: '#009e60', score: '#b97e04', best: '#00875a', prevLine: 'rgba(160,70,70,0.5)', barLine: '#5d6a8c' },
};
function rr(c, x, y, w, h, r) {
c.beginPath();
c.moveTo(x + r, y);
c.arcTo(x + w, y, x + w, y + h, r);
c.arcTo(x + w, y + h, x, y + h, r);
c.arcTo(x, y + h, x, y, r);
c.arcTo(x, y, x + w, y, r);
c.closePath();
}
function renderPnlPng() {
if (!lastCard || !lastSummary) return null;
const P = PNL_PAINT[themeName];
const card = lastCard;
const toneColor = card.tone === 'green' ? P.best : card.tone === 'red' ? P.red : P.ink;
const cv = document.createElement('canvas');
cv.width = 1080; cv.height = 1080;
const c = cv.getContext('2d');
c.fillStyle = P.bg;
c.fillRect(0, 0, 1080, 1080);
// The card identity, same as the screen: a coloured border band
// with a soft halo, green on a cleared bar, red on a missed one.
const glowColor = card.win ? P.best : P.red;
c.save();
c.shadowColor = glowColor; c.shadowBlur = 46;
rr(c, 28, 28, 1024, 1024, 36);
c.strokeStyle = glowColor; c.lineWidth = 4; c.stroke();
c.restore();
rr(c, 17, 17, 1046, 1046, 42);
c.strokeStyle = glowColor; c.globalAlpha = 0.3; c.lineWidth = 2; c.stroke();
c.globalAlpha = 1;
// Header: a little rocket + the wordmark, preset on the right.
c.save();
c.translate(104, 96);
c.fillStyle = P.pink;
c.beginPath(); c.moveTo(-13, -8); c.lineTo(-26, -22); c.lineTo(-7, -15); c.closePath(); c.fill();
c.beginPath(); c.moveTo(-13, 8); c.lineTo(-26, 22); c.lineTo(-7, 15); c.closePath(); c.fill();
c.beginPath(); c.ellipse(0, 0, 24, 13, 0, 0, Math.PI * 2);
c.fillStyle = themeName === 'dark' ? '#f5f7ff' : '#ffffff'; c.fill();
c.beginPath(); c.arc(8, -2, 6, 0, Math.PI * 2);
c.fillStyle = '#45d0ff'; c.fill();
c.restore();
c.fillStyle = P.ink;
c.font = '900 46px system-ui, sans-serif';
c.textBaseline = 'middle';
c.fillText('PUMP RIDER', 148, 98);
// NEAR attribution on the card itself — the angular N glyph and
// the word NEAR beside the wordmark, both in the affiliation
// green — so every downloaded or shared card carries it visibly.
// Default stamp only: the product build draws no affiliation.
if (VARIANT !== 'product') {
const wmW = c.measureText('PUMP RIDER').width;
const gx = 148 + wmW + 22; const gy = 84; const gs = 28;
c.beginPath();
[[5, 19], [5, 5], [19, 19], [19, 5]].forEach(([u, v], i) => {
const X = gx + (u / 24) * gs; const Y = gy + (v / 24) * gs;
if (i === 0) c.moveTo(X, Y); else c.lineTo(X, Y);
});
c.strokeStyle = P.best; c.lineWidth = 3.4; c.lineCap = 'square'; c.lineJoin = 'miter'; c.stroke();
c.font = '800 30px system-ui, sans-serif';
c.fillStyle = P.best;
c.fillText('NEAR', gx + gs + 10, 98);
}
c.font = '800 34px system-ui, sans-serif';
c.fillStyle = P.green;
const presetW = c.measureText(card.preset || 'CUSTOM').width;
c.fillText(card.preset || 'CUSTOM', 1008 - presetW, 98);
// Hero score: gold on a win, red on a loss, like the screen. The
// verdict pill gets its OWN line under the hero, left-aligned,
// exactly as on screen: on the hero's row it ran out of width at
// 4-digit scores and painted over the PTS.
c.font = '900 132px system-ui, sans-serif';
c.fillStyle = card.win ? P.score : P.red;
c.fillText(card.heroText, 72, 226);
const heroW = c.measureText(card.heroText).width;
c.font = '800 44px system-ui, sans-serif';
c.fillStyle = P.dim;
c.fillText('PTS', 84 + heroW, 244);
c.font = '800 27px system-ui, sans-serif';
const vW = c.measureText(card.verdictLabel).width + 44;
rr(c, 72, 262, vW, 50, 25);
c.strokeStyle = toneColor; c.lineWidth = 2.5; c.stroke();
c.fillStyle = toneColor;
c.fillText(card.verdictLabel, 94, 288);
// Sub stats: time in trade (left), gain + best combo (right).
c.font = '700 25px system-ui, sans-serif';
c.fillStyle = P.dim;
c.fillText('TIME IN TRADE', 72, 352);
const gainLabel = 'GAIN';
c.fillText(gainLabel, 1008 - c.measureText(gainLabel).width, 352);
c.font = '800 50px system-ui, sans-serif';
c.fillStyle = P.ink;
c.fillText(card.timeText, 72, 400);
const gainW = c.measureText(card.gainText).width;
c.fillStyle = toneColor;
c.fillText(card.gainText, 1008 - gainW, 400);
c.font = '800 30px system-ui, sans-serif';
c.fillStyle = P.gold;
const multText = `${card.multText} BEST COMBO`;
c.fillText(multText, 1008 - c.measureText(multText).width, 444);
// The stake settlement strip, same string and tone as on screen:
// what went in, what came out, and the difference.
rr(c, 72, 462, 936, 44, 12);
c.fillStyle = P.panel; c.fill();
c.strokeStyle = P.line; c.lineWidth = 2; c.stroke();
c.font = '800 29px system-ui, sans-serif';
c.fillStyle = card.moneyTone === 'green' ? P.best : card.moneyTone === 'red' ? P.red : P.dim;
const moneyW = c.measureText(card.moneyText).width;
c.fillText(card.moneyText, (1080 - moneyW) / 2, 485);
// The quiet note under the strip, same as the screen.
c.font = '700 24px system-ui, sans-serif';
c.fillStyle = P.dim;
const noteW = c.measureText(card.vsPrevText).width;
c.fillText(card.vsPrevText, (1080 - noteW) / 2, 534);
// The chart of the run itself — the card's centrepiece now: twice
// the old box, funded by the tighter headline, sub, strip and
// tiles around it, so the run's steps and slopes read properly.
const bx = 72; const by = 552; const bw = 936; const bh = 320;
rr(c, bx, by, bw, bh, 18);
c.fillStyle = P.panel; c.fill();
c.strokeStyle = P.line; c.lineWidth = 2; c.stroke();
const m = chartModel(lastSummary, lastPrev);
const chartColor = card.tone === 'green' ? P.best : card.tone === 'red' ? P.red : P.chart;
const X0 = bx + 20; const X1 = bx + bw - 20; const YT = by + 22; const YB = by + bh - 18;
const px = (u) => X0 + u * (X1 - X0);
const py = (u) => YB - u * (YB - YT);
if (m.line.length >= 2) {
c.save();
rr(c, bx, by, bw, bh, 18);
c.clip();
// The called bar: a neutral dashed line across the frame at the
// needed height on the common scale, labelled at the left.
c.beginPath();
c.setLineDash([8, 8]);
c.moveTo(X0, py(m.targetY)); c.lineTo(X1, py(m.targetY));
c.strokeStyle = P.barLine; c.lineWidth = 2; c.globalAlpha = 0.85; c.stroke();
c.setLineDash([]);
c.globalAlpha = 1;
c.font = '800 22px system-ui, sans-serif';
c.fillStyle = P.barLine;
c.fillText(`BAR ${m.needed}`, X0 + 4, Math.max(by + 30, py(m.targetY) - 10));
// The previous run, quiet underneath on the same scale.
if (m.hasPrev && m.prevLine.length >= 2) {
c.beginPath();
m.prevLine.forEach((p, i) => { if (i === 0) c.moveTo(px(p.x), py(p.y)); else c.lineTo(px(p.x), py(p.y)); });
c.strokeStyle = P.prevLine; c.lineWidth = 2.5; c.lineJoin = 'round'; c.lineCap = 'round'; c.stroke();
const lp = m.prevLine[m.prevLine.length - 1];
c.font = '700 20px system-ui, sans-serif';
c.fillStyle = P.prevLine;
const lpw = c.measureText('last run').width;
c.fillText('last run', Math.max(X0, px(lp.x) - lpw), Math.min(YB - 4, py(lp.y) + 26));
}
if (m.peakScore > 0) {
c.beginPath();
c.setLineDash([10, 9]);
c.moveTo(X0, py(m.peakY)); c.lineTo(px(m.crash.x) - 26, py(m.peakY)); // stops short of the crash X
c.strokeStyle = chartColor; c.lineWidth = 2; c.globalAlpha = 0.55; c.stroke();
c.setLineDash([]);
c.globalAlpha = 1;
}
c.beginPath();
m.line.forEach((p, i) => { if (i === 0) c.moveTo(px(p.x), py(p.y)); else c.lineTo(px(p.x), py(p.y)); });
c.lineTo(px(m.crash.x), YB); c.lineTo(X0, YB); c.closePath();
c.fillStyle = chartColor; c.globalAlpha = 0.15; c.fill();
c.globalAlpha = 1;
c.beginPath();
m.line.forEach((p, i) => { if (i === 0) c.moveTo(px(p.x), py(p.y)); else c.lineTo(px(p.x), py(p.y)); });
c.strokeStyle = chartColor; c.lineWidth = 5; c.lineJoin = 'round'; c.lineCap = 'round'; c.stroke();
c.fillStyle = P.gold;
for (const o of m.orbs) c.fillRect(px(o.x) - 6, py(o.y) - 6, 12, 12);
c.fillStyle = P.pink;
for (const r of m.rugs) {
c.beginPath();
c.moveTo(px(r.x), py(r.y) - 13);
c.lineTo(px(r.x) + 10, py(r.y) + 8);
c.lineTo(px(r.x) - 10, py(r.y) + 8);
c.closePath(); c.fill();
}
c.beginPath();
c.moveTo(px(m.crash.x) - 10, py(m.crash.y) - 10); c.lineTo(px(m.crash.x) + 10, py(m.crash.y) + 10);
c.moveTo(px(m.crash.x) + 10, py(m.crash.y) - 10); c.lineTo(px(m.crash.x) - 10, py(m.crash.y) + 10);
c.strokeStyle = P.red; c.lineWidth = 6; c.stroke();
c.restore();
c.font = '800 25px system-ui, sans-serif';
c.fillStyle = P.dim;
const highText = `HIGH ${m.peakScore}`;
// Ends 30px left of the chart's right edge: the crash X owns the
// top-right corner and the label must never sit under it.
c.fillText(highText, X1 - 30 - c.measureText(highText).width, by + 36);
}
// Milestone tiles — one slim row now, so the chart can be tall.
const tileY = 888; const tileH = 94; const gap = 18;
const tileW = (bw - gap * 3) / 4;
card.tiles.forEach((tile, i) => {
const tx = bx + i * (tileW + gap);
rr(c, tx, tileY, tileW, tileH, 16);
c.fillStyle = P.panel; c.fill();
c.strokeStyle = P.line; c.lineWidth = 2; c.stroke();
c.font = '800 42px system-ui, sans-serif';
c.fillStyle = P.ink;
const vw = c.measureText(tile.value).width;
c.fillText(tile.value, tx + (tileW - vw) / 2, tileY + 42);
c.font = '700 19px system-ui, sans-serif';
c.fillStyle = P.dim;
const lw = c.measureText(tile.label).width;
c.fillText(tile.label, tx + (tileW - lw) / 2, tileY + 72);
});
// Footer pun.
c.font = '700 27px system-ui, sans-serif';
c.fillStyle = P.dim;
const fw = c.measureText(card.footer).width;
c.fillText(card.footer, (1080 - fw) / 2, 1028);
return cv.toDataURL('image/png');
}
function downloadPnl() {
const url = renderPnlPng();
if (!url) return;
const a = document.createElement('a');
a.href = url;
a.download = 'pump-rider-pnl.png';
document.body.append(a);
a.click();
a.remove();
}
// Sharing carries the CARD IMAGE, not just words. Preferred path:
// the share sheet (Web Share API) with the painted PNG attached as a
// File and the share text riding along, so the image lands in the
// post wherever the player sends it. Where file sharing is
// unsupported, the two-step fallback: the PNG downloads exactly like
// the Download button, the X intent opens with the text, and a toast
// tells the player to attach the saved card. A user-cancelled share
// sheet (AbortError) is silent — no fallback cascade.
async function shareOnX() {
if (!lastCard) return;
const text = lastCard.shareText;
try {
const pngUrl = renderPnlPng();
if (pngUrl && typeof navigator.canShare === 'function' && typeof navigator.share === 'function') {
const blob = await (await fetch(pngUrl)).blob();
const file = new File([blob], 'pump-rider-pnl.png', { type: 'image/png' });
if (navigator.canShare({ files: [file] })) {
await navigator.share({ files: [file], text });
return;
}
}
} catch (err) {
if (err && err.name === 'AbortError') return; // cancelled at the sheet: silent
// Any other share failure falls through to the download path.
}
downloadPnl();
const url = `https://x.com/intent/tweet?text=${encodeURIComponent(text)}`;
window.open(url, '_blank', 'noopener');
toast('card saved. attach it to your post.');
}
// Difficulty panels are collapsible on BOTH cards and closed by
// default: one row, tapped to open. Expanding the game-over one hides
// the PnL chart via CSS, so the taller card still fits the stage.
function setSettingsPanel(btn, body, open, onToggle) {
body.classList.toggle('hidden', !open);