开发者

Best way to calculate player places in php

开发者 https://www.devze.com 2023-03-30 21:36 出处:网络
I have points assigned to each player like this: player1 = 20 , player2 = 30, player3 = 50, playe开发者_如何转开发r4 = 10, player5 = 25 and so on.

I have points assigned to each player like this:

player1 = 20 , player2 = 30, player3 = 50, playe开发者_如何转开发r4 = 10, player5 = 25 and so on.

and these points change as they score more points. What i am trying to do is if player2 looks at his page he should see which player of all has highest score, where he is in all players and who has the lowest score? What is the best way to do this in php? So it will show something like this:

Highest Scorer                 Player2                              Lowest Score
    player3       you are 2nd highest out of 5 players               player4


// $users is an array in the form: playerID => score
$minScore = 0; $minScoreUser;
$maxScore = -1; $maxScoreUser;
$thisScore; $thisPlayer;
foreach ($users as $player => $score)
{
   if ( $player == $queryPlayer )
   {
       $thisScore = $score;
       $thisPlayer = $player;
   }
   if ( $maxScore < 0 || $score > $maxScore )
   {
       $maxScore = $score;
       $maxScorePlayer = $player;
   }
   if ( $score < $minScore )
   {
       $minScore = $score;
       $minScorePlayer = $player;
   }
}

$higherCount = 0;
foreach ($users as $player => $score)
    if ( $score > $thisScore ) $higherCount++;

$minScore and $minScorePlayer is the lowest player

$maxScore and $maxScorePlayer is the highest player

$thisScore and $thisPlayer is the player you are interested in

$higherCount is the number of players with higher score than your player

The above has a complexity of O(n)

The sorting solution has a complexity of O(n * logn )


Use php uasort function, and then you can have place in which your player stands in. Taken from php.net:

function cmp($a, $b) {
if ($a == $b) {
    return 0;
}
    return ($a < $b) ? -1 : 1;
}

// Array to be sorted
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);

// Sort and print the resulting array
uasort($array, 'cmp');

the array will look like this:

Array
(
    [d] => -9
    [h] => -4
    [c] => -1
    [e] => 2
    [g] => 3
    [a] => 4
    [f] => 5
    [b] => 8
)

Then you can use which place it stands in:

function key_offset(&$array, $searchKey){
    if(is_array($array) && array_key_exists($searchKey, $array)){
        $counter = 0;
        foreach($array as $key => $value){
            if($searchKey == $key){
                return $counter;
            } else {
                $counter++;
            }
        }
    }
} 

So now it would look like this:

// Array to be sorted
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);

// Sort and print the resulting array
uasort($array, 'cmp');

// Return index of the place in which your player is now.
$index = key_offset($array, "PlayerName");


I'm not sure the existing answers are sufficient.

If you're just trying to find everyone's score and rank at once, it's easy: just do

SELECT * FROM TableOfScores ORDER BY score

That'll give you the scores in order, and you can increment a counter each time you iterate to get their rank. However, I think what you want is to simply show one person's screen, judging by your "You are the 2nd highest" message. In that case, it's even simpler because you certainly don't need to sort the whole list just to get one person's rank.

Note that you should build an index on score. This will take up a bit of time when you're inserting/deleting player scores, but it will make the this query run MUCH faster.

To find the number of total tests taken just do

SELECT COUNT(*) AS TotalScores FROM TableOfScores

To find the place of the current user, do

SELECT COUNT(*) AS CurrentPersonsRank FROM TableOfScores WHERE score <= $currentPersonScore

I hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消