|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GolfLeague\Services; |
|
4
|
|
|
|
|
5
|
|
|
use GolfLeague\Storage\MatchRound\MatchRoundRepository as MatchRound; |
|
6
|
|
|
use \Course; |
|
7
|
|
|
|
|
8
|
|
|
class LeaderboardService |
|
9
|
|
|
{ |
|
10
|
|
|
public function __construct(MatchRound $matchRoundRepo) |
|
11
|
|
|
{ |
|
12
|
|
|
$this->matchround = $matchRoundRepo; |
|
|
|
|
|
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
public function calculate($matchId, $type) |
|
16
|
|
|
{ |
|
17
|
|
|
$rounds = $this->matchround->getByMatch($matchId); |
|
18
|
|
|
$leaderboard = array(); |
|
19
|
|
|
foreach ($rounds as $round){ |
|
20
|
|
|
$holescores = $round->holescores; |
|
21
|
|
|
//Filter out NULL scores |
|
22
|
|
|
$filteredHolecores = $holescores->filter(function($holescore){ |
|
23
|
|
|
if ($holescore->score != null) { |
|
24
|
|
|
return true; |
|
25
|
|
|
} |
|
26
|
|
|
}); |
|
27
|
|
|
//at this point have a collection of filtered holescores |
|
28
|
|
|
$holescoreCount = $filteredHolecores->count(); |
|
29
|
|
|
$par = $this->currentParTotal($round->course_id, $holescoreCount); |
|
30
|
|
|
|
|
31
|
|
|
if($type === 'net') { |
|
32
|
|
|
|
|
33
|
|
|
$handicap = round($this->matchround->getMatchPlayerHandicap($matchId, $round->player->id),0); |
|
34
|
|
|
//$handicap = round($round->player->handicap,0); |
|
|
|
|
|
|
35
|
|
|
$par = $handicap + $par; |
|
36
|
|
|
} |
|
37
|
|
|
$playerScore = $filteredHolecores->sum('score'); |
|
38
|
|
|
|
|
39
|
|
|
$player = [ |
|
40
|
|
|
"name" => $round->player->name, |
|
41
|
|
|
"score" => $playerScore - $par, |
|
42
|
|
|
]; |
|
43
|
|
|
array_push($leaderboard, $player); |
|
44
|
|
|
} |
|
45
|
|
|
$name = array(); |
|
46
|
|
|
$score = array(); |
|
47
|
|
View Code Duplication |
foreach ($leaderboard as $key => $player) { |
|
|
|
|
|
|
48
|
|
|
$name[$key] = $player['name']; |
|
49
|
|
|
$score[$key] = $player['score']; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
array_multisort($score, SORT_ASC, $name, SORT_ASC, $leaderboard); |
|
53
|
|
|
return $leaderboard; |
|
54
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
/* |
|
57
|
|
|
* Given a courseID and #of holes played return par total |
|
58
|
|
|
* @return int |
|
59
|
|
|
*/ |
|
60
|
|
|
public function currentParTotal($courseId, $holesPlayed) |
|
61
|
|
|
{ |
|
62
|
|
|
//find par total for course given number of holes played |
|
63
|
|
|
$holes = Course::find($courseId)->holes; |
|
64
|
|
|
$filteredHoles =$holes->slice(0,$holesPlayed); |
|
65
|
|
|
return $filteredHoles->sum('par'); // Par for given number of holes |
|
66
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: