|
1
|
|
|
<?php namespace GolfLeague\Storage\Leaderboard; |
|
2
|
|
|
|
|
3
|
|
|
use \Match as Match; |
|
4
|
|
|
|
|
5
|
|
|
class EloquentLeaderboardRepository implements LeaderboardRepository |
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
public function __construct(Match $match) |
|
9
|
|
|
{ |
|
10
|
|
|
$this->match = $match; |
|
|
|
|
|
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
public function get($year) |
|
14
|
|
|
{ |
|
15
|
|
|
//Get all match ids for a given year |
|
16
|
|
|
//get all player ids |
|
17
|
|
|
//for each player id get total winnings from pivot where (player id and match id) |
|
18
|
|
|
$year = $year . '-01-01'; |
|
19
|
|
|
$matches = $this->match->with('players')->where('created_at', '>', $year)->get(); //get match eventually by year |
|
20
|
|
|
|
|
21
|
|
|
//filter out players from match |
|
22
|
|
|
$moneyList = array(); |
|
23
|
|
|
$players = array(); |
|
|
|
|
|
|
24
|
|
|
foreach($matches as $key => $item){ |
|
25
|
|
|
$players = $item->players; |
|
26
|
|
|
foreach($players as $playersKey => $player){ |
|
27
|
|
|
if (array_key_exists($player->id, $moneyList)) { |
|
28
|
|
|
//add previois to new |
|
29
|
|
|
$moneyList[$player->id]['winnings'] = sprintf('%01.2f', ($moneyList[$player->id]['winnings'] + $player->pivot->winnings)); |
|
30
|
|
|
$moneyList[$player->id]['entryfees'] += 5; |
|
31
|
|
|
$moneyList[$player->id]['entryfees'] = sprintf('%01.2f',$moneyList[$player->id]['entryfees']); |
|
32
|
|
|
$moneyList[$player->id]['net'] = $moneyList[$player->id]['winnings'] - $moneyList[$player->id]['entryfees']; |
|
33
|
|
|
$moneyList[$player->id]['net'] = sprintf('%01.2f',$moneyList[$player->id]['net']); |
|
34
|
|
|
} |
|
35
|
|
|
else { |
|
36
|
|
|
$moneyList[$player->id]['id'] = $player->id; |
|
37
|
|
|
$moneyList[$player->id]['name'] = $player->name; |
|
38
|
|
|
$moneyList[$player->id]['winnings'] = sprintf('%01.2f',$player->pivot->winnings); |
|
39
|
|
|
$moneyList[$player->id]['entryfees'] = 5; |
|
40
|
|
|
$moneyList[$player->id]['entryfees'] = sprintf('%01.2f',$moneyList[$player->id]['entryfees']); |
|
41
|
|
|
$moneyList[$player->id]['net'] = $moneyList[$player->id]['winnings'] - $moneyList[$player->id]['entryfees']; |
|
42
|
|
|
$moneyList[$player->id]['net'] = sprintf('%01.2f',$moneyList[$player->id]['net']); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $moneyList; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
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: