/** * try to create a new game, returns the game as model on success * * @param string $game_name * @param int $players * @param string $password1 * @param string $password2 * @param bool $creator_joins * @param int $id_color * @throws GameCreationException * @return ModelGame * @throws JoinUserException */ public function create($game_name, $players, $password1, $password2, $creator_joins, $id_color) { $id_color = intval($id_color); $players = intval($players); if (empty($game_name) || empty($players)) { throw new GameCreationException('Fill in name and players.'); } if (!preg_match("/^([a-zA-Z0-9]+[a-zA-Z0-9' -]+[a-zA-Z0-9']+)?\$/", $game_name)) { throw new GameCreationException('Invalid name, only use those letters: a-Z 0-9 \'-'); } if (!preg_match("/[2-6]{1}/", $players)) { throw new GameCreationException('Invalid number of players.'); } if ($password1 !== $password2) { throw new GameCreationException('Passwords have to match.'); } if (!preg_match("/^([a-zA-Z0-9\$%'-]{5,})?\$/", $password1)) { throw new GameCreationException('Invalid password. At least 5 of the following letters: a-Z 0-9 $%\'-'); } $game = ModelGame::createGame($game_name, $players, ModelUser::getCurrentUser()->getId(), $password1); // join user if ($creator_joins) { ModelIsInGameInfo::joinGame(ModelUser::getCurrentUser()->getId(), $game->getId(), $id_color); } return $game; }
/** * tries to join a game with the user * * @param int $id_color * @param int $id_game * @param string $password * @throws JoinUserException, NullPointerException * @return boolean */ public function join($id_color = null, $id_game, $password = null) { $game = ModelGame::getGame($id_game); // check password if ($game->checkPasswordProtection() && !$game->checkPassword($password)) { throw new JoinUserException('Invalid password.'); } if ($this->checkInGame($id_game)) { throw new JoinUserException('User already in this game!'); } if ($game->getFreeSlots() <= 0) { throw new JoinUserException('Game is full.'); } // join game ModelIsInGameInfo::joinGame(ModelUser::getCurrentUser()->getId(), $id_game, $id_color); return true; }