Harden gameplay timing and map loading
This commit is contained in:
19
shared/round-clock.ts
Normal file
19
shared/round-clock.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
// 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,
|
||||
),
|
||||
);
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user