|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: mschmidt |
|
5
|
|
|
* Date: 8/4/2016 |
|
6
|
|
|
* Time: 1:21 PM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace GolfLeague\Tournament; |
|
10
|
|
|
|
|
11
|
|
|
use \Tournament; |
|
12
|
|
|
use GolfLeague\Storage\MatchRound\EloquentMatchRoundRepository as MatchRound; |
|
13
|
|
|
use GolfLeague\Services\LeaderboardService as LeaderboardService; |
|
14
|
|
|
|
|
15
|
|
|
class Net |
|
16
|
|
|
{ |
|
17
|
|
|
private $tournament; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(array $tournament) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->tournament = Tournament::where('id', '=', $tournament['id'])->with('dates')->get(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
function get() |
|
|
|
|
|
|
25
|
|
|
{ |
|
26
|
|
|
return 'im a net tournament with an ID of ' . $this->tournament->id; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
function getLeaderboard() |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
//Quick and Dirty code for 2016 Tournament only |
|
32
|
|
|
// Will expand and extract later |
|
33
|
|
|
$matches = []; |
|
34
|
|
|
//for each date get the matchid that corresponds |
|
35
|
|
|
foreach($this->tournament as $tournament){ |
|
36
|
|
|
foreach($tournament->dates as $date){ |
|
37
|
|
|
$match = \Match::where('date', '=', $date->date)->get(); |
|
38
|
|
|
foreach ($match as $m){ |
|
39
|
|
|
$matchid = $m->id; |
|
40
|
|
|
} |
|
41
|
|
|
$matchRound = new MatchRound(); |
|
42
|
|
|
$leaderboardService = new LeaderboardService($matchRound); |
|
43
|
|
|
$current = $leaderboardService->calculate($matchid,'net'); |
|
|
|
|
|
|
44
|
|
|
$matches = array_merge($matches, $current); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$leaderboard = []; |
|
49
|
|
|
|
|
50
|
|
|
foreach($matches as $key=>$player){ |
|
51
|
|
|
$inArrayKey = $this->in_array_r($player['name'], $leaderboard, true); |
|
52
|
|
|
if($inArrayKey === false){ |
|
53
|
|
|
$leaderboard[$key]['name'] = $player['name']; |
|
54
|
|
|
$leaderboard[$key]['score'] = $player['score']; |
|
55
|
|
|
} |
|
56
|
|
|
else{ |
|
57
|
|
|
$score = $player['score'] + $leaderboard[$inArrayKey]['score']; |
|
58
|
|
|
$leaderboard[$inArrayKey]['score'] = $score; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$name = []; |
|
63
|
|
|
$score = []; |
|
64
|
|
View Code Duplication |
foreach ($leaderboard as $key => $player) { |
|
|
|
|
|
|
65
|
|
|
$name[$key] = $player['name']; |
|
66
|
|
|
$score[$key] = $player['score']; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
array_multisort($score, SORT_ASC, $name, SORT_ASC, $leaderboard); |
|
70
|
|
|
return $leaderboard; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
function in_array_r($needle, $haystack, $strict = false) { |
|
|
|
|
|
|
74
|
|
|
foreach ($haystack as $key=>$item) { |
|
75
|
|
|
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && $this->in_array_r($needle, $item, $strict))) { |
|
76
|
|
|
return $key; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
} |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.