public function postCrossbet() { if (!Auth::check()) { return array('success' => false, 'errors' => 'You must be logged in to place any bets.'); } $bet = Bet::find(Input::get('bet_id')); if (is_null($bet)) { return array('success' => false, 'errors' => 'This is not a valid bet!'); } if (time() > strtotime($bet->expires_at)) { return array('success' => false, 'errors' => 'This bet has expired!'); } if (!is_null($bet->accepted_by_user_id)) { return array('success' => false, 'errors' => 'Somebody else just placed this bet!'); } if ($bet->deleted != 0) { return array('success' => false, 'errors' => 'This bet has been deleted!'); } if ($bet->cross_bet_amount > Auth::user()->getBalance()) { return array('success' => false, 'errors' => 'You don\'t have enough funds to place this bet!'); } $bet->accepted_by_user_id = Auth::user()->id; $bet->accepted_at = time(); if ($bet->save()) { Auth::user()->subtractFromBalance($bet->cross_bet_amount); return array('success' => true); } else { return array('success' => false, 'errors' => 'Bet couldn\'t be placed, please contact support if this persists.'); } }
function getVotes() { $bets = Bet::find('matchid = ?', array($this->matchid)); $votes = array(); foreach ($bets as $bet) { $votes[$bet->teamid]++; } return $votes; }
/** * Ajoute une transaction * Utilisé uniquement par le code * * @param $user_id * @param $bet_id * @param $value * @param $type * @return bool */ public static function addTransaction($user_id, $bet_id, $value, $type) { if (in_array($type, array('gain', 'bet')) && Bet::find($bet_id)) { $transaction = new Transaction(); $transaction->user_id = $user_id; $transaction->bet_id = $bet_id; $transaction->value = $value; $transaction->type = $type; $transaction->save(); //On déduit/ajoute les point de la transaction $user = User::find($user_id); if ($transaction->type == "bet") { $user->points -= $transaction->value; } else { if ($transaction->type == "gain") { $user->points += $transaction->value; } } $user->save(); } return false; }
/** * @return void */ public function show() { $matches = Match::find('seasonid = ? and result != 0', [$this->season->seasonid]); $matches = Model::indexBy($matches, 'matchid'); $teams = Model::indexBy(Team::find('1=1'), 'teamid'); $matchWinner = []; foreach($matches as $match) { if ($this->season->weekIsPublished($match->week) && !$match->isDelayed()) { $matchWinner[$match->matchid] = $match->getWinner(); } } $totalBets = []; $correctBets = []; $usernames = []; $avatars = Model::pluck(Avatar::find('1=1'), 'url', 'userid'); foreach(Bet::find('1=1') as $bet) { if (isset($matchWinner[$bet->matchid])) { if ($bet->teamid && $matchWinner[$bet->matchid] == $bet->teamid) { $correctBets[$bet->userid]++; } $totalBets[$bet->userid]++; } if ($bet->username) { $usernames[$bet->userid] = $bet->username; } } $tiebreakers = []; foreach(array_keys($correctBets) as $userid) { $tiebreakers[$userid] = [-$correctBets[$userid], $totalBets[$userid], strtolower($usernames[$userid])]; } asort($tiebreakers); if (TwitterAuth::isLogged()) { $userid = TwitterAuth::getUserId(); $userBets = Bet::find('userid = ? order by matchid desc', [$userid]); $userpos = array_search($userid, array_keys($tiebreakers)); $userpos = ($userpos === FALSE) ? 0 : $userpos+1; ?> <div class="inblock" style="text-align: left; margin-right: 20px"> <h2>Tus estadísticas</h2> <table> <thead> <tr> <td>Puesto</td> <td>Nombre</td> <td>Aciertos</td> <td>Fallos</td> </tr> </thead> <tr> <td><?= $userpos ?>º</td> <td style="text-align: left"> <div class="inblock" style="vertical-align: middle"> <a href="http://twitter.com/<?=htmlentities($usernames[$userid])?>" target="_blank"> <img src="<?= htmlentities($avatars[$userid]) ?>" style="width:40px; height:40px; border-radius: 20px"> </a> </div> <div class="inblock" style="vertical-align: middle"> <a href="http://twitter.com/<?=htmlentities($usernames[$userid])?>" target="_blank"> <?= htmlentities($usernames[$userid]) ?> <? if (!isset($usernames[$userid])) echo "<i>$userid</i>"; ?> </a> </div> </td> <td><?= $correctBets[$userid] ?></td> <td><?= $totalBets[$userid]-$correctBets[$userid] ?></td> </tr> </table> <h2>Tus apuestas</h2> <table> <thead> <tr> <td>Jornada</td> <td>Enfrentamiento</td> <td>Acierto</td> </tr> </thead> <? foreach($userBets as $bet) { if (!isset($matches[$bet->matchid])) continue; /** * @var $match Match */ $match = $matches[$bet->matchid]; if (!$match->isPublished() || $match->isDelayed()) continue; $team1 = $teams[$match->team1id]; $team2 = $teams[$match->team2id]; $success = $match->getWinner() == $bet->teamid; ?> <tr> <td><?= $match->week ?></td> <td> <div class="inblock"> <div class="teamimg64"> <img src="/<?= $team1->getImageLink(64, 64) ?>" class="<?= $match->getWinner() == $team1->teamid ? '' : 'grayscale' ?>"> </div> <? if ($bet->teamid == $team1->teamid) { ?> <br><i style="font-size:11px">Votado</i> <? } ?> </div> <div class="inblock" style="line-height: 64px; margin: 0px 4px"> VS </div> <div class="inblock"> <div class="teamimg64"> <img src="/<?= $team2->getImageLink(64, 64) ?>" class="<?= $match->getWinner() == $team2->teamid ? '' : 'grayscale' ?>"> </div> <? if ($bet->teamid == $team2->teamid) { ?> <br><i style="font-size:11px">Votado</i> <? } ?> </div> </td> <td> <?= $success ? '<div class="success-icon">✔</div>' : '<div class="fail-icon">✘</div>' ?> </td> </tr> <? } ?> </table> </div> <? } ?> <div class="inblock"> <h2>Clasificación</h2> <table> <thead> <tr> <td>Puesto</td> <td>Nombre</td> <td>Aciertos</td> <td>Fallos</td> </tr> </thead> <? $lastTiebreakers = null; $lastPos = 0; foreach(array_keys($tiebreakers) as $pos => $userid) { unset($tiebreakers[$userid][2]); if ($lastTiebreakers != $tiebreakers[$userid]) { $lastPos = $pos; } if (!isset($avatars[$userid])) { Avatar::setUsersAvatar($userid, $usernames[$userid]); } ?> <tr> <td><?= $lastPos+1 ?>º</td> <td style="text-align: left"> <div class="inblock" style="vertical-align: middle"> <a href="http://twitter.com/<?=htmlentities($usernames[$userid])?>" target="_blank"> <img src="<?= htmlentities($avatars[$userid]) ?>" style="width:40px; height:40px; border-radius: 20px"> </a> </div> <div class="inblock" style="vertical-align: middle"> <a href="http://twitter.com/<?=htmlentities($usernames[$userid])?>" target="_blank"> <?= htmlentities($usernames[$userid]) ?> <? if (!isset($usernames[$userid])) echo "<i>$userid</i>"; ?> </a> </div> </td> <td><?= $correctBets[$userid] ?></td> <td><?= $totalBets[$userid]-$correctBets[$userid] ?></td> </tr> <? $lastTiebreakers = $tiebreakers[$userid]; } ?> </table> </div> <? }