Skip to content

gitflarePublicPartly agent-ready40

orbit-duel

Real-time two-player orbital combat game on Cloudflare Workers and Durable Objects.

main
Add the Apache-2.0 licence0413e56
JavaScript67 lines2.4 KB
import test from 'node:test';import assert from 'node:assert/strict';import { createState, addPlayer, step, sanitizeInput, hashState, snapshotOf, PLANET_R, CX, CY, MAX_HP } from '../public/shared/sim.js';import { encodeSnapshot, applySnapshot } from '../public/shared/protocol.js'; function run(seed) {  const st = createState(seed);  addPlayer(st, 1, 'a'); addPlayer(st, 2, 'b');  for (let i = 0; i < 300; i++) {    const inp = (k) => ({ seq: i, rotate: ((i + k) % 3) - 1, thrust: (i >> 2) & 1, fire: i % 5 === 0 ? 1 : 0 });    step(st, { 1: inp(0), 2: inp(1) });  }  return hashState(st);} test('deterministic replay', () => {  assert.equal(run(42), run(42));  assert.notEqual(run(42), run(43));}); test('planet destroys ship', () => {  const st = createState(1);  const s = addPlayer(st, 1, 'a');  s.x = CX + PLANET_R; s.y = CY; s.invuln = 0;  const ev = step(st, {});  assert.ok(s.dead);  assert.ok(ev.some((e) => e[0] === 'kill'));}); test('bullet hits count and 3 hits kill and score', () => {  const st = createState(1);  const a = addPlayer(st, 1, 'a'), b = addPlayer(st, 2, 'b');  step(st, {});  a.x = 200; a.y = 200; a.vx = a.vy = 0; a.angle = 0; a.invuln = 0;  b.x = 300; b.y = 200; b.vx = b.vy = 0; b.invuln = 0;  for (let i = 0; i < MAX_HP; i++) {    st.bullets.push({ id: 900 + i, owner: 1, spawn: 0, x: 290, y: 200, vx: 10, vy: 0, life: 1 });    step(st, {});    b.x = 300; b.y = 200; b.vx = b.vy = 0;  }  assert.ok(b.dead);  assert.equal(a.kills, 1);  assert.equal(b.deaths, 1);}); test('sanitizeInput', () => {  assert.deepEqual(sanitizeInput(['i', 3, -1, 1, 0, 9, 'x']), { seq: 3, rotate: -1, thrust: 1, fire: 0, tick: 9 });  for (const bad of [null, 'i', ['i', NaN, 0, 0, 0], ['i', 1, 2, 0, 0], ['i', 1, 0, 1e9, 0], ['i', 1, 0, 0, '1'], ['x', 1, 0, 0, 0], ['i', -1, 0, 0, 0], ['i', 1.5, 0, 0, 0]]) {    assert.equal(sanitizeInput(bad), null);  }}); test('delta round trip with removals and full', () => {  const st = createState(5);  addPlayer(st, 1, 'a'); addPlayer(st, 2, 'b'); addPlayer(st, 3, 'c');  const s0 = snapshotOf(st);  step(st, { 1: { seq: 1, rotate: 1, thrust: 1, fire: 0 } });  const s1 = snapshotOf(st);  assert.deepEqual(applySnapshot(null, encodeSnapshot(null, s0)), s0);  assert.deepEqual(applySnapshot(s0, encodeSnapshot(s0, s1)), s1);  st.ships.delete(2);  const s2 = snapshotOf(st);  const d = encodeSnapshot(s1, s2);  assert.deepEqual(d[1], [2]);  assert.deepEqual(applySnapshot(s1, d), s2);});