Skip to content

gitflarePublicPartly agent-ready40

orbit-duel

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

Add the Apache-2.0 licence0413e56
JavaScript73 lines2.8 KB
export const FULL_EVERY = 60;export const RING = 64; export const encodeJoin = (name, token, lastTick, watch) => (watch ? ['j', name, token ?? null, lastTick ?? null, 1] : ['j', name, token ?? null, lastTick ?? null]);export const encodeInput = (seq, rotate, thrust, fire, tick) => ['i', seq, rotate, thrust, fire, tick];export const encodeAck = (tick) => ['a', tick];export const encodePing = (t) => ['p', t]; const same = (a, b) => {  if (a.length !== b.length) return false;  for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;  return true;}; /** * Snapshot: { ships: {id: number[]}, meta: number[] }. * Delta: [changed: number[][], removedIds: number[], meta|null]. prev null => full. */export function encodeSnapshot(prev, next) {  const changed = [];  const removed = [];  for (const id in next.ships) {    const p = prev && prev.ships[id];    if (!p || !same(p, next.ships[id])) changed.push(next.ships[id]);  }  if (prev) for (const id in prev.ships) if (!(id in next.ships)) removed.push(+id);  const meta = prev && same(prev.meta, next.meta) ? null : next.meta;  return [changed, removed, meta];} export function applySnapshot(prev, delta) {  const ships = {};  if (prev) for (const id in prev.ships) ships[id] = prev.ships[id];  for (const id of delta[1]) delete ships[id];  for (const e of delta[0]) ships[e[0]] = e;  return { ships, meta: delta[2] || (prev ? prev.meta : [0, 0, 0]) };} export const encodeSnapshotMsg = (tick, baseTick, lastSeq, delta, ups, names, bullets) => {  const m = ['s', tick, baseTick, lastSeq, delta];  if (ups || names || bullets) m.push(ups || null, names || null, bullets || null);  return m;}; /** ups delta: [changed: number[][], removedIds: number[]] against prev ups (null => full). */export function encodeUps(prev, next) {  const changed = [];  const removed = [];  for (const id in next) if (!prev || !prev[id] || !same(prev[id], next[id])) changed.push(next[id]);  if (prev) for (const id in prev) if (!(id in next)) removed.push(+id);  return changed.length || removed.length || !prev ? [changed, removed] : null;}export function applyUps(prev, d) {  const ups = {};  if (prev) for (const id in prev) ups[id] = prev[id];  if (d) {    for (const id of d[1]) delete ups[id];    for (const e of d[0]) ups[e[0]] = e;  }  return ups;}export const encodeWelcome = (id, token, code, role, tick, full, extras, bullets) => ['w', id, token, code, role, tick, full, extras || null, bullets || null];export const encodePong = (t, tick) => ['o', t, tick];export const encodeError = (code, message) => ['e', code, message];export const encodeEvents = (tick, events) => ['v', tick, ...events]; export function parseMessage(text) {  try {    const m = JSON.parse(text);    return Array.isArray(m) && typeof m[0] === 'string' ? m : null;  } catch { return null; }}