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
JavaScript55 lines1.9 KB
import test from 'node:test';import assert from 'node:assert/strict';import { newBucket, checkBucket, planTick } from '../src/room-logic.ts';import { encodeSnapshot, applySnapshot, encodeUps, applyUps } from '../public/shared/protocol.js'; test('planTick: integer alarm >= now+1, never loses alarm', () => {  let next = 1000.4;  for (const now of [1000, 1000.2, 1033, 1034, 1500, 5000]) {    const p = planTick(now, next);    assert.ok(Number.isInteger(p.alarmAt));    assert.ok(p.alarmAt >= now + 1);    assert.ok(p.steps <= 3);    next = p.nextTickAt;  }  assert.equal(planTick(1000, 1033.3).steps, 0);  assert.equal(planTick(1033, 1033.3).steps, 1);  assert.equal(planTick(2000, 1000).steps, 1);}); test('token bucket', () => {  const b = newBucket(0);  for (let t = 0; t < 10000; t += 1000 / 31) {    assert.ok(checkBucket(b, t, 30).ok);    if (Math.floor(t) % 1000 < 33) assert.ok(checkBucket(b, t, 20).ok);  }  assert.equal(b.strikes, 0);  const f = newBucket(0);  let closed = false;  for (let i = 0; i < 200; i++) closed = checkBucket(f, 500, 10).close || closed;  assert.ok(closed);}); test('delta with 5 ticks ack latency and ups', () => {  const ring = new Map();  const mk = (t) => ({ ships: { 1: [1, t, 0, 0, 0, 0, 3, 0, 0], ...(t < 8 ? { 2: [2, 5, 5, 0, 0, 0, 3, 0, 0] } : {}) }, meta: [1, 100 - t, 10] });  let client = null;  const acked = new Map();  for (let t = 1; t <= 12; t++) {    const next = mk(t);    ring.set(t, next);    const ackTick = t - 5;    const base = ring.get(ackTick) ?? null;    const d = encodeSnapshot(base, next);    const cbase = base ? acked.get(ackTick) : null;    client = applySnapshot(cbase, JSON.parse(JSON.stringify(d)));    acked.set(t, client);    assert.deepEqual(client, next);  }  const u1 = { 3: [3, 1, 1, 2] };  const d = encodeUps(null, u1);  assert.deepEqual(applyUps(null, d), u1);  const d2 = encodeUps(u1, {});  assert.deepEqual(applyUps(u1, d2), {});});