/** * 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; }