Beispiel #1
0
 public static function fillCPU($tournament)
 {
     $need = $tournament->getSize() - count($tournament->getTournamentPlayers());
     $db = new Util\Data();
     if ($need > 0) {
         $sqlBusyPlayer = 'SELECT DISTINCT tp.player_id
             FROM bus_tournament_player tp
             JOIN bus_tournament t ON t.id = tp.tournament_id
             WHERE t.status < 3';
         $rows = $db->select($sqlBusyPlayer);
         $busyPlayers = [0];
         if (count($rows) > 0) {
             $list = $rows->fetchAll(\PDO::FETCH_COLUMN);
             foreach ($list as $key => $value) {
                 $busyPlayers[] = $value;
             }
         }
         $sql = 'SELECT DISTINCT p.id
             FROM bus_player p
             LEFT JOIN bus_tournament_player tp ON tp.player_id = p.id
             WHERE p.active = 1
             AND p.user_id = 0
             AND (tp.player_id IS NULL OR tp.player_id NOT IN (?))
             ORDER BY p.tour_ranking';
         $players = $db->select($sql, [implode(',', $busyPlayers)]);
         if (count($players) == 0) {
             Util\Log::log(Util\Code::CRON, 'Not enough CPU, tournamentId: ' . $tournament->getId() . ', 0 / ' . $need, __FILE__, __LINE__);
             return false;
         }
         $players = $players->fetchAll(\PDO::FETCH_ASSOC);
         if (count($players) < $need) {
             Util\Log::log(Util\Code::CRON, 'Not enough CPU, tournamentId: ' . $tournament->getId() . ', ' . count($players) . ' / ' . $need, __FILE__, __LINE__);
             return false;
         }
         $playerIds = Engine::findCPU($players, $need, $tournament->getClassification());
         if (count($playerIds) < $need) {
             Util\Log::log(Util\Code::CRON, 'Not enough CPU found, tournamentId: ' . $tournament->getId() . ', ' . count($playerIds) . ' / ' . $need, __FILE__, __LINE__);
             return false;
         }
         $options = [];
         $options['clearSelect'] = true;
         $options['select'][] = \Own\Bus\Player\Data::getList(['id']);
         $options['where'][] = 'player.id IN (' . implode(',', $playerIds) . ')';
         $players = \Own\Bus\Player\Data::loadAll($options);
         foreach ($players as $player) {
             $tp = new \Own\Bus\TournamentPlayer\Model();
             $tp->setTournamentId($tournament->getId());
             $tp->setPlayerId($player->getId());
             $tp->save();
         }
     }
     return true;
 }
Beispiel #2
0
 public function sign()
 {
     $isAllowed = Util\Auth::isAuthorized($this->signedUser, 'member');
     $json = [];
     $json['result'] = \Rebond\Core\ResultType::ERROR;
     if (!$isAllowed) {
         $json['message'] = Util\Lang::lang('accessNonAuthorized');
         return json_encode($json);
     }
     $tournamentId = Util\Converter::toInt('tournamentId', 'post');
     $action = Util\Converter::toString('action', 'post');
     if ($tournamentId == 0 || !in_array($action, ['up', 'out'])) {
         $json['message'] = 'Invalid options';
         return json_encode($json);
     }
     $tournament = \Own\Bus\Tournament\Data::loadById($tournamentId);
     $playerId = $this->player->getId();
     if ($tournament->getClassification() == Classification::AMATEUR && $this->player->getTourPoint() >= 10) {
         $json['message'] = Util\Lang::lang('tooManyPointsForAmateur');
         return json_encode($json);
     }
     if ($tournament->getClassification() != Classification::AMATEUR && $this->player->getTourPoint() < 10) {
         $json['message'] = Util\Lang::lang('notEnoughPointForTournament');
         return json_encode($json);
     }
     if ($tournament->getClassification() <= Classification::ATP_250 && $this->player->getTourPoint() <= 100) {
         $json['message'] = Util\Lang::lang('needMorePointsForATP');
         return json_encode($json);
     }
     $options = [];
     $options['where'][] = 'tournament_player.tournament_id = ' . $tournamentId;
     $tp = \Own\Bus\TournamentPlayer\Data::loadAllByPlayerId($playerId, $options);
     if ($action == 'up') {
         if (count($tp) > 0) {
             $json['message'] = Util\Lang::lang('alreadySignedUp');
             return json_encode($json);
         }
         if ($this->player->getIsInTournament() || $this->player->getIsRegistered()) {
             $json['message'] = Util\Lang::lang('alreadySignedUpInOtherTournament');
             return json_encode($json);
         }
         $tp = new \Own\Bus\TournamentPlayer\Model();
         $tp->setTournamentId($tournamentId);
         $tp->setPlayerId($playerId);
         $tp->save();
         $this->player->setIsRegistered(true);
         $this->player->save();
         $json['result'] = \Rebond\Core\ResultType::SUCCESS;
         $json['message'] = Util\Lang::lang('signedUp') . '!';
         $json['newAction'] = 'out';
         $json['html'] = Util\Lang::lang('signOut');
         return json_encode($json);
     }
     if ($action == 'out') {
         if (count($tp) == 0) {
             $json['message'] = Util\Lang::lang('notRegistered');
             return json_encode($json);
         }
         \Own\Bus\TournamentPlayer\Data::deleteById($tp[0]->getId());
         $this->player->setIsRegistered(false);
         $this->player->save();
         $json['result'] = \Rebond\Core\ResultType::SUCCESS;
         $json['message'] = Util\Lang::lang('signedOut') . '!';
         $json['newAction'] = 'up';
         $json['html'] = Util\Lang::lang('signUp');
         return json_encode($json);
     }
     $json['message'] = 'nothing to do';
     return json_encode($json);
 }