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 range is incorrect when using before parameter #85

Open
cetteup opened this issue Dec 18, 2024 · 1 comment
Open

Leaderboard range is incorrect when using before parameter #85

cetteup opened this issue Dec 18, 2024 · 1 comment

Comments

@cetteup
Copy link

cetteup commented Dec 18, 2024

When requesting any leaderboard with before > 0, the returned range of leaderboard positions is incorrect. Using e.g. pos=5&before=2&after=2, one should receive a total of five entries (position itself, two before, two after). However, only three positions are returned (position itself, two before).

The bug is caused by max = after + 1 being treated as an absolute position index (similar to min = (pos - 1) - before), when it is actually used as the number of items to return in the LIMIT statement (where min = (pos - 1) - before acts as the OFFSET).

// Optional parameters
$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;

$query = "SELECT id, name, rank_id, country, time, score FROM player WHERE score > 0
ORDER BY score DESC, name DESC LIMIT " . $min . ", " . $max;

The correct way to calculate max would be max = position + after - min. In the above example, min = (pos 5 - 1) - before 2 would result in OFFSET 2 and max = position 5 + after 2 - min 2 would set LIMIT 5.

@cetteup
Copy link
Author

cetteup commented Dec 18, 2024

Note: The overall number of returned entries should be limited regardless to prevent #82.

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