Skip to content

Commit

Permalink
Admin remove player updates podiums, admin panel
Browse files Browse the repository at this point in the history
  • Loading branch information
ryoung786 committed May 18, 2024
1 parent 40c15f6 commit e45871a
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 7 deletions.
10 changes: 10 additions & 0 deletions lib/jeopardy/admin_actions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Jeopardy.AdminActions do
"""

alias Jeopardy.FSM
alias Jeopardy.FSM.Messages.PlayerRemoved
alias Jeopardy.Game

def handle_action(:set_score, game, {name, score}) do
Expand All @@ -17,4 +18,13 @@ defmodule Jeopardy.AdminActions do
_ -> {:ok, FSM.to_state(game, FSM.RecappingRound)}
end
end

def handle_action(:remove_contestant, game, name) do
if name in Map.keys(game.players) do
FSM.broadcast(game, %PlayerRemoved{name: name})
{:ok, %{game | contestants: Map.delete(game.contestants, name)}}
else
{:error, :player_not_found}
end
end
end
3 changes: 1 addition & 2 deletions lib/jeopardy/fsm/state.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
defmodule Jeopardy.FSM.State do
@moduledoc false
@callback valid_actions() :: [atom]
@callback handle_action(atom, %Jeopardy.Game{}, term) ::
{:ok, %Jeopardy.Game{}} | {:error, term}
@callback handle_action(atom, %Jeopardy.Game{}, term) :: {:ok, %Jeopardy.Game{}} | {:error, term}
@callback initial_data(%Jeopardy.Game{}) :: any()

defmacro __using__(_opts) do
Expand Down
5 changes: 5 additions & 0 deletions lib/jeopardy_web/components/fsm_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ defmodule JeopardyWeb.FSMComponent do
|> assign(trebek: game.trebek)
|> assign_init(game)}
end

defp handle_tv_player_removed(player_name, socket) do
game = socket.assigns.game || %{contestants: []}
{:ok, assign(socket, game: %{game | contestants: Map.delete(game.contestants, player_name)})}
end
end
end
end
11 changes: 6 additions & 5 deletions lib/jeopardy_web/components/trebek/trebek_admin_panel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ defmodule JeopardyWeb.Components.Trebek.TrebekAdminPanel do
alias Jeopardy.GameServer

def handle_event("remove-contestant", %{"player_name" => name}, socket) do
contestants = Enum.reject(socket.assigns.contestants, &match?({^name, _score}, &1))
{:noreply, assign(socket, contestants: contestants)}
GameServer.admin_action(socket.assigns.code, :remove_contestant, name)
{:noreply, socket}
end

def handle_event("edit-score", %{"player_name" => name, "score" => score}, socket) do
Expand Down Expand Up @@ -68,11 +68,12 @@ defmodule JeopardyWeb.Components.Trebek.TrebekAdminPanel do

def confirm_remove(js \\ %JS{}, name) do
js
|> JS.push("remove-contestant", value: %{player_name: name})
|> JS.show(to: "#trebek-admin-panel .actions", transition: @fade_in)
|> JS.hide(
to: "#trebek-admin-panel [data-contestant-name='#{name}']",
transition: {"transition-all ease-out", "opacity-100 translate-y-0", "opacity-0 -translate-y-4"}
to: "#trebek-admin-panel [data-contestant-name='#{name}'] .confirm-remove",
transition: {"transition-all ease-out", "translate-x-0", "translate-x-full"}
)
|> JS.push("remove-contestant", value: %{player_name: name})
end

def show_edit_score(js \\ %JS{}, name) do
Expand Down
5 changes: 5 additions & 0 deletions lib/jeopardy_web/components/tv/awaiting_answer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule JeopardyWeb.Components.Tv.AwaitingAnswer do
@moduledoc false
use JeopardyWeb.FSMComponent

alias Jeopardy.FSM.Messages.PlayerRemoved

def render(assigns) do
~H"""
<div>
Expand All @@ -11,4 +13,7 @@ defmodule JeopardyWeb.Components.Tv.AwaitingAnswer do
</div>
"""
end

def handle_game_server_msg(%PlayerRemoved{name: name}, socket), do: handle_tv_player_removed(name, socket)
def handle_game_server_msg(_, socket), do: {:ok, socket}
end
4 changes: 4 additions & 0 deletions lib/jeopardy_web/components/tv/awaiting_buzz.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule JeopardyWeb.Components.Tv.AwaitingBuzz do
@moduledoc false
use JeopardyWeb.FSMComponent

alias Jeopardy.FSM.Messages.PlayerRemoved
alias Jeopardy.Timers

def assign_init(socket, game) do
Expand All @@ -20,4 +21,7 @@ defmodule JeopardyWeb.Components.Tv.AwaitingBuzz do
</div>
"""
end

def handle_game_server_msg(%PlayerRemoved{name: name}, socket), do: handle_tv_player_removed(name, socket)
def handle_game_server_msg(_, socket), do: {:ok, socket}
end
5 changes: 5 additions & 0 deletions lib/jeopardy_web/components/tv/awaiting_daily_double_wager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule JeopardyWeb.Components.Tv.AwaitingDailyDoubleWager do
@moduledoc false
use JeopardyWeb.FSMComponent

alias Jeopardy.FSM.Messages.PlayerRemoved

def render(assigns) do
~H"""
<div>
Expand All @@ -14,4 +16,7 @@ defmodule JeopardyWeb.Components.Tv.AwaitingDailyDoubleWager do
</div>
"""
end

def handle_game_server_msg(%PlayerRemoved{name: name}, socket), do: handle_tv_player_removed(name, socket)
def handle_game_server_msg(_, socket), do: {:ok, socket}
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule JeopardyWeb.Components.Tv.AwaitingFinalJeopardyWagers do
@moduledoc false
use JeopardyWeb.FSMComponent

alias Jeopardy.FSM.Messages.PlayerRemoved
alias Jeopardy.Timers

def assign_init(socket, game) do
Expand All @@ -23,4 +24,7 @@ defmodule JeopardyWeb.Components.Tv.AwaitingFinalJeopardyWagers do
</div>
"""
end

def handle_game_server_msg(%PlayerRemoved{name: name}, socket), do: handle_tv_player_removed(name, socket)
def handle_game_server_msg(_, socket), do: {:ok, socket}
end
2 changes: 2 additions & 0 deletions lib/jeopardy_web/components/tv/awaiting_players.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ defmodule JeopardyWeb.Components.Tv.AwaitingPlayers do
{:ok, assign(socket, air_date: game.air_date)}
end

def handle_game_server_msg(_, socket), do: {:ok, socket}

def handle_event("remove-player", %{"player" => name}, socket) do
case Jeopardy.GameServer.action(socket.assigns.code, :remove_player, name) do
{:ok, game} ->
Expand Down
5 changes: 5 additions & 0 deletions lib/jeopardy_web/components/tv/reading_answer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule JeopardyWeb.Components.Tv.ReadingAnswer do
@moduledoc false
use JeopardyWeb.FSMComponent

alias Jeopardy.FSM.Messages.PlayerRemoved

def render(assigns) do
~H"""
<div>
Expand All @@ -11,4 +13,7 @@ defmodule JeopardyWeb.Components.Tv.ReadingAnswer do
</div>
"""
end

def handle_game_server_msg(%PlayerRemoved{name: name}, socket), do: handle_tv_player_removed(name, socket)
def handle_game_server_msg(_, socket), do: {:ok, socket}
end
5 changes: 5 additions & 0 deletions lib/jeopardy_web/components/tv/reading_clue.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule JeopardyWeb.Components.Tv.ReadingClue do
@moduledoc false
use JeopardyWeb.FSMComponent

alias Jeopardy.FSM.Messages.PlayerRemoved

def render(assigns) do
~H"""
<div>
Expand All @@ -11,4 +13,7 @@ defmodule JeopardyWeb.Components.Tv.ReadingClue do
</div>
"""
end

def handle_game_server_msg(%PlayerRemoved{name: name}, socket), do: handle_tv_player_removed(name, socket)
def handle_game_server_msg(_, socket), do: {:ok, socket}
end
5 changes: 5 additions & 0 deletions lib/jeopardy_web/components/tv/reading_daily_double_clue.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule JeopardyWeb.Components.Tv.ReadingDailyDoubleClue do
@moduledoc false
use JeopardyWeb.FSMComponent

alias Jeopardy.FSM.Messages.PlayerRemoved

def render(assigns) do
~H"""
<div>
Expand All @@ -11,4 +13,7 @@ defmodule JeopardyWeb.Components.Tv.ReadingDailyDoubleClue do
</div>
"""
end

def handle_game_server_msg(%PlayerRemoved{name: name}, socket), do: handle_tv_player_removed(name, socket)
def handle_game_server_msg(_, socket), do: {:ok, socket}
end
4 changes: 4 additions & 0 deletions lib/jeopardy_web/components/tv/reading_final_jeopardy_clue.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule JeopardyWeb.Components.Tv.ReadingFinalJeopardyClue do
use JeopardyWeb.FSMComponent

alias Jeopardy.FSM.Messages.FinalJeopardyAnswerSubmitted
alias Jeopardy.FSM.Messages.PlayerRemoved
alias Jeopardy.FSM.Messages.TimerStarted
alias Jeopardy.Timers

Expand Down Expand Up @@ -38,4 +39,7 @@ defmodule JeopardyWeb.Components.Tv.ReadingFinalJeopardyClue do
def handle_game_server_msg(%FinalJeopardyAnswerSubmitted{}, socket) do
{:ok, socket}
end

def handle_game_server_msg(%PlayerRemoved{name: name}, socket), do: handle_tv_player_removed(name, socket)
def handle_game_server_msg(_, socket), do: {:ok, socket}
end
5 changes: 5 additions & 0 deletions lib/jeopardy_web/components/tv/recapping_round.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule JeopardyWeb.Components.Tv.RecappingRound do
@moduledoc false
use JeopardyWeb.FSMComponent

alias Jeopardy.FSM.Messages.PlayerRemoved

def render(assigns) do
~H"""
<div>
Expand All @@ -11,4 +13,7 @@ defmodule JeopardyWeb.Components.Tv.RecappingRound do
</div>
"""
end

def handle_game_server_msg(%PlayerRemoved{name: name}, socket), do: handle_tv_player_removed(name, socket)
def handle_game_server_msg(_, socket), do: {:ok, socket}
end
4 changes: 4 additions & 0 deletions lib/jeopardy_web/components/tv/revealing_board.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule JeopardyWeb.Components.Tv.RevealingBoard do
@moduledoc false
use JeopardyWeb.FSMComponent

alias Jeopardy.FSM.Messages.PlayerRemoved
alias Jeopardy.FSM.Messages.RevealedCategory

def assign_init(socket, game) do
Expand All @@ -24,4 +25,7 @@ defmodule JeopardyWeb.Components.Tv.RevealingBoard do
def handle_game_server_msg(%RevealedCategory{index: index}, socket) do
{:ok, assign(socket, index: index)}
end

def handle_game_server_msg(%PlayerRemoved{name: name}, socket), do: handle_tv_player_removed(name, socket)
def handle_game_server_msg(_, socket), do: {:ok, socket}
end
5 changes: 5 additions & 0 deletions lib/jeopardy_web/components/tv/selecting_clue.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule JeopardyWeb.Components.Tv.SelectingClue do
@moduledoc false
use JeopardyWeb.FSMComponent

alias Jeopardy.FSM.Messages.PlayerRemoved

def assign_init(socket, game) do
assign(socket,
categories: game.board.categories,
Expand All @@ -21,4 +23,7 @@ defmodule JeopardyWeb.Components.Tv.SelectingClue do
</div>
"""
end

def handle_game_server_msg(%PlayerRemoved{name: name}, socket), do: handle_tv_player_removed(name, socket)
def handle_game_server_msg(_, socket), do: {:ok, socket}
end

0 comments on commit e45871a

Please sign in to comment.