public function matchStat() { $json = []; $json['result'] = \Rebond\Core\ResultType::ERROR; $matchId = Util\Converter::toString('matchId', 'post'); if (!isset($matchId)) { $json['message'] = Util\Lang::lang('matchNotFound'); return json_encode($json); } $options = []; $options['where'][] = 'match.id = ' . $matchId; $options['where'][] = 'match.status IN (' . MatchStatus::PLAYING . ', ' . MatchStatus::FINISHED . ')'; $match = \Own\Bus\Match\Data::load($options); if (!isset($match)) { $json['message'] = Util\Lang::lang('matchNotFound'); return json_encode($json); } $tpl = new Util\Template(Util\Template::MODULE, ['bus', 'match']); $tpl->set('serveLabels', \Own\Bus\PlayerMatch\Service::getServeLabels()); $tpl->set('player1ServeStats', \Own\Bus\PlayerMatch\Service::getServeStats($match->getPlayerMatch1())); $tpl->set('player2ServeStats', \Own\Bus\PlayerMatch\Service::getServeStats($match->getPlayerMatch2())); $tpl->set('returnLabels', \Own\Bus\PlayerMatch\Service::getReturnLabels()); $tpl->set('player1ReturnStats', \Own\Bus\PlayerMatch\Service::getReturnStats($match->getPlayerMatch1())); $tpl->set('player2ReturnStats', \Own\Bus\PlayerMatch\Service::getReturnStats($match->getPlayerMatch2())); $tpl->set('otherLabels', \Own\Bus\PlayerMatch\Service::getOtherLabels()); $tpl->set('player1OtherStats', \Own\Bus\PlayerMatch\Service::getOtherStats($match->getPlayerMatch1())); $tpl->set('player2OtherStats', \Own\Bus\PlayerMatch\Service::getOtherStats($match->getPlayerMatch2())); $tpl->set('attitudeLabels', \Own\Bus\PlayerMatch\Service::getAttitudeLabels()); $tpl->set('player1AttitudeStats', \Own\Bus\PlayerMatch\Service::getAttitudeStats($match->getPlayerMatch1())); $tpl->set('player2AttitudeStats', \Own\Bus\PlayerMatch\Service::getAttitudeStats($match->getPlayerMatch2())); $tpl->set('mentalLabels', \Own\Bus\PlayerMatch\Service::getMentalLabels()); $tpl->set('player1MentalStats', \Own\Bus\PlayerMatch\Service::getMentalStats($match->getPlayerMatch1())); $tpl->set('player2MentalStats', \Own\Bus\PlayerMatch\Service::getMentalStats($match->getPlayerMatch2())); $tpl->set('shotLabels', \Own\Bus\PlayerMatch\Service::getShotLabels()); $tpl->set('player1ShotStats', \Own\Bus\PlayerMatch\Service::getShotStats($match->getPlayerMatch1())); $tpl->set('player2ShotStats', \Own\Bus\PlayerMatch\Service::getShotStats($match->getPlayerMatch2())); $json['html'] = $tpl->render('match-stat'); $json['result'] = \Rebond\Core\ResultType::SUCCESS; return json_encode($json); }
public function match() { $options = []; $options['select'][] = \Own\Bus\Tournament\Data::getList([], 'match_tournament'); $options['select'][] = \Own\Bus\PlayerMatch\Data::getList([], 'match_player_match1'); $options['select'][] = \Own\Bus\PlayerMatch\Data::getList([], 'match_player_match2'); $options['leftJoin'][] = 'bus_tournament match_tournament ON match_tournament.id = match.tournament_id'; $options['join'][] = 'bus_player_match match_player_match1 ON match_player_match1.id = match.player_match1_id'; $options['join'][] = 'bus_player_match match_player_match2 ON match_player_match2.id = match.player_match2_id'; $options['join'][] = 'bus_player match_player_match1_player ON match_player_match1_player.id = match_player_match1.player_id'; $options['join'][] = 'bus_player match_player_match2_player ON match_player_match2_player.id = match_player_match2.player_id'; $options['where'][] = '(match.status IN (1,2) AND match.scheduled < NOW()) OR (match.status = 3)'; $matches = \Own\Bus\Match\Data::loadAll($options); $playing = 0; $done = 0; if (count($matches > 0)) { foreach ($matches as $match) { $matchService = new \Own\Bus\Match\Service($this->app, $match); $winnerId = $match->findWinnerId(); if ($match->getStatus() == MatchStatus::READY) { $matchService->preMatch(); } $match->setStatus(MatchStatus::PLAYING); $match->save(); // play point $firstPoint = true; while ($winnerId == 0) { if (!$firstPoint && $match->isTurnOver()) { $match->getPlayerMatch1()->save(); $match->getPlayerMatch2()->save(); $match->save(); $playing++; continue 2; } if ($firstPoint) { $firstPoint = false; } $matchService->playPoint(); $winnerId = $match->findWinnerId(); if (!$this->cron && $this->app->timer() > 25) { $match->getPlayerMatch1()->save(); $match->getPlayerMatch2()->save(); $match->save(); break 2; } } // close match if ($match->getPlayerMatch1()->getPlayer()->getUserId() == 0) { $match->getPlayerMatch1()->setHasViewed(true); } if ($match->getPlayerMatch2()->getPlayer()->getUserId() == 0) { $match->getPlayerMatch2()->setHasViewed(true); } $match->setStatus(MatchStatus::FINISHED); $matchService->endMatch(); $match->save(); if ($match->getLeagueId() != 0) { $this->leagueIds[] = $match->getLeagueId(); } $this->matchPlayerIds[] = $match->getPlayerMatch1()->getPlayerId(); $this->matchPlayerIds[] = $match->getPlayerMatch2()->getPlayerId(); $isWinner = $winnerId == $match->getPlayerMatch1Id(); // update form, experience, surface, energy, league points $result1 = $match->getPlayerMatch1()->getPlayer()->updateAfterMatch($isWinner, $match->getId(), $match->renderScore(true), $match->getGamesCount(), $match->getSurface(), $match->getType(), $match->getPosition(), $match->getPlayerMatch2()->getPlayer()); $result2 = $match->getPlayerMatch2()->getPlayer()->updateAfterMatch(!$isWinner, $match->getId(), $match->renderScore(true), $match->getGamesCount(), $match->getSurface(), $match->getType(), $match->getPosition(), $match->getPlayerMatch1()->getPlayer()); $match->getPlayerMatch1()->save(); $match->getPlayerMatch2()->save(); $match->getPlayerMatch1()->getPlayer()->save(); $match->getPlayerMatch2()->getPlayer()->save(); // if player changes league if ($match->getLeagueId() != 0) { $this->leagueIds[] = $match->getPlayerMatch1()->getPlayer()->getLeagueId(); $this->leagueIds[] = $match->getPlayerMatch2()->getPlayer()->getLeagueId(); } // create next match if ($match->getPosition() > 1) { $matchTime = $match->getScheduled()->getTimestamp() + Engine::DAY * 3600; $position = floor($match->getPosition() / 2); $options = []; $options['where'][] = ['match.tournament_id = ?', $match->getTournamentId()]; $options['where'][] = ['match.position = ?', $position]; $nextMatch = \Own\Bus\Match\Data::load($options); if (!isset($nextMatch)) { $nextMatch = new \Own\Bus\Match\Model(); $nextMatch->setTournamentId($match->getTournamentId()); $nextMatch->setSurface($match->getTournament()->getSurface()); $nextMatch->setPosition($position); $nextMatch->setBestOfSets($match->getTournament()->getBestOfSets()); $nextMatch->setType(MatchType::TOURNAMENT); } $playerMatch = new \Own\Bus\PlayerMatch\Model(); if ($isWinner) { $playerMatch->setPlayerId($match->getPlayerMatch1()->getPlayerId()); $playerMatch->setLevel($match->getPlayerMatch1()->getPlayer()->calculateLevel()); if ($match->getPlayerMatch1()->getPlayer()->getUserId() != 0) { \Own\Bus\Notification\Service::create($match->getPlayerMatch1()->getPlayerId(), 0, 'matchStart', [['scheduledMatch', $matchTime]]); } } else { $playerMatch->setPlayerId($match->getPlayerMatch2()->getPlayerId()); $playerMatch->setLevel($match->getPlayerMatch2()->getPlayer()->calculateLevel()); if ($match->getPlayerMatch2()->getPlayer()->getUserId() != 0) { \Own\Bus\Notification\Service::create($match->getPlayerMatch2()->getPlayerId(), 0, 'matchStart', [['scheduledMatch', $matchTime]]); } } $playerMatch->save(); if ($match->getPosition() % 2 == 0) { $nextMatch->setPlayerMatch2Id($playerMatch->getId()); } else { $nextMatch->setPlayerMatch1Id($playerMatch->getId()); } $nextMatch->setScheduled(date('Y-m-d H:i:s', $matchTime)); $nextMatch->setStatus(MatchStatus::READY); $nextMatch->save(); \Own\Bus\Player\Data::updateIsInMatch($playerMatch->getPlayerId()); } else { if ($match->getPosition() == 1) { $match->getTournament()->setWinnerId($match->getWinner()->getPlayerId()); $match->getTournament()->setStatus(TournamentStatus::FINISHING); $match->getTournament()->save(); } } $done++; } } $this->log('match: ' . $playing . ' + ' . $done . ' / ' . count($matches)); $this->playerAfter(); $this->rankingLeague(); }
public static function loadRecentByPlayerId($playerId) { $options = self::link(); $options['where'][] = ['(match_player_match1.player_id = ? AND match_player_match1.has_viewed = false) OR (match_player_match2.player_id = ? AND match_player_match2.has_viewed = false)', $playerId, $playerId]; $options['where'][] = ['match.status IN (?, ?)', MatchStatus::PLAYING, MatchStatus::FINISHED]; return \Own\Bus\Match\Data::load($options); }
public function view() { $matchId = Util\Converter::toInt('id'); $live = Util\Converter::toBool('live'); $key = Util\Converter::toString('key'); $options = []; $options['where'][] = 'match.id = ' . $matchId; $options['where'][] = 'match.status IN (' . MatchStatus::PLAYING . ', ' . MatchStatus::FINISHED . ')'; $match = \Own\Bus\Match\Data::load($options); if (!isset($match)) { Util\Session::siteError('matchNotFound', null, '/match/schedule'); } if ($this->player == null) { $this->player = new \Own\Bus\Player\Model(); } $realKey = \Rebond\Util\Security::encrypt($match->getId()); $hasViewed = $match->hasViewed($this->player->getId()); $options = []; $options['where'][] = ['match_id = ?', $match->getId()]; $options['order'][] = 'id'; $matchLog = \Own\Bus\Log\Data::loadAll($options); if ($live && count($matchLog) == 0) { $live = false; Util\Session::set('siteError', Util\Lang::lang('noMatchLog')); $key = $realKey; } // auth if (!$live) { if ($match->getStatus() == MatchStatus::PLAYING) { Util\Session::siteError('matchPlaying', null, '/match/schedule'); } if ($key != $realKey && !$hasViewed && count($matchLog) > 0 && ($this->player->getId() == $match->getPlayerMatch1()->getPlayerId() || $this->player->getId() == $match->getPlayerMatch2()->getPlayerId())) { $live = true; } } // view $this->setTpl(); // main $tplMain = new Util\Template(Util\Template::SITE, ['www']); $tplMain->set('match', $match); if ($live) { $logs = []; foreach ($matchLog as $log) { $logs[] = $log->toArray(); } $tplMain->set('logs', $logs); $isLive = !$hasViewed && $match->getModifiedDate() > time() - 1800 ? 1 : 0; $tplMain->set('isLive', $isLive); $tplMain->set('key', $realKey); $tpl = 'match-view-live'; $this->tplMaster->set('bodyClass', 'body-' . $match->getSurface()); } else { if ($key == $realKey) { $match->setViewed($this->player->getId()); \Own\Bus\Notification\Data::updateViewedByPlayerIdAndMatchId($this->player->getId(), $match->getId()); } $tpl = 'match-view'; $options = []; $options['where'][] = ['match_id = ?', $match->getId()]; $notification = \Own\Bus\Notification\Data::load($options); $tplMain->set('notification', $notification); $tplMain->set('serveLabels', \Own\Bus\PlayerMatch\Service::getServeLabels()); $tplMain->set('player1ServeStats', \Own\Bus\PlayerMatch\Service::getServeStats($match->getPlayerMatch1())); $tplMain->set('player2ServeStats', \Own\Bus\PlayerMatch\Service::getServeStats($match->getPlayerMatch2())); $tplMain->set('returnLabels', \Own\Bus\PlayerMatch\Service::getReturnLabels()); $tplMain->set('player1ReturnStats', \Own\Bus\PlayerMatch\Service::getReturnStats($match->getPlayerMatch1())); $tplMain->set('player2ReturnStats', \Own\Bus\PlayerMatch\Service::getReturnStats($match->getPlayerMatch2())); $tplMain->set('otherLabels', \Own\Bus\PlayerMatch\Service::getOtherLabels()); $tplMain->set('player1OtherStats', \Own\Bus\PlayerMatch\Service::getOtherStats($match->getPlayerMatch1())); $tplMain->set('player2OtherStats', \Own\Bus\PlayerMatch\Service::getOtherStats($match->getPlayerMatch2())); $tplMain->set('attitudeLabels', \Own\Bus\PlayerMatch\Service::getAttitudeLabels()); $tplMain->set('player1AttitudeStats', \Own\Bus\PlayerMatch\Service::getAttitudeStats($match->getPlayerMatch1())); $tplMain->set('player2AttitudeStats', \Own\Bus\PlayerMatch\Service::getAttitudeStats($match->getPlayerMatch2())); $tplMain->set('mentalLabels', \Own\Bus\PlayerMatch\Service::getMentalLabels()); $tplMain->set('player1MentalStats', \Own\Bus\PlayerMatch\Service::getMentalStats($match->getPlayerMatch1())); $tplMain->set('player2MentalStats', \Own\Bus\PlayerMatch\Service::getMentalStats($match->getPlayerMatch2())); $tplMain->set('shotLabels', \Own\Bus\PlayerMatch\Service::getShotLabels()); $tplMain->set('player1ShotStats', \Own\Bus\PlayerMatch\Service::getShotStats($match->getPlayerMatch1())); $tplMain->set('player2ShotStats', \Own\Bus\PlayerMatch\Service::getShotStats($match->getPlayerMatch2())); $tplMain->set('notificationCount', \Own\Bus\Notification\Data::countByPlayerId($this->player->getId())); } // layout $this->tplLayout->set('column1', $tplMain->render($tpl)); // template $this->tplMaster->set('layout', $this->tplLayout->render('layout-center')); return $this->tplMaster->render('tpl-default'); }