Skip to content

Commit

Permalink
Add balancer dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
jauggy committed Jun 15, 2024
1 parent 95d352e commit 0dd4639
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
14 changes: 4 additions & 10 deletions lib/teiserver/battle/libs/balance_lib.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ defmodule Teiserver.Battle.BalanceLib do
}
end

def get_algorithm_names() do
Map.keys(algorithm_modules())
end

@doc """
Teifion only allowed force_party to be used by mods because it led to noob-stomping unbalanced teams
"""
Expand Down Expand Up @@ -181,16 +185,6 @@ defmodule Teiserver.Battle.BalanceLib do
|> Map.put(:time_taken, System.system_time(:microsecond) - start_time)
end

def is_valid_algorithm?(algo_name) do
case algorithm_modules()[algo_name] do
nil ->
false

_ ->
true
end
end

@doc """
Sometimes groups have missing data so we need to refetch it.
If we go through balancer_server then all the required data should be there
Expand Down
49 changes: 27 additions & 22 deletions lib/teiserver_web/live/battles/match/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,24 @@ defmodule TeiserverWeb.Battle.MatchLive.Show do
|> assign(:site_menu_active, "match")
|> assign(:view_colour, Teiserver.Battle.MatchLib.colours())
|> assign(:tab, "details")
|> assign(
:balancer_options,
BalanceLib.get_algorithm_names()
)
|> assign(:balancer, BalanceLib.get_default_algorithm())

{:ok, socket}
end

@impl true
def handle_params(params, _url, socket) do
id = Map.get(params, "id")
default_balancer = BalanceLib.get_default_algorithm()
balancer = Map.get(params, "balancer", default_balancer)

if(!BalanceLib.is_valid_algorithm?(balancer)) do
{:noreply,
socket
|> put_flash(:error, "#{balancer} is not a valid balancer")
|> push_patch(to: "/battle/#{id}/balance/#{default_balancer}")}
else
socket =
socket
|> assign(:id, String.to_integer(id))
|> assign(:balancer, balancer)
|> get_match()
|> assign(:tab, socket.assigns.live_action)

{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end
def handle_params(%{"id" => id} = params, _url, socket) do
socket =
socket
|> assign(:id, String.to_integer(id))
|> get_match()
|> assign(:tab, socket.assigns.live_action)

{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end

defp apply_action(%{assigns: %{match_name: match_name}} = socket, :overview, _params) do
Expand Down Expand Up @@ -195,7 +188,6 @@ defmodule TeiserverWeb.Battle.MatchLive.Show do
|> assign(:new_balance, new_balance)
|> assign(:events_by_type, events_by_type)
|> assign(:events_by_team_and_type, events_by_team_and_type)
|> assign(:balancer, balancer)
else
socket
|> assign(:match, nil)
Expand All @@ -207,7 +199,6 @@ defmodule TeiserverWeb.Battle.MatchLive.Show do
|> assign(:new_balance, %{})
|> assign(:events_by_type, %{})
|> assign(:events_by_team_and_type, %{})
|> assign(:balancer, balancer)
end
end

Expand Down Expand Up @@ -240,4 +231,18 @@ defmodule TeiserverWeb.Battle.MatchLive.Show do
BalanceLib.create_balance(groups, match.team_count, algorithm: balancer)
|> Map.put(:balance_mode, :grouped)
end

@doc """
Handles the dropdown for balancer changing
"""
@impl true
def handle_event("update-balancer", event, socket) do
[key] = event["_target"]
value = event[key]

{:noreply,
socket
|> assign(:balancer, value)
|> get_match()}
end
end
15 changes: 13 additions & 2 deletions lib/teiserver_web/live/battles/match/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<% end %>

<%= if @rating_logs != %{} and allow?(@current_user, "Overwatch") do %>
<.tab_nav url={~p"/battle/#{@match.id}/balance/loser_picks"} selected={@tab == :balance}>
<.tab_nav url={~p"/battle/#{@match.id}/balance"} selected={@tab == :balance}>
<Fontawesome.icon icon="fa-solid fa-scale-balanced" style="solid" /> Balance
</.tab_nav>
<% end %>
Expand Down Expand Up @@ -284,7 +284,18 @@

<%= if allow?(@current_user, "Overwatch") do %>
<div :if={@tab == :balance} class="p-4">
<p>Balancer: <%= @balancer %></p>
<form method="post" class="">
<.input
type="select"
label="Balancer"
options={@balancer_options}
name="balancer"
value={@balancer}
phx-change="update-balancer"
/>
</form>
<br />

<h4>Based on data at the time</h4>

<table class="table">
Expand Down

0 comments on commit 0dd4639

Please sign in to comment.