public function tournament() { $created = 0; $options = []; $options['where'][] = ['tournament.start_date BETWEEN NOW() + INTERVAL ? HOUR AND NOW() + INTERVAL ? HOUR', Engine::DAY * 4, Engine::DAY * 6]; $options['where'][] = ['tournament.status != ?', TournamentStatus::CANCELED]; $count = \Own\Bus\Tournament\Data::count($options); if ($count == 0) { $tournament = new \Own\Bus\Tournament\Model(); $tournament->setTitle(Engine::generateName()); $tournament->setBestOfSets(3); $tournament->setSurface(Engine::selectSurface()); $classification = round(pow(Engine::dice(1, 42), 0.5)) + 2; $tournament->setClassification($classification); $tournament->setSize(32); if ($classification == Classification::AMATEUR) { $tournament->setSize(16); } else { if ($classification == Classification::ATP_1000) { $tournament->setSize(64); } } $startDate = ceil(time() / 3600) * 3600 + Engine::DAY * 3600 * 5; $tournament->setStartDate((new Util\DateTime())->setTimestamp($startDate)); $tournament->setEndDate(Engine::findTournamentEndDate($tournament->getStartDate(), $tournament->getSize())); $tournament->save(); $created = 1; } $this->log('tournament (creation): ' . $created); $done = 0; $options = []; $options['where'][] = 'tournament.start_date < NOW()'; $options['where'][] = ['tournament.status = ?', TournamentStatus::NOT_STARTED]; $tournaments = \Own\Bus\Tournament\Data::loadAll($options); foreach ($tournaments as $tournament) { if (\Own\Bus\Tournament\Service::fillCPU($tournament)) { $tournament->setStatus(TournamentStatus::PREPARATION); $done++; } else { $tournament->setStatus(TournamentStatus::CANCELED); } $tournament->save(); \Own\Bus\Tournament\Data::updateIsRegisteredPlayers($tournament); } $this->log('tournament (fill): ' . $done . ' / ' . count($tournaments)); $done = 0; $options = []; $options['where'][] = 'tournament.start_date < NOW()'; $options['where'][] = ['tournament.status = ?', TournamentStatus::PREPARATION]; $tournaments = \Own\Bus\Tournament\Data::loadAll($options); foreach ($tournaments as $tournament) { if (\Own\Bus\Tournament\Service::generate($tournament)) { $tournament->setStatus(TournamentStatus::PLAYING); \Own\Bus\Tournament\Data::updateIsInMatchPlayers($tournament); $done++; } else { $tournament->setStatus(TournamentStatus::CANCELED); } $tournament->save(); } $this->log('tournament (draw creation): ' . $done . ' / ' . count($tournaments)); $options = []; $options['where'][] = ['tournament.status = ?', TournamentStatus::FINISHING]; $tournaments = \Own\Bus\Tournament\Data::loadAll($options); foreach ($tournaments as $tournament) { $matches = \Own\Bus\Match\Data::loadAllByTournamentId($tournament->getId()); $tournamentPlayers = \Own\Bus\TournamentPlayer\Data::loadAllByTournamentId($tournament->getId()); $points = pow(2, 11 - $tournament->getClassification()); for ($p = 1; $p < $tournament->getSize(); $p++) { if (in_array($p, [2, 4, 8, 16, 32, 64])) { $points = floor($points / 2); } if (!in_array($matches[$p]->getWinner()->getPlayerId(), $this->tourPlayerIds)) { foreach ($tournamentPlayers as $tournamentPlayer) { if ($tournamentPlayer->getPlayerId() == $matches[$p]->getWinner()->getPlayerId()) { if ($matches[$p]->getWinner()->getPlayer()->getUserId() != 0) { \Own\Bus\Notification\Service::create($tournamentPlayer->getPlayerId(), 0, 'tourPoint', [['tourPoint', $points]]); } $tournamentPlayer->setPoints($points); $tournamentPlayer->save(); $this->tourPlayerIds[] = $tournamentPlayer->getPlayerId(); break; } } } } $tournament->setStatus(TournamentStatus::FINISHED); $tournament->save(); } $this->rankingTour(); $this->log('tournament (point attribution): ' . count($tournaments)); }
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'); }