Skip to content

gitflarePublicPartly agent-ready40

neon-drift

Real-time multiplayer arcade racer on Cloudflare Workers and Durable Objects.

main
Add the Apache-2.0 licence3efe3da
JavaScript62 lines2.5 KB
export function cleanName(raw) {  return String(raw ?? '').replace(/[\u0000-\u001f\u007f]/g, '').replace(/\s+/g, ' ').trim().slice(0, 16) || 'Player';}export function fmtTime(sec) {  const s = Math.max(0, Math.floor(sec || 0));  return Math.floor(s / 60) + ':' + String(s % 60).padStart(2, '0');}export function parseRoomPath(path) {  const m = /^\/r\/([A-Za-z0-9]{6})\/?$/.exec(path);  return m ? m[1].toUpperCase() : null;}export function liveText(h) {  if (!h || typeof h.players !== 'number') return 'servers warming up…';  return h.players + (h.players === 1 ? ' player' : ' players') + ' in ' + h.rooms + (h.rooms === 1 ? ' arena' : ' arenas');}/** Push a crash into a feed capped at max entries (newest last). */export function pushFeed(feed, text, max = 4) {  feed.push(text);  while (feed.length > max) feed.shift();  return feed;}export function crashText(names, victim, killer) {  const v = names.get ? names.get(victim) : names[victim];  const k = killer >= 0 ? (names.get ? names.get(killer) : names[killer]) : null;  return (k && killer !== victim) ? `${k} ✕ ${v ?? '?'}` : `${v ?? '?'} crashed`;}/** Banner text for waiting/queued/spectating states ('' if none applies). */export function queuedBanner(net) {  if (net.role === 'spectator' && net.queued) {    if (net.phase === 'results' || net.phase === 'countdown') {      const n = Math.max(0, Math.ceil(net.resLeft != null ? net.resLeft : (net.secondsLeft || 0)));      return net.phase === 'results' ? `You're in — next round starts in ${n} s` : "You're in — round starting…";    }    return "You're in — next round starts soon";  }  return '';}export function hueColor(hue) { return `hsl(${Math.round(hue || 0)} 100% 60%)`; }export function resultLine(score, kills) {  return `${score} pts · ${kills} ${kills === 1 ? 'kill' : 'kills'}`;}export function waitingText(n) {  return `Waiting for riders · ${n}/16 — share the invite link`;}export function elimText(names, killer) {  const k = killer >= 0 ? (names.get ? names.get(killer) : names[killer]) : null;  return k ? `Eliminated by ${k}` : 'Hit the wall';}export function leaderName(net) {  let best = null, bs = -1;  if (net.trails && net.names) {    for (const [id, t] of net.trails) {      if (!t.alive || id === net.id) continue;      if (bs < 0) { best = id; bs = 0; }    }  }  return best == null ? null : (net.names[best] ?? null);}export function lastWinners(entries, n = 5) {  return (entries || []).slice(0, n).map((e) => e.name);}