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); }
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'); }