Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reveal is a toggle #30

Merged
merged 2 commits into from
Oct 7, 2023
Merged
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
7 changes: 5 additions & 2 deletions lib/quick_average/display_state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ defmodule QuickAverage.DisplayState do
@enforce_keys [:users, :average]
defstruct [:users, :average]

def from_input_state(%{presences: presences, manual_reveal: manual_reveal}) do
def from_input_state(%{
presences: presences,
is_revealed_manually: is_revealed_manually
}) do
presences
|> Map.values()
|> Enum.map(&metas_to_user/1)
|> Enum.sort()
|> determine_active_users()
|> determine_should_reveal(manual_reveal)
|> determine_should_reveal(is_revealed_manually)
|> update_user_display_numbers()
|> determine_average()
end
Expand Down
38 changes: 25 additions & 13 deletions lib/quick_average/room_manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ defmodule QuickAverage.RoomManager do
def init(room_id) do
Logger.info("Starting RoomManager for #{room_id} 🤖")
presences = PresenceInterface.list(room_id)
manual_reveal = false
is_revealed_manually = false

display_state =
DisplayState.from_input_state(%{
presences: presences,
manual_reveal: manual_reveal
is_revealed_manually: is_revealed_manually
})

state = %{
display_state: display_state,
room_id: room_id,
presences: presences,
manual_reveal: manual_reveal,
is_revealed_manually: is_revealed_manually,
start_time: now(),
version: 0,
display_version: 0
Expand All @@ -45,7 +45,7 @@ defmodule QuickAverage.RoomManager do
%{
version: version,
display_version: display_version,
manual_reveal: manual_reveal,
is_revealed_manually: is_revealed_manually,
presences: presences
} = state
)
Expand All @@ -59,7 +59,7 @@ defmodule QuickAverage.RoomManager do
display_state =
DisplayState.from_input_state(%{
presences: presences,
manual_reveal: manual_reveal
is_revealed_manually: is_revealed_manually
})

if Enum.empty?(users) do
Expand Down Expand Up @@ -87,25 +87,30 @@ defmodule QuickAverage.RoomManager do
end

@impl true
def handle_call({:toggle_reveal, name}, _from, state) do
def handle_call({:set_reveal, is_revealed} = message, _from, state)
when is_boolean(is_revealed) do
new_state = %{
state
| manual_reveal: !state.manual_reveal,
| is_revealed_manually: is_revealed,
version: state.version + 1
}

PubSubInterface.broadcast(
state.room_id,
{:set_reveal, name, new_state.manual_reveal}
message
)

{:reply, :ok, new_state}
end

@impl true
def handle_call({:set_reveal, false}, _from, state) do
new_state = %{state | manual_reveal: false, version: state.version + 1}
{:reply, :ok, new_state}
def handle_call({:send_flash, level, message}, _from, state) do
PubSubInterface.broadcast(
state.room_id,
{:show_flash, level, message}
)

{:reply, :ok, state}
end

@impl true
Expand Down Expand Up @@ -146,10 +151,17 @@ defmodule QuickAverage.RoomManager do
)
end

def set_reveal(room_id, false) do
def set_reveal(room_id, is_revealed) when is_boolean(is_revealed) do
GenServer.call(
name(room_id),
{:set_reveal, is_revealed}
)
end

def send_flash(room_id, level, message) do
GenServer.call(
name(room_id),
{:set_reveal, false}
{:send_flash, level, message}
)
end

Expand Down
54 changes: 35 additions & 19 deletions lib/quick_average_web/live/average_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule QuickAverageWeb.AverageLive do
changeset: User.changeset(%{}),
flash_timer: nil,
is_admin: false,
manual_reveal?: false,
is_revealed_manually: false,
name: "",
number: nil,
only_viewing: false,
Expand Down Expand Up @@ -128,20 +128,39 @@ defmodule QuickAverageWeb.AverageLive do
end

