Skip to content

Leaderboard positions are incorrect when using before parameter #84

@cetteup

Description

@cetteup

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions