Reputation: 1636
I have a (relatively) simple platform which handles running competitions. When calculating the results, the first placed finisher gets 20 points, second placed 19 points, and so on, down to a minimum of 3 points just for taking part.
The existing loop looks like this - results is an array of objects, ordered by finish time (ascending) :
$pos = 1;
$posscore = 20;
foreach($results as $result) {
$result->position = $pos;
$result->race_points = $posscore;
$result->save();
$pos += 1;
if($posscore > 3) {
$posscore -= 1;
}
// Other, unrelated, code removed
}
The problem arises when it handles two (or more) finishers with the same finish time. The first one in the array will get a higher finish position (ie. lower number) and higher points than the second one, when the ideal outcome would be for both to get the same points and finishing position, and it then jump one
So at the moment if the 4th and 5th finishers both finish at the same time it will give :
1st / 20
2nd / 19
3rd / 18
4th / 17
5th / 16
6th / 15
when the actual outcome should be
1st / 20
2nd / 19
3rd / 18
4th / 17
4th / 17
6th / 15
How would I best go about maintaining a note of, or otherwise accessing, the previous finish times whilst iterating through the loop?
Upvotes: 0
Views: 29
Reputation: 1547
Try this code. But of course you need to change arrived_time
to yours
<?php
$maxScore = 20;
foreach($results as $key => $result) {
$result->position = $key + 1;
$result->race_points = $maxScore - $result->position + 1;
if (isset($result[$key-1])){
$previous = $result[$key-1];
if ($previous->arrived_time == $result->arrived_time){
$result->race_points = $previous->race_points;
}
}
if ($result->race_points <=3){
$result->race_points = 3;
}
$result->save();
}
P.S.: It is not optimized code. But I guess, if you have only 20 items, or close to it, no need to overengineer here.
Upvotes: 0