WIP: Add input broadcasting and client-side prediction features
Changes include: - Client: INPUT_BROADCAST packet handling and opponent prediction rendering - Client: Protocol parsing for INPUT_BROADCAST packets - Server: Input broadcasting to all clients except sender 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -158,3 +158,57 @@ export function parseStateFullBody(dv, off) {
|
||||
return { snakes, apples, off };
|
||||
}
|
||||
|
||||
export function parseStateDeltaBody(dv, off) {
|
||||
// Parse STATE_DELTA body format (mirrors server/protocol.py build_state_delta_body)
|
||||
// update_id (u16)
|
||||
const updateId = dv.getUint16(off); off += 2;
|
||||
|
||||
// changes count (QUIC varint)
|
||||
let [changesCount, p1] = quicVarintDecode(dv, off); off = p1;
|
||||
|
||||
const changes = [];
|
||||
for (let i = 0; i < changesCount; i++) {
|
||||
const snakeId = dv.getUint8(off); off += 1;
|
||||
const flags = dv.getUint8(off); off += 1;
|
||||
const direction = dv.getUint8(off) & 0x03; off += 1;
|
||||
|
||||
const headMoved = (flags & 0x01) !== 0;
|
||||
const tailRemoved = (flags & 0x02) !== 0;
|
||||
const grew = (flags & 0x04) !== 0;
|
||||
const blocked = (flags & 0x08) !== 0;
|
||||
|
||||
let newHeadX = 0, newHeadY = 0;
|
||||
if (headMoved) {
|
||||
newHeadX = dv.getUint8(off); off += 1;
|
||||
newHeadY = dv.getUint8(off); off += 1;
|
||||
}
|
||||
|
||||
changes.push({
|
||||
snakeId,
|
||||
headMoved,
|
||||
tailRemoved,
|
||||
grew,
|
||||
blocked,
|
||||
direction,
|
||||
newHeadX,
|
||||
newHeadY
|
||||
});
|
||||
}
|
||||
|
||||
// apples added (count + coords)
|
||||
let [applesAddedCount, p2] = quicVarintDecode(dv, off); off = p2;
|
||||
const applesAdded = [];
|
||||
for (let i = 0; i < applesAddedCount; i++) {
|
||||
applesAdded.push([dv.getUint8(off++), dv.getUint8(off++)]);
|
||||
}
|
||||
|
||||
// apples removed (count + coords)
|
||||
let [applesRemovedCount, p3] = quicVarintDecode(dv, off); off = p3;
|
||||
const applesRemoved = [];
|
||||
for (let i = 0; i < applesRemovedCount; i++) {
|
||||
applesRemoved.push([dv.getUint8(off++), dv.getUint8(off++)]);
|
||||
}
|
||||
|
||||
return { updateId, changes, applesAdded, applesRemoved, off };
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user