Files
MineGeo/shared/round-clock.ts

20 lines
552 B
TypeScript

// Synchronize at receipt using a bounded estimate of the return journey. The
// countdown uses performance.now(), so changing the OS clock cannot extend it.
export function roundClock(
serverNow: number,
sentAt: number,
receivedAt: number,
) {
const serverAtReceipt = serverNow + Math.max(0, receivedAt - sentAt) / 2;
return {
remaining(deadline: string, now: number) {
return Math.max(
0,
Math.ceil(
(Date.parse(deadline) - serverAtReceipt - (now - receivedAt)) / 1000,
),
);
},
};
}