Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/jamstack/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Jamstack.Application do
children = [
Jamstack.JS.SessionCode,
Jamstack.JS.Youtube,
Jamstack.Party.Lobby,
# Start the Ecto repository
Jamstack.Repo,
# Start the endpoint when the application starts
Expand Down
78 changes: 78 additions & 0 deletions lib/jamstack/party/lobby.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
defmodule Jamstack.Party.Lobby do
use Agent

def start_link(_opts) do
Agent.start_link(
fn ->
%{ }
end,
name: __MODULE__
)
end

def create_lobby(join_code) do
Agent.update(
__MODULE__,
fn lobby ->
Map.put(
lobby,
join_code,
[]
)
end
)
end

def join_lobby(join_code, user_name) do
Agent.update(
__MODULE__,
fn lobby ->
Map.put(
lobby,
join_code,
[ user_name | lobby[join_code] ]
)
end
)
end

def leave_lobby(join_code, user_name) do
Agent.update(
__MODULE__,
fn lobby ->
Map.put(
lobby,
join_code,
Enum.filter(
lobby[join_code],
fn user -> user != user_name end
)
)
end
)
end

def get_lobby(join_code) do
Agent.get(
__MODULE__,
fn lobby ->
Map.get(
lobby,
join_code
)
end
)
end

def remove_lobby(join_code) do
Agent.update(
__MODULE__,
fn lobby ->
Map.pop(
lobby,
join_code
)
end
)
end
end