cmnord / jep

🌎 Jep! multiplayer trivia online
https://whatis.club
MIT License
11 stars 4 forks source link

Fix handling of ties #51

Closed menonsamir closed 1 year ago

menonsamir commented 1 year ago

Currently, when two players exactly tie (in milliseconds) on a buzz, their clients can end up in a state where each client believes it has won the buzz.

In the getWinningBuzzer function, we iterate through the buzzes map to find a winner. Since JavaScript Map's iterate in insertion order, this can create a local client-dependent ordering on buzzes. In the case of a tie, when we reduce to find the minimum delayMs time, this order will decided the winner.

The fix in this PR orders the iteration through the entries of buzzes by userId prior to searching for a minimum delay. This ensures that the user with a lower userId wins ties, making all clients determine the same winner each time.

This slightly suboptimal; we'd prefer that each tie get resolved randomly (so that no one player has an advantage). However, ties are rare in the first place, and I'd suggest a more robust solution is to use performance.now() instead of Date.now(), which would yield sub-millisecond accuracy that would make ties very unlikely. I'll leave that fix for a later PR.

vercel[bot] commented 1 year ago

Someone is attempting to deploy a commit to a Personal Account owned by @cmnord on Vercel.

@cmnord first needs to authorize it.

vercel[bot] commented 1 year ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
jep ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 21, 2023 4:36am