/** * @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> <? }
private function showFriendlyMatches() { $csrf = $_SESSION['csrf']; $opponents = Model::indexBy($this->season->getTeams(), 'teamid'); $postCsrf = HTMLResponse::fromPOST('friendlycsrf', ''); if ($postCsrf == $csrf && $this->team->isManager()) { $url = HTMLResponse::fromPOST('friendlyurl'); $opponentsId = HTMLResponse::fromPOST('friendlyopponentsid'); $publishDate = HTMLResponse::fromPOST('friendlydate'); $publishTime = HTMLResponse::fromPOST('friendlytime'); if (!strlen($publishDate)) $publishDate = date('Y-m-d'); if (!strlen($publishTime)) $publishTime = date('H').':00'; $possibleOpponents = Model::pluck($this->season->getTeams(), 'teamid'); $regex = '/^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/'; $timeRegex = "'^[0-9]{2}:[0-9]{2}$'"; $dateRegex = "'^[0-9]{4}\\-[0-9]{2}\\-[0-9]{2}$'"; $removeId = HTMLResponse::fromPOST('removeid'); if ($removeId) { /** @var Video $video */ if ($video = Video::findOne('seasonid = ? and type = ? and videoid = ? and teamid = ?', [$this->season->seasonid, 3, $removeId, $this->team->teamid])) { $video->delete(); HTMLResponse::exitWithRoute(HTMLResponse::getRoute()); } } if (!strlen($opponentsId) || !strlen($publishTime) || !strlen($publishDate) || !strlen($url)) { $this->design->addJavaScript(" $(function() { alert(\"No has rellenado todos los datos\"); }) ", false); } else { if ($opponentsId != $this->team->teamid && in_array($opponentsId, $possibleOpponents)) { if (!preg_match($regex, $url)) { $this->design->addJavaScript(" $(function() { alert(\"El enlace que has puesto no es un enlace de YouTube válido\"); }) ", false); } else { if (!preg_match($timeRegex, $publishTime)) { $this->design->addJavaScript(" $(function() { alert(\"La hora que has puesto tiene un formato inválido (ha de ser 08:06)\"); }) ", false); } else { if (!preg_match($dateRegex, $publishDate)) { $this->design->addJavaScript(" $(function() { alert(\"La fecha que has puesto tiene un formato inválido (ha de ser 2099-12-31)\"); }) ", false); } else { $video = Video::create(); $video->dateline = time(); $video->publishdate = $publishDate; $video->publishtime = $publishTime; $video->link = $url; $video->opponentid = $opponentsId * 1; $video->teamid = $this->team->teamid; $video->type = 3; $video->seasonid = $this->season->seasonid; $video->save(); HTMLResponse::exitWithRoute(HTMLResponse::getRoute()); } } } } } } $videos = Video::find('seasonid = ? and teamid = ? and type = ? order by publishdate asc, publishtime asc', [$this->season->seasonid, $this->team->teamid, 3]); if ($videos || $this->team->isManager()) { ?> <h2>Combates amistosos</h2> <? if ($this->team->isManager()) { ?> <form action="<?=HTMLResponse::getRoute()?>" method="post"> <? } ?> <table> <thead> <tr> <td>Fecha</td> <td>Hora</td> <td>Oponentes</td> <td>Vídeo</td> </tr> </thead> <? foreach($videos as $video) { if (!$this->team->isManager() && ($video->publishdate > date('Y-m-d') || ($video->publishdate == date('Y-m-d') && $video->publishtime > date('H:i')))) { continue; } ?> <tr> <td><?= $video->publishdate ?></td> <td><?= $video->publishtime ?></td> <td> <a href="/<?=$this->season->getLink()?>/equipos/<?=$opponents[$video->opponentid]->getLink()?>/"> <?= htmlentities($opponents[$video->opponentid]->name) ?> </a> </td> <td> <a href="<?=htmlentities($video->link)?>" target="_blank"> Ver combate </a> <? if ($this->team->isManager()) { ?> <a style="font-size: 10px" href="javascript:void(0)" onclick="removeFriendlyVideo(this, <?=$video->videoid?>)"> (Quitar) </a> <? } ?> </td> </tr> <? } ?> <? if ($this->team->isManager()) { ?> <tr> <td> <input type="date" name="friendlydate" placeholder="<?=date('Y-m-d')?>" style="width:80px"> </td> <td> <input name="friendlytime" placeholder="<?=date('H:i')?>" style="width: 64px"> </td> <td> <select name="friendlyopponentsid"> <option value="">-- Elige oponentes --</option> <? foreach($this->season->getTeams() as $team) { if ($team->teamid == $this->team->teamid) continue; ?> <option value="<?=$team->teamid?>"> <?= htmlentities($team->name) ?> </option> <? } ?> </select> </td> <td> <input name="friendlyurl" placeholder="http://youtube.com/..." style="width:200px"> </td> </tr> <? } ?> </table> <? if ($this->team->isManager()) { ?> <div style="height: 6px"></div> <button type="submit">Añadir amostoso</button> <input type="hidden" name="friendlycsrf" value="<?=$csrf?>"> <input type="hidden" name="removeid" value=""> </form> <? } } }
/** * @return void */ public function show() { $matches = Match::find('seasonid = ?', [$this->season->seasonid]); $wins = []; $games = []; $kills = []; $deaths = []; $mainPositions = []; $playoffsDefeat = []; $playoffsLast = []; foreach($this->season->getTeams() as $team) { $wins[$team->teamid] = 0; $kills[$team->teamid] = 0; $games[$team->teamid] = 0; $deaths[$team->teamid] = 0; } foreach($matches as $match) { if ($match->week > $this->season->mainweeks) continue; if (!$this->season->weekIsPublished($match->week)) continue; if ($match->isDelayed()) continue; $winner = $match->getWinner(); if (!$winner) continue; $looser = $match->getLooser(); $games[$winner]++; $games[$looser]++; $wins[$winner]++; $kills[$winner] += 6; $deaths[$looser] += 6; $looserKills = $match->getLooserKills(); $kills[$looser] += $looserKills; $deaths[$winner] += $looserKills; } foreach(array_keys($kills) as $teamid) { $tiebreakers[$teamid] = array($wins[$teamid], $kills[$teamid]-$deaths[$teamid], $kills[$teamid], -$deaths[$teamid]); } arsort($tiebreakers); $lastPos = 0; $lastTiebreakers = null; foreach(array_keys($tiebreakers) as $pos => $teamid) { if ($lastTiebreakers != $tiebreakers[$teamid]) { $lastPos = $pos; } $mainPositions[$teamid] = $lastPos; $lastTiebreakers = $tiebreakers[$teamid]; } foreach($matches as $match) { if ($match->week <= $this->season->mainweeks) continue; if (!$this->season->weekIsPublished($match->week)) continue; if ($match->isDelayed()) continue; $winner = $match->getWinner(); if (!$winner) continue; $looser = $match->getLooser(); $playoffsDefeat[$looser] = -$match->week; $playoffsLast[$winner] = $match->week; $playoffsLast[$looser] = $match->week; $playoffsPlayed[$winner] = 1; $playoffsPlayed[$looser] = 1; $games[$winner]++; $games[$looser]++; $wins[$winner]++; $kills[$winner] += 6; $deaths[$looser] += 6; $looserKills = $match->getLooserKills(); $kills[$looser] += $looserKills; $deaths[$winner] += $looserKills; } foreach(array_keys($kills) as $teamid) { $tiebreakers[$teamid] = array($playoffsLast[$teamid]*1, $playoffsDefeat[$teamid]*1, -$mainPositions[$teamid]); } arsort($tiebreakers); ?> <table> <thead><tr> <td>Puesto</td> <td>Equipo</td> <td>Combates</td> <td>Victorias</td> <td>Derrotas</td> <td>Debilitados</td> <td>Perdidos</td> </tr></thead> <? /** * @var $teams Team[] */ $teams = Model::indexBy(Team::find('1=1'), 'teamid'); $lastTiebreakers = null; $lastPos = 0; foreach(array_keys($tiebreakers) as $pos => $teamid) { $team = $teams[$teamid]; if ($lastTiebreakers != $tiebreakers[$teamid]) { $lastPos = $pos; } ?> <tr> <td><?= $lastPos+1 ?>º <? if (isset($mainPositions[$teamid]) && $lastPos != $mainPositions[$teamid]) { ?> <div style="height: 4px"></div> <i title="<?= $mainPositions[$teamid]+1 ?>º en las jornadas" style="cursor: pointer; color: #666"> <?= $mainPositions[$teamid]+1 ?>º </i> <? } ?> </td> <td style="text-align: left"> <div class="teamimg64"> <img src="/<?=$team->getImageLink(64, 64)?>"> </div> <a href="/<?=$this->season->getLink()?>/equipos/<?=$team->getLink()?>/" class="inblock" style="vertical-align:middle"> <?= $team->name ?> </a></td> <td><?= $games[$teamid] ?></td> <td><b><?= $wins[$teamid]*1 ?></b></td> <td><?= $games[$teamid]-$wins[$teamid] ?></td> <td><?= $kills[$teamid] ?></td> <td><?= $deaths[$teamid] ?></td> </tr> <? $lastTiebreakers = $tiebreakers[$teamid]; } ?> </table> <? }