Beispiel #1
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;
 }