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
JavaScript133 lines5.3 KB
import test from 'node:test';import assert from 'node:assert/strict';import { createState, addPlayer, step, stepCycle, appendTrail, hashState, TICK_HZ } from '../public/shared/sim.js'; function game(n, seed = 5) {  const st = createState(seed);  for (let i = 1; i <= n; i++) addPlayer(st, i, 'p' + i);  return st;}function toPlaying(st) {  let ev = [];  for (let i = 0; i < 3 * TICK_HZ + 2; i++) ev = ev.concat(step(st, new Map()));  assert.equal(st.phase, 'playing');  return ev;}const park = (st) => { // move everyone to safe spots, stationary-ish  let k = 0;  for (const p of st.players.values()) { p.x = -200 + 100 * k; p.y = 0; p.heading = Math.PI / 2; p.trail = [p.x, p.y]; k++; }}; test('deterministic replay', () => {  const run = () => {    const st = game(4);    for (let i = 0; i < 400; i++) step(st, new Map([[1, { turn: (i >> 4) % 3 - 1, boost: i % 50 < 10 ? 1 : 0 }]]));    return hashState(st);  };  assert.equal(run(), run());}); test('stepCycle boost drain/refill and appendTrail', () => {  const p = { x: 0, y: 0, heading: 0, meter: 100, boost: 0, speed: 0, trail: [0, 0], lastHeading: 0, sinceAppend: 0, trailStart: 0, trailBase: 0 };  stepCycle(p, { turn: 0, boost: 1 }, 1 / 30);  assert.ok(Math.abs(p.speed - 272) < 1e-9 && p.meter < 100);  p.meter = 0; stepCycle(p, { turn: 0, boost: 1 }, 1 / 30);  assert.equal(p.boost, 0);  for (let i = 0; i < 6; i++) appendTrail(p);  assert.equal(p.trail.length, 4);}); test('countdown, spawn ring, distinct hues, protection', () => {  const st = game(4);  step(st, new Map());  assert.equal(st.phase, 'countdown');  assert.equal(new Set([...st.players.values()].map((p) => p.hue)).size, 4);  toPlaying(st);  const p = st.players.get(1);  p.trail = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; p.x = 0; p.y = 0; p.spawnProt = 10;  const q = st.players.get(2); q.x = 1; q.y = 0; q.heading = 0; q.spawnProt = 10; q.trail = [1, 0];  step(st, new Map());  assert.equal(q.alive, true);}); test('kill credit, survival and orb scoring, events', () => {  const st = game(3); toPlaying(st); park(st);  for (const p of st.players.values()) p.spawnProt = 0;  const a = st.players.get(1), b = st.players.get(2);  a.trail = [0, -100, 0, 100]; a.x = 500; a.y = 500;  b.x = -3; b.y = 0; b.heading = 0; b.trail = [-3, 0];  const c0 = st.players.get(3); c0.x = 300; c0.y = 300; c0.trail = [300, 300];  const ev = step(st, new Map());  assert.ok(ev.some((e) => e[0] === 'crash' && e[1] === 2 && e[2] === 1));  assert.equal(a.kills, 1); assert.ok(a.score >= 50);  const c = st.players.get(3); c.x = 300; c.y = 300; c.heading = 0; c.trail = [300, 300];  st.orbs.push([999, 305, 300]);  const s0 = c.score; c.meter = 10;  const ev2 = step(st, new Map());  assert.ok(ev2.some((e) => e[0] === 'orb' && e[1] === 3 && e[2] === 999));  assert.ok(c.score >= s0 + 10 && c.meter > 40);  step(st, new Map()); assert.ok(st.orbs.length >= 6 + 2);}); test('shrink warning at 17s, shrink at 20s over 2s', () => {  const st = game(2); toPlaying(st);  const t0 = st.tick; let warn = -1, shrink = -1;  for (let i = 0; i < 25 * TICK_HZ && shrink < 0; i++) {    park(st);    for (const p of st.players.values()) p.heading = 0;    for (const e of step(st, new Map())) {      if (e[0] === 'warn') warn = st.tick - t0;      if (e[0] === 'shrink') shrink = st.tick - t0;    }    if (st.phase !== 'playing') break;  }  assert.ok(Math.abs(warn - 17 * TICK_HZ) <= 3, 'warn ' + warn);  assert.ok(Math.abs(shrink - 20 * TICK_HZ) <= 3, 'shrink ' + shrink);  assert.ok(Math.abs(st.targetRadius - 880) < 1e-6);  for (let i = 0; i < 2 * TICK_HZ + 1; i++) { park(st); step(st, new Map()); }  assert.ok(Math.abs(st.radius - 880) < 1e-6);  assert.equal(st.warnUntilTick, -1);}); test('match ends by last alive with results and rematch', () => {  const st = game(3); toPlaying(st);  st.players.get(2).x = 5000; st.players.get(3).x = 5000;  let res = null;  for (let i = 0; i < 5 && !res; i++) for (const e of step(st, new Map())) if (e[0] === 'results') res = e[1];  assert.ok(res);  assert.equal(res.length, 3);  assert.equal(res[0][4], 1);  assert.equal(res[0][0], 1);  assert.deepEqual(res.map((r) => r[4]), [1, 2, 3]);  assert.equal(st.phase, 'results');  for (let i = 0; i < 10 * TICK_HZ + 1; i++) step(st, new Map());  assert.equal(st.phase, 'countdown');  assert.ok([...st.players.values()].every((p) => p.alive && p.score === 0));}); test('match ends after 3 minutes', () => {  const st = game(2); toPlaying(st);  let ended = false;  for (let i = 0; i < 181 * TICK_HZ && !ended; i++) {    park(st);    for (const p of st.players.values()) { p.heading = 0; p.trail = [p.x, p.y]; }    st.radius = st.targetRadius = 1000; st.nextShrinkTick = st.tick + 1000;    for (const e of step(st, new Map())) if (e[0] === 'results') ended = true;  }  assert.ok(ended);}); test('perf: 16 players full trails', () => {  const st = game(16); toPlaying(st);  for (const p of st.players.values()) {    p.spawnProt = 0;    const a = Math.random() * 6;    for (let i = 0; i < 600; i++) p.trail.push(Math.cos(i / 20 + a) * (100 + i / 2), Math.sin(i / 20 + a) * (100 + i / 2));  }  const t = performance.now();  const keep = () => { for (const p of st.players.values()) { p.alive = true; p.x = 0; p.y = 0; } };  for (let i = 0; i < 300; i++) { keep(); step(st, new Map()); }  const avg = (performance.now() - t) / 300;  assert.ok(avg < 4, 'avg ms ' + avg);});