1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use GolfLeague\Services\MatchService as MatchService; |
4
|
|
|
use GolfLeague\Storage\MatchRound\MatchRoundRepository as MatchRoundRepository; |
5
|
|
|
use GolfLeague\Storage\Team\TeamMatchesRepository as TeamMatchesRepository; |
6
|
|
|
|
7
|
|
|
class TeamMatchesController extends \BaseController { |
|
|
|
|
8
|
|
|
|
9
|
|
|
public function __construct(MatchService $match, MatchRoundRepository $matchRoundRepo, TeamMatchesRepository $teamMatchesRepository) |
10
|
|
|
{ |
11
|
|
|
$this->match = $match; |
12
|
|
|
$this->matchRoundRepo = $matchRoundRepo; |
13
|
|
|
$this->teamMatchesRepo = $teamMatchesRepository; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Display a listing of the resource. |
18
|
|
|
* |
19
|
|
|
* @return Response |
20
|
|
|
*/ |
21
|
|
|
public function index() |
22
|
|
|
{ |
23
|
|
|
// |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Show the form for creating a new resource. |
29
|
|
|
* |
30
|
|
|
* @return Response |
31
|
|
|
*/ |
32
|
|
|
public function create() |
33
|
|
|
{ |
34
|
|
|
// |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Store a newly created resource in storage. |
40
|
|
|
* |
41
|
|
|
* @return Response |
42
|
|
|
*/ |
43
|
|
|
public function store() |
44
|
|
|
{ |
45
|
|
|
// |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Display the specified resource. |
51
|
|
|
* |
52
|
|
|
* @param int $id |
53
|
|
|
* @return Response |
54
|
|
|
*/ |
55
|
|
|
public function show($id) |
56
|
|
|
{ |
57
|
|
|
$group = Input::get('group'); // Stay in controller |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
$groupPlayers = $this->matchRoundRepo->matchGroup($id, $group); |
61
|
|
|
|
62
|
|
|
// |
63
|
|
|
// Create Player data from MatchRound Repo |
64
|
|
|
// Takes group of players |
65
|
|
|
// |
66
|
|
|
// Creates data array for each player in the match |
67
|
|
|
// |
68
|
|
|
$matchUp = array(); |
69
|
|
|
foreach($groupPlayers as $key=>$groupPlayer){ |
70
|
|
|
$matchUp[$key]['player'] = $groupPlayer->pivot->player_id; |
71
|
|
|
$matchUp[$key]['matchHandicap'] = round($groupPlayer->pivot->handicap ,0); |
72
|
|
|
foreach ($groupPlayer->round as $round){ |
73
|
|
|
$matchUp[$key]['team'] = $round->team_id; |
74
|
|
|
$matchUp[$key]['course'] = $round->course_id; |
75
|
|
|
$matchUp[$key]['holescores'] = $round->holescores; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Change lowest handicap to ZERO and change others to reflect it |
80
|
|
|
// Create array of each players handicap |
81
|
|
|
foreach($matchUp as $key=>$match){ |
82
|
|
|
$matchHandicaps[] = $match['matchHandicap']; |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// Determine the lowest handicap in the group |
86
|
|
|
$lowestMatchHandicap = min($matchHandicaps); |
|
|
|
|
87
|
|
|
|
88
|
|
|
//handicapChange is the number of strokes to offset on the others handicap |
89
|
|
|
$handicapChange = $lowestMatchHandicap * -1; |
90
|
|
|
|
91
|
|
|
// Adjust all handicaps |
92
|
|
|
foreach($matchUp as $key=>$match){ |
93
|
|
|
$matchUp[$key]['matchHandicap'] = $match['matchHandicap'] + $handicapChange; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// Separate group into two teams |
97
|
|
|
|
98
|
|
|
foreach($matchUp as $key=>$item){ |
99
|
|
|
$teamIds[$key] = $item['team']; |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
sort($teamIds); |
103
|
|
|
|
104
|
|
|
//$teamIds = array_slice($teamIds, 1, -1); |
|
|
|
|
105
|
|
|
$teamIds = array_unique($teamIds); |
106
|
|
|
$teamIds = array_values($teamIds); //reset array keys |
107
|
|
|
|
108
|
|
|
$team1Id = $teamIds[0]; |
109
|
|
|
$team2Id = $teamIds[1]; |
110
|
|
|
|
111
|
|
|
$team1 = array(); |
112
|
|
|
$team2 = array(); |
113
|
|
|
|
114
|
|
|
foreach($matchUp as $key=>$match){ |
115
|
|
|
if($match['team'] ==$team1Id ){ |
116
|
|
|
$team1[] = $matchUp[$key]; |
117
|
|
|
} else { |
118
|
|
|
$team2[] = $matchUp[$key]; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
$team1Name = Team::select('name')->where('id', '=', $team1Id)->get()->toArray(); |
124
|
|
|
foreach($matchUp as $key=>$match){ |
125
|
|
|
if($match['team'] == $team1[0]['team']){ |
126
|
|
|
$team1[] = $match; |
127
|
|
|
unset($matchUp[$key]); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$team2 = $matchUp; |
132
|
|
|
|
133
|
|
|
$team2Name = Team::select('name')->where('id', '=', $team2Id)->get()->toArray(); |
134
|
|
|
|
135
|
|
|
$holesData = Hole::select('handicap')->where('course_id', '=', $team1[0]['course'])->get(); |
136
|
|
|
|
137
|
|
|
$team1Scores = $this->getTeamNetScore($team1, $holesData); |
138
|
|
|
$team2Scores = $this->getTeamNetScore($team2, $holesData); |
139
|
|
|
|
140
|
|
|
$team1Points = $this->calculatePoints($team1Scores, $team2Scores); |
141
|
|
|
$team2Points = $this->calculatePoints($team2Scores, $team1Scores); |
142
|
|
|
|
143
|
|
|
// Bonus point logic |
144
|
|
|
// Occurs after 9th hole score is added to both teams |
145
|
|
|
|
146
|
|
|
$team1Bonus = null; |
147
|
|
|
$team2Bonus = null; |
148
|
|
|
|
149
|
|
|
if($team1Scores[8] != null && $team2Scores[8] != null){ |
150
|
|
|
if($team1Points > $team2Points){ |
151
|
|
|
$team1Points = $team1Points + 1; |
152
|
|
|
$team1Bonus = 1; |
153
|
|
|
} |
154
|
|
|
if($team2Points > $team1Points){ |
155
|
|
|
$team2Points = $team2Points + 1; |
156
|
|
|
$team2Bonus = 1; |
157
|
|
|
} |
158
|
|
|
if($team1Points == $team2Points){ |
159
|
|
|
$team1Points = $team1Points + .5; |
160
|
|
|
$team1Bonus = .5; |
161
|
|
|
$team2Points = $team2Points + .5; |
162
|
|
|
$team2Bonus = .5; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
//Save Points in Teammatches |
167
|
|
|
Teammatch::where('match_id', '=', $id)->where('team_id', '=', $team1Id)->update(array('pointsWon' => $team1Points)); |
168
|
|
|
Teammatch::where('match_id', '=', $id)->where('team_id', '=', $team2Id)->update(array('pointsWon' => $team2Points)); |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
//Need to determine if same amount of scores are in both |
172
|
|
|
// If not then do not return |
173
|
|
|
|
174
|
|
|
foreach($team1Scores as $key=>$teamScore){ |
175
|
|
|
if($teamScore <= 0){ |
176
|
|
|
$team1Scores[$key] = ''; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
$team1Net = array_sum($team1Scores); |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
foreach($team2Scores as $key=>$teamScore){ |
183
|
|
|
if($teamScore <= 0){ |
184
|
|
|
$team2Scores[$key] = ''; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
$team2Net = array_sum($team2Scores); |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
$team[0] = [ |
|
|
|
|
191
|
|
|
"team" => $team1Name[0]['name'], |
192
|
|
|
"hole1" => $team1Scores[0], |
193
|
|
|
"hole2" => $team1Scores[1], |
194
|
|
|
"hole3" => $team1Scores[2], |
195
|
|
|
"hole4" => $team1Scores[3], |
196
|
|
|
"hole5" => $team1Scores[4], |
197
|
|
|
"hole6" => $team1Scores[5], |
198
|
|
|
"hole7" => $team1Scores[6], |
199
|
|
|
"hole8" => $team1Scores[7], |
200
|
|
|
"hole9" => $team1Scores[8], |
201
|
|
|
"bonus" => $team1Bonus, |
202
|
|
|
"points" => $team1Points, |
203
|
|
|
"netscore" => $team1Net |
204
|
|
|
]; |
205
|
|
|
|
206
|
|
|
$team[1] = [ |
207
|
|
|
"team" => $team2Name[0]['name'], |
208
|
|
|
"hole1" => $team2Scores[0], |
209
|
|
|
"hole2" => $team2Scores[1], |
210
|
|
|
"hole3" => $team2Scores[2], |
211
|
|
|
"hole4" => $team2Scores[3], |
212
|
|
|
"hole5" => $team2Scores[4], |
213
|
|
|
"hole6" => $team2Scores[5], |
214
|
|
|
"hole7" => $team2Scores[6], |
215
|
|
|
"hole8" => $team2Scores[7], |
216
|
|
|
"hole9" => $team2Scores[8], |
217
|
|
|
"bonus" => $team2Bonus, |
218
|
|
|
"points" => $team2Points, |
219
|
|
|
"netscore" => $team2Net, |
220
|
|
|
]; |
221
|
|
|
|
222
|
|
|
$data['data'] = $team; |
|
|
|
|
223
|
|
|
return $data; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
private function getTeamNetScore($team, $holesData) |
227
|
|
|
{ |
228
|
|
|
foreach($team as $key=>$item){ |
229
|
|
|
// Create holes array for NET |
230
|
|
|
$holes = array(); |
231
|
|
|
$i = 1; |
232
|
|
|
foreach($holesData as $key=>$holeData){ |
233
|
|
|
$holes[$i] = $holeData->handicap; |
234
|
|
|
$i++; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
// Create stroke array - how many to take away from score |
238
|
|
|
$strokeArray = [ |
239
|
|
|
1 => 0, |
240
|
|
|
2 => 0, |
241
|
|
|
3 => 0, |
242
|
|
|
4 => 0, |
243
|
|
|
5 => 0, |
244
|
|
|
6 => 0, |
245
|
|
|
7 => 0, |
246
|
|
|
8 => 0, |
247
|
|
|
9 => 0 |
248
|
|
|
]; |
249
|
|
|
//Create array of strokes |
250
|
|
|
for($counter = $item['matchHandicap']; $counter > 0; $counter--){ |
251
|
|
|
if($counter > 9){ |
252
|
|
|
$newCounter = $counter - 9; |
253
|
|
|
while($newCounter > 0) { |
254
|
|
|
$strokeArray[$newCounter] = $strokeArray[$newCounter] + 1; |
255
|
|
|
$counter--; |
256
|
|
|
$newCounter--; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
$strokeArray[$counter] = $strokeArray[$counter] + 1; |
260
|
|
|
} |
261
|
|
|
// Plus handicaps don't hit previous loop so need its own |
262
|
|
|
//Plus handicap logic |
263
|
|
|
|
264
|
|
|
foreach($strokeArray as $strokeKey=>$stroke){ |
265
|
|
|
$holeKey = array_search($strokeKey,$holes); |
266
|
|
|
$holes[$holeKey] = $stroke; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
///now have array of strokes to subtract |
270
|
|
|
//get array of holescores |
271
|
|
|
|
272
|
|
|
$holeScores = $item['holescores']; |
273
|
|
|
foreach($holeScores as $key=>$holeScore){ |
274
|
|
|
//check if new score is less PlayerScores |
275
|
|
|
if(isset($teamScores[$key])){ // 2nd time in for hole |
276
|
|
|
|
277
|
|
|
//set temp handi score |
278
|
|
|
$tempScore = $holeScore['score'] - $holes[$key+1]; |
279
|
|
|
if($teamScores[$key] >= $tempScore){ |
280
|
|
|
$teamScores[$key] = $holeScore['score'] - $holes[$key+1]; |
281
|
|
|
} |
282
|
|
|
} else{ // first time in for hole |
283
|
|
|
if($holeScore['score'] != null){ |
284
|
|
|
$teamScores[$key] = $holeScore['score'] - $holes[$key+1]; |
|
|
|
|
285
|
|
|
} else{ |
286
|
|
|
$teamScores[$key] = null; |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
} |
292
|
|
|
return $teamScores; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
private function calculatePoints($team, $opponent) |
296
|
|
|
{ |
297
|
|
|
$points = 0; |
298
|
|
|
$teamTotalPoints = 0; |
|
|
|
|
299
|
|
|
$opponentTotalPoints = 0; |
|
|
|
|
300
|
|
|
foreach ($team as $key=>$score){ |
301
|
|
|
if($score != null){ |
302
|
|
|
if($score < $opponent[$key]){ |
303
|
|
|
$points++; |
304
|
|
|
} |
305
|
|
|
if($score == $opponent[$key]){ |
306
|
|
|
$points = $points + .5; |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
return $points; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Show the form for editing the specified resource. |
317
|
|
|
* |
318
|
|
|
* @param int $id |
319
|
|
|
* @return Response |
320
|
|
|
*/ |
321
|
|
|
public function edit($id) |
322
|
|
|
{ |
323
|
|
|
// |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Update the specified resource in storage. |
329
|
|
|
* |
330
|
|
|
* @param int $id |
331
|
|
|
* @return Response |
332
|
|
|
*/ |
333
|
|
|
public function update($id) |
334
|
|
|
{ |
335
|
|
|
// |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Remove the specified resource from storage. |
341
|
|
|
* |
342
|
|
|
* @param int $id |
343
|
|
|
* @return Response |
344
|
|
|
*/ |
345
|
|
|
public function destroy($id) |
346
|
|
|
{ |
347
|
|
|
// |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
public function getPointsByYear($year) |
351
|
|
|
{ |
352
|
|
|
$teamMatches = Teammatch::select('team_id','pointsWon')->whereYear('created_at', '=', $year)->with('team')->get(); |
353
|
|
|
foreach($teamMatches as $key=>$teamMatch){ |
354
|
|
|
$pointsData[$key]['name'] = $teamMatch['team']['name']; |
|
|
|
|
355
|
|
|
$pointsData[$key]['points'] = Teammatch::select('team_id','pointsWon') |
356
|
|
|
->where('team_id', '=', $teamMatch->team_id) |
357
|
|
|
->whereYear('created_at', '=', $year) |
358
|
|
|
->with('team') |
359
|
|
|
->sum('pointsWon'); |
360
|
|
|
|
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
$pointsData = array_map("unserialize", array_unique(array_map("serialize", $pointsData))); |
|
|
|
|
364
|
|
|
$data['data'] = $pointsData; |
|
|
|
|
365
|
|
|
return $data; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
|
369
|
|
|
} |
370
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.