Ejemplo n.º 1
0
Archivo: Cron.php Proyecto: vincium/lot
 public function computerMatch()
 {
     $created = 0;
     $options = [];
     $options['clearSelect'] = true;
     $options['select'][] = \Own\Bus\Player\Data::getList(['id', 'league_id', 'experience']);
     $options['where'][] = 'player.is_in_tournament = 0';
     $options['where'][] = 'player.is_in_match = 0';
     $options['where'][] = 'player.user_id = 0';
     $options['where'][] = 'player.active = 1';
     $options['where'][] = 'player.mental >= 750';
     $players = \Own\Bus\Player\Data::loadAll($options);
     $count = count($players);
     if ($count > 0) {
         $pick = Engine::dice(0, $count - 1);
         $player = $players[$pick];
         unset($players);
         $player2 = \Own\Bus\Player\Data::searchByLeague($player->getId(), $player->getLeagueId());
         if (isset($player2)) {
             $match = new \Own\Bus\Match\Model();
             $playerMatch1 = new \Own\Bus\PlayerMatch\Model();
             $playerMatch2 = new \Own\Bus\PlayerMatch\Model();
             $match->setSurface($player->getLeague()->getSurface());
             $match->setType(MatchType::LEAGUE);
             $match->setLeagueId($player->getLeagueId());
             $match->setBestOfSets($player->getLeague()->getBestOfSets());
             $matchTime = (new Util\DateTime())->add(new \DateInterval('PT120S'));
             $playerMatch1->setPlayerId($player->getId());
             $playerMatch1->setLevel($player->calculateLevel());
             $playerMatch1->save();
             $playerMatch2->setPlayerId($player2->getId());
             $playerMatch2->setLevel($player2->calculateLevel());
             $playerMatch2->save();
             $match->setPlayerMatch1Id($playerMatch1->getId());
             $match->setPlayerMatch2Id($playerMatch2->getId());
             $match->setScheduled($matchTime);
             $match->setStatus(MatchStatus::READY);
             $match->save();
             if ($player2->getUserId() != 0) {
                 \Own\Bus\Notification\Service::create($player2->getId(), 0, 'matchStart', [['scheduledMatch', $matchTime->format('datetime')]]);
             }
             \Own\Bus\Player\Data::updateIsInMatch($player->getId(), $player2->getId());
             $created = 1;
         }
     }
     $this->log('computer match: ' . $created);
 }
Ejemplo n.º 2
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');
 }