Skip to content

Instantly share code, notes, and snippets.

@TylerPachal
Last active October 3, 2019 19:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TylerPachal/6434b70bf2311027d4a4b26c96722f51 to your computer and use it in GitHub Desktop.
Save TylerPachal/6434b70bf2311027d4a4b26c96722f51 to your computer and use it in GitHub Desktop.
def join_game(user_id, game_id) do
with {:user, {:ok, user}} <- {:user, Users.get(user_id)},
{:game, {:ok, game}} <- {:game, Games.get(game_id)},
{:full, false} <- {:full, Game.is_full?(game)},
{:started, false} <- {:started, Game.is_started?(game)},
{:allowed, true} <- {:allowed, User.has_permission?(user, game)}
do
Game.add_user(game, user)
else
{:user, :not_found} -> {:error, "User not found"}
{:game, :not_found} -> {:error, "Game not found"}
{:full, _} -> {:error, "Game is full"}
{:started, _} -> {:error, "Game has already started"}
{:allowed, _} -> {:error, "User is not allowed to join this game"}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment