From 2f4106576fc56720ef92ca17ff5dab42d7d545ad Mon Sep 17 00:00:00 2001 From: some1chan <31833556+some1chan@users.noreply.github.com> Date: Tue, 14 Nov 2023 18:16:57 -0600 Subject: [PATCH] fix: game offset handling Previously, values would output negative numbers, as leastDelayedTimeToSend wasn't of type Timestamp; rather, it was a number. This caused the values to never subtract properly (as number - undefined will still equal that same number), which can cause stuff to wait for negative amounts of time, which turns into 0ms, instead of the 0ms and onward that's required to time everything correctly. JSON.parse and JSON.stringify has been refactored out, as cloning a number can simply be done by declaring a variable. --- src/App.svelte | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/App.svelte b/src/App.svelte index 1bfbf9a..a8db709 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -475,24 +475,21 @@ // // This is because if the game offset is -500ms for example, // if we theoretically send synced inputs according to just ping, - // the game offset will make the game run 500ms behind, and will - // start the chart 500ms behind. - let largestSongLatencyConnection: DataConnection; + // the game offset will make the chart send the first note with + // a 500ms delay compared to what's normal (0ms game offset). + // + // If it was positive instead, the chart will send that first note + // 500ms before what's normal. + let largestSongLatencyConnection: DataConnection | undefined; let largestGlassToGlassLatency = -1; - let largestGameOffset = -1; + let largestGameOffset = -Infinity; + + let largestSongLatencyMs = -Infinity; for (const [, connection] of connections) { const glassToGlassLatency = getPing(connection).recentPing / 2; const gameOffset = connection.metadata["gameOffset"]; - // Set our defaults, if we don't have any - if (!largestSongLatencyConnection) { - largestSongLatencyConnection = connection; - largestGlassToGlassLatency = glassToGlassLatency; - largestGameOffset = -gameOffset; - continue; - } - // Grab the game offset // Since negative game offset causes the game to be behind, // if we do have negative offset, the ping needs to be earlier. @@ -504,12 +501,20 @@ // it's impossible to have negative ping, // and we'll need to compensate for this. const songLatencyConnection = glassToGlassLatency - -gameOffset; - if (songLatencyConnection > largestGameOffset) { + if (songLatencyConnection > largestSongLatencyMs) { largestSongLatencyConnection = connection; largestGlassToGlassLatency = glassToGlassLatency; largestGameOffset = gameOffset; + largestSongLatencyMs = songLatencyConnection; } } + if (!largestSongLatencyConnection) throw new Error(); + + console.log( + `largestSongLatencyConnection: ${largestSongLatencyConnection.metadata["username"]} (${largestSongLatencyConnection.peer})` + ); + console.log(`largestGlassToGlassLatency: ${largestGlassToGlassLatency}`); + console.log(`largestGameOffset: ${largestGameOffset}`); // Calculate packets for countdowns on delayed timings let timestamps: Timestamp[] = []; @@ -536,19 +541,14 @@ // negative, we increase everything by it. This is to prevent suggesting // to setTimeout() that we want to send a packet back in time, via a // negative number. - // - // leastDelayedTimestamp is cloned, to prevent the function from changing - // the value, and making sure all values will not be negative numbers. - const leastDelayedTimeToSend: Timestamp = JSON.parse( - JSON.stringify( - timestamps.sort((a, b) => a.timeToSend - b.timeToSend)[0].timeToSend - ) - ); - if (leastDelayedTimeToSend?.timeToSend < 0) { + const leastDelayedTimeToSend = timestamps.sort( + (a, b) => a.timeToSend - b.timeToSend + )[0].timeToSend; + if (leastDelayedTimeToSend < 0) { for (const timestamp of timestamps) { // Using -= operator, as the value is negative, // and double-negative makes it positive - timestamp.timeToSend -= leastDelayedTimeToSend.timeToSend; + timestamp.timeToSend -= leastDelayedTimeToSend; } }