public static function generate($tournament) { $options = []; $options['where'][] = ['match.tournament_id = ?', $tournament->getId()]; $matchCount = \Own\Bus\Match\Data::count($options); if ($matchCount == 0) { $tournamentTime = $tournament->getStartDate()->add(new \DateInterval('PT1M')); if (count($tournament->getTournamentPlayers()) >= $tournament->getSize()) { $position = $tournament->getSize() - 1; $players = self::getOrderedPlayers($tournament); $i = 0; while ($position >= $tournament->getSize() / 2) { $matchTime = $tournamentTime->getTimestamp() + 60 * ($tournament->getSize() - $position) * (6 + 256 / $tournament->getSize()) * (Engine::DAY / 24); $match = new \Own\Bus\Match\Model(); $match->setTournamentId($tournament->getId()); $match->setSurface($tournament->getSurface()); $match->setBestOfSets($tournament->getBestOfSets()); $match->setPosition($position); $playerMatch1 = new \Own\Bus\PlayerMatch\Model(); $playerMatch1->setPlayerId($players[$i]['id']); $playerMatch1->setHasViewed($players[$i]['hasViewed']); if (isset($players[$i]['seed'])) { $playerMatch1->setSeed($players[$i]['seed'] + 1); } $playerMatch1->save(); if ($players[$i]['userId'] != 0) { \Own\Bus\Notification\Service::create($players[$i]['id'], 0, 'tournamentStart', [['scheduledMatch', $matchTime]]); } $i++; $playerMatch2 = new \Own\Bus\PlayerMatch\Model(); $playerMatch2->setPlayerId($players[$i]['id']); $playerMatch2->setHasViewed($players[$i]['hasViewed']); if (isset($players[$i]['seed'])) { $playerMatch2->setSeed($players[$i]['seed'] + 1); } $playerMatch2->save(); if ($players[$i]['userId'] != 0) { \Own\Bus\Notification\Service::create($players[$i]['id'], 0, 'tournamentStart', [['scheduledMatch', $matchTime]]); } $i++; $match->setPlayerMatch1Id($playerMatch1->getId()); $match->setPlayerMatch2Id($playerMatch2->getId()); $match->setScheduled(date('Y-m-d H:i:s', $matchTime)); $match->setType(MatchType::TOURNAMENT); $match->setStatus(MatchStatus::READY); $match->save(); $position--; } } else { Util\Log::log(Util\Code::CRON, 'Tournament not generated, tournamentId: ' . $tournament->getId() . ', ' . count($tournament->getTournamentPlayers()) . ' / ' . $tournament->getSize(), __FILE__, __LINE__); return false; } } return true; }
public function index() { if (isset($_POST['sign-out'])) { \Rebond\Core\UserSecurity\Service::signOut($this->signedUser); } // main $tplMain = new Util\Template(Util\Template::SITE, ['www']); if ($this->player->getId() == 0) { $tpl = 'index-guest'; $matchView = \Own\Bus\Match\Data::loadLastMatch(); } else { $tpl = 'index'; $matchView = \Own\Bus\Match\Data::loadRecentByPlayerId($this->player->getId()); $nextMatch = \Own\Bus\Match\Data::loadNextMatchByPlayerId($this->player->getId()); $soon = isset($nextMatch) && $nextMatch->getScheduled() < time() + Engine::DAY * 360 ? true : false; $tplMain->set('player', $this->player); $tplMain->set('notificationCount', \Own\Bus\Notification\Data::countByPlayerId($this->player->getId())); $tplMain->set('isBonusDayXp', $this->player->isBonusDayXp()); $tplMain->set('nextBonusDayXp', $this->player->getNextBonusDayXp()); $tplMain->set('nextMatch', $nextMatch); $tplMain->set('soon', $soon); } $tplMain->set('matchView', $matchView); $options = []; $options['where'][] = 'configuration.property = \'nextWeek\''; $nextWeek = \Own\Bus\Configuration\Data::load($options); $options = []; $options['where'][] = 'configuration.property = \'nextMonth\''; $nextMonth = \Own\Bus\Configuration\Data::load($options); $options = []; $options['where'][] = 'configuration.property = \'nextYear\''; $nextYear = \Own\Bus\Configuration\Data::load($options); if (!isset($nextWeek, $nextMonth, $nextYear)) { Util\error::kill(Util\Code::UNHANDLED_ERROR, 'nextWeek, nextMonth or nextYear is not set in bus_configuration', __FILE__, __LINE__); } $tplMain->set('gameTime', Engine::renderTime($this->app->site()->getCreatedDate())); $tplMain->set('endWeek', (new Util\DateTime())->setTimestamp($nextWeek->getValue())); $tplMain->set('endMonth', (new Util\DateTime())->setTimestamp($nextMonth->getValue())); $tplMain->set('endYear', (new Util\DateTime())->setTimestamp($nextYear->getValue())); // view $this->setTpl(); // layout $this->tplLayout->set('column1', $tplMain->render($tpl)); // template $this->tplMaster->set('layout', $this->tplLayout->render('layout-center')); return $this->tplMaster->render('tpl-default'); }
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 tour_points() { // auth Util\Auth::isAuthorized($this->signedUser, 'member', false, '/profile/sign-in'); \Own\Bus\Match\Data::checkMatchToView($this->player->getId()); $race = Util\Converter::toBool('race', 'get', true); // params $id = Util\Converter::toInt('id'); if ($id == 0) { $player = $this->player; } else { $player = \Own\Bus\Player\Data::loadById($id); if (!isset($player)) { $player = $this->player; } } if ($race) { $options = []; $options['join'][] = 'bus_tournament tournament ON tournament.id = tournament_player.tournament_id'; $options['where'][] = 'tournament.status = ' . TournamentStatus::FINISHED; $options['where'][] = 'tournament.start_date > \'' . Engine::getStartYearDate($this->app->site()->getCreatedDate()) . '\''; $options['order'][] = 'points DESC'; $tournaments = \Own\Bus\TournamentPlayer\Data::loadAllByPlayerId($player->getId(), $options); $oldTournaments = null; } else { $options = []; $options['join'][] = 'bus_tournament tournament ON tournament.id = tournament_player.tournament_id'; $options['where'][] = 'tournament.status = ' . TournamentStatus::FINISHED; $options['where'][] = 'tournament.start_date > NOW() - INTERVAL ' . Engine::DAY * 336 . ' HOUR'; $options['order'][] = 'points DESC'; $tournaments = \Own\Bus\TournamentPlayer\Data::loadAllByPlayerId($player->getId(), $options); $options = []; $options['join'][] = 'bus_tournament tournament ON tournament.id = tournament_player.tournament_id'; $options['where'][] = 'tournament.status = ' . TournamentStatus::FINISHED; $options['where'][] = 'tournament.start_date < NOW() - INTERVAL ' . Engine::DAY * 336 . ' HOUR'; $options['order'][] = 'tournament.start_date DESC'; $oldTournaments = \Own\Bus\TournamentPlayer\Data::loadAllByPlayerId($player->getId(), $options); } // view $this->setTpl(); // filter $tplFilter = new Util\Template(Util\Template::SITE, ['www']); $tplFilter->set('race', $race); $tplFilter->set('playerId', $player->getId()); // main $tplMain = new Util\Template(Util\Template::SITE, ['www']); $tplMain->set('username', $player->getUsername()); $tplMain->set('tournaments', $tournaments); $tplMain->set('oldTournaments', $oldTournaments); // layout $this->tplLayout->set('column1', $tplFilter->render('profile-tour-point-filter')); $this->tplLayout->add('column1', $tplMain->render('profile-tour-point')); // template $this->tplMaster->set('layout', $this->tplLayout->render('layout-center')); return $this->tplMaster->render('tpl-default'); }
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 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'); }
public function reloadMatch() { $json = []; $json['result'] = \Rebond\Core\ResultType::ERROR; // check $matchId = Util\Converter::toInt('matchId', 'post'); $logId = Util\Converter::toInt('logId', 'post'); $match = \Own\Bus\Match\Data::loadById($matchId); if (!isset($match)) { $json['message'] = Util\Lang::lang('matchNotFound'); return json_encode($json); } $options = []; $options['where'][] = ['match_id = ?', $matchId]; $options['where'][] = ['id > ?', $logId]; $options['order'][] = 'id'; $matchLog = \Own\Bus\Log\Data::loadAll($options); if (count($matchLog) == 0) { $json['message'] = Util\Lang::lang('pleaseWait'); return json_encode($json); } $logs = []; foreach ($matchLog as $log) { $logs[] = $log->toArray(); } // layout $json['result'] = \Rebond\Core\ResultType::SUCCESS; $json['logs'] = $logs; $json['matchStatus'] = $match->getStatus(); $json['message'] = Util\Lang::lang('loaded'); return json_encode($json); }
public function view() { $id = Converter::toInt('id'); $playerId = $this->player->getId(); $tournament = \Own\Bus\Tournament\Data::loadById($id); if (!isset($tournament)) { Session::siteError('itemNotFound', [$id], '/tournament'); } // view $this->setTpl(); // main $tplMain = new Template(Template::MODULE, ['bus', 'match']); $tplMain->set('tournament', $tournament); if ($tournament->getStatus() >= TournamentStatus::PLAYING) { $orderedMatches = \Own\Bus\Match\Data::loadAllByTournamentId($tournament->getId()); foreach ($orderedMatches as $match) { if ($match->getPlayerMatch1() != null && $match->getPlayerMatch1()->getPlayerId() == $playerId && !$match->getPlayerMatch1()->getHasViewed() || $match->getPlayerMatch2() != null && $match->getPlayerMatch2()->getPlayerId() == $playerId && !$match->getPlayerMatch2()->getHasViewed()) { $position = $match->getPosition(); $nextPosition = floor($position / 2); while ($nextPosition >= 1) { if (!isset($orderedMatches[$nextPosition])) { break; } if ($nextPosition == 1) { $orderedMatches[$nextPosition]->setWinnerId(0); } if ($position % 2 == 1) { $orderedMatches[$nextPosition]->setPlayerMatch1Id(0); } else { $orderedMatches[$nextPosition]->setPlayerMatch2Id(0); } $position = $nextPosition; $nextPosition = floor($position / 2); } } } $tplMain->set('items', $orderedMatches); $tplMain->set('size', $tournament->getSize() / 2); $tplMain->set('round', 1); $tplMain->set('playerId', $this->player->getId()); // layout $this->tplLayout->set('column1', $tplMain->render('draw')); } else { $tplMain->set('players', \Own\Bus\Tournament\Data::getPlayerList($tournament->getId())); // layout $this->tplLayout->set('column1', $tplMain->render('registration')); } // template $this->tplMaster->set('layout', $this->tplLayout->render('layout-center')); return $this->tplMaster->render('tpl-default'); }
public function __construct($app) { parent::__construct($app); \Own\Bus\Match\Data::checkMatchToView($this->player->getId()); }