Completed
Push — master ( ecb587...f26f82 )
by Michael
02:55
created
app/library/Services/MatchService.php 1 patch
Indentation   +268 added lines, -268 removed lines patch added patch discarded remove patch
@@ -18,273 +18,273 @@
 block discarded – undo
18 18
  */
19 19
 class MatchService
20 20
 {
21
-    // Containing our matchRepository to make all our database calls to
22
-    protected $matchRepo;
23
-
24
-    /**
25
-     * Loads our $matchRepo
26
-     *
27
-     * @param MatchRepository $matchRepo
28
-     * @return MatchService
29
-     */
30
-    public function __construct(MatchRoundRepository $matchRoundRepo,
31
-                                MatchRepository $matchRepo,
32
-                                PrizeMoney $prizeMoney,
33
-                                Player $player,
34
-                                Match $match,
35
-                                CtpRepository $ctp,
36
-                                Dispatcher $events)
37
-    {
38
-        $this->matchRoundRepo = $matchRoundRepo;
39
-        $this->matchRepo = $matchRepo;
40
-        $this->prizeMoney = $prizeMoney;
41
-        $this->player = $player;
42
-        $this->match = $match;
43
-        $this->ctp = $ctp;
44
-        $this->events = $events;
45
-    }
46
-
47
-    /**
48
-     * Method to create match from input Match data
49
-     *
50
-     * @param mixed $matchdata
51
-     * @return
52
-     */
53
-    public function create($matchdata)
54
-    {
55
-
56
-        $this->prizeMoney->setPurse($matchdata['purse']);
57
-
58
-        $matchdata['purse'] = number_format($matchdata['purse'], 2);
59
-        $matchdata['grossmoney'] = $this->prizeMoney->getlowScore();
60
-        $matchdata['netmoney'] = $this->prizeMoney->getlowScore();
61
-
62
-        //How many A and B players
63
-        $totalPlayers = 0;
64
-
65
-        $aPlayerCount = 0;
21
+	// Containing our matchRepository to make all our database calls to
22
+	protected $matchRepo;
23
+
24
+	/**
25
+	 * Loads our $matchRepo
26
+	 *
27
+	 * @param MatchRepository $matchRepo
28
+	 * @return MatchService
29
+	 */
30
+	public function __construct(MatchRoundRepository $matchRoundRepo,
31
+								MatchRepository $matchRepo,
32
+								PrizeMoney $prizeMoney,
33
+								Player $player,
34
+								Match $match,
35
+								CtpRepository $ctp,
36
+								Dispatcher $events)
37
+	{
38
+		$this->matchRoundRepo = $matchRoundRepo;
39
+		$this->matchRepo = $matchRepo;
40
+		$this->prizeMoney = $prizeMoney;
41
+		$this->player = $player;
42
+		$this->match = $match;
43
+		$this->ctp = $ctp;
44
+		$this->events = $events;
45
+	}
46
+
47
+	/**
48
+	 * Method to create match from input Match data
49
+	 *
50
+	 * @param mixed $matchdata
51
+	 * @return
52
+	 */
53
+	public function create($matchdata)
54
+	{
55
+
56
+		$this->prizeMoney->setPurse($matchdata['purse']);
57
+
58
+		$matchdata['purse'] = number_format($matchdata['purse'], 2);
59
+		$matchdata['grossmoney'] = $this->prizeMoney->getlowScore();
60
+		$matchdata['netmoney'] = $this->prizeMoney->getlowScore();
61
+
62
+		//How many A and B players
63
+		$totalPlayers = 0;
64
+
65
+		$aPlayerCount = 0;
66 66
         
67
-        $bPlayerCount = 0;
68
-        foreach($matchdata['player'] as $player){
69
-            if ($player['level_id'] == '1'){
70
-                $aPlayerCount++;
71
-            }
72
-            else {
73
-                $bPlayerCount++;
74
-            }
75
-            $totalPlayers++;
76
-        }
77
-
78
-        //Calculate Skins money based on how many players in each group
79
-
80
-        $matchdata['skinsamoney'] = $this->prizeMoney->skinsGroupPot($matchdata['purse'], $totalPlayers, $aPlayerCount);
81
-        $matchdata['skinsbmoney'] = $this->prizeMoney->skinsGroupPot($matchdata['purse'], $totalPlayers, $bPlayerCount);
82
-        //check for carry over money and if there is add it to skins money
83
-        $carryOver = new CarryOver;
84
-        $carryOverMoney = $carryOver->calculate();
85
-        $matchdata['skinsamoney'] +=  $carryOverMoney[1];
86
-        $matchdata['skinsbmoney'] +=  $carryOverMoney[2];
87
-
88
-        //append current handicap and set winnings to 0 for each player
89
-        foreach ($matchdata['player'] as $key=>$player) {
90
-            //get each player's current handicap
91
-            $currentPlayer = $this->player->find($player['player_id']);
92
-            $matchdata['player'][$key]['handicap'] = $currentPlayer->handicap;
93
-            $matchdata['player'][$key]['winnings'] = 0;
94
-        }// End foreach
95
-
96
-        $matchid = $this->matchRepo->create($matchdata);
97
-        $matchdata['match_id'] = $matchid;
98
-        $this->events->fire('match.create', array($matchdata));
99
-    }
100
-
101
-    public function finalize($matchdata)
102
-    {
103
-        // post CTP1 and CTP2
104
-        $ctp1 = array(
105
-            'match_id' => $matchdata['match'],
106
-            'player_id' => $matchdata['ctp1'],
107
-            'hole_id' => $matchdata['ctp1hole'],
108
-            'money' => $this->prizeMoney->getCtp()
109
-        );
110
-        $this->ctp->create($ctp1);
111
-        $ctp2 = array(
112
-            'match_id' => $matchdata['match'],
113
-            'player_id' => $matchdata['ctp2'],
114
-            'hole_id' => $matchdata['ctp2hole'],
115
-            'money' => $this->prizeMoney->getCtp()
116
-        );
117
-        $this->ctp->create($ctp2);
118
-
119
-        //calculate Gross winner and post to grossWinnersTable
120
-
121
-        $matchRound = $this->matchRoundRepo->getByMatch($matchdata['match']);
122
-
123
-
124
-        $lowGross = array();
125
-        foreach ($matchRound as $key => $match){
126
-            $lowGross[$match['player']->id] = $match['score'];
127
-        }
128
-        $arrayLowGross = array_keys($lowGross, min($lowGross));
129
-        foreach($arrayLowGross as $key => $lowgrossPlayer) {
130
-            $grossWinner = new Grosswinner;
131
-            $grossWinner->player_id = $lowgrossPlayer;
132
-            $grossWinner->match_id = $matchdata['match'];
133
-            $grossWinner->score = $lowGross[$lowgrossPlayer];
134
-            $grossWinner->money = $this->prizeMoney->getlowScore() / count($arrayLowGross);
135
-            $grossWinner->save();
136
-        }
137
-
138
-        //Calculate NET winner
139
-        $lowNet = array();
140
-        $scores =array();
141
-        foreach ($matchRound as $key => $match){
142
-            $netScore = ($match['score'] - round($match['player']->handicap,0)); //calculate net score
143
-            $lowNet[$match['player']->id] = $netScore;
144
-        }
145
-        $arrayLowNet = array_keys($lowNet, min($lowNet));
146
-
147
-        foreach($arrayLowNet as $key => $lownetPlayer) {
148
-            $netWinner = new Netwinner;
149
-            $netWinner->player_id = $lownetPlayer;
150
-            $netWinner->match_id = $matchdata['match'];
151
-            $netWinner->score = $lowNet[$lownetPlayer];
152
-            $netWinner->money = $this->prizeMoney->getlowScore() /  count($arrayLowNet);
153
-            $netWinner->save();
154
-        }
155
-
156
-        //Calculate Skins
157
-
158
-        //determine A and B players
159
-        //using pivot table match_player
160
-        $match = $this->match->find($matchdata['match']);
161
-        $aPlayers = array();
162
-        $bPlayers = array();
163
-        foreach($match->players as $player)
164
-        {
165
-            if($player->pivot->level_id == 1){
166
-                $aPlayers[] = $player->pivot->player_id;
167
-
168
-            }
169
-            if($player->pivot->level_id == 2){
170
-                $bPlayers[] = $player->pivot->player_id;
171
-            }
172
-        }
173
-
174
-        //Create Skins arrays
175
-        //player_id, 'holescores'
176
-        foreach($matchRound as $key => $round) {
177
-            if (in_array($round->player_id,$aPlayers)){
178
-                $aPlayersSkins[$key]['player_id'] = $round->player_id;
179
-                $aPlayersSkins[$key]['holescores'] = $round->holescores;
180
-            }
181
-            if (in_array($round->player_id,$bPlayers)){
182
-                $bPlayersSkins[$key]['player_id'] = $round->player_id;
183
-                $bPlayersSkins[$key]['holescores'] = $round->holescores;
184
-            }
185
-        }
186
-
187
-        //Run A Skins analysis
188
-        $scores = array();
189
-        foreach($aPlayersSkins as $key => $playerSkin){
190
-            foreach($playerSkin['holescores'] as $hole => $holescore){
191
-                $scores[$holescore['hole_id']][$playerSkin['player_id']] = $holescore['score'];
192
-            }
193
-        }
194
-
195
-        foreach($scores as $hole_id => $hole) {
196
-            $minScore = min($hole);
197
-            $winners[$hole_id] = array_keys($hole, min($hole)); //gets player id of lowest score
198
-        }
199
-        $aSkinsWon = 0;
200
-        foreach($winners as $key => $winner) {
201
-            if(count($winner)  ===  1) {
202
-                //post to DB
203
-                $skinWinner = new Skin;
204
-                $skinWinner->player_id = $winner[0];
205
-                $skinWinner->level_id = 1;
206
-                $skinWinner->match_id = intval($matchdata['match']);
207
-                $skinWinner->hole_id = $key;
208
-                $skinWinner->save();
209
-                $aSkinsWon++;
210
-            }
211
-        }
212
-
213
-        //Run B Skins analysis
214
-        $scores = array();
215
-        foreach($bPlayersSkins as $key => $playerSkin){
216
-            foreach($playerSkin['holescores'] as $hole => $holescore){
217
-                $scores[$holescore['hole_id']][$playerSkin['player_id']] = $holescore['score'];
218
-            }
219
-        }
220
-
221
-        foreach($scores as $hole_id => $hole) {
222
-            $minScore = min($hole);
223
-            $winners[$hole_id] = array_keys($hole, min($hole)); //gets player id of lowest score
224
-        }
225
-        $bSkinsWon = 0;
226
-        foreach($winners as $key => $winner) {
227
-            if(count($winner)  ===  1) {
228
-                //post to DB
229
-                $skinWinner = new Skin;
230
-                $skinWinner->player_id = $winner[0];
231
-                $skinWinner->level_id = 2;
232
-                $skinWinner->match_id = intval($matchdata['match']);
233
-                $skinWinner->hole_id = $key;
234
-                $skinWinner->save();
235
-                $bSkinsWon++;
236
-            }
237
-        }
238
-
239
-        $match =  Match::find($matchdata['match']);
240
-
241
-        //Need to add Carry over money if there no skins are won
242
-        //check for carry over money
243
-        $skinsamoney = $match->skinsamoney; // + carryover A money if any
244
-        $skinsbmoney = $match->skinsbmoney; // + carryover B money if any
245
-
246
-        if($aSkinsWon > 0) {
247
-            $moneyperskinA = $skinsamoney / $aSkinsWon;
248
-
249
-            $aSkins = Skin::where('match_id', '=', $matchdata['match'])->where('level_id', '=', 1)->get();
250
-            foreach ($aSkins as $askin){
251
-                $askin->money = $moneyperskinA;
252
-                $askin->save();
253
-            }
254
-        }
255
-
256
-        if($bSkinsWon > 0) {
257
-            $moneyperskinB = $skinsbmoney / $bSkinsWon;
258
-
259
-            $bSkins = Skin::where('match_id', '=', $matchdata['match'])->where('level_id', '=', 2)->get();
260
-            foreach ($bSkins as $bskin){
261
-                $bskin->money = $moneyperskinB;
262
-                $bskin->save();
263
-            }
264
-        }
265
-        //foreach player in pivot table create player and run handicap analysis
266
-        foreach($match->players as $matchplayer)
267
-        {
268
-            $player = Player::find($matchplayer->pivot->player_id);
269
-            $handicap = new Handicap($player);
270
-            $player->handicap = $handicap->calculate();
271
-            $player->save();
272
-        }
273
-
274
-        //Fire event to calculate money won and add to pivot table match_player
275
-        $this->events->fire('match.finalize', $match);
276
-
277
-    }
278
-
279
-    /**
280
-     * Method to get match from input Match data
281
-     *
282
-     * @param mixed $matchdata
283
-     * @return JSON object
284
-     */
285
-    public function get($matchid)
286
-    {
287
-        $matchdata =  $this->matchRepo->get($matchid);
288
-        return $matchdata;
289
-    }
67
+		$bPlayerCount = 0;
68
+		foreach($matchdata['player'] as $player){
69
+			if ($player['level_id'] == '1'){
70
+				$aPlayerCount++;
71
+			}
72
+			else {
73
+				$bPlayerCount++;
74
+			}
75
+			$totalPlayers++;
76
+		}
77
+
78
+		//Calculate Skins money based on how many players in each group
79
+
80
+		$matchdata['skinsamoney'] = $this->prizeMoney->skinsGroupPot($matchdata['purse'], $totalPlayers, $aPlayerCount);
81
+		$matchdata['skinsbmoney'] = $this->prizeMoney->skinsGroupPot($matchdata['purse'], $totalPlayers, $bPlayerCount);
82
+		//check for carry over money and if there is add it to skins money
83
+		$carryOver = new CarryOver;
84
+		$carryOverMoney = $carryOver->calculate();
85
+		$matchdata['skinsamoney'] +=  $carryOverMoney[1];
86
+		$matchdata['skinsbmoney'] +=  $carryOverMoney[2];
87
+
88
+		//append current handicap and set winnings to 0 for each player
89
+		foreach ($matchdata['player'] as $key=>$player) {
90
+			//get each player's current handicap
91
+			$currentPlayer = $this->player->find($player['player_id']);
92
+			$matchdata['player'][$key]['handicap'] = $currentPlayer->handicap;
93
+			$matchdata['player'][$key]['winnings'] = 0;
94
+		}// End foreach
95
+
96
+		$matchid = $this->matchRepo->create($matchdata);
97
+		$matchdata['match_id'] = $matchid;
98
+		$this->events->fire('match.create', array($matchdata));
99
+	}
100
+
101
+	public function finalize($matchdata)
102
+	{
103
+		// post CTP1 and CTP2
104
+		$ctp1 = array(
105
+			'match_id' => $matchdata['match'],
106
+			'player_id' => $matchdata['ctp1'],
107
+			'hole_id' => $matchdata['ctp1hole'],
108
+			'money' => $this->prizeMoney->getCtp()
109
+		);
110
+		$this->ctp->create($ctp1);
111
+		$ctp2 = array(
112
+			'match_id' => $matchdata['match'],
113
+			'player_id' => $matchdata['ctp2'],
114
+			'hole_id' => $matchdata['ctp2hole'],
115
+			'money' => $this->prizeMoney->getCtp()
116
+		);
117
+		$this->ctp->create($ctp2);
118
+
119
+		//calculate Gross winner and post to grossWinnersTable
120
+
121
+		$matchRound = $this->matchRoundRepo->getByMatch($matchdata['match']);
122
+
123
+
124
+		$lowGross = array();
125
+		foreach ($matchRound as $key => $match){
126
+			$lowGross[$match['player']->id] = $match['score'];
127
+		}
128
+		$arrayLowGross = array_keys($lowGross, min($lowGross));
129
+		foreach($arrayLowGross as $key => $lowgrossPlayer) {
130
+			$grossWinner = new Grosswinner;
131
+			$grossWinner->player_id = $lowgrossPlayer;
132
+			$grossWinner->match_id = $matchdata['match'];
133
+			$grossWinner->score = $lowGross[$lowgrossPlayer];
134
+			$grossWinner->money = $this->prizeMoney->getlowScore() / count($arrayLowGross);
135
+			$grossWinner->save();
136
+		}
137
+
138
+		//Calculate NET winner
139
+		$lowNet = array();
140
+		$scores =array();
141
+		foreach ($matchRound as $key => $match){
142
+			$netScore = ($match['score'] - round($match['player']->handicap,0)); //calculate net score
143
+			$lowNet[$match['player']->id] = $netScore;
144
+		}
145
+		$arrayLowNet = array_keys($lowNet, min($lowNet));
146
+
147
+		foreach($arrayLowNet as $key => $lownetPlayer) {
148
+			$netWinner = new Netwinner;
149
+			$netWinner->player_id = $lownetPlayer;
150
+			$netWinner->match_id = $matchdata['match'];
151
+			$netWinner->score = $lowNet[$lownetPlayer];
152
+			$netWinner->money = $this->prizeMoney->getlowScore() /  count($arrayLowNet);
153
+			$netWinner->save();
154
+		}
155
+
156
+		//Calculate Skins
157
+
158
+		//determine A and B players
159
+		//using pivot table match_player
160
+		$match = $this->match->find($matchdata['match']);
161
+		$aPlayers = array();
162
+		$bPlayers = array();
163
+		foreach($match->players as $player)
164
+		{
165
+			if($player->pivot->level_id == 1){
166
+				$aPlayers[] = $player->pivot->player_id;
167
+
168
+			}
169
+			if($player->pivot->level_id == 2){
170
+				$bPlayers[] = $player->pivot->player_id;
171
+			}
172
+		}
173
+
174
+		//Create Skins arrays
175
+		//player_id, 'holescores'
176
+		foreach($matchRound as $key => $round) {
177
+			if (in_array($round->player_id,$aPlayers)){
178
+				$aPlayersSkins[$key]['player_id'] = $round->player_id;
179
+				$aPlayersSkins[$key]['holescores'] = $round->holescores;
180
+			}
181
+			if (in_array($round->player_id,$bPlayers)){
182
+				$bPlayersSkins[$key]['player_id'] = $round->player_id;
183
+				$bPlayersSkins[$key]['holescores'] = $round->holescores;
184
+			}
185
+		}
186
+
187
+		//Run A Skins analysis
188
+		$scores = array();
189
+		foreach($aPlayersSkins as $key => $playerSkin){
190
+			foreach($playerSkin['holescores'] as $hole => $holescore){
191
+				$scores[$holescore['hole_id']][$playerSkin['player_id']] = $holescore['score'];
192
+			}
193
+		}
194
+
195
+		foreach($scores as $hole_id => $hole) {
196
+			$minScore = min($hole);
197
+			$winners[$hole_id] = array_keys($hole, min($hole)); //gets player id of lowest score
198
+		}
199
+		$aSkinsWon = 0;
200
+		foreach($winners as $key => $winner) {
201
+			if(count($winner)  ===  1) {
202
+				//post to DB
203
+				$skinWinner = new Skin;
204
+				$skinWinner->player_id = $winner[0];
205
+				$skinWinner->level_id = 1;
206
+				$skinWinner->match_id = intval($matchdata['match']);
207
+				$skinWinner->hole_id = $key;
208
+				$skinWinner->save();
209
+				$aSkinsWon++;
210
+			}
211
+		}
212
+
213
+		//Run B Skins analysis
214
+		$scores = array();
215
+		foreach($bPlayersSkins as $key => $playerSkin){
216
+			foreach($playerSkin['holescores'] as $hole => $holescore){
217
+				$scores[$holescore['hole_id']][$playerSkin['player_id']] = $holescore['score'];
218
+			}
219
+		}
220
+
221
+		foreach($scores as $hole_id => $hole) {
222
+			$minScore = min($hole);
223
+			$winners[$hole_id] = array_keys($hole, min($hole)); //gets player id of lowest score
224
+		}
225
+		$bSkinsWon = 0;
226
+		foreach($winners as $key => $winner) {
227
+			if(count($winner)  ===  1) {
228
+				//post to DB
229
+				$skinWinner = new Skin;
230
+				$skinWinner->player_id = $winner[0];
231
+				$skinWinner->level_id = 2;
232
+				$skinWinner->match_id = intval($matchdata['match']);
233
+				$skinWinner->hole_id = $key;
234
+				$skinWinner->save();
235
+				$bSkinsWon++;
236
+			}
237
+		}
238
+
239
+		$match =  Match::find($matchdata['match']);
240
+
241
+		//Need to add Carry over money if there no skins are won
242
+		//check for carry over money
243
+		$skinsamoney = $match->skinsamoney; // + carryover A money if any
244
+		$skinsbmoney = $match->skinsbmoney; // + carryover B money if any
245
+
246
+		if($aSkinsWon > 0) {
247
+			$moneyperskinA = $skinsamoney / $aSkinsWon;
248
+
249
+			$aSkins = Skin::where('match_id', '=', $matchdata['match'])->where('level_id', '=', 1)->get();
250
+			foreach ($aSkins as $askin){
251
+				$askin->money = $moneyperskinA;
252
+				$askin->save();
253
+			}
254
+		}
255
+
256
+		if($bSkinsWon > 0) {
257
+			$moneyperskinB = $skinsbmoney / $bSkinsWon;
258
+
259
+			$bSkins = Skin::where('match_id', '=', $matchdata['match'])->where('level_id', '=', 2)->get();
260
+			foreach ($bSkins as $bskin){
261
+				$bskin->money = $moneyperskinB;
262
+				$bskin->save();
263
+			}
264
+		}
265
+		//foreach player in pivot table create player and run handicap analysis
266
+		foreach($match->players as $matchplayer)
267
+		{
268
+			$player = Player::find($matchplayer->pivot->player_id);
269
+			$handicap = new Handicap($player);
270
+			$player->handicap = $handicap->calculate();
271
+			$player->save();
272
+		}
273
+
274
+		//Fire event to calculate money won and add to pivot table match_player
275
+		$this->events->fire('match.finalize', $match);
276
+
277
+	}
278
+
279
+	/**
280
+	 * Method to get match from input Match data
281
+	 *
282
+	 * @param mixed $matchdata
283
+	 * @return JSON object
284
+	 */
285
+	public function get($matchid)
286
+	{
287
+		$matchdata =  $this->matchRepo->get($matchid);
288
+		return $matchdata;
289
+	}
290 290
 }
Please login to merge, or discard this patch.