Files
drip-runner/public/play.js
T

230 lines
28 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* Drip Runner client. The server owns the run (seed, drip claims, chapter clears, money);
this file draws it, takes taps, and reports what happened. */
(function () {
'use strict';
var $ = function (id) { return document.getElementById(id); };
var canvas = $('game'), ctx = canvas.getContext('2d');
var W = 390, H = 760, DPR = 1, floorY = H - DRSpawn.FLOOR;
var player = { x: 100, y: 300, vy: 0, r: 13, tilt: 0 };
var ME = null, P = null, BAL = null, POOL = null, IAP = 'https://instantadpay.com';
var R = null; // the live run
var muted = false; try { muted = localStorage.getItem('dr.muted') === '1'; } catch (e) {}
function api(path, body) {
return fetch(path, body ? { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) } : {}).then(function (r) { return r.json().then(function (j) { j._status = r.status; return j; }); });
}
function resize() { var r = $('phone').getBoundingClientRect(); W = Math.max(300, Math.round(r.width)); H = Math.max(500, Math.round(r.height)); DPR = Math.min(2, window.devicePixelRatio || 1); canvas.width = W * DPR; canvas.height = H * DPR; ctx.setTransform(DPR, 0, 0, DPR, 0, 0); floorY = H - DRSpawn.FLOOR; player.x = W * DRSpawn.PX; buildLayers(); }
window.addEventListener('resize', resize);
/* ---------- audio ---------- */
var AC = null; function ac() { if (muted) return null; try { if (!AC) AC = new (window.AudioContext || window.webkitAudioContext)(); if (AC.state === 'suspended') AC.resume(); return AC; } catch (e) { return null; } }
function tone(f, d, type, g, slide) { var a = ac(); if (!a) return; var o = a.createOscillator(), v = a.createGain(); o.type = type || 'square'; o.frequency.setValueAtTime(f, a.currentTime); if (slide) o.frequency.exponentialRampToValueAtTime(slide, a.currentTime + d); v.gain.setValueAtTime(g || .05, a.currentTime); v.gain.exponentialRampToValueAtTime(.0001, a.currentTime + d); o.connect(v); v.connect(a.destination); o.start(); o.stop(a.currentTime + d); }
var sfx = { hop: function () { tone(300, .09, 'square', .04, 520); }, coin: function () { tone(880, .08, 'sine', .05, 1320); setTimeout(function () { tone(1320, .1, 'sine', .04, 1760); }, 40); }, crash: function () { tone(180, .35, 'sawtooth', .08, 40); }, clear: function () { [523, 659, 784, 1047].forEach(function (f, i) { setTimeout(function () { tone(f, .18, 'triangle', .06); }, i * 90); }); }, shield: function () { tone(200, .2, 'triangle', .06, 600); }, buy: function () { tone(660, .08, 'sine', .04, 990); } };
$('mute').textContent = muted ? '×' : '♪';
$('mute').addEventListener('click', function () { muted = !muted; try { localStorage.setItem('dr.muted', muted ? '1' : '0'); } catch (e) {} $('mute').textContent = muted ? '×' : '♪'; });
/* ---------- sprites ---------- */
function sprite(sz, draw) { var c = document.createElement('canvas'); c.width = sz * 2; c.height = sz * 2; var x = c.getContext('2d'); x.scale(2, 2); draw(x, sz); return c; }
var dripSpr = sprite(36, function (x, s) { var c = s / 2; var g = x.createRadialGradient(c, c, 2, c, c, c); g.addColorStop(0, 'rgba(255,194,75,.9)'); g.addColorStop(.35, 'rgba(255,194,75,.35)'); g.addColorStop(1, 'rgba(255,194,75,0)'); x.fillStyle = g; x.fillRect(0, 0, s, s); var d = x.createRadialGradient(c - 3, c - 3, 1, c, c, 8); d.addColorStop(0, '#FFF4CC'); d.addColorStop(.5, '#FFC24B'); d.addColorStop(1, '#D97706'); x.fillStyle = d; x.beginPath(); x.arc(c, c, 8, 0, 6.283); x.fill(); x.strokeStyle = 'rgba(255,255,255,.7)'; x.lineWidth = 1.2; x.beginPath(); x.arc(c, c, 5.5, 3.6, 5.2); x.stroke(); });
var botSpr = sprite(56, function (x, s) { var c = s / 2; var g = x.createRadialGradient(c, c, 4, c, c, c); g.addColorStop(0, 'rgba(255,59,122,.7)'); g.addColorStop(.4, 'rgba(255,59,122,.25)'); g.addColorStop(1, 'rgba(255,59,122,0)'); x.fillStyle = g; x.fillRect(0, 0, s, s); x.translate(c, c); x.fillStyle = '#FF3B7A'; for (var i = 0; i < 4; i++) { x.rotate(Math.PI / 2); x.beginPath(); x.moveTo(0, -9); x.lineTo(5, -20); x.lineTo(-5, -20); x.closePath(); x.fill(); } var b = x.createLinearGradient(-10, -10, 10, 10); b.addColorStop(0, '#FF7AA6'); b.addColorStop(1, '#B3123F'); x.fillStyle = b; x.fillRect(-10, -10, 20, 20); x.fillStyle = '#070C18'; x.fillRect(-5, -5, 10, 10); x.fillStyle = '#FFE1EB'; x.fillRect(-2, -2, 4, 4); });
function shipSpr(gold) { return sprite(64, function (x, s) { var c = s / 2; x.translate(c, c); var glow = gold ? '255,194,75' : '34,211,238'; var g = x.createRadialGradient(0, 0, 4, 0, 0, c); g.addColorStop(0, 'rgba(' + glow + ',.55)'); g.addColorStop(1, 'rgba(' + glow + ',0)'); x.fillStyle = g; x.fillRect(-c, -c, s, s); var h = x.createLinearGradient(0, -10, 0, 10); h.addColorStop(0, '#FFFFFF'); h.addColorStop(.5, '#DCE6F5'); h.addColorStop(1, '#8FA3C4'); x.fillStyle = h; x.beginPath(); x.moveTo(-16, -8); x.lineTo(10, -9); x.quadraticCurveTo(22, -6, 22, 0); x.quadraticCurveTo(22, 6, 10, 9); x.lineTo(-16, 8); x.quadraticCurveTo(-20, 0, -16, -8); x.fill(); x.fillStyle = gold ? '#FFC24B' : '#22D3EE'; x.beginPath(); x.arc(9, 0, 4, 0, 6.283); x.fill(); x.fillStyle = '#fff'; x.beginPath(); x.arc(8, -1, 1.5, 0, 6.283); x.fill(); x.fillStyle = gold ? '#E08A00' : '#0EA5C9'; x.fillRect(-14, -3, 8, 6); x.fillStyle = '#4E5F7A'; x.fillRect(-12, -12, 10, 4); x.fillRect(-12, 8, 10, 4); }); }
var shipA = shipSpr(false), shipB = shipSpr(true);
/* ---------- background layers ---------- */
var stars = [], nodes = [], cols = [];
function buildLayers() { stars = []; for (var i = 0; i < 70; i++) stars.push({ x: Math.random() * W, y: Math.random() * (H - 60), s: Math.random() * 1.4 + .3, a: Math.random() }); nodes = []; for (var j = 0; j < 14; j++) nodes.push({ x: Math.random() * W, y: 80 + Math.random() * (H - 200), r: 2 + Math.random() * 2 }); cols = []; for (var k = 0; k < 6; k++) cols.push({ x: Math.random() * W, w: 30 + Math.random() * 60, h: 40 + Math.random() * 90, a: .05 + Math.random() * .08 }); }
/* ---------- ui helpers ---------- */
function hideAll() { ['ovLobby', 'ovClear', 'ovOver', 'ovHangar', 'ovBal', 'ovLoad'].forEach(function (id) { $(id).hidden = true; }); }
function show(id) { hideAll(); $(id).hidden = false; }
var toastT = null; function toast(m) { $('toast').textContent = m; $('toast').classList.add('on'); clearTimeout(toastT); toastT = setTimeout(function () { $('toast').classList.remove('on'); }, 2600); }
function pill(m) { var p = $('pill'); p.textContent = m; p.classList.remove('on'); void p.offsetWidth; p.classList.add('on'); setTimeout(function () { p.classList.remove('on'); }, 900); }
var hudCache = {}; function setHud() { if (!R) return; var v = { d: R.drips, c: R.chapter, b: Math.max(P.best, R.drips), g: R.chapDrips, gg: R.goal }; if (v.d !== hudCache.d) $('hDrips').textContent = v.d; if (v.c !== hudCache.c) $('hChap').textContent = v.c; if (v.b !== hudCache.b) $('hBest').textContent = v.b; if (v.g !== hudCache.g) $('hGoalN').textContent = v.g; if (v.gg !== hudCache.gg) $('hGoal').textContent = v.gg; hudCache = v; $('fuelBar').style.width = Math.max(0, R.fuel) + '%'; $('fuel').classList.toggle('low', R.fuel < 22); }
function f3(n) { return Number(n || 0).toFixed(3); }
/* ---------- lobby / me ---------- */
function loadMe() { return api('/api/me').then(function (r) { if (r._status !== 200) throw r; ME = r.me; P = r.player; BAL = r.balance; POOL = r.pool; IAP = r.iap || IAP; return r; }); }
function lobby() {
R = null; $('hud').classList.remove('on'); $('tapdesk').hidden = true;
$('lChap').textContent = P.chapter; $('lDrips').textContent = P.bank.toLocaleString(); $('lPol').textContent = f3(BAL.unpaid);
$('lPool').innerHTML = 'POL pool <b>' + f3(POOL.available) + '</b> · today’s cap left <b>' + f3(POOL.capLeft) + '</b> · a chapter drip is <b>' + f3(POOL.dripPol) + '</b>';
$('lMember').innerHTML = '<i class="' + (ME.wallet ? '' : 'no') + '"></i>InstantAdPay · ' + (ME.username ? '@' + ME.username : '#' + ME.memberId) + ' · ' + (ME.wallet ? ME.wallet.slice(0, 6) + '…' + ME.wallet.slice(-4) : 'no wallet on file');
show('ovLobby');
}
/* ---------- the run ---------- */
function newRun() {
ac();
return api('/api/run/start', { W: W, H: H }).then(function (r) {
if (r._status !== 200) { toast(r.error || 'Could not start a run.'); return; }
R = { id: r.runId, seed: r.seed, speed: r.speed, chapter: r.chapter, goal: r.goal, on: true, paused: false, t: 0, dist: 0, drips: 0, chapDrips: 0, fuel: 100, ents: [], parts: [], shake: 0, inv: 2, shield: !!r.armed.shield, magnet: !!r.armed.magnet, slow: r.armed.slow ? 15 : 0, ship: r.ship, refuels: 0, pending: [], consumed: { d: 0, b: 0, g: 0 }, lastFlush: 0, W: W, H: H };
makeGens(); player.y = floorY - 120; player.vy = 0; hudCache = {}; setHud(); $('hud').classList.add('on'); $('tapdesk').hidden = !matchMedia('(min-width:700px)').matches; hideAll();
if (R.magnet) toast('Drip magnet on'); if (R.shield) toast('Shield core armed');
});
}
function makeGens() { R.gen = { d: DRSpawn.drips(R.seed, R.W, R.H), b: DRSpawn.bots(R.seed, R.chapter, R.H), g: DRSpawn.gates(R.seed, R.chapter, R.H) }; R.next = { d: R.gen.d(), b: R.gen.b(), g: R.gen.g() }; for (var i = 0; i < R.consumed.d; i++) R.next.d = R.gen.d(); for (var j = 0; j < R.consumed.b; j++) R.next.b = R.gen.b(); for (var k = 0; k < R.consumed.g; k++) R.next.g = R.gen.g(); }
function spawn() {
while (R.next.d.t <= R.t) { var g = R.next.d; for (var i = 0; i < g.drips.length; i++) { var d = g.drips[i]; R.ents.push({ k: 'd', i: d.i, x: d.x0 - (R.t - g.t) * R.speed, y: d.y, r: 9, ph: Math.random() * 6 }); } R.next.d = R.gen.d(); R.consumed.d++; }
while (R.next.b.t <= R.t) { var b = R.next.b; R.ents.push({ k: 'b', x: R.W + 60 - (R.t - b.t) * R.speed, y: b.y, r: 15, amp: b.amp, ph: b.ph, rot: 0 }); R.next.b = R.gen.b(); R.consumed.b++; }
while (R.next.g.t <= R.t) { var q = R.next.g; if (R.chapter >= 2) R.ents.push({ k: 'g', x: R.W + 60 - (R.t - q.t) * R.speed, y: q.y, gap: q.gap, w: 34 }); R.next.g = R.gen.g(); R.consumed.g++; }
}
function hop() { if (!R || !R.on || R.paused) return; player.vy = -(R.ship === 2 ? 500 : 520); sfx.hop(); burst(player.x - 12, player.y + 4, 4, R.ship === 2 ? '255,194,75' : '34,211,238', 60); }
canvas.addEventListener('pointerdown', function (e) { e.preventDefault(); ac(); hop(); });
window.addEventListener('keydown', function (e) { if (e.code === 'Space' || e.code === 'ArrowUp') { if (R && R.on && !R.paused) { e.preventDefault(); hop(); } } });
function burst(x, y, n, rgb, sp) { if (!R) return; for (var i = 0; i < n; i++) { var a = Math.random() * 6.283, s = sp * (.4 + Math.random()); R.parts.push({ x: x, y: y, vx: Math.cos(a) * s, vy: Math.sin(a) * s, l: .5 + Math.random() * .4, c: rgb, r: 1.5 + Math.random() * 2 }); } }
function flush(goal) {
if (!R) return Promise.resolve({});
var evs = R.pending; R.pending = []; R.lastFlush = R.t;
if (!evs.length && !goal) return Promise.resolve({});
return api('/api/run/events', { runId: R.id, t: R.t, events: evs, goal: !!goal }).then(function (r) {
if (r._status !== 200) { if (r.over && R) { R.on = false; } toast(r.error || 'Sync problem'); return r; }
if (R) { R.drips = r.drips; R.chapDrips = r.chapDrips; R.goal = r.goal; if (r.refused) toast('Server refused ' + r.refused + ' drip' + (r.refused > 1 ? 's' : '') + ' (' + (r.why || []).join(',') + ')'); }
return r;
}).catch(function () { R && (R.pending = evs.concat(R.pending)); return {}; });
}
function update(dt) {
if (!R || !R.on || R.paused) return; R.t += dt; var sp = R.speed; R.dist += sp * dt; if (R.slow > 0) R.slow -= dt; if (R.inv > 0) R.inv -= dt;
R.fuel -= dt * 2.4; if (R.fuel <= 0) { R.fuel = 0; return over('Out of fuel', 'The tank is dry.'); }
player.vy += 1500 * dt; if (player.vy > 900) player.vy = 900; player.y += player.vy * dt;
if (player.y > floorY - player.r) { player.y = floorY - player.r; if (player.vy > 200) burst(player.x, player.y + 8, 3, '34,211,238', 40); player.vy = 0; }
if (player.y < 70) { player.y = 70; player.vy = 0; }
player.tilt += ((player.vy / 900) * .6 - player.tilt) * Math.min(1, dt * 10);
spawn();
var ents = R.ents, i;
for (i = ents.length - 1; i >= 0; i--) { var e = ents[i]; var es = e.k === 'b' && R.slow > 0 ? sp * .45 : sp; e.x -= es * dt;
if (e.k === 'd') { e.ph += dt * 4; if (R.magnet) { var dx = player.x - e.x, dy = player.y - e.y, dd = Math.hypot(dx, dy); if (dd < 150) { e.x += dx / dd * 420 * dt; e.y += dy / dd * 420 * dt; } }
if (Math.hypot(player.x - e.x, player.y - e.y) < player.r + e.r + 2) { ents.splice(i, 1); R.drips++; R.chapDrips++; R.fuel = Math.min(100, R.fuel + 3.5); sfx.coin(); burst(e.x, e.y, 8, '255,194,75', 140); R.pending.push({ k: 'd', i: e.i, t: Math.round(R.t * 1000) / 1000 });
if (R.chapDrips >= R.goal) return cleared(); continue; } }
if (e.k === 'b') { e.rot += dt * 3; var by = e.y + (e.amp ? Math.sin(R.t * 2 + e.ph) * e.amp : 0); e.cy = by; if (R.inv <= 0 && Math.hypot(player.x - e.x, player.y - by) < player.r + e.r - 2) { if (R.shield) { R.shield = false; R.inv = 1.5; ents.splice(i, 1); sfx.shield(); burst(e.x, by, 20, '255,106,26', 220); pill('Shield!'); R.shake = .25; continue; } return crash(e.x, by); } }
if (e.k === 'g') { if (R.inv <= 0 && player.x + player.r > e.x && player.x - player.r < e.x + e.w && (player.y - player.r < e.y || player.y + player.r > e.y + e.gap)) { if (R.shield) { R.shield = false; R.inv = 1.5; sfx.shield(); pill('Shield!'); R.shake = .25; } else return crash(player.x, player.y); } }
if (e.x < -90) ents.splice(i, 1); }
var ps = R.parts; for (i = ps.length - 1; i >= 0; i--) { var p = ps[i]; p.l -= dt; p.x += p.vx * dt; p.y += p.vy * dt; p.vy += 300 * dt; if (p.l <= 0) ps.splice(i, 1); }
if (R.shake > 0) R.shake -= dt;
if (Math.random() < .9) R.parts.push({ x: player.x - 16, y: player.y + (Math.random() - .5) * 6, vx: -sp * .5 - 40, vy: (Math.random() - .5) * 30, l: .3, c: R.ship === 2 ? '255,194,75' : '34,211,238', r: 2 + Math.random() * 2 });
if (R.t - R.lastFlush > 2.5 && R.pending.length) flush(false);
setHud();
}
function crash(x, y) { sfx.crash(); burst(x, y, 36, '255,59,122', 260); burst(x, y, 14, '255,255,255', 160); R.shake = .5; over('Courier hit', 'A bot got you.'); }
function over(title, why) {
R.paused = true; flush(false);
setTimeout(function () { if (!R) return; $('oEyebrow').textContent = title; $('oTitle').textContent = 'Run over at ' + R.drips; $('oText').textContent = why + ' Refuel with a sponsor visit and keep this run, drips and chapter progress intact.'; $('bRefuel').hidden = false; $('oChip').hidden = false; show('ovOver'); }, 450);
}
function cleared() {
R.paused = true; pill('Verifying…');
flush(true).then(function (r) {
if (!R) return;
if (!r.cleared) { R.paused = false; if (r.short) toast('Server count says ' + r.short + ' more to go'); return; }
var c = r.cleared; sfx.clear(); burst(player.x, player.y, 40, '255,194,75', 220);
$('cEyebrow').textContent = 'Chapter ' + c.chapter + ' cleared'; $('cTitle').textContent = 'Banked ' + (R.goal) + ' drips';
var m = Math.floor(c.playTime / 60), s = c.playTime % 60; var tm = m + ':' + (s < 10 ? '0' : '') + s;
$('cBig').innerHTML = '+' + f3(c.pol) + '<small>POL</small>'; $('cBig').classList.toggle('dim', !c.pol);
var why = { paid: 'First clear, verified. Added to your game balance.', replay: c.first ? 'Verified.' : 'You cleared this one before. Only the first clear pays.', pool: 'The POL pool is empty right now. Refuel visits fill it, so this clear counts but pays nothing.', cap: 'Today’s pool cap is spent. Chapter counts, no POL today.', flagged: 'This run was flagged by the server check, so it pays nothing.' };
$('cText').textContent = why[c.reason] || ''; $('cChip').className = 'chip ' + (c.pol ? 'ok' : 'sig'); $('cChip').textContent = (c.pol ? 'verified' : c.reason) + ' · ' + tm + ' play time';
R.chapter = c.next; R.goal = c.nextGoal; R.chapDrips = 0; if (r.pool != null) POOL.available = r.pool;
setTimeout(function () { show('ovClear'); }, 500);
});
}
$('bNext').addEventListener('click', function () { if (!R) return; R.paused = false; R.inv = 1.5; hideAll(); });
$('bRestart').addEventListener('click', function () { endRun().then(newRun); });
function endRun() { var id = R && R.id; R = null; hideAll(); return id ? api('/api/run/end', { runId: id }).catch(function () {}) : Promise.resolve(); }
['bLobby1', 'bLobby2'].forEach(function (id) { $(id).addEventListener('click', function () { endRun().then(loadMe).then(lobby); }); });
['bLobby3', 'bLobby4'].forEach(function (id) { $(id).addEventListener('click', function () { loadMe().then(lobby); }); });
$('bPlay').addEventListener('click', function () { newRun(); });
/* ---------- refuel: hand the run to InstantAdPay and come back ---------- */
function snapshot(nonce) {
var s = { runId: R.id, nonce: nonce, seed: R.seed, speed: R.speed, chapter: R.chapter, goal: R.goal, t: R.t, dist: R.dist, drips: R.drips, chapDrips: R.chapDrips, ents: R.ents.map(function (e) { var o = {}; for (var k in e) if (k !== 'cy') o[k] = e[k]; return o; }), consumed: R.consumed, y: player.y, shield: R.shield, magnet: R.magnet, slow: R.slow, ship: R.ship, refuels: R.refuels, pending: R.pending, W: R.W, H: R.H, at: Date.now() };
try { sessionStorage.setItem('dr.snap', JSON.stringify(s)); } catch (e) {}
}
$('bRefuel').addEventListener('click', function () {
if (!R) return; $('bRefuel').disabled = true;
api('/api/refuel/start', { runId: R.id }).then(function (r) {
$('bRefuel').disabled = false;
if (r._status !== 200) return toast(r.error || 'Refuel is not available right now.');
snapshot(r.nonce); location.href = r.url;
});
});
function tryResume(q) {
var snap = null; try { snap = JSON.parse(sessionStorage.getItem('dr.snap') || 'null'); } catch (e) {}
if (!snap || snap.runId !== q.get('resume')) { toast('That run could not be restored.'); return lobby(); }
return api('/api/refuel/resume', { runId: snap.runId, nonce: q.get('n') || snap.nonce, receipt: q.get('r') || '', none: q.get('none') === '1' }).then(function (r) {
try { sessionStorage.removeItem('dr.snap'); } catch (e) {}
R = { id: snap.runId, seed: snap.seed, speed: snap.speed, chapter: snap.chapter, goal: snap.goal, on: true, paused: true, t: snap.t, dist: snap.dist, drips: snap.drips, chapDrips: snap.chapDrips, fuel: 0, ents: snap.ents, parts: [], shake: 0, inv: 3, shield: snap.shield, magnet: snap.magnet, slow: snap.slow, ship: snap.ship, refuels: snap.refuels, pending: snap.pending || [], consumed: snap.consumed, lastFlush: snap.t, W: snap.W, H: snap.H };
if (R.W !== W || R.H !== H) { var sx = W / R.W, sy = H / R.H; R.ents.forEach(function (e) { e.x *= sx; e.y *= sy; }); }
makeGens(); player.y = snap.y; player.vy = 0; hudCache = {}; $('hud').classList.add('on'); hideAll();
if (!r.ok) { $('oEyebrow').textContent = r.reason === 'none' ? 'No sponsor pages right now' : 'Visit not verified'; $('oTitle').textContent = 'Run over at ' + R.drips; $('oText').textContent = r.reason === 'none' ? 'InstantAdPay had no verified-visit page for you at the moment. Restart, or try refuelling again later.' : 'The visit did not complete, so the tank stays empty. Refuel again or restart.'; $('bRefuel').hidden = r.reason === 'none'; $('oChip').hidden = r.reason === 'none'; setHud(); return show('ovOver'); }
R.fuel = 100; R.refuels++; R.ents = R.ents.filter(function (e) { return e.k === 'd' || e.x > player.x + 260; }); if (r.pool != null) POOL.available = r.pool;
setHud(); var n = 3; pill('Refuelled · ' + n); var iv = setInterval(function () { n--; if (n <= 0) { clearInterval(iv); pill('Go'); if (R) R.paused = false; if (R && R.pending.length) flush(false); } else pill('' + n); }, 800);
});
}
/* ---------- hangar / balance ---------- */
function hangar() {
show('ovHangar'); $('hgBal').textContent = P.bank.toLocaleString() + ' drips · ' + f3(BAL.unpaid) + ' POL';
var pr = P.prices;
set('itMagnet', 'magnet', P.items.magnet, P.bank >= pr.drips.magnet, pr.drips.magnet + ' drips', 'armed');
set('itShip', 'ship', P.items.ship, P.bank >= pr.drips.ship, pr.drips.ship + ' drips', 'owned');
set('itShield', 'shield', P.items.shield, BAL.unpaid >= pr.pol.shield, f3(pr.pol.shield) + ' POL', 'armed');
set('itSlow', 'slow', P.items.slow, BAL.unpaid >= pr.pol.slow, f3(pr.pol.slow) + ' POL', 'armed');
function set(id, key, own, can, label, ownLabel) { var it = $(id), b = it.querySelector('.buy'); it.classList.toggle('owned', !!own); b.className = 'buy' + (/POL/.test(label) ? ' pol' : '') + (own ? ' own' : (can ? '' : ' no')); b.textContent = own ? ownLabel : label; }
}
$('ovHangar').addEventListener('click', function (e) { var b = e.target.closest('[data-buy]'); if (!b) return; b.disabled = true;
api('/api/hangar/buy', { item: b.dataset.buy }).then(function (r) { b.disabled = false; if (r._status !== 200) return toast(r.error || 'Could not buy that.'); P = r.player; BAL = r.balance; sfx.buy(); toast('Paid ' + r.paid); hangar(); }); });
function balance() {
show('ovBal'); api('/api/balance').then(function (b) { BAL = b;
$('bPol').textContent = f3(b.unpaid); var pct = Math.min(100, b.unpaid / b.threshold * 100); $('bProg').style.width = pct + '%';
$('bNote').textContent = 'Pays to your wallet at ' + b.threshold + ' POL. ' + pct.toFixed(0) + '% of the way. Paid so far: ' + f3(b.paidTotal) + ' POL.';
var h = ''; if (!b.history.length) h = '<div class="led"><span>No drips yet. Clear a chapter.</span><span class="m">0.000</span></div>';
b.history.forEach(function (l) { var cls = l.kind === 'payout' ? (l.status === 'paid' ? 'm ok' : l.status === 'failed' ? 'm neg' : 'm') : (l.pol < 0 ? 'm neg' : 'm'); h += '<div class="led"><span>' + esc(l.label) + (l.tx ? ' · <a href="https://polygonscan.com/tx/' + esc(l.tx) + '" target="_blank" rel="noopener">tx</a>' : '') + '</span><span class="' + cls + '">' + (l.kind === 'payout' ? '' : (l.pol < 0 ? '' : '+')) + f3(l.pol) + '</span></div>'; });
$('ledger').innerHTML = h;
$('bWallet').textContent = b.wallet ? 'Wallet: ' + b.wallet + ' (from your InstantAdPay account).' : 'No wallet on your InstantAdPay account yet. Link one there or payouts cannot be sent.';
});
}
function esc(s) { return String(s == null ? '' : s).replace(/[&<>"']/g, function (c) { return { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c]; }); }
$('bHangar').addEventListener('click', hangar); $('bBalance').addEventListener('click', balance);
/* ---------- draw ---------- */
function draw() {
ctx.clearRect(0, 0, W, H); var t = R ? R.t : performance.now() / 1000, dist = R ? R.dist : performance.now() * .05;
var bg = ctx.createLinearGradient(0, 0, 0, H); bg.addColorStop(0, '#03060E'); bg.addColorStop(.6, '#0A1327'); bg.addColorStop(1, '#101C3C'); ctx.fillStyle = bg; ctx.fillRect(0, 0, W, H);
var s2 = ctx.createRadialGradient(W * .5, H * .85, 10, W * .5, H * .85, W * .9); s2.addColorStop(0, 'rgba(34,211,238,.12)'); s2.addColorStop(1, 'rgba(34,211,238,0)'); ctx.fillStyle = s2; ctx.fillRect(0, 0, W, H);
ctx.save(); if (R && R.shake > 0) ctx.translate((Math.random() - .5) * R.shake * 24, (Math.random() - .5) * R.shake * 24);
var i; for (i = 0; i < stars.length; i++) { var st = stars[i]; var sx = ((st.x - dist * .15) % W + W) % W; ctx.globalAlpha = .35 + .5 * Math.abs(Math.sin(t * 1.5 + st.a * 6)); ctx.fillStyle = '#CFE3FF'; ctx.fillRect(sx, st.y, st.s, st.s); } ctx.globalAlpha = 1;
for (i = 0; i < cols.length; i++) { var c = cols[i]; var cx = ((c.x - dist * .3) % (W + 120) + W + 120) % (W + 120) - 60; ctx.fillStyle = 'rgba(31,111,235,' + c.a + ')'; ctx.fillRect(cx, floorY - c.h, c.w, c.h); ctx.fillStyle = 'rgba(143,240,255,' + c.a * 1.5 + ')'; for (var wy = floorY - c.h + 8; wy < floorY - 6; wy += 12) for (var wx = cx + 5; wx < cx + c.w - 5; wx += 10) if (((wx * 7 + wy * 3) | 0) % 5 < 2) ctx.fillRect(wx, wy, 4, 5); }
ctx.lineWidth = 1; for (i = 0; i < nodes.length; i++) { var n = nodes[i]; n.sx = ((n.x - dist * .55) % (W + 80) + W + 80) % (W + 80) - 40; }
for (i = 0; i < nodes.length; i++) { var a = nodes[i]; for (var j = i + 1; j < nodes.length; j++) { var b = nodes[j], dd = Math.hypot(a.sx - b.sx, a.y - b.y); if (dd < 150) { ctx.strokeStyle = 'rgba(34,211,238,' + (0.16 * (1 - dd / 150)) + ')'; ctx.beginPath(); ctx.moveTo(a.sx, a.y); ctx.lineTo(b.sx, b.y); ctx.stroke(); } } }
for (i = 0; i < nodes.length; i++) { var q = nodes[i]; ctx.fillStyle = 'rgba(143,240,255,.5)'; ctx.beginPath(); ctx.arc(q.sx, q.y, q.r, 0, 6.283); ctx.fill(); }
ctx.strokeStyle = 'rgba(34,211,238,.08)'; var gx = -(dist * .8 % 44); for (var x = gx; x < W; x += 44) { ctx.beginPath(); ctx.moveTo(x, 60); ctx.lineTo(x, floorY); ctx.stroke(); } for (var y = 60; y < floorY; y += 44) { ctx.beginPath(); ctx.moveTo(0, y); ctx.lineTo(W, y); ctx.stroke(); }
var fg = ctx.createLinearGradient(0, floorY, 0, H); fg.addColorStop(0, '#0E1B3B'); fg.addColorStop(1, '#060B18'); ctx.fillStyle = fg; ctx.fillRect(0, floorY, W, H - floorY);
var rail = ctx.createLinearGradient(0, floorY - 6, 0, floorY + 6); rail.addColorStop(0, 'rgba(34,211,238,0)'); rail.addColorStop(.5, 'rgba(143,240,255,.9)'); rail.addColorStop(1, 'rgba(34,211,238,0)'); ctx.fillStyle = rail; ctx.fillRect(0, floorY - 6, W, 12);
ctx.strokeStyle = 'rgba(34,211,238,.35)'; ctx.lineWidth = 2; ctx.setLineDash([26, 18]); ctx.lineDashOffset = -(dist * 1.6 % 44); ctx.beginPath(); ctx.moveTo(0, floorY + 26); ctx.lineTo(W, floorY + 26); ctx.stroke(); ctx.setLineDash([]);
ctx.fillStyle = 'rgba(255,106,26,.5)'; for (var fx = -(dist * 2.2 % 60); fx < W; fx += 60) ctx.fillRect(fx, floorY + 48, 22, 3);
if (!R) { ctx.restore(); return; }
for (i = 0; i < R.ents.length; i++) { var e = R.ents[i];
if (e.k === 'g') { var gg = ctx.createLinearGradient(e.x, 0, e.x + e.w, 0); gg.addColorStop(0, '#FF6A1A'); gg.addColorStop(.5, '#FFB088'); gg.addColorStop(1, '#D44E00'); ctx.fillStyle = gg; ctx.fillRect(e.x, 60, e.w, e.y - 60); ctx.fillRect(e.x, e.y + e.gap, e.w, floorY - e.y - e.gap); ctx.fillStyle = 'rgba(0,0,0,.35)'; for (var sy = 64 + (dist % 24); sy < e.y - 4; sy += 24) ctx.fillRect(e.x, sy, e.w, 8); for (sy = e.y + e.gap + 4 + (dist % 24); sy < floorY - 4; sy += 24) ctx.fillRect(e.x, sy, e.w, 8); ctx.fillStyle = '#FFE1CC'; ctx.shadowColor = '#FF6A1A'; ctx.shadowBlur = 18; ctx.fillRect(e.x - 3, e.y - 4, e.w + 6, 4); ctx.fillRect(e.x - 3, e.y + e.gap, e.w + 6, 4); ctx.shadowBlur = 0; }
else if (e.k === 'd') { var dy = e.y + Math.sin(e.ph) * 3, sc = 1 + Math.sin(e.ph * 1.3) * .08; ctx.drawImage(dripSpr, e.x - 18 * sc, dy - 18 * sc, 36 * sc, 36 * sc); }
else { ctx.save(); ctx.translate(e.x, e.cy || e.y); ctx.rotate(e.rot); if (R.slow > 0) ctx.globalAlpha = .75; ctx.drawImage(botSpr, -28, -28, 56, 56); ctx.restore(); } }
for (i = 0; i < R.parts.length; i++) { var p = R.parts[i]; ctx.globalAlpha = Math.max(0, Math.min(1, p.l * 2)); ctx.fillStyle = 'rgb(' + p.c + ')'; ctx.beginPath(); ctx.arc(p.x, p.y, p.r, 0, 6.283); ctx.fill(); } ctx.globalAlpha = 1;
ctx.save(); ctx.translate(player.x, player.y); ctx.rotate(player.tilt); if (R.inv > 0 && Math.floor(R.t * 12) % 2 === 0) ctx.globalAlpha = .45;
if (R.magnet) { ctx.strokeStyle = 'rgba(255,194,75,.35)'; ctx.lineWidth = 1.5; ctx.setLineDash([4, 6]); ctx.beginPath(); ctx.arc(0, 0, 150, 0, 6.283); ctx.stroke(); ctx.setLineDash([]); }
ctx.drawImage(R.ship === 2 ? shipB : shipA, -32, -32, 64, 64);
if (R.shield) { ctx.strokeStyle = 'rgba(255,106,26,.85)'; ctx.lineWidth = 2; ctx.shadowColor = '#FF6A1A'; ctx.shadowBlur = 14; ctx.beginPath(); ctx.arc(0, 0, 22, 0, 6.283); ctx.stroke(); ctx.shadowBlur = 0; }
ctx.restore();
if (R.slow > 0) { ctx.fillStyle = 'rgba(139,92,246,' + Math.min(.12, R.slow * .03) + ')'; ctx.fillRect(0, 0, W, H); }
ctx.restore();
}
var last = performance.now(); function loop(now) { var dt = Math.min(.033, (now - last) / 1000); last = now; update(dt); draw(); requestAnimationFrame(loop); }
/* ---------- boot ---------- */
resize(); requestAnimationFrame(loop);
loadMe().then(function () {
var q = new URLSearchParams(location.search);
if (q.get('resume')) { history.replaceState(null, '', '/play'); return tryResume(q); }
lobby();
}).catch(function (r) {
$('loadTitle').textContent = 'Sign in first'; $('loadText').textContent = (r && r.error) || 'Open Drip Runner from your InstantAdPay dashboard.'; var b = $('loadBtn'); b.hidden = false; b.textContent = 'Open through InstantAdPay'; b.href = (r && r.iap) || IAP + '/api/my/driprunner';
});
})();