· 7 years ago · Dec 06, 2018, 09:02 AM
1<?php
2
3App::uses('AppController', 'Controller');
4
5class NflContestsController extends AppController {
6
7 var $helpers = array('Html', 'Form', 'Time', 'Js' => array('Jquery'), 'Util');
8 var $uses = array('Contest', 'User', 'Team', 'NflContest', 'NflSchedule', 'NbaContest', 'Payout', 'NflContestTeam', 'NbaContestTeam', 'NflTeam', 'NbaTeam', 'NflPlayer', 'NbaPlayer', 'NflTeamName', 'NflScore');
9 var $paginate = array('limit' => 10);
10 var $components = array('RequestHandler');
11
12 public function beforeFilter() {
13 parent::beforeFilter();
14
15 if ($this->Auth->login()) {
16 $dataval = $this->User->read(null, $this->Auth->user('id'));
17 $group_id = $dataval['User']['group_id'];
18
19 if ($group_id === '1') {
20 $this->Auth->allow('*');
21 }
22
23 if ($group_id === '2') {
24 $this->Auth->allow('my_contests','get_leaderboard','get_users', 'create','payouts','schedule','validate_contest','players_listing','players','add_contest','join','enter_contest','opponents_listing','contest_lobby','get_contest_team','get_prize','view','invite','my_contests','status_inactive','get_player_score','get_contest_id'); //$this->Auth->allow('create','lobby');
25 }
26//
27 } else {
28 $this->Auth->allow('lobby','status_inactive','index','get_leaderboard','get_users','create');
29
30 }
31 }
32
33 public function index($sports = 'nfl', $size = 'all') {
34 $this->layout = 'contest';
35 $fee = 0;
36 $fee2 = 500;
37 $nameList = '';
38 $dataval = $this->User->read(null, $this->Auth->user('id'));
39
40 /*
41 $options['fields'] = array('*');
42 $options['joins'] = array(
43 array('table' => 'Contest_teams',
44 'alias' => 'Contest_team',
45 'type' => 'left',
46 'conditions' => array(
47 'Contest.id = Contest_team.contest_id'
48 )
49 ),
50 array('table' => 'contests_types',
51 'alias' => 'Contests_type',
52 'type' => 'left',
53 'conditions' => array(
54 'Contest.type_id = Contests_type.type_id'
55 )
56 ),
57 array('table' => 'teams',
58 'alias' => 'Team',
59 'type' => 'left',
60 'conditions' => array(
61 'Contest_team.team_id = Team.team_id'
62 )
63 )
64 );
65 //$contests = $this->Contest->find('all', array('recursive' => 2));
66 $contests = $this->Contest->find('all',$options);
67 */
68 if (strtolower($sports) == 'all') {
69 $this->request->data = $this->NflContest->getAllContest($fee, $fee2, $size);
70 } else {
71 $this->request->data = $this->NflContest->getContest($sports, $fee, $fee2, $size);
72 }
73
74 if ($this->request->data) {
75 $players = $this->request->data[0]['NflContest']['contestants'];
76 $entries = explode(',', $players);
77
78 foreach ($entries as $entry) {
79 $list = $this->User->read(null, $entry);
80 $nameList .= '<div><a href="' . $this->webroot . 'users/view/' . $list['User']['id'] . '"><img src="' . $this->webroot . $list['User']['photo_url'] . '" width="13" height="13" alt="" /> ' . $list['User']['screen_name'] . '</a></div>';
81 }
82
83
84 //$this->set('contestants','<td class="left entries"><div><a href="#"><img src="images/test/avatar-tiny.jpg" width="13" height="13" alt="" /> Condia</a></div></td>');
85 //$this->set('contestants', $nameList);
86 }
87
88 $this->set('sports', trim($sports));
89 $this->set('fee', trim($fee));
90 $this->set('size', trim($size));
91 $this->set('balance', $dataval['User']['balance']);
92 $this->set('userid', $dataval['User']['id']);
93 $this->set('contest', $this->request->data);
94 }
95
96 function get_leaderboard() {
97 return $this->NflContest->getLeaders();
98 }
99
100 function get_users($contest_id) {
101
102 $nameList = '';
103 $opponents = $this->NflContest->find('first', array(
104 'conditions' => array('NflContest.id' => $contest_id),
105 'field' => array('contestants')));
106 $entries = explode(',', $opponents['NflContest']['contestants']);
107 foreach ($entries as $entry) {
108 $user = $this->User->read(null, $entry);
109 $nameList .= '<div><a href="' . $this->webroot . 'users/view/' . $user['User']['id'] . '"><img src="' . $this->webroot . $user['User']['photo_url'] . '" width="13" height="13" alt="" /> ' . $user['User']['screen_name'] . '</a></div>';
110 }
111 $this->set('opponents', $nameList);
112 $this->render('/Elements/contest/users');
113 }
114
115 public function update_cash($fee) {
116 $id = $this->Auth->user('id');
117 $this->User->id = $id;
118 $user = $this->User->read(null, $id);
119 $data['cashes'] = $user['User']['cashes'] - $fee;
120 $this->User->save($data);
121 }
122
123 public function ajax_create() {
124 $error = '';
125 $total_fee = 0;
126 $dataval = $this->User->read(null, $this->Auth->user('id'));
127 $fee = array(0,2,5,10,25,55,109,215,530);
128 $n_contest = $this->request->data['NflTeam']['num_contest'];
129 if($this->request->is('ajax')) {
130 if ($this->Auth->login()) {
131 if(in_array($this->request->data['NflTeam']['fee'], $fee)) {
132 if(is_numeric($n_contest)) {
133 $total_fee = $this->request->data['NflTeam']['fee'] * $n_contest;
134 if($dataval['User']['cashes'] >= $total_fee) {
135 $NflTeamData = $this->request->data['NflTeam'];
136 $error = $this->validate_contest($NflTeamData);
137
138 if(empty($error)){
139 $nflID = $this->add_contest($NflTeamData);
140 $this->update_cash($total_fee);
141 //$error = $nflID;
142 //$this->redirect(array('controller' => 'nfl_contests', 'action' => 'lobby'));
143 }
144 } else {
145 $error .= 'Insufficient funds. You must <a href="'.$this->webroot.'users/login">sign in</a> and make <a href="'.$this->webroot.'deposits/deposit">deposit</a> inorder to create this contest <br />';
146 }
147 } else {
148 $error .= 'Number of contest must be an integer <br />';
149 }
150 } else {
151 $error .= 'Invalid Fee';
152 }
153 } else {
154 $error .= 'You must <a href="'.$this->webroot.'users/login">sign in</a> or make <a href="'.$this->webroot.'deposits/deposit">deposit</a> inorder to create this contest <br />';
155 }
156
157 }
158 if(empty($error)){
159 echo $nflID;
160 } else {
161 echo $error;
162 }
163 }
164
165 public function create() {
166 $error = '';
167 $this->layout = 'contest';
168 $this->loadModel('NflSchedule');
169
170 //$sched_2 = $this->NflSchedule->find('first', array(
171 // 'conditions' => array('NflSchedule.start_date >' => date('Y-m-d H:i:s', strtotime("+10 minutes")), 'NflSchedule.id' => $this->request->data['NflTeam']['schedule_id'])
172 //));
173 /*
174 $fee = array(0,2,5,10,25,55,109,215,530);
175
176 if($this->request->isPost()) {
177
178 if(in_array($this->request->data['NflTeam']['fee'], $fee)) {
179 if($dataval['User']['cashes'] >= $this->request->data['NflTeam']['fee']) {
180
181 $NflTeamData = $this->request->data['NflTeam'];
182 $error = $this->validate_contest($NflTeamData);
183
184 if(empty($error)){
185 $this->add_contest($NflTeamData);
186 $this->update_cash($NflTeamData['fee']);
187 $this->redirect(array('controller' => 'nfl_contests', 'action' => 'lobby'));
188 } else {
189
190 }
191 } else {
192 $error .= 'You dont have enough funds <br />';
193 }
194 }
195 } else {
196
197 }
198 */
199 $sched = $this->NflSchedule->find('first', array(
200 'conditions' => array('NflSchedule.start_date >' => date('Y-m-d G:i:s', strtotime("+10 minutes"))),
201 'order' => array('NflSchedule.start_date' => 'ASC')
202 ));
203
204
205 $this->set('error', $error);
206 $this->set('contest', $sched);
207 }
208
209 public function payouts($nplayers, $pay) {
210 $this->layout = 'none';
211 $this->loadModel('Payout');
212 $payouts = $this->Payout->find('first', array(
213 'conditions' => array('Payout.fee' => $pay, 'Payout.num_players' => $nplayers)
214 ));
215 $this->set('payouts', $payouts);
216 }
217
218 public function schedule($sports) {
219 $sched = '';
220 $this->layout = 'none';
221 $this->loadModel('NflSchedule');
222
223 if (strtolower($sports) == 'nfl') {
224 $sched = $this->NflSchedule->find('first', array(
225 'conditions' => array('NflSchedule.start_date >' => date('Y-m-d G:i:s', strtotime("+10 minutes"))),
226 'order' => array('NflSchedule.start_date' => 'ASC')
227 ));
228 }
229 $this->set('contests', $sched);
230 }
231
232 public function validate_contest($data = array()) {
233 $error = '';
234
235 /*
236 check if sched id exist
237 check if time if sched is 30 mins before the game start
238 */
239
240 $sched = $this->NflSchedule->find('first', array(
241 'conditions' => array('NflSchedule.start_date >' => date('Y-m-d H:i:s', strtotime("+10 minutes")), 'NflSchedule.id' => $data['schedule_id'])
242 ));
243
244 $size = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
245 $fee = array(0,2,5,10,25,55,109,215,530);
246
247 if(empty($sched)) {
248 $error .= 'Schedule is not valid <br />';
249
250 } else {
251 $conditions = array('OR'=> array('NflPlayer.team_id' => $sched['NflSchedule']['home_team_id'], array('NflPlayer.team_id' => $sched['NflSchedule']['away_team_id'])));
252 $players = $this->NflPlayer->find('all', array('conditions' => $conditions));
253
254 foreach($players as $player) {
255 $list[] = $player['NflPlayer']['id'];
256 }
257
258
259
260
261 $dont_exist = 0;
262 $player_id = array();
263 $player_a = array();
264 for($i=1; $i <= 9; $i++) {
265 if (!in_array($data['pos'.$i], $list)) {
266 $dont_exist++;
267 $player_a[] .= $data['pos'.$i];
268 }
269
270 $player_id[] .= $data['pos'.$i];
271 }
272
273 $positions = array("QB", "RB", "WR", "TE");
274 $qb = $rb = $wr = $te = 0;
275 //echo '<pre>';
276 //print_r($player_id);
277 //echo '</pre>';
278 //echo '<pre>';
279 //print_r($players);
280 //echo '</pre>';
281 foreach($players as $player) {
282 if(in_array($player['NflPlayer']['id'],$player_id)) {
283 if($player['NflPlayer']['position'] == 'QB')
284 $qb++;
285 if($player['NflPlayer']['position'] == 'RB')
286 $rb++;
287 if($player['NflPlayer']['position'] == 'WR')
288 $wr++;
289 if($player['NflPlayer']['position'] == 'TE')
290 $te++;
291 }
292 }
293
294 if($qb == 0 || $qb > 2) {
295 $error .= 'QB must not be empty and must be only one <br />';
296 }
297 if($rb < 2 || $rb > 4 ) {
298 $error .= 'RB must not be less than 2 or more than 4 <br />';
299 }
300 if($wr < 2 || $wr > 4) {
301 $error .= 'WR must not be less than 2 or more than 4 <br />';
302 }
303 if($te < 1 || $te > 3) {
304 $error .= 'TE must not be empty and less than 1 or more than 3 <br />';
305 }
306
307
308 // if player doesnt exist in a team
309 if($dont_exist != 0) {
310 $error .= 'Some player dont exist <br />';
311 }
312
313 $unique_players = array_unique($player_id);
314
315 // check if players are unique
316 if(count($unique_players) < 9) {
317 $error .= 'Duplicate players <br />';
318 }
319 if (!in_array($data['size'], $size)) {
320 $error .= 'Size is not valid <br />';
321 }
322 if (!in_array($data['fee'], $fee)) {
323 $error .= 'Fee is not valid <br />';
324 }
325 }
326
327 return $error;
328 }
329
330 public function players() {
331 $list = array();
332 $conditions = array('OR'=> array('NflPlayer.team_id' => 16, array('NflPlayer.team_id' => 17)));
333 //$conditions = array(
334 //'NflPlayer.team_id' => 24 //,
335 //'OR' => array(
336 // 'NflPlayer.team_id' => 20)
337 //'NflPlayer.team_id' => 20)
338 //);//, 'OR NflPlayer.team_id' => 24);
339 //debug($this->NflPlayer->find('list',array('conditions'=>$final)));
340 $players = $this->NflPlayer->find('all', array('conditions' => $conditions));
341 //if()
342 echo '<pre>';
343 //print_r($players);
344
345 foreach($players as $data) {
346 $list[] = $data['NflPlayer']['id'].'<br />';
347 }
348
349 $NflTeamData['pos1'] = 56114;
350 $NflTeamData['pos2'] = 3289;
351 $NflTeamData['pos3'] = 33911;
352 $NflTeamData['pos4'] = 22094;
353 $NflTeamData['pos5'] = 56192;
354 $NflTeamData['pos6'] = 23665;
355 $NflTeamData['pos7'] = 28390;
356 $NflTeamData['pos8'] = 3728;
357 $NflTeamData['pos9'] = 3930;
358 $dont_exist = 0;
359 $player_id = array();
360 for($i=1; $i <= 9; $i++) {
361 if (!in_array($NflTeamData['pos'.$i], $list)) {
362 $dont_exist++;
363 echo $NflTeamData['pos'.$i].'<br />';
364 }
365
366 $player_id[] .= $NflTeamData['pos'.$i];
367 }
368
369 if($dont_exist != 0) {
370 echo 'Player dont exist';
371 } else {
372 echo 'exist';
373 }
374
375 $unique_players = array_unique($player_id);
376
377 if(count($unique_players) < 9) {
378 $error[] .= 'Duplicate';
379 }
380 }
381
382 public function add_contest($NflTeamData) {
383
384 $loggedin = $this->Auth->user('id');
385 $contestants = $loggedin;
386 //$NflTeamData = $this->request->data['NflTeam'];
387
388 //$error = $this->validate_contest($NflTeamData);
389 //if(empty($error)){
390 //$Nfl = $data['NflSchedule'];
391 $Nfl = $this->NflSchedule->get_contest_by_id($NflTeamData['schedule_id']);
392
393 //if (!empty($Nfl['start_date'])) {
394
395 //$this->validate_contest($NflTeamData);
396 $data['privacy'] = $NflTeamData['privacy'];
397 $data['sport'] = 'NFL';//strtoupper($NflTeamData['sport']);
398 $data['size'] = $NflTeamData['size'];
399 $data['fee'] = $NflTeamData['fee'];
400 $data['start_time'] = $Nfl['start_date'];
401 $data['schedule_id'] = $Nfl['id'];
402 $data['created_by'] = $loggedin;
403 $data['contestants'] = $loggedin;
404 $data['home_id'] = $Nfl['home_team_id'];
405 $data['away_id'] = $Nfl['away_team_id'];
406
407 if ($data['fee'] > 0)
408 $prize = $this->get_prize($data['size'], $data['fee']);
409 else
410 $prize['prize_1'] = 0;
411
412 $data['prize'] = $prize['prize_1'];
413
414 for ($ctr = 1; $ctr <= $NflTeamData['num_contest']; $ctr++) {
415 $this->NflContest->create();
416 $this->NflContest->save($data);
417 $nflID = $this->NflContest->getInsertID();
418 //if an existing team was chosen
419 if ($NflTeamData['chosen_t'] == 'existingTab') {
420 if ($this->NflContestTeam->save(array('team_id' => $NflTeamData['id'], 'nfl_contest_id' => $nflID))) {
421 //$this->NflContest->save(array('contestants' => $contestants));
422 }
423 //if a new team was chosen
424 } else {
425 $this->NflTeam->create();
426 $saveTeam = $this->NflTeam->save(array(
427 'name' => $NflTeamData['name'],
428 'roster' => $NflTeamData['pos1'] . ',' . $NflTeamData['pos2'] . ',' . $NflTeamData['pos3'] . ',' . $NflTeamData['pos4'] . ',' . $NflTeamData['pos5'] . ',' . $NflTeamData['pos6'] . ',' . $NflTeamData['pos7'] . ',' . $NflTeamData['pos8'] . ',' . $NflTeamData['pos9'],
429 'owner' => $loggedin,
430 'home_id' => $NflTeamData['home'],
431 'away_id' => $NflTeamData['away']
432 ));
433 if ($saveTeam) {
434 $nflTeamID = $this->NflTeam->getInsertID();
435 $this->NflContestTeam->create();
436 if ($this->NflContestTeam->save(array('team_id' => $nflTeamID, 'nfl_contest_id' => $nflID))) {
437 //$this->NflContest->save(array('contestants' => $contestants));
438 }
439 }
440 }
441 }
442 //} else {
443 //print_r($NflTeamData);exit;
444 // echo 'wala sud';
445 // print_r($error);
446 // exit;
447
448 //}
449 //} else {
450 //print_r($error);exit;
451 // $error_list = implode(',',$error);
452 // $this->redirect(array('controller' => 'nfl_contests', 'action' => 'create', 'error' => $error_list));
453
454 //}
455 //$this->redirect(array('controller' => 'nfl_contests', 'action' => 'lobby'));
456 return $nflID;
457 }
458
459 // UPDATED...
460 public function lobby($sports = 'nfl', $size = 'all') {
461 $this->layout = 'contest';
462 $fee = 0;
463 $fee2 = 530;
464 $nameList = '';
465 $dataval = $this->User->read(null, $this->Auth->user('id'));
466
467 /*
468 $options['fields'] = array('*');
469 $options['joins'] = array(
470 array('table' => 'Contest_teams',
471 'alias' => 'Contest_team',
472 'type' => 'left',
473 'conditions' => array(
474 'Contest.id = Contest_team.contest_id'
475 )
476 ),
477 array('table' => 'contests_types',
478 'alias' => 'Contests_type',
479 'type' => 'left',
480 'conditions' => array(
481 'Contest.type_id = Contests_type.type_id'
482 )
483 ),
484 array('table' => 'teams',
485 'alias' => 'Team',
486 'type' => 'left',
487 'conditions' => array(
488 'Contest_team.team_id = Team.team_id'
489 )
490 )
491 );
492 //$contests = $this->Contest->find('all', array('recursive' => 2));
493 $contests = $this->Contest->find('all',$options);
494 */
495 if (strtolower($sports) == 'all') {
496 $this->request->data = $this->NflContest->getAllContest($fee, $fee2, $size);
497 } else {
498 $this->request->data = $this->NflContest->getContest($sports, $fee, $fee2, $size);
499 }
500
501 if ($this->request->data) {
502 $players = $this->request->data[0]['NflContest']['contestants'];
503 $entries = explode(',', $players);
504
505 foreach ($entries as $entry) {
506 $list = $this->User->read(null, $entry);
507 $nameList .= '<div><a href="' . $this->webroot . 'users/view/' . $list['User']['id'] . '"><img src="' . $this->webroot . $list['User']['photo_url'] . '" width="13" height="13" alt="" /> ' . $list['User']['screen_name'] . '</a></div>';
508 }
509
510
511 //$this->set('contestants','<td class="left entries"><div><a href="#"><img src="images/test/avatar-tiny.jpg" width="13" height="13" alt="" /> Condia</a></div></td>');
512 //$this->set('contestants', $nameList);
513 }
514
515 if(!empty($dataval)) {
516 $this->set('balance', $dataval['User']['balance']);
517 $this->set('userid', $dataval['User']['id']);
518 } else {
519 $this->set('balance', 0);
520 $this->set('userid', 0);
521 }
522
523 $this->set('sports', trim($sports));
524 $this->set('fee', trim($fee));
525 $this->set('size', trim($size));
526
527 $this->set('contest', $this->request->data);
528 $this->status_inactive();
529 }
530
531 public function join($id = null) {
532 //$id = $this->Util->_decodeParams($contestId);
533 $isMultiple = false;
534 $this->layout = 'contest';
535 if ($id == null) {
536 $this->layout = 'contest-wide';
537 $id = 2;
538 $isMultiple = true;
539 }
540
541 $con = $this->NflContest->find('first', array(
542 'conditions' => array('NflContest.id' => $id)
543 ));
544 $contest = $con['NflContest'];
545 $payouts = $this->Payout->get_by_fee_size($contest['fee'], $contest['size']);
546
547 $this->set('contest', $contest);
548 $this->set('payouts', $payouts['Payout']);
549 $this->set('isMultiple', $isMultiple);
550 $this->set('valid', true);
551// $this->set('invalidPlayers',$this->NflContestTeam->get_all_players($contest['id']));
552 if (strtotime($contest["start_time"]) < strtotime("now")) {
553 $this->set('valid', false);
554 $this->Session->setFlash('You are not allowed to join this contest.', 'flash_error');
555 }
556
557
558 //when user clicks on enter contest
559 if ($this->request->is('post') || $this->request->is('put')) {
560 $loggedin = $this->Auth->user('id');
561 $con = $this->NflContest->find('first', array(
562 'conditions' => array('NflContest.id' => $id)
563 ));
564
565 $contestants = explode(",", $con['NflContest']['contestants']);
566 $contestants[] = $loggedin;
567 $contestants = implode(",", $contestants);
568
569 $this->NflContest->id = $id;
570
571 $NflTeamData = $this->request->data['NflTeam'];
572 $this->enter_contest($NflTeamData, $contestants, $loggedin, $id);
573 }
574 }
575
576 private function enter_contest($NflTeamData, $contestants, $loggedin, $id) {
577 $this->layout = 'contest';
578 //if an existing team was chosen
579 if ($NflTeamData['chosen_t'] == 'existingTab') {
580 $this->NflContestTeam->create();
581 if ($this->NflContestTeam->save(array('team_id' => $NflTeamData['id'], 'nfl_contest_id' => $id))) {
582 if ($this->NflContest->save(array('contestants' => $contestants))) {
583 $this->redirect(array('action' => 'contest_lobby', $id));
584 }
585 }
586
587 //if a new team was chosen
588 } else {
589 if ($this->NflTeam->is_unique_team($loggedin, $NflTeamData['name'])) {
590 $this->NflTeam->create();
591 $saveTeam = $this->NflTeam->save(array(
592 'name' => $NflTeamData['name'],
593 'roster' => $NflTeamData['pos1'] . ',' . $NflTeamData['pos2'] . ',' . $NflTeamData['pos3'] . ',' . $NflTeamData['pos4'] . ',' . $NflTeamData['pos5'] . ',' . $NflTeamData['pos6'] . ',' . $NflTeamData['pos7'] . ',' . $NflTeamData['pos8'] . ',' . $NflTeamData['pos9'],
594 'owner' => $loggedin,
595 'home_id' => $NflTeamData['home'],
596 'away_id' => $NflTeamData['away']
597 ));
598 if ($saveTeam) {
599 $nflTeamID = $this->NflTeam->getInsertID();
600 $this->NflContestTeam->create();
601 if ($this->NflContestTeam->save(array('team_id' => $nflTeamID, 'nfl_contest_id' => $id))) {
602 if ($this->NflContest->save(array('contestants' => $contestants))) {
603 $this->redirect(array('action' => 'contest_lobby', $id));
604 }
605 }
606 }
607 } else {
608 $this->Session->setFlash('Team name already exist. Please enter another team name.', 'flash_error');
609 }
610 }
611 }
612
613 public function opponents_listing() {
614 // avoid problems when requestAction calls this function
615 $this->autoRender = false;
616
617 $loggedin = $this->Auth->user('id');
618
619 $opList = array();
620
621 $this->User->recursive = 0;
622 if (isset($this->params['opList'])) {
623 // first call
624 $passedOp = explode(",", $this->params['opList']);
625 $postListF = array();
626 foreach ($passedOp as $op) {
627 $this->User->id = $op;
628 if ($op != $loggedin && $this->User->exists()) {
629 $postListF[] = $op;
630 }
631 }
632 $opponents = $this->paginate('User', array('User.id' => $postListF));
633 $opList = $postListF;
634 } else {
635 // succeeding calls
636 $opponents = $this->paginate('User', array('User.id' => $this->passedArgs['opList']));
637 $opList = $this->passedArgs['opList'];
638 }
639
640 $this->set('opponentsList', $opponents);
641 $this->set('opList', $opList);
642
643 // if this is an ajax request, render the element
644 if ($this->RequestHandler->isAjax()) {
645 $this->render('/elements/contest/opponents');
646 return;
647 }
648
649 if (isset($this->params['requested'])) {
650 // set the correct params on the paging helper of the requesting controller (not this controller)
651 return array('opponentsList' => $opponents, 'paging' => $this->params['paging'], 'opList' => $opList);
652 } else {
653 $this->render('/elements/contest/opponents');
654 }
655 }
656
657 public function contest_lobby($id) {
658 //$id = $this->Util->_decodeParams($contestId);
659 $this->layout = 'contest';
660 $sports = 'nfl';
661 $loggedin = $this->Auth->user('id');
662 $con = $this->NflContest->find('first', array(
663 'conditions' => array('NflContest.id' => $id)
664 ));
665 $contest = $con['NflContest'];
666 $payouts = $this->Payout->get_by_fee_size($contest['fee'], $contest['size']);
667
668 $this->set('sports', $sports);
669 $this->set('contest', $contest);
670 $this->set('payouts', $payouts['Payout']);
671 $this->set('userId', $loggedin);
672 }
673
674 public function players_listing() {
675 // avoid problems when requestAction calls this function
676 $this->autoRender = false;
677 $this->paginate = array('limit' => 5);
678 $loggedin = $this->Auth->user('id');
679
680 $plList = array();
681 $test = array();
682 $this->User->recursive = 0;
683
684 if (isset($this->params['plList'])) {
685 // first call
686 $passedOp = explode(",", $this->params['plList']);
687 $postListF = array();
688
689 foreach ($passedOp as $op) {
690 $this->User->id = $op;
691 if ($this->User->exists()) {
692 $postListF[] = $op;
693 }
694 }
695
696 $players = $this->paginate('User', array('User.id' => $postListF));
697 $plList = $postListF;
698
699 } else {
700 // succeeding calls
701 $players = $this->paginate('User', array('User.id' => $this->passedArgs['plList']));
702 $plList = $this->passedArgs['plList'];
703 $contest = $this->passedArgs['contest'];
704 }
705
706 $this->set('playersList', $players);
707 $this->set('plList', $plList);
708 $this->set('paging', $this->params['paging']);
709 $this->set('authId', $loggedin);
710 if (isset($contest)) {
711 $this->set('contest', $contest);
712 }
713
714 // if this is an ajax request, render the element
715 if ($this->RequestHandler->isAjax()) {
716 $this->render('/elements/contest_lobby/players');
717 return;
718 }
719
720 if (isset($this->params['requested'])) {
721 // set the correct params on the paging helper of the requesting controller (not this controller)
722 return array('playersList' => $players, 'paging' => $this->params['paging'], 'plList' => $plList, 'authId' => $loggedin);
723 } else {
724 $this->render('/elements/contest_lobby/players');
725 }
726
727
728 }
729
730 public function get_contest_team() {
731 $this->autoRender = false;
732
733 if (isset($this->params['url']['userId']) && isset($this->params['url']['contestId'])) {
734 $userId = $this->params['url']['userId'];
735 $contestId = $this->params['url']['contestId'];
736 } else {
737 $userId = $this->passedArgs['userId'];
738 $contestId = $this->passedArgs['contestId'];
739 }
740 $screenName = $this->User->find('first', array('field' => array('User.screen_name'), 'conditions' => array('User.id' => $userId)));
741
742 $team = $this->NflTeam->get_by_contest_user($contestId, $userId);
743 $awayName = $this->NflTeamName->get_field_value_by_id($team['NflTeam']['away_id'], "team_shortname");
744 $homeName = $this->NflTeamName->get_field_value_by_id($team['NflTeam']['home_id'], "team_shortname");
745 $roster = explode(",", $team['NflTeam']['roster']);
746 $players = $this->NflPlayer->get_by_team_roster($roster);
747 $contest = $this->NflContest->get_schedule_id_by_contest_id($contestId);
748 $data = array('screenName' => $screenName['User']['screen_name'], 'photo_url' => $screenName['User']['photo_url'], 'players' => $players, 'homeName' => $homeName, 'awayName' => $awayName, 'team' => $team, 'contest' => $contest);
749
750 if (isset($this->params['url']['userId']) && isset($this->params['url']['contestId'])) {
751 $this->set('data', $data);
752 $this->render('/Elements/contest/team_players');
753 return;
754 } else {
755 return $data;
756 }
757 }
758
759 public function get_prize($size, $fee) {
760 $data = array();
761 $data = $this->Payout->find('first', array(
762 'fields' => array('Payout.prize_1'),
763 'conditions' => array('Payout.fee' => $fee, 'Payout.num_players' => $size)
764 ));
765 return $data['Payout'];
766 }
767
768 public function view($contestId) {
769 $this->layout = 'contest';
770 $id = $contestId; //$this->Util->_decodeParams($contestId);
771 $loggedin = $this->Auth->user('id');
772 $con = $this->NflContest->find('first', array(
773 'conditions' => array('NflContest.id' => $id)
774 ));
775 $contest = $con['NflContest'];
776 $payouts = $this->Payout->get_by_fee_size($contest['fee'], $contest['size']);
777
778 $this->set('contest', $contest);
779 $this->set('payouts', $payouts['Payout']);
780 $this->set('userId', $loggedin);
781 }
782
783 public function invite($contestId) {
784 $id = $contestId; //$this->Util->_decodeParams($contestId);
785 $email_add = explode(',', $this->request->data['emails']);
786
787 $email = new CakeEmail();
788 $email->template('invite')
789 ->emailFormat('both');
790 $email->helpers(array('Html'));
791 $email->from(array('harreyson.chavez@gmail.com' => 'gmail.com'));
792 $email->to($email_add);
793
794 $email->subject('Rotowar.com Game Invitation');
795
796 $email->viewVars(array('contest_id' => $id));
797
798 $email->send();
799
800 $this->redirect(array('controller' => 'nfl_contests', 'action' => 'view/' . $id));
801 }
802
803 public function my_contests($tab = null) {
804 $this->layout = 'contest-wide';
805 $loggedin = $this->Auth->user('id');
806
807 $live_1 = $this->NflContest->get_live_contests($loggedin);
808 $live_2 = $this->NbaContest->get_live_contests($loggedin);
809 $live = array_merge($live_1, $live_2);
810
811 $upcoming_1 = $this->NflContest->get_upcoming_contests($loggedin);
812 $upcoming_2 = $this->NbaContest->get_upcoming_contests($loggedin);
813 $upcoming = array_merge($upcoming_1, $upcoming_2);
814
815 $history_1 = $this->NflContest->get_contests_by_status($loggedin, 'complete');
816 $history_2 = $this->NbaContest->get_contests_by_status($loggedin, 'complete');
817 $history = array_merge($history_1,$history_2);
818
819 $this->set('live', $live);
820 $this->set('upcoming', $upcoming);
821 $this->set('history', $history);
822 $this->set('tab', $tab);
823 }
824
825 public function status_inactive() {
826 $con = $this->NflContest->find('all', array(
827 'conditions' => array('NflContest.start_time <' => date('Y-m-d G:i:s'))
828 ));
829
830 foreach ($con as $key => $data) {
831 $num_contestants = explode(',', $data['NflContest']['contestants']);
832 $max = $data['NflContest']['size'];
833 $cont = count($num_contestants);
834 if ($cont < $max) {
835 $value['status'] = 0;
836 $this->NflContest->id = $data['NflContest']['id'];
837 $this->NflContest->save($value);
838 }
839 }
840 }
841
842 public function get_player_score() {
843 $contest_id = $this->passedArgs['cid'];
844 $tid = $this->passedArgs['tid'];
845 $con = $this->NflScore->find('first', array(
846 'fields' => array('NflScore.scores'),
847 'conditions' => array(
848 'NflScore.competition_id ' => $contest_id,
849 'NflScore.team_id ' => $tid
850 )
851 ));
852 return $con['NflScore'];
853 }
854
855 public function save_player_score() {
856 //$data[''] =
857
858 $cid = $this->passedArgs['cid'];
859 $tid = $this->passedArgs['tid'];
860 $pid = $this->passedArgs['pid'];
861 $score = $this->passedArgs['score'];
862
863 $result = $this->NflScore->score_exist($cid,$tid);
864
865 if(empty($result)) {
866 foreach ($pid as $player) {
867 foreach ($score as $value) {
868 $scoring = explode(':',$value);
869 if($player == $scoring[0]) {
870 //$scoring[] =
871 //echo $player.' - '. $scoring[1] .'<br />';
872 $total = $scoring[1];
873 break;
874
875 } else {
876 $total = 0;
877 }
878 }
879 $player_score[] = $total;
880 }
881
882 $pscore = implode(',',$player_score);
883 $data['competition_id'] = $cid;
884 $data['team_id'] = $tid;
885 $data['scores'] = $pscore;
886
887 $this->NflScore->save($data);
888 }
889
890 }
891
892 public function if_score_exist() {
893 $cid = $this->passedArgs['cid'];
894 $tid = $this->passedArgs['tid'];
895 $result = $this->NflScore->score_exist($cid,$tid);
896 return $result;
897 }
898
899 public function get_contest_id () {
900 $contestId = $this->passedArgs['contestId'];
901 $contest = $this->NflContest->get_schedule_id_by_contest_id($contestId);
902 return $contest;
903 }
904
905}