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
|
|
|
|
58
|
|
|
|
59
|
|
|
$group = Input::get('group'); |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
$groupPlayers = $this->matchRoundRepo->matchGroup($id, $group); |
63
|
|
|
|
64
|
|
|
//Create Player data |
65
|
|
|
$matchUp = array(); |
66
|
|
|
foreach($groupPlayers as $key=>$groupPlayer){ |
67
|
|
|
$matchUp[$key]['player'] = $groupPlayer->pivot->player_id; |
68
|
|
|
$matchUp[$key]['matchHandicap'] = round($groupPlayer->pivot->handicap ,0); |
69
|
|
|
foreach ($groupPlayer->round as $round){ |
70
|
|
|
$matchUp[$key]['team'] = $round->team_id; |
71
|
|
|
$matchUp[$key]['course'] = $round->course_id; |
72
|
|
|
$matchUp[$key]['holescores'] = $round->holescores; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Change lowest handicap to ZERO and change others to reflect it |
78
|
|
|
foreach($matchUp as $key=>$match){ |
79
|
|
|
$matchHandicaps[] = $match['matchHandicap']; |
|
|
|
|
80
|
|
|
} |
81
|
|
|
$lowestMatchHandicap = min($matchHandicaps); |
|
|
|
|
82
|
|
|
//handicap change |
83
|
|
|
$handicapChange = $lowestMatchHandicap * -1; |
84
|
|
|
foreach($matchUp as $key=>$match){ |
85
|
|
|
$matchUp[$key]['matchHandicap'] = $match['matchHandicap'] + $handicapChange; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Separate two teams |
89
|
|
|
$team1 = array(array_shift ($matchUp)); |
90
|
|
|
$team1Id = $team1[0]['team']; |
91
|
|
|
$team1Name = Team::select('name')->where('id', '=', $team1Id)->get()->toArray(); |
92
|
|
|
foreach($matchUp as $key=>$match){ |
93
|
|
|
if($match['team'] == $team1[0]['team']){ |
94
|
|
|
$team1[] = $match; |
95
|
|
|
unset($matchUp[$key]); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
$team2 = $matchUp; |
99
|
|
|
$team2Id = $team2[1]['team']; |
100
|
|
|
$team2Name = Team::select('name')->where('id', '=', $team2Id)->get()->toArray(); |
101
|
|
|
|
102
|
|
|
$holesData = Hole::select('handicap')->where('course_id', '=', $team1[0]['course'])->get(); |
103
|
|
|
|
104
|
|
|
$team1Scores = $this->getTeamNetScore($team1, $holesData); |
105
|
|
|
$team2Scores = $this->getTeamNetScore($team2, $holesData); |
106
|
|
|
|
107
|
|
|
$team1Points = $this->calculatePoints($team1Scores, $team2Scores); |
108
|
|
|
$team2Points = $this->calculatePoints($team2Scores, $team1Scores); |
109
|
|
|
|
110
|
|
|
// Bonus point logic |
111
|
|
|
// Occurs after 9th hole score is added to both teams |
112
|
|
|
|
113
|
|
|
if($team1Scores[8] != null && $team2Scores[8] != null){ |
114
|
|
|
if($team1Points > $team2Points){ |
115
|
|
|
$team1Points = $team1Points + 1; |
116
|
|
|
} |
117
|
|
|
if($team2Points > $team1Points){ |
118
|
|
|
$team2Points = $team2Points + 1; |
119
|
|
|
} |
120
|
|
|
if($team1Points == $team2Points){ |
121
|
|
|
$team1Points = $team1Points + .5; |
122
|
|
|
$team2Points = $team2Points + .5; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
//Save Points in Teammatches |
127
|
|
|
Teammatch::where('match_id', '=', $id)->where('team_id', '=', $team1Id)->update(array('pointsWon' => $team1Points)); |
128
|
|
|
Teammatch::where('match_id', '=', $id)->where('team_id', '=', $team2Id)->update(array('pointsWon' => $team2Points)); |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
//Need to determine if same amount of scores are in both |
132
|
|
|
// If not then do not return |
133
|
|
|
|
134
|
|
|
foreach($team1Scores as $key=>$teamScore){ |
135
|
|
|
if($teamScore <= 0){ |
136
|
|
|
$team1Scores[$key] = ''; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
foreach($team2Scores as $key=>$teamScore){ |
141
|
|
|
if($teamScore <= 0){ |
142
|
|
|
$team2Scores[$key] = ''; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$team[0] = [ |
|
|
|
|
147
|
|
|
"team" => $team1Name[0]['name'], |
148
|
|
|
"hole1" => $team1Scores[0], |
149
|
|
|
"hole2" => $team1Scores[1], |
150
|
|
|
"hole3" => $team1Scores[2], |
151
|
|
|
"hole4" => $team1Scores[3], |
152
|
|
|
"hole5" => $team1Scores[4], |
153
|
|
|
"hole6" => $team1Scores[5], |
154
|
|
|
"hole7" => $team1Scores[6], |
155
|
|
|
"hole8" => $team1Scores[7], |
156
|
|
|
"hole9" => $team1Scores[8], |
157
|
|
|
"points" =>$team1Points |
158
|
|
|
]; |
159
|
|
|
|
160
|
|
|
$team[1] = [ |
161
|
|
|
"team" => $team2Name[0]['name'], |
162
|
|
|
"hole1" => $team2Scores[0], |
163
|
|
|
"hole2" => $team2Scores[1], |
164
|
|
|
"hole3" => $team2Scores[2], |
165
|
|
|
"hole4" => $team2Scores[3], |
166
|
|
|
"hole5" => $team2Scores[4], |
167
|
|
|
"hole6" => $team2Scores[5], |
168
|
|
|
"hole7" => $team2Scores[6], |
169
|
|
|
"hole8" => $team2Scores[7], |
170
|
|
|
"hole9" => $team2Scores[8], |
171
|
|
|
"points" => $team2Points |
172
|
|
|
]; |
173
|
|
|
|
174
|
|
|
$data['data'] = $team; |
|
|
|
|
175
|
|
|
return $data; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
private function getTeamNetScore($team, $holesData) |
179
|
|
|
{ |
180
|
|
|
foreach($team as $key=>$item){ |
181
|
|
|
// Create holes array for NET |
182
|
|
|
$holes = array(); |
183
|
|
|
$i = 1; |
184
|
|
|
foreach($holesData as $key=>$holeData){ |
185
|
|
|
$holes[$i] = $holeData->handicap; |
186
|
|
|
$i++; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
// Create stroke array - how many to take away from score |
190
|
|
|
$strokeArray = [ |
191
|
|
|
1 => 0, |
192
|
|
|
2 => 0, |
193
|
|
|
3 => 0, |
194
|
|
|
4 => 0, |
195
|
|
|
5 => 0, |
196
|
|
|
6 => 0, |
197
|
|
|
7 => 0, |
198
|
|
|
8 => 0, |
199
|
|
|
9 => 0 |
200
|
|
|
]; |
201
|
|
|
//Create array of strokes |
202
|
|
|
for($counter = $item['matchHandicap']; $counter > 0; $counter--){ |
203
|
|
|
if($counter > 9){ |
204
|
|
|
$newCounter = $counter - 9; |
205
|
|
|
while($newCounter > 0) { |
206
|
|
|
$strokeArray[$newCounter] = $strokeArray[$newCounter] + 1; |
207
|
|
|
$counter--; |
208
|
|
|
$newCounter--; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
$strokeArray[$counter] = $strokeArray[$counter] + 1; |
212
|
|
|
} |
213
|
|
|
// Plus handicaps don't hit previous loop so need its own |
214
|
|
|
//Plus handicap logic |
215
|
|
|
|
216
|
|
|
foreach($strokeArray as $strokeKey=>$stroke){ |
217
|
|
|
$holeKey = array_search($strokeKey,$holes); |
218
|
|
|
$holes[$holeKey] = $stroke; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
///now have array of strokes to subtract |
222
|
|
|
//get array of holescores |
223
|
|
|
|
224
|
|
|
$holeScores = $item['holescores']; |
225
|
|
|
foreach($holeScores as $key=>$holeScore){ |
226
|
|
|
//check if new score is less PlayerScores |
227
|
|
|
if(isset($playerScores[$key])){ // 2nd time in for hole |
228
|
|
|
if($playerScores[$key] >= $holeScore['score']){ |
229
|
|
|
$playerScores[$key] = $holeScore['score'] - $holes[$key+1]; |
230
|
|
|
} |
231
|
|
|
} else{ // first time in for hole |
232
|
|
|
if($holeScore['score'] != null){ |
233
|
|
|
$playerScores[$key] = $holeScore['score'] - $holes[$key+1]; |
|
|
|
|
234
|
|
|
} else{ |
235
|
|
|
$playerScores[$key] = null; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
} |
241
|
|
|
return $playerScores; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
private function calculatePoints($team, $opponent) |
245
|
|
|
{ |
246
|
|
|
$points = 0; |
247
|
|
|
$teamTotalPoints = 0; |
|
|
|
|
248
|
|
|
$opponentTotalPoints = 0; |
|
|
|
|
249
|
|
|
foreach ($team as $key=>$score){ |
250
|
|
|
if($score != null){ |
251
|
|
|
if($score < $opponent[$key]){ |
252
|
|
|
$points++; |
253
|
|
|
} |
254
|
|
|
if($score == $opponent[$key]){ |
255
|
|
|
$points = $points + .5; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return $points; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Show the form for editing the specified resource. |
266
|
|
|
* |
267
|
|
|
* @param int $id |
268
|
|
|
* @return Response |
269
|
|
|
*/ |
270
|
|
|
public function edit($id) |
271
|
|
|
{ |
272
|
|
|
// |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Update the specified resource in storage. |
278
|
|
|
* |
279
|
|
|
* @param int $id |
280
|
|
|
* @return Response |
281
|
|
|
*/ |
282
|
|
|
public function update($id) |
283
|
|
|
{ |
284
|
|
|
// |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Remove the specified resource from storage. |
290
|
|
|
* |
291
|
|
|
* @param int $id |
292
|
|
|
* @return Response |
293
|
|
|
*/ |
294
|
|
|
public function destroy($id) |
295
|
|
|
{ |
296
|
|
|
// |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
public function getPointsByYear($year) |
300
|
|
|
{ |
301
|
|
|
$teamMatches = Teammatch::select('team_id','pointsWon')->whereYear('created_at', '=', $year)->with('team')->get(); |
302
|
|
|
foreach ($teamMatches as $key=>$teamMatch) { |
303
|
|
|
$pointsData[$key]['name'] = $teamMatch['team']['name']; |
|
|
|
|
304
|
|
|
$pointsData[$key]['points'] = $teamMatch['pointsWon']; |
305
|
|
|
} |
306
|
|
|
$data['data'] = $pointsData; |
|
|
|
|
307
|
|
|
return $data; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
|
311
|
|
|
} |
312
|
|
|
|
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.