@impl true
def handle_event("clear_clicked", _params, socket) do
def handle_event("clear_clicked", _params, %{assigns: assigns} = socket) do
PubSubInterface.broadcast(
socket.assigns.room_id,
{:clear_number, socket.assigns.name}
assigns.room_id,
{:clear_number, assigns.name}
)

RoomManager.set_reveal(socket.assigns.room_id, false)
RoomManager.set_reveal(assigns.room_id, false)

{:noreply, socket}
end

@impl true
def handle_event("reveal_clicked", _params, socket) do
RoomManager.toggle_reveal(socket.assigns.room_id, socket.assigns.name)
def handle_event(
"reveal_manually",
params,
%{assigns: assigns} = socket
) do
is_revealed = params["value"] == "true"

RoomManager.set_reveal(assigns.room_id, is_revealed)

verb =
if is_revealed do
"revealed"
else
"hidden"
end

RoomManager.send_flash(
assigns.room_id,
:info,
"Numbers #{verb} by #{assigns.name}"
)

{:noreply, socket}
end
Expand Down Expand Up @@ -169,11 +188,11 @@ defmodule QuickAverageWeb.AverageLive do
end

@impl true
def handle_info({:clear_number, clearer_name}, socket) do
def handle_info({:clear_number, clearer_name}, %{assigns: assigns} = socket) do
user_params = %{
"name" => socket.assigns.name,
"name" => assigns.name,
"number" => nil,
"only_viewing" => socket.assigns.only_viewing
"only_viewing" => assigns.only_viewing
}

changeset = User.changeset(user_params)
Expand All @@ -192,17 +211,14 @@ defmodule QuickAverageWeb.AverageLive do
end

@impl true
def handle_info({:set_reveal, name, manual_reveal?}, socket) do
verb =
if manual_reveal? do
"revealed"
else
"hidden"
end
def handle_info({:show_flash, level, message}, socket) do
{:noreply, put_flash(socket, level, message)}
end

@impl true
def handle_info({:set_reveal, is_revealed_manually}, socket) do
new_socket =
assign(socket, manual_reveal?: manual_reveal?)
|> put_flash(:info, "Numbers #{verb}ed by #{name}")
assign(socket, is_revealed_manually: is_revealed_manually)

{:noreply, new_socket}
end
Expand Down
15 changes: 12 additions & 3 deletions lib/quick_average_web/live/average_live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,18 @@
<%= if @is_admin do %>
<div class="mb-3">
<.button phx-click="clear_clicked">Clear Numbers</.button>
<.button phx-click="reveal_clicked">
<%= reveal_text(@manual_reveal?) %>
</.button>
</div>

<div class="mb-3">
<.input
phx-click="reveal_manually"
id="is_revealed_manually_toggle"
name="is_revealed_manually_toggle"
field={@is_revealed_manually}
value={@is_revealed_manually}
type="toggle"
label="Reveal Manually 🎩🐇"
/>
</div>
<% end %>

Expand Down
4 changes: 2 additions & 2 deletions test/quick_average/display_state_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ defmodule QuickAverage.DisplayStateTest do
]
|> Factory.presences_for()

%{input_state: %{presences: presences, manual_reveal: true}}
%{input_state: %{presences: presences, is_revealed_manually: true}}
end

test("the average is Waiting", %{
Expand Down Expand Up @@ -164,7 +164,7 @@ defmodule QuickAverage.DisplayStateTest do
]
|> Factory.presences_for()

%{input_state: %{presences: presences, manual_reveal: true}}
%{input_state: %{presences: presences, is_revealed_manually: true}}
end

test("the average is Waiting", %{
Expand Down
2 changes: 1 addition & 1 deletion test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Support.Factory do
alias QuickAverage.User

def input_state_for(users) when is_list(users) do
%{presences: presences_for(users), manual_reveal: false}
%{presences: presences_for(users), is_revealed_manually: false}
end

def presences_for(users) when is_list(users) do
Expand Down