|
1
|
|
|
<?php namespace GolfLeague\Services; |
|
2
|
|
|
|
|
3
|
|
|
use GolfLeague\Storage\Match\MatchRepository; |
|
4
|
|
|
use GolfLeague\Storage\MatchRound\MatchRoundRepository; |
|
5
|
|
|
use GolfLeague\PrizeMoney; |
|
6
|
|
|
use GolfLeague\Handicap; |
|
7
|
|
|
use \Player; |
|
8
|
|
|
use \Match; |
|
9
|
|
|
use \GolfLeague\Storage\Ctp\CtpRepository; |
|
10
|
|
|
use \Grosswinner; |
|
11
|
|
|
use \Netwinner; |
|
12
|
|
|
use \Skin; |
|
13
|
|
|
use GolfLeague\Services\CarryOver; |
|
14
|
|
|
use Illuminate\Events\Dispatcher; |
|
15
|
|
|
use \Teammatch; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Our MatchService, containing all useful methods for business logic around Matches |
|
19
|
|
|
*/ |
|
20
|
|
|
class MatchService |
|
21
|
|
|
{ |
|
22
|
|
|
// Containing our matchRepository to make all our database calls to |
|
23
|
|
|
protected $matchRepo; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Loads our $matchRepo |
|
27
|
|
|
* |
|
28
|
|
|
* @param MatchRepository $matchRepo |
|
29
|
|
|
* @return MatchService |
|
|
|
|
|
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(MatchRoundRepository $matchRoundRepo, |
|
32
|
|
|
MatchRepository $matchRepo, |
|
33
|
|
|
PrizeMoney $prizeMoney, |
|
34
|
|
|
Player $player, |
|
35
|
|
|
Match $match, |
|
36
|
|
|
CtpRepository $ctp, |
|
37
|
|
|
Teammatch $teammatch, |
|
38
|
|
|
Dispatcher $events) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->matchRoundRepo = $matchRoundRepo; |
|
|
|
|
|
|
41
|
|
|
$this->matchRepo = $matchRepo; |
|
42
|
|
|
$this->prizeMoney = $prizeMoney; |
|
|
|
|
|
|
43
|
|
|
$this->player = $player; |
|
|
|
|
|
|
44
|
|
|
$this->match = $match; |
|
|
|
|
|
|
45
|
|
|
$this->ctp = $ctp; |
|
|
|
|
|
|
46
|
|
|
$this->teammatch = $teammatch; |
|
|
|
|
|
|
47
|
|
|
$this->events = $events; |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Method to create match from input Match data |
|
52
|
|
|
* |
|
53
|
|
|
* @param mixed $matchdata |
|
54
|
|
|
* @return |
|
55
|
|
|
*/ |
|
56
|
|
|
public function create($matchdata) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->prizeMoney->setPurse($matchdata['purse']); |
|
59
|
|
|
|
|
60
|
|
|
$matchdata['purse'] = number_format($matchdata['purse'], 2); |
|
61
|
|
|
$matchdata['grossmoney'] = $this->prizeMoney->getlowScore(); |
|
62
|
|
|
$matchdata['netmoney'] = $this->prizeMoney->getlowScore(); |
|
63
|
|
|
|
|
64
|
|
|
//How many A and B players |
|
65
|
|
|
$totalPlayers = 0; |
|
66
|
|
|
|
|
67
|
|
|
$aPlayerCount = 0; |
|
68
|
|
|
|
|
69
|
|
|
$bPlayerCount = 0; |
|
70
|
|
|
foreach($matchdata['player'] as $player){ |
|
71
|
|
|
if ($player['level_id'] == '1'){ |
|
72
|
|
|
$aPlayerCount++; |
|
73
|
|
|
} |
|
74
|
|
|
else { |
|
75
|
|
|
$bPlayerCount++; |
|
76
|
|
|
} |
|
77
|
|
|
$totalPlayers++; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
//Calculate Skins money based on how many players in each group |
|
81
|
|
|
|
|
82
|
|
|
$matchdata['skinsamoney'] = $this->prizeMoney->skinsGroupPot($matchdata['purse'], $totalPlayers, $aPlayerCount); |
|
83
|
|
|
$matchdata['skinsbmoney'] = $this->prizeMoney->skinsGroupPot($matchdata['purse'], $totalPlayers, $bPlayerCount); |
|
84
|
|
|
//check for carry over money and if there is add it to skins money |
|
85
|
|
|
$carryOver = new CarryOver; |
|
86
|
|
|
$carryOverMoney = $carryOver->calculate(); |
|
87
|
|
|
$matchdata['skinsamoney'] += $carryOverMoney[1]; |
|
88
|
|
|
$matchdata['skinsbmoney'] += $carryOverMoney[2]; |
|
89
|
|
|
|
|
90
|
|
|
//append current handicap and set winnings to 0 for each player |
|
91
|
|
|
foreach ($matchdata['player'] as $key=>$player) { |
|
92
|
|
|
//get each player's current handicap |
|
93
|
|
|
$currentPlayer = $this->player->find($player['player_id']); |
|
94
|
|
|
$matchdata['player'][$key]['handicap'] = $currentPlayer->handicap; |
|
95
|
|
|
$matchdata['player'][$key]['winnings'] = 0; |
|
96
|
|
|
}// End foreach |
|
97
|
|
|
|
|
98
|
|
|
$matchid = $this->matchRepo->create($matchdata); |
|
99
|
|
|
$matchdata['match_id'] = $matchid; |
|
100
|
|
|
$this->events->fire('match.create', array($matchdata)); // MatchHandler and teamMatchHandler are listening... |
|
101
|
|
|
|
|
102
|
|
|
// Run for team Matches |
|
103
|
|
|
if($matchdata['matchType'] === 'team' || $matchdata['matchType'] === 'both'){ |
|
104
|
|
|
|
|
105
|
|
|
foreach ($matchdata['teamMatchUp1'] as $teamKey => $team){ |
|
106
|
|
|
$matchUp1[] = $this->search($matchdata['player'], 'team', $team); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
//Save matchUp 1 |
|
110
|
|
|
$match1['match_id'] = $matchdata['match_id']; // This is generated below and passed to listener |
|
|
|
|
|
|
111
|
|
|
$match1['team_id'] = $matchUp1[0][0]['team']; |
|
|
|
|
|
|
112
|
|
|
$match1['player1'] = $matchUp1[0][0]['player_id']; |
|
113
|
|
View Code Duplication |
if(isset($matchUp1[0][1]['player_id'])){ |
|
|
|
|
|
|
114
|
|
|
$match1['player2'] = $matchUp1[0][1]['player_id']; |
|
115
|
|
|
} |
|
116
|
|
|
$match1['opponent'] = $matchUp1[1][0]['team']; |
|
117
|
|
|
$this->teammatch->create($match1); //save to match table |
|
118
|
|
|
|
|
119
|
|
|
$match1['match_id'] = $matchdata['match_id']; // This is generated below and passed to listener |
|
120
|
|
|
$match1['team_id'] = $matchUp1[1][0]['team']; |
|
121
|
|
|
$match1['player1'] = $matchUp1[1][0]['player_id']; |
|
122
|
|
View Code Duplication |
if(isset($matchUp1[1][1]['player_id'])){ |
|
|
|
|
|
|
123
|
|
|
$match1['player2'] = $matchUp1[1][1]['player_id']; |
|
124
|
|
|
} |
|
125
|
|
|
$match1['opponent'] = $matchUp1[0][0]['team']; |
|
126
|
|
|
$this->teammatch->create($match1); //save to match table |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
foreach ($matchdata['teamMatchUp2'] as $teamKey => $team){ |
|
130
|
|
|
$matchUp2[] = $this->search($matchdata['player'], 'team', $team); |
|
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
//Save matchUp 2 |
|
134
|
|
|
$match2['match_id'] = $matchdata['match_id']; // This is generated below and passed to listener |
|
|
|
|
|
|
135
|
|
|
$match2['team_id'] = $matchUp2[0][0]['team']; |
|
|
|
|
|
|
136
|
|
|
$match2['player1'] = $matchUp2[0][0]['player_id']; |
|
137
|
|
View Code Duplication |
if(isset($matchUp2[0][1]['player_id'])){ |
|
|
|
|
|
|
138
|
|
|
$match2['player2'] = $matchUp2[0][1]['player_id']; |
|
139
|
|
|
} |
|
140
|
|
|
$match2['opponent'] = $matchUp2[1][0]['team']; |
|
141
|
|
|
$this->teammatch->create($match2); //save to match table |
|
142
|
|
|
|
|
143
|
|
|
$match2['match_id'] = $matchdata['match_id']; // This is generated below and passed to listener |
|
144
|
|
|
$match2['team_id'] = $matchUp2[1][0]['team']; |
|
145
|
|
|
$match2['player1'] = $matchUp2[1][0]['player_id']; |
|
146
|
|
View Code Duplication |
if(isset($matchUp2[1][1]['player_id'])){ |
|
|
|
|
|
|
147
|
|
|
$match2['player2'] = $matchUp2[1][1]['player_id']; |
|
148
|
|
|
} |
|
149
|
|
|
$match2['opponent'] = $matchUp2[0][0]['team']; |
|
150
|
|
|
$this->teammatch->create($match2); //save to match table |
|
151
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
foreach ($matchdata['teamMatchUp3'] as $teamKey => $team){ |
|
154
|
|
|
$matchUp3[] = $this->search($matchdata['player'], 'team', $team); |
|
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
//Save matchUp 3 |
|
158
|
|
|
$match3['match_id'] = $matchdata['match_id']; // This is generated below and passed to listener |
|
|
|
|
|
|
159
|
|
|
$match3['team_id'] = $matchUp3[0][0]['team']; |
|
|
|
|
|
|
160
|
|
|
$match3['player1'] = $matchUp3[0][0]['player_id']; |
|
161
|
|
View Code Duplication |
if(isset($matchUp3[0][1]['player_id'])){ |
|
|
|
|
|
|
162
|
|
|
$match3['player2'] = $matchUp3[0][1]['player_id']; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$match3['opponent'] = $matchUp3[1][0]['team']; |
|
166
|
|
|
$this->teammatch->create($match3); //save to match table |
|
167
|
|
|
|
|
168
|
|
|
$match3['match_id'] = $matchdata['match_id']; // This is generated below and passed to listener |
|
169
|
|
|
$match3['team_id'] = $matchUp3[1][0]['team']; |
|
170
|
|
|
$match3['player1'] = $matchUp3[1][0]['player_id']; |
|
171
|
|
View Code Duplication |
if(isset($matchUp3[1][1]['player_id'])){ |
|
|
|
|
|
|
172
|
|
|
$match3['player2'] = $matchUp3[1][1]['player_id']; |
|
173
|
|
|
} |
|
174
|
|
|
$match3['opponent'] = $matchUp3[0][0]['team']; |
|
175
|
|
|
$this->teammatch->create($match3); //save to match table |
|
176
|
|
|
|
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
public function finalize($matchdata) |
|
181
|
|
|
{ |
|
182
|
|
|
// post CTP1 and CTP2 |
|
183
|
|
|
$ctp1 = array( |
|
184
|
|
|
'match_id' => $matchdata['match'], |
|
185
|
|
|
'player_id' => $matchdata['ctp1'], |
|
186
|
|
|
'hole_id' => $matchdata['ctp1hole'], |
|
187
|
|
|
'money' => $this->prizeMoney->getCtp() |
|
188
|
|
|
); |
|
189
|
|
|
$this->ctp->create($ctp1); |
|
190
|
|
|
$ctp2 = array( |
|
191
|
|
|
'match_id' => $matchdata['match'], |
|
192
|
|
|
'player_id' => $matchdata['ctp2'], |
|
193
|
|
|
'hole_id' => $matchdata['ctp2hole'], |
|
194
|
|
|
'money' => $this->prizeMoney->getCtp() |
|
195
|
|
|
); |
|
196
|
|
|
$this->ctp->create($ctp2); |
|
197
|
|
|
|
|
198
|
|
|
//calculate Gross winner and post to grossWinnersTable |
|
199
|
|
|
|
|
200
|
|
|
$matchRound = $this->matchRoundRepo->getByMatch($matchdata['match']); |
|
201
|
|
|
|
|
202
|
|
|
|
|
203
|
|
|
$lowGross = array(); |
|
204
|
|
|
foreach ($matchRound as $key => $match){ |
|
205
|
|
|
$lowGross[$match['player']->id] = $match['score']; |
|
206
|
|
|
} |
|
207
|
|
|
$arrayLowGross = array_keys($lowGross, min($lowGross)); |
|
208
|
|
View Code Duplication |
foreach($arrayLowGross as $key => $lowgrossPlayer) { |
|
|
|
|
|
|
209
|
|
|
$grossWinner = new Grosswinner; |
|
210
|
|
|
$grossWinner->player_id = $lowgrossPlayer; |
|
211
|
|
|
$grossWinner->match_id = $matchdata['match']; |
|
212
|
|
|
$grossWinner->score = $lowGross[$lowgrossPlayer]; |
|
213
|
|
|
$grossWinner->money = $this->prizeMoney->getlowScore() / count($arrayLowGross); |
|
214
|
|
|
$grossWinner->save(); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
//Calculate NET winner |
|
218
|
|
|
$lowNet = array(); |
|
219
|
|
|
$scores =array(); |
|
|
|
|
|
|
220
|
|
|
foreach ($matchRound as $key => $match){ |
|
221
|
|
|
$netScore = ($match['score'] - round($match['player']->handicap,0)); //calculate net score |
|
222
|
|
|
$lowNet[$match['player']->id] = $netScore; |
|
223
|
|
|
} |
|
224
|
|
|
$arrayLowNet = array_keys($lowNet, min($lowNet)); |
|
225
|
|
|
|
|
226
|
|
View Code Duplication |
foreach($arrayLowNet as $key => $lownetPlayer) { |
|
|
|
|
|
|
227
|
|
|
$netWinner = new Netwinner; |
|
228
|
|
|
$netWinner->player_id = $lownetPlayer; |
|
229
|
|
|
$netWinner->match_id = $matchdata['match']; |
|
230
|
|
|
$netWinner->score = $lowNet[$lownetPlayer]; |
|
231
|
|
|
$netWinner->money = $this->prizeMoney->getlowScore() / count($arrayLowNet); |
|
232
|
|
|
$netWinner->save(); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
//Calculate Skins |
|
236
|
|
|
|
|
237
|
|
|
//determine A and B players |
|
238
|
|
|
//using pivot table match_player |
|
239
|
|
|
$match = $this->match->find($matchdata['match']); |
|
240
|
|
|
$aPlayers = array(); |
|
241
|
|
|
$bPlayers = array(); |
|
242
|
|
|
foreach($match->players as $player) |
|
243
|
|
|
{ |
|
244
|
|
|
if($player->pivot->level_id == 1){ |
|
245
|
|
|
$aPlayers[] = $player->pivot->player_id; |
|
246
|
|
|
|
|
247
|
|
|
} |
|
248
|
|
|
if($player->pivot->level_id == 2){ |
|
249
|
|
|
$bPlayers[] = $player->pivot->player_id; |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
//Create Skins arrays |
|
254
|
|
|
//player_id, 'holescores' |
|
255
|
|
|
foreach($matchRound as $key => $round) { |
|
256
|
|
View Code Duplication |
if (in_array($round->player_id,$aPlayers)){ |
|
|
|
|
|
|
257
|
|
|
$aPlayersSkins[$key]['player_id'] = $round->player_id; |
|
|
|
|
|
|
258
|
|
|
$aPlayersSkins[$key]['holescores'] = $round->holescores; |
|
|
|
|
|
|
259
|
|
|
} |
|
260
|
|
View Code Duplication |
if (in_array($round->player_id,$bPlayers)){ |
|
|
|
|
|
|
261
|
|
|
$bPlayersSkins[$key]['player_id'] = $round->player_id; |
|
|
|
|
|
|
262
|
|
|
$bPlayersSkins[$key]['holescores'] = $round->holescores; |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
//Run A Skins analysis |
|
267
|
|
|
$scores = array(); |
|
268
|
|
View Code Duplication |
foreach($aPlayersSkins as $key => $playerSkin){ |
|
|
|
|
|
|
269
|
|
|
foreach($playerSkin['holescores'] as $hole => $holescore){ |
|
270
|
|
|
$scores[$holescore['hole_id']][$playerSkin['player_id']] = $holescore['score']; |
|
271
|
|
|
} |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
View Code Duplication |
foreach($scores as $hole_id => $hole) { |
|
|
|
|
|
|
275
|
|
|
$minScore = min($hole); |
|
|
|
|
|
|
276
|
|
|
$winners[$hole_id] = array_keys($hole, min($hole)); //gets player id of lowest score |
|
|
|
|
|
|
277
|
|
|
} |
|
278
|
|
|
$aSkinsWon = 0; |
|
279
|
|
View Code Duplication |
foreach($winners as $key => $winner) { |
|
|
|
|
|
|
280
|
|
|
if(count($winner) === 1) { |
|
281
|
|
|
//post to DB |
|
282
|
|
|
$skinWinner = new Skin; |
|
283
|
|
|
$skinWinner->player_id = $winner[0]; |
|
284
|
|
|
$skinWinner->level_id = 1; |
|
285
|
|
|
$skinWinner->match_id = intval($matchdata['match']); |
|
286
|
|
|
$skinWinner->hole_id = $key; |
|
287
|
|
|
$skinWinner->save(); |
|
288
|
|
|
$aSkinsWon++; |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
//Run B Skins analysis |
|
293
|
|
|
$scores = array(); |
|
294
|
|
View Code Duplication |
foreach($bPlayersSkins as $key => $playerSkin){ |
|
|
|
|
|
|
295
|
|
|
foreach($playerSkin['holescores'] as $hole => $holescore){ |
|
296
|
|
|
$scores[$holescore['hole_id']][$playerSkin['player_id']] = $holescore['score']; |
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
View Code Duplication |
foreach($scores as $hole_id => $hole) { |
|
|
|
|
|
|
301
|
|
|
$minScore = min($hole); |
|
|
|
|
|
|
302
|
|
|
$winners[$hole_id] = array_keys($hole, min($hole)); //gets player id of lowest score |
|
303
|
|
|
} |
|
304
|
|
|
$bSkinsWon = 0; |
|
305
|
|
View Code Duplication |
foreach($winners as $key => $winner) { |
|
|
|
|
|
|
306
|
|
|
if(count($winner) === 1) { |
|
307
|
|
|
//post to DB |
|
308
|
|
|
$skinWinner = new Skin; |
|
309
|
|
|
$skinWinner->player_id = $winner[0]; |
|
310
|
|
|
$skinWinner->level_id = 2; |
|
311
|
|
|
$skinWinner->match_id = intval($matchdata['match']); |
|
312
|
|
|
$skinWinner->hole_id = $key; |
|
313
|
|
|
$skinWinner->save(); |
|
314
|
|
|
$bSkinsWon++; |
|
315
|
|
|
} |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
$match = Match::find($matchdata['match']); |
|
319
|
|
|
|
|
320
|
|
|
//Need to add Carry over money if there no skins are won |
|
321
|
|
|
//check for carry over money |
|
322
|
|
|
$skinsamoney = $match->skinsamoney; // + carryover A money if any |
|
323
|
|
|
$skinsbmoney = $match->skinsbmoney; // + carryover B money if any |
|
324
|
|
|
|
|
325
|
|
View Code Duplication |
if($aSkinsWon > 0) { |
|
|
|
|
|
|
326
|
|
|
$moneyperskinA = $skinsamoney / $aSkinsWon; |
|
327
|
|
|
|
|
328
|
|
|
$aSkins = Skin::where('match_id', '=', $matchdata['match'])->where('level_id', '=', 1)->get(); |
|
329
|
|
|
foreach ($aSkins as $askin){ |
|
330
|
|
|
$askin->money = $moneyperskinA; |
|
331
|
|
|
$askin->save(); |
|
332
|
|
|
} |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
View Code Duplication |
if($bSkinsWon > 0) { |
|
|
|
|
|
|
336
|
|
|
$moneyperskinB = $skinsbmoney / $bSkinsWon; |
|
337
|
|
|
|
|
338
|
|
|
$bSkins = Skin::where('match_id', '=', $matchdata['match'])->where('level_id', '=', 2)->get(); |
|
339
|
|
|
foreach ($bSkins as $bskin){ |
|
340
|
|
|
$bskin->money = $moneyperskinB; |
|
341
|
|
|
$bskin->save(); |
|
342
|
|
|
} |
|
343
|
|
|
} |
|
344
|
|
|
//foreach player in pivot table create player and run handicap analysis |
|
345
|
|
|
foreach($match->players as $matchplayer) |
|
346
|
|
|
{ |
|
347
|
|
|
$player = Player::find($matchplayer->pivot->player_id); |
|
348
|
|
|
$handicap = new Handicap($player); |
|
349
|
|
|
$player->handicap = $handicap->calculate(); |
|
350
|
|
|
$player->save(); |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
//Fire event to calculate money won and add to pivot table match_player |
|
354
|
|
|
$this->events->fire('match.finalize', $match); |
|
355
|
|
|
|
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
/** |
|
359
|
|
|
* Method to get match from input Match data |
|
360
|
|
|
* |
|
361
|
|
|
* @param mixed $matchdata |
|
|
|
|
|
|
362
|
|
|
* @return JSON object |
|
363
|
|
|
*/ |
|
364
|
|
|
public function get($matchid) |
|
365
|
|
|
{ |
|
366
|
|
|
$matchdata = $this->matchRepo->get($matchid); |
|
367
|
|
|
return $matchdata; |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
|
|
371
|
|
|
|
|
372
|
|
|
private function search($array, $key, $value) |
|
373
|
|
|
{ |
|
374
|
|
|
$results = array(); |
|
375
|
|
|
if (is_array($array)) { |
|
376
|
|
|
if (isset($array[$key]) && $array[$key] == $value) { |
|
377
|
|
|
$results[] = $array; |
|
378
|
|
|
} |
|
379
|
|
|
foreach ($array as $subarray) { |
|
380
|
|
|
$results = array_merge($results, $this->search($subarray, $key, $value)); |
|
381
|
|
|
} |
|
382
|
|
|
} |
|
383
|
|
|
return $results; |
|
384
|
|
|
} |
|
385
|
|
|
} |
|
386
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.