Exemplo n.º 1
0
Arquivo: Cron.php Projeto: vincium/lot
 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();
 }
Exemplo n.º 2
0
 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;
 }
Exemplo n.º 3
0
 public function index()
 {
     // auth
     Util\Auth::isAuthorized($this->signedUser, 'member', false, '/profile/sign-in');
     \Own\Bus\Match\Data::getCurrentMatch($this->player->getId(), [1, 2, 3]);
     $playLeague = Util\Converter::toBool('league');
     $playPractice = Util\Converter::toString('play', 'post');
     $lastMatchPlayed = (new \Rebond\Util\DateTime())->getTimestamp() - $this->player->getLastMatchPlayed()->getTimestamp();
     // minimum in 2 minutes, maximum 10 matches per day
     $scheduled = $lastMatchPlayed > Engine::DAY * 60 * 6 ? 120 : Engine::DAY * 60 * 6 - $lastMatchPlayed;
     $options = [];
     $options['where'][] = 'configuration.property = \'nextMonth\'';
     $nextMonth = \Own\Bus\Configuration\Data::load($options);
     if ($playLeague && time() + $scheduled > (int) $nextMonth->getValue()) {
         Util\Session::setAndRedirect('siteError', Util\Lang::lang('leagueMonthFinished'), '/match');
     }
     if ($playLeague || $playPractice != '') {
         $match = new \Own\Bus\Match\Model();
         $playerMatch1 = new \Own\Bus\PlayerMatch\Model();
         $playerMatch2 = new \Own\Bus\PlayerMatch\Model();
         if ($playLeague) {
             $match->setSurface($this->player->getLeague()->getSurface());
             $match->setType(MatchType::LEAGUE);
             $match->setLeagueId($this->player->getLeagueId());
             $match->setBestOfSets($this->player->getLeague()->getBestOfSets());
             $player2 = \Own\Bus\Player\Data::searchByLeague($this->player->getId(), $this->player->getLeagueId());
         } else {
             $surface = Util\Converter::toInt('surface', 'post');
             $level = Util\Converter::toInt('level', 'post');
             if (!in_array($level, [-1, 0, 1])) {
                 $level = 0;
             }
             $match->setType(MatchType::PRACTICE);
             $match->setSurface($surface);
             $player2 = \Own\Bus\Player\Data::searchByLevel($level, $this->player->getExperience(), $this->player->getId());
         }
         if (!isset($player2)) {
             Util\Session::setAndRedirect('siteError', Util\Lang::lang('noPlayerFound'), '/match');
         }
         \Own\Bus\Player\Data::updateIsInMatch($this->player->getId(), $player2->getId());
         $playerMatch1->setPlayerId($this->player->getId());
         $playerMatch1->setLevel($this->player->calculateLevel());
         $playerMatch1->save();
         $playerMatch2->setPlayerId($player2->getId());
         $playerMatch2->setLevel($player2->calculateLevel());
         $playerMatch2->save();
         $match->setPlayerMatch1Id($playerMatch1->getId());
         $match->setPlayerMatch2Id($playerMatch2->getId());
         $scheduledDate = \Rebond\Util\DateTime::createFromTimestamp(time() + $scheduled);
         $match->setScheduled($scheduledDate);
         $match->setStatus(MatchStatus::READY);
         $match->save();
         Util\Session::setAndRedirect('siteSuccess', 'Match scheduled ' . $scheduledDate->format(), '/match/vs?matchId=' . $match->getId());
     }
     // view
     $this->setTpl();
     // main
     $tplMain = new Util\Template(Util\Template::SITE, ['www']);
     $tplMain->set('surface', [1 => 'Clay', 2 => 'Grass', 3 => 'Hard']);
     $tplMain->set('level', [-1 => Util\Lang::lang('opponentWeak'), 0 => Util\Lang::lang('opponentEqual'), 1 => Util\Lang::lang('opponentStrong')]);
     $tplMain->set('league', $this->player->getLeague());
     $tplMain->set('scheduled', $scheduled);
     // layout
     $this->tplLayout->set('column1', $tplMain->render('match-play'));
     // template
     $this->tplMaster->set('layout', $this->tplLayout->render('layout-center'));
     return $this->tplMaster->render('tpl-default');
 }