Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leaderboard positions are incorrect when using before parameter #84

Open
cetteup opened this issue Dec 18, 2024 · 0 comments
Open

Leaderboard positions are incorrect when using before parameter #84

cetteup opened this issue Dec 18, 2024 · 0 comments

Comments

@cetteup
Copy link

cetteup commented Dec 18, 2024

When requesting a non-default range from the leaderboard using e.g. pos=5&before=2&after=2, the positions of the players are incorrect. The reason is pretty simple: before is subtracted from pos to get min (the row number of the first player to return).

$after = (isset($_GET['after'])) ? (int)$_GET['after'] : 19;
$before = (isset($_GET['before'])) ? (int)$_GET['before'] : 0;
$pos = (isset($_GET['pos'])) ? (int)$_GET['pos'] : 1;
$min = ($pos - 1) - $before;
$max = $after + 1;
// Negative correction
if ($min < 0) $min = 0;
if ($max < 0) $max = 0;

However, the first returned player is not given min+1 (query is zero-indexed, leaderboard is one-indexed) as their position but pos.

$query = "SELECT id, name, rank_id, country, time, score FROM player WHERE score > 0
ORDER BY score DESC, name DESC LIMIT " . $min . ", " . $max;
$result = $connection->query($query);
while ($row = $result->fetch())
{
$Response->writeDataLine(
$pos++,
$row['id'],
trim($row['name']),
$row['score'],
$row['time'],
$row['rank_id'],
strtoupper($row['country'])
);

Thus in the above example, the player that should be on position 3 (pos 5 - before 2) is shows as position 5. Every following player is shifted accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant