diff --git a/lib/teiserver_web/live/battles/match/give_accolade/index.ex b/lib/teiserver_web/live/battles/match/give_accolade/index.ex deleted file mode 100644 index 85694e8df..000000000 --- a/lib/teiserver_web/live/battles/match/give_accolade/index.ex +++ /dev/null @@ -1,131 +0,0 @@ -defmodule TeiserverWeb.Battle.GiveAccoladeLive.Index do - alias Teiserver.Account.AccoladeLib - use TeiserverWeb, :live_view - alias Teiserver.{Account} - alias Teiserver.Config - import Central.Helpers.ComponentHelper - - @impl true - def mount(_params, _session, socket) do - badge_types = - Account.list_accolade_types() - - gift_limit = Config.get_site_config_cache("teiserver.Accolade gift limit") - gift_window = Config.get_site_config_cache("teiserver.Accolade gift window") - - socket = - socket - |> assign(:view_colour, Teiserver.Account.UserLib.colours()) - |> assign(:user, nil) - |> assign(:stage, :loading) - |> assign(:extra_text, "") - |> assign(:badge_types, badge_types) - |> assign(:gift_limit, gift_limit) - |> assign(:gift_window, gift_window) - |> add_breadcrumb(name: "Give Accolade", url: ~p"/") - - {:ok, socket} - end - - @impl true - def handle_params(%{"user_id" => user_id, "id" => match_id}, _url, socket) do - user = Account.get_user_by_id(user_id) - {match_id, _} = Integer.parse(match_id) - - socket = - socket - |> assign(:id_str, user_id) - |> assign(:user, user) - |> assign(:stage, :form) - |> assign(:match_id, match_id) - |> allowed_to_use_form - - {:noreply, socket} - end - - defp allowed_to_use_form(%{assigns: %{current_user: current_user, user: target_user}} = socket) do - {allowed, failure_reason} = - cond do - current_user == nil -> - {false, "You must be logged in to give an accolade to someone"} - - current_user.id == target_user.id -> - {false, "You cannot give accolades to yourself"} - - true -> - {true, nil} - end - - if allowed do - socket - |> assign_gift_history() - else - socket - |> assign(:failure_reason, failure_reason) - |> assign(:stage, :not_allowed) - end - end - - defp assign_gift_history(socket) do - gift_window = socket.assigns.gift_window - user_id = socket.assigns.current_user.id - gift_limit = socket.assigns.gift_limit - recipient_id = socket.assigns.user.id - match_id = socket.assigns.match_id - - with {:ok, gift_count} <- check_gift_count(user_id, gift_limit, gift_window), - :ok <- check_already_gifted(user_id, recipient_id, match_id) do - socket - |> assign(:gift_count, gift_count) - else - {:error, reason} -> - socket - |> assign( - :failure_reason, - reason - ) - |> assign(:stage, :not_allowed) - end - end - - defp check_gift_count(user_id, gift_limit, gift_window) do - gift_count = AccoladeLib.get_number_of_gifted_accolades(user_id, gift_window) - - if gift_count >= gift_limit do - {:error, "You can only give #{gift_limit} accolades every #{gift_window} days."} - else - {:ok, gift_count} - end - end - - defp check_already_gifted(user_id, recipient_id, match_id) do - if AccoladeLib.does_accolade_exist?(user_id, recipient_id, match_id) do - {:error, "You have already given an accolade to this user for this match."} - else - :ok - end - end - - @impl true - def handle_event("give-accolade", args, socket) do - %{"id" => badge_id} = args - - recipient_id = socket.assigns.user.id - - current_user = socket.assigns.current_user - - match_id = socket.assigns.match_id - - Account.create_accolade(%{ - recipient_id: recipient_id, - giver_id: current_user.id, - match_id: match_id, - inserted_at: Timex.now(), - badge_type_id: badge_id - }) - - socket = socket |> assign(:stage, :complete) - - {:noreply, socket} - end -end diff --git a/lib/teiserver_web/live/battles/match/give_accolade/index.html.heex b/lib/teiserver_web/live/battles/match/give_accolade/index.html.heex deleted file mode 100644 index df766d197..000000000 --- a/lib/teiserver_web/live/battles/match/give_accolade/index.html.heex +++ /dev/null @@ -1,79 +0,0 @@ -
-
-
-
-

- - - Give accolade to user: <%= @user.name %> - -

-
- -
- Loading -
- -
-
- <%= @failure_reason %> -
-
- -
- You have succesfully gifted an accolade to <%= @user.name %>!

- -   - View your accolades - - -   - View <%= @user.name %>'s accolades - -
- - -
-
-
diff --git a/lib/teiserver_web/live/battles/match/show.ex b/lib/teiserver_web/live/battles/match/show.ex index d1e20edc7..5163cdb0c 100644 --- a/lib/teiserver_web/live/battles/match/show.ex +++ b/lib/teiserver_web/live/battles/match/show.ex @@ -1,9 +1,13 @@ defmodule TeiserverWeb.Battle.MatchLive.Show do @moduledoc false use TeiserverWeb, :live_view - alias Teiserver.{Battle, Game, Telemetry} + alias Teiserver.{Account, Battle, Game, Telemetry} alias Teiserver.Battle.{MatchLib, BalanceLib} alias Teiserver.Helper.NumberHelper + alias Teiserver.Config + alias Teiserver.Account.AccoladeLib + import Central.Helpers.ComponentHelper + import Teiserver.Helper.ColourHelper @impl true def mount(_params, _session, socket) do @@ -17,6 +21,7 @@ defmodule TeiserverWeb.Battle.MatchLive.Show do BalanceLib.get_allowed_algorithms(true) ) |> assign(:algorithm, BalanceLib.get_default_algorithm()) + |> assign(:give_accolade, nil) {:ok, socket} end @@ -451,4 +456,185 @@ defmodule TeiserverWeb.Battle.MatchLive.Show do |> assign(:algorithm, value) |> get_match()} end + + def handle_event( + "give-accolade", + %{"recipient_id" => recipient_id, "recipient_name" => recipient_name}, + socket + ) do + badge_types = + Account.list_accolade_types() + + gift_limit = Config.get_site_config_cache("teiserver.Accolade gift limit") + gift_window = Config.get_site_config_cache("teiserver.Accolade gift window") + user_id = socket.assigns.current_user.id + match_id = socket.assigns.id + {recipient_id, _} = Integer.parse(recipient_id) + + with {:ok, gift_count} <- + check_gift_count(socket.assigns.current_user.id, gift_limit, gift_window), + :ok <- check_already_gifted(user_id, recipient_id, match_id) do + {:noreply, + socket + |> assign(:give_accolade, %{ + recipient: %{ + id: recipient_id, + name: recipient_name + }, + stage: :form, + badge_types: badge_types, + gift_window: gift_window, + gift_count: gift_count, + gift_limit: gift_limit + })} + else + {:error, reason} -> + {:noreply, + socket + |> assign(:give_accolade, %{ + recipient: %{ + id: recipient_id, + name: recipient_name + }, + stage: :not_allowed, + failure_reason: reason + })} + end + end + + def handle_event( + "give-accolade-submit", + %{"badgeid" => badge_id}, + socket + ) do + recipient_id = socket.assigns.give_accolade.recipient.id + current_user = socket.assigns.current_user + match_id = socket.assigns.id + + Account.create_accolade(%{ + recipient_id: recipient_id, + giver_id: current_user.id, + match_id: match_id, + inserted_at: Timex.now(), + badge_type_id: badge_id + }) + + {:noreply, + socket + |> assign(:give_accolade, Map.put(socket.assigns.give_accolade, :stage, :complete))} + end + + def handle_event( + "return-to-match", + _, + socket + ) do + {:noreply, + socket + |> assign(:give_accolade, nil)} + end + + defp check_gift_count(user_id, gift_limit, gift_window) do + gift_count = AccoladeLib.get_number_of_gifted_accolades(user_id, gift_window) + + if gift_count >= gift_limit do + {:error, "You can only give #{gift_limit} accolades every #{gift_window} days."} + else + {:ok, gift_count} + end + end + + defp check_already_gifted(user_id, recipient_id, match_id) do + if AccoladeLib.does_accolade_exist?(user_id, recipient_id, match_id) do + {:error, "You have already given an accolade to this user for this match."} + else + :ok + end + end + + def give_accolade_form(assigns) do + ~H""" +
+
+
+
+

+ + + Give accolade to user: <%= @recipient.name %> + +

+
+ +
+
+ <%= @failure_reason %> +
+ + Return to match details + +
+ +
+ You have succesfully gifted an accolade to <%= @recipient.name %>!

+ + Return to match details + + +   + View your accolades + + +   + View <%= @recipient.name %>'s accolades + +
+ + +
+
+
+ """ + end end diff --git a/lib/teiserver_web/live/battles/match/show.html.heex b/lib/teiserver_web/live/battles/match/show.html.heex index 06af25d8f..18caea656 100644 --- a/lib/teiserver_web/live/battles/match/show.html.heex +++ b/lib/teiserver_web/live/battles/match/show.html.heex @@ -19,262 +19,148 @@   -

- <%= @match_name %>      - <%= if @match.winning_team != nil do %> - Team <%= @match.winning_team + 1 %> won + <%= if @give_accolade do %> + <.give_accolade_form + recipient={@give_accolade.recipient} + stage={@give_accolade.stage} + badge_types={@give_accolade[:badge_types]} + gift_window={@give_accolade[:gift_window]} + gift_count={@give_accolade[:gift_count]} + gift_limit={@give_accolade[:gift_limit]} + current_user={@current_user} + failure_reason={@give_accolade[:failure_reason]} + /> + <% else %> +

+ <%= @match_name %>      + <%= if @match.winning_team != nil do %> + Team <%= @match.winning_team + 1 %> won + <% end %> +

+
+ + <%= if @rating_status != nil and allow?(@current_user, "Tester") do %> + <% {alert_type, status} = @rating_status %> + <% end %> - -
- - <%= if @rating_status != nil and allow?(@current_user, "Tester") do %> - <% {alert_type, status} = @rating_status %> -