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