Skip to content

Commit

Permalink
Handle balancer via url
Browse files Browse the repository at this point in the history
Add comment
Undo changes
  • Loading branch information
jauggy committed Jun 15, 2024
1 parent 093c8e4 commit dacd9fd
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 22 deletions.
7 changes: 1 addition & 6 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,7 @@ config :teiserver, Teiserver,
heartbeat_timeout: nil,
enable_discord_bridge: false,
enable_hailstorm: true,
accept_all_emails: true,

# The balance algorithm to use on the admin/matches/match/:id page
# It is purely used for analysis and not for actual games
# TODO move this into dropdown on admin/matches/match/:id page
analysis_balance_algorithm: "loser_picks"
accept_all_emails: true

# Watch static and templates for browser reloading.
config :teiserver, TeiserverWeb.Endpoint,
Expand Down
28 changes: 13 additions & 15 deletions lib/teiserver_web/live/battles/match/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ defmodule TeiserverWeb.Battle.MatchLive.Show do
end

@impl true
def handle_params(%{"id" => id} = params, _url, socket) do
def handle_params(params, _url, socket) do
id = Map.get(params, "id")
balancer = Map.get(params, "balancer", "loser_picks")
# TODO Handle use case when given an invalid balancer

socket =
socket
|> assign(:id, String.to_integer(id))
|> assign(:balancer, balancer)
|> get_match()
|> assign(:tab, socket.assigns.live_action)

Expand Down Expand Up @@ -59,7 +64,7 @@ defmodule TeiserverWeb.Battle.MatchLive.Show do
# {:noreply, assign(socket, :tab, tab)}
# end

defp get_match(%{assigns: %{id: id, current_user: _current_user}} = socket) do
defp get_match(%{assigns: %{id: id, balancer: balancer, current_user: _current_user}} = socket) do
if connected?(socket) do
match =
Battle.get_match!(id,
Expand Down Expand Up @@ -128,13 +133,11 @@ defmodule TeiserverWeb.Battle.MatchLive.Show do
|> List.flatten()

past_balance =
BalanceLib.create_balance(groups, match.team_count,
algorithm: get_analysis_balance_algorithm()
)
BalanceLib.create_balance(groups, match.team_count, algorithm: balancer)
|> Map.put(:balance_mode, :grouped)

# What about new balance?
new_balance = generate_new_balance_data(match)
new_balance = generate_new_balance_data(match, balancer)

raw_events =
Telemetry.list_simple_match_events(where: [match_id: match.id], preload: [:event_types])
Expand Down Expand Up @@ -185,6 +188,7 @@ 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 @@ -196,10 +200,11 @@ defmodule TeiserverWeb.Battle.MatchLive.Show do
|> assign(:new_balance, %{})
|> assign(:events_by_type, %{})
|> assign(:events_by_team_and_type, %{})
|> assign(:balancer, balancer)
end
end

defp generate_new_balance_data(match) do
defp generate_new_balance_data(match, balancer) do
rating_type = MatchLib.game_type(match.team_size, match.team_count)

partied_players =
Expand All @@ -225,14 +230,7 @@ defmodule TeiserverWeb.Battle.MatchLive.Show do
end)
|> List.flatten()

BalanceLib.create_balance(groups, match.team_count,
algorithm: get_analysis_balance_algorithm()
)
BalanceLib.create_balance(groups, match.team_count, algorithm: balancer)
|> Map.put(:balance_mode, :grouped)
end

defp get_analysis_balance_algorithm() do
# TODO move this from config into a dropdown so it can be selected on this page
Application.get_env(:teiserver, Teiserver)[:analysis_balance_algorithm] || "loser_picks"
end
end
2 changes: 2 additions & 0 deletions lib/teiserver_web/live/battles/match/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@

<%= if allow?(@current_user, "Overwatch") do %>
<div :if={@tab == :balance} class="p-4">
<p>Balancer: <%= @balancer %></p>
<h4>Based on data at the time</h4>

<table class="table">
<tbody>
<tr>
Expand Down
1 change: 1 addition & 0 deletions lib/teiserver_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ defmodule TeiserverWeb.Router do
live "/:id/players", MatchLive.Show, :players
live "/:id/ratings", MatchLive.Show, :ratings
live "/:id/balance", MatchLive.Show, :balance
live "/:id/balance/:balancer", MatchLive.Show, :balance
live "/:id/events", MatchLive.Show, :events
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
active: @active,
icon: Teiserver.Admin.AdminLib.icon(),
bsname: bsname,
url: ~p"/teiserver/admin/matches/#{@match.id}"
url: ~p"/battle/#{@match.id}"
) %>
<% end %>
</div>

0 comments on commit dacd9fd

Please sign in to comment.