public static function loadAllScheduled() { $options = self::link(true, true, true); $options['where'][] = 'match.status IN (1,2,3)'; $options['order'][] = 'match.scheduled'; return \Own\Bus\Match\Data::loadAll($options); }
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 function viewProto() { // auth Util\Auth::isAuthorized($this->signedUser, 'member', false, '/profile/sign-in'); $matchId = Util\Converter::toInt('id'); $options = []; $options['where'][] = 'match.id = ' . $matchId; $options['where'][] = 'match.status IN (' . MatchStatus::FINISHED . ', ' . MatchStatus::PLAYING . ')'; $matches = \Own\Bus\Match\Data::loadAll($options); if (count($matches) != 1) { Util\Session::siteError('matchNotFound', null, '/match/schedule'); } $match = $matches[0]; $matchLog = \Rebond\Config::getPath('config') . 'match/match_' . $match->getId() . '.json'; if (!file_exists($matchLog)) { Util\Session::siteError('noMatchLog', null, '/match/schedule'); } // view $this->setTpl(); // main $tplMain = new Util\Template(Util\Template::SITE, ['www']); $tplMain->set('match', $match); $logs = json_decode(file_get_contents($matchLog), true); $tplMain->set('logs', $logs); // layout $this->tplLayout->set('column1', $tplMain->render('match-view-proto')); // template $this->tplMaster->set('layout', $this->tplLayout->render('layout-center')); $this->tplMaster->addJs('/js/proto.js'); return $this->tplMaster->render('tpl-default'); }