@@ -6,14 +6,14 @@ discard block |
||
6 | 6 | |
7 | 7 | class TeamMatchesController extends \BaseController { |
8 | 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 | - /** |
|
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 | 17 | * Display a listing of the resource. |
18 | 18 | * |
19 | 19 | * @return Response |
@@ -56,211 +56,211 @@ discard block |
||
56 | 56 | { |
57 | 57 | |
58 | 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[0]['team']; |
|
100 | - |
|
101 | - $team2Name = Team::select('name')->where('id', '=', $team2Id)->get()->toArray(); |
|
102 | - |
|
103 | - $holesData = Hole::select('handicap')->where('course_id', '=', $team1[0]['course'])->get(); |
|
104 | - |
|
105 | - $team1Scores = $this->getTeamNetScore($team1, $holesData); |
|
106 | - $team2Scores = $this->getTeamNetScore($team2, $holesData); |
|
107 | - |
|
108 | - $team1Points = $this->calculatePoints($team1Scores, $team2Scores); |
|
109 | - $team2Points = $this->calculatePoints($team2Scores, $team1Scores); |
|
110 | - |
|
111 | - // Bonus point logic |
|
112 | - // Occurs after 9th hole score is added to both teams |
|
113 | - |
|
114 | - if($team1Scores[8] != null && $team2Scores[8] != null){ |
|
115 | - if($team1Points > $team2Points){ |
|
116 | - $team1Points = $team1Points + 1; |
|
117 | - } |
|
118 | - if($team2Points > $team1Points){ |
|
119 | - $team2Points = $team2Points + 1; |
|
120 | - } |
|
121 | - if($team1Points == $team2Points){ |
|
122 | - $team1Points = $team1Points + .5; |
|
123 | - $team2Points = $team2Points + .5; |
|
124 | - } |
|
125 | - } |
|
126 | - |
|
127 | - //Save Points in Teammatches |
|
128 | - Teammatch::where('match_id', '=', $id)->where('team_id', '=', $team1Id)->update(array('pointsWon' => $team1Points)); |
|
129 | - Teammatch::where('match_id', '=', $id)->where('team_id', '=', $team2Id)->update(array('pointsWon' => $team2Points)); |
|
130 | - |
|
131 | - |
|
132 | - //Need to determine if same amount of scores are in both |
|
133 | - // If not then do not return |
|
134 | - |
|
135 | - foreach($team1Scores as $key=>$teamScore){ |
|
136 | - if($teamScore <= 0){ |
|
137 | - $team1Scores[$key] = ''; |
|
138 | - } |
|
139 | - } |
|
140 | - |
|
141 | - foreach($team2Scores as $key=>$teamScore){ |
|
142 | - if($teamScore <= 0){ |
|
143 | - $team2Scores[$key] = ''; |
|
144 | - } |
|
145 | - } |
|
146 | - |
|
147 | - $team[0] = [ |
|
148 | - "team" => $team1Name[0]['name'], |
|
149 | - "hole1" => $team1Scores[0], |
|
150 | - "hole2" => $team1Scores[1], |
|
151 | - "hole3" => $team1Scores[2], |
|
152 | - "hole4" => $team1Scores[3], |
|
153 | - "hole5" => $team1Scores[4], |
|
154 | - "hole6" => $team1Scores[5], |
|
155 | - "hole7" => $team1Scores[6], |
|
156 | - "hole8" => $team1Scores[7], |
|
157 | - "hole9" => $team1Scores[8], |
|
158 | - "points" =>$team1Points |
|
159 | - ]; |
|
160 | - |
|
161 | - $team[1] = [ |
|
162 | - "team" => $team2Name[0]['name'], |
|
163 | - "hole1" => $team2Scores[0], |
|
164 | - "hole2" => $team2Scores[1], |
|
165 | - "hole3" => $team2Scores[2], |
|
166 | - "hole4" => $team2Scores[3], |
|
167 | - "hole5" => $team2Scores[4], |
|
168 | - "hole6" => $team2Scores[5], |
|
169 | - "hole7" => $team2Scores[6], |
|
170 | - "hole8" => $team2Scores[7], |
|
171 | - "hole9" => $team2Scores[8], |
|
172 | - "points" => $team2Points |
|
173 | - ]; |
|
174 | - |
|
175 | - $data['data'] = $team; |
|
176 | - return $data; |
|
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[0]['team']; |
|
100 | + |
|
101 | + $team2Name = Team::select('name')->where('id', '=', $team2Id)->get()->toArray(); |
|
102 | + |
|
103 | + $holesData = Hole::select('handicap')->where('course_id', '=', $team1[0]['course'])->get(); |
|
104 | + |
|
105 | + $team1Scores = $this->getTeamNetScore($team1, $holesData); |
|
106 | + $team2Scores = $this->getTeamNetScore($team2, $holesData); |
|
107 | + |
|
108 | + $team1Points = $this->calculatePoints($team1Scores, $team2Scores); |
|
109 | + $team2Points = $this->calculatePoints($team2Scores, $team1Scores); |
|
110 | + |
|
111 | + // Bonus point logic |
|
112 | + // Occurs after 9th hole score is added to both teams |
|
113 | + |
|
114 | + if($team1Scores[8] != null && $team2Scores[8] != null){ |
|
115 | + if($team1Points > $team2Points){ |
|
116 | + $team1Points = $team1Points + 1; |
|
117 | + } |
|
118 | + if($team2Points > $team1Points){ |
|
119 | + $team2Points = $team2Points + 1; |
|
120 | + } |
|
121 | + if($team1Points == $team2Points){ |
|
122 | + $team1Points = $team1Points + .5; |
|
123 | + $team2Points = $team2Points + .5; |
|
124 | + } |
|
125 | + } |
|
126 | + |
|
127 | + //Save Points in Teammatches |
|
128 | + Teammatch::where('match_id', '=', $id)->where('team_id', '=', $team1Id)->update(array('pointsWon' => $team1Points)); |
|
129 | + Teammatch::where('match_id', '=', $id)->where('team_id', '=', $team2Id)->update(array('pointsWon' => $team2Points)); |
|
130 | + |
|
131 | + |
|
132 | + //Need to determine if same amount of scores are in both |
|
133 | + // If not then do not return |
|
134 | + |
|
135 | + foreach($team1Scores as $key=>$teamScore){ |
|
136 | + if($teamScore <= 0){ |
|
137 | + $team1Scores[$key] = ''; |
|
138 | + } |
|
139 | + } |
|
140 | + |
|
141 | + foreach($team2Scores as $key=>$teamScore){ |
|
142 | + if($teamScore <= 0){ |
|
143 | + $team2Scores[$key] = ''; |
|
144 | + } |
|
145 | + } |
|
146 | + |
|
147 | + $team[0] = [ |
|
148 | + "team" => $team1Name[0]['name'], |
|
149 | + "hole1" => $team1Scores[0], |
|
150 | + "hole2" => $team1Scores[1], |
|
151 | + "hole3" => $team1Scores[2], |
|
152 | + "hole4" => $team1Scores[3], |
|
153 | + "hole5" => $team1Scores[4], |
|
154 | + "hole6" => $team1Scores[5], |
|
155 | + "hole7" => $team1Scores[6], |
|
156 | + "hole8" => $team1Scores[7], |
|
157 | + "hole9" => $team1Scores[8], |
|
158 | + "points" =>$team1Points |
|
159 | + ]; |
|
160 | + |
|
161 | + $team[1] = [ |
|
162 | + "team" => $team2Name[0]['name'], |
|
163 | + "hole1" => $team2Scores[0], |
|
164 | + "hole2" => $team2Scores[1], |
|
165 | + "hole3" => $team2Scores[2], |
|
166 | + "hole4" => $team2Scores[3], |
|
167 | + "hole5" => $team2Scores[4], |
|
168 | + "hole6" => $team2Scores[5], |
|
169 | + "hole7" => $team2Scores[6], |
|
170 | + "hole8" => $team2Scores[7], |
|
171 | + "hole9" => $team2Scores[8], |
|
172 | + "points" => $team2Points |
|
173 | + ]; |
|
174 | + |
|
175 | + $data['data'] = $team; |
|
176 | + return $data; |
|
177 | 177 | } |
178 | 178 | |
179 | - private function getTeamNetScore($team, $holesData) |
|
180 | - { |
|
181 | - foreach($team as $key=>$item){ |
|
182 | - // Create holes array for NET |
|
183 | - $holes = array(); |
|
184 | - $i = 1; |
|
185 | - foreach($holesData as $key=>$holeData){ |
|
186 | - $holes[$i] = $holeData->handicap; |
|
187 | - $i++; |
|
188 | - } |
|
189 | - |
|
190 | - // Create stroke array - how many to take away from score |
|
191 | - $strokeArray = [ |
|
192 | - 1 => 0, |
|
193 | - 2 => 0, |
|
194 | - 3 => 0, |
|
195 | - 4 => 0, |
|
196 | - 5 => 0, |
|
197 | - 6 => 0, |
|
198 | - 7 => 0, |
|
199 | - 8 => 0, |
|
200 | - 9 => 0 |
|
201 | - ]; |
|
202 | - //Create array of strokes |
|
203 | - for($counter = $item['matchHandicap']; $counter > 0; $counter--){ |
|
204 | - if($counter > 9){ |
|
205 | - $newCounter = $counter - 9; |
|
206 | - while($newCounter > 0) { |
|
207 | - $strokeArray[$newCounter] = $strokeArray[$newCounter] + 1; |
|
208 | - $counter--; |
|
209 | - $newCounter--; |
|
210 | - } |
|
211 | - } |
|
212 | - $strokeArray[$counter] = $strokeArray[$counter] + 1; |
|
213 | - } |
|
214 | - // Plus handicaps don't hit previous loop so need its own |
|
215 | - //Plus handicap logic |
|
216 | - |
|
217 | - foreach($strokeArray as $strokeKey=>$stroke){ |
|
218 | - $holeKey = array_search($strokeKey,$holes); |
|
219 | - $holes[$holeKey] = $stroke; |
|
220 | - } |
|
221 | - |
|
222 | - ///now have array of strokes to subtract |
|
223 | - //get array of holescores |
|
224 | - |
|
225 | - $holeScores = $item['holescores']; |
|
226 | - foreach($holeScores as $key=>$holeScore){ |
|
227 | - //check if new score is less PlayerScores |
|
228 | - if(isset($playerScores[$key])){ // 2nd time in for hole |
|
229 | - if($playerScores[$key] >= $holeScore['score']){ |
|
230 | - $playerScores[$key] = $holeScore['score'] - $holes[$key+1]; |
|
231 | - } |
|
232 | - } else{ // first time in for hole |
|
233 | - if($holeScore['score'] != null){ |
|
234 | - $playerScores[$key] = $holeScore['score'] - $holes[$key+1]; |
|
235 | - } else{ |
|
236 | - $playerScores[$key] = null; |
|
237 | - } |
|
238 | - } |
|
239 | - } |
|
240 | - |
|
241 | - } |
|
242 | - return $playerScores; |
|
243 | - } |
|
244 | - |
|
245 | - private function calculatePoints($team, $opponent) |
|
246 | - { |
|
247 | - $points = 0; |
|
248 | - $teamTotalPoints = 0; |
|
249 | - $opponentTotalPoints = 0; |
|
250 | - foreach ($team as $key=>$score){ |
|
251 | - if($score != null){ |
|
252 | - if($score < $opponent[$key]){ |
|
253 | - $points++; |
|
254 | - } |
|
255 | - if($score == $opponent[$key]){ |
|
256 | - $points = $points + .5; |
|
257 | - } |
|
258 | - } |
|
259 | - |
|
260 | - } |
|
261 | - |
|
262 | - return $points; |
|
263 | - } |
|
179 | + private function getTeamNetScore($team, $holesData) |
|
180 | + { |
|
181 | + foreach($team as $key=>$item){ |
|
182 | + // Create holes array for NET |
|
183 | + $holes = array(); |
|
184 | + $i = 1; |
|
185 | + foreach($holesData as $key=>$holeData){ |
|
186 | + $holes[$i] = $holeData->handicap; |
|
187 | + $i++; |
|
188 | + } |
|
189 | + |
|
190 | + // Create stroke array - how many to take away from score |
|
191 | + $strokeArray = [ |
|
192 | + 1 => 0, |
|
193 | + 2 => 0, |
|
194 | + 3 => 0, |
|
195 | + 4 => 0, |
|
196 | + 5 => 0, |
|
197 | + 6 => 0, |
|
198 | + 7 => 0, |
|
199 | + 8 => 0, |
|
200 | + 9 => 0 |
|
201 | + ]; |
|
202 | + //Create array of strokes |
|
203 | + for($counter = $item['matchHandicap']; $counter > 0; $counter--){ |
|
204 | + if($counter > 9){ |
|
205 | + $newCounter = $counter - 9; |
|
206 | + while($newCounter > 0) { |
|
207 | + $strokeArray[$newCounter] = $strokeArray[$newCounter] + 1; |
|
208 | + $counter--; |
|
209 | + $newCounter--; |
|
210 | + } |
|
211 | + } |
|
212 | + $strokeArray[$counter] = $strokeArray[$counter] + 1; |
|
213 | + } |
|
214 | + // Plus handicaps don't hit previous loop so need its own |
|
215 | + //Plus handicap logic |
|
216 | + |
|
217 | + foreach($strokeArray as $strokeKey=>$stroke){ |
|
218 | + $holeKey = array_search($strokeKey,$holes); |
|
219 | + $holes[$holeKey] = $stroke; |
|
220 | + } |
|
221 | + |
|
222 | + ///now have array of strokes to subtract |
|
223 | + //get array of holescores |
|
224 | + |
|
225 | + $holeScores = $item['holescores']; |
|
226 | + foreach($holeScores as $key=>$holeScore){ |
|
227 | + //check if new score is less PlayerScores |
|
228 | + if(isset($playerScores[$key])){ // 2nd time in for hole |
|
229 | + if($playerScores[$key] >= $holeScore['score']){ |
|
230 | + $playerScores[$key] = $holeScore['score'] - $holes[$key+1]; |
|
231 | + } |
|
232 | + } else{ // first time in for hole |
|
233 | + if($holeScore['score'] != null){ |
|
234 | + $playerScores[$key] = $holeScore['score'] - $holes[$key+1]; |
|
235 | + } else{ |
|
236 | + $playerScores[$key] = null; |
|
237 | + } |
|
238 | + } |
|
239 | + } |
|
240 | + |
|
241 | + } |
|
242 | + return $playerScores; |
|
243 | + } |
|
244 | + |
|
245 | + private function calculatePoints($team, $opponent) |
|
246 | + { |
|
247 | + $points = 0; |
|
248 | + $teamTotalPoints = 0; |
|
249 | + $opponentTotalPoints = 0; |
|
250 | + foreach ($team as $key=>$score){ |
|
251 | + if($score != null){ |
|
252 | + if($score < $opponent[$key]){ |
|
253 | + $points++; |
|
254 | + } |
|
255 | + if($score == $opponent[$key]){ |
|
256 | + $points = $points + .5; |
|
257 | + } |
|
258 | + } |
|
259 | + |
|
260 | + } |
|
261 | + |
|
262 | + return $points; |
|
263 | + } |
|
264 | 264 | |
265 | 265 | /** |
266 | 266 | * Show the form for editing the specified resource. |
@@ -298,15 +298,15 @@ discard block |
||
298 | 298 | } |
299 | 299 | |
300 | 300 | public function getPointsByYear($year) |
301 | - { |
|
302 | - $teamMatches = Teammatch::select('team_id','pointsWon')->whereYear('created_at', '=', $year)->with('team')->get(); |
|
303 | - foreach ($teamMatches as $key=>$teamMatch) { |
|
304 | - $pointsData[$key]['name'] = $teamMatch['team']['name']; |
|
305 | - $pointsData[$key]['points'] = $teamMatch['pointsWon']; |
|
306 | - } |
|
307 | - $data['data'] = $pointsData; |
|
308 | - return $data; |
|
309 | - } |
|
301 | + { |
|
302 | + $teamMatches = Teammatch::select('team_id','pointsWon')->whereYear('created_at', '=', $year)->with('team')->get(); |
|
303 | + foreach ($teamMatches as $key=>$teamMatch) { |
|
304 | + $pointsData[$key]['name'] = $teamMatch['team']['name']; |
|
305 | + $pointsData[$key]['points'] = $teamMatch['pointsWon']; |
|
306 | + } |
|
307 | + $data['data'] = $pointsData; |
|
308 | + return $data; |
|
309 | + } |
|
310 | 310 | |
311 | 311 | |
312 | 312 | } |