Skip to content

Commit

Permalink
Better balance display (#507)
Browse files Browse the repository at this point in the history
* Better balance display

Sort players by rating desc
Add trophy icon
Fix the team id

* Show stdev and fix bug in respect_avoids

Fix failing tests

* Better display on balance tab
  • Loading branch information
jauggy authored Oct 20, 2024
1 parent 37fd26f commit f030eb3
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 13 deletions.
10 changes: 5 additions & 5 deletions lib/teiserver/battle/balance/respect_avoids.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ defmodule Teiserver.Battle.Balance.RespectAvoids do
players = flatten_members(expanded_group)
parties = get_parties(expanded_group)

noobs = get_noobs(players) |> sort_noobs()
noobs = get_solo_noobs(players) |> sort_noobs()
experienced_players = get_experienced_players(players, noobs)
experienced_player_ids = experienced_players |> Enum.map(fn x -> x.id end)
players_in_parties_count = parties |> List.flatten() |> Enum.count()
Expand Down Expand Up @@ -140,7 +140,7 @@ defmodule Teiserver.Battle.Balance.RespectAvoids do
]

true ->
"New players: None"
"Solo new players: None"
end

logs =
Expand Down Expand Up @@ -514,10 +514,10 @@ defmodule Teiserver.Battle.Balance.RespectAvoids do
end

# Noobs have high uncertainty and chev 1,2,3
@spec get_noobs([RA.player()]) :: any()
def get_noobs(players) do
@spec get_solo_noobs([RA.player()]) :: any()
def get_solo_noobs(players) do
Enum.filter(players, fn player ->
is_newish_player?(player.rank, player.uncertainty)
is_newish_player?(player.rank, player.uncertainty) && !player.in_party?
end)
end

Expand Down
38 changes: 37 additions & 1 deletion lib/teiserver_web/live/battles/match/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ defmodule TeiserverWeb.Battle.MatchLive.Show do
match_id: match.id
]
)
|> Map.new(fn log -> {log.user_id, log} end)
|> Map.new(fn log -> {log.user_id, get_prematch_log(log)} end)

# Creates a map where the party_id refers to an integer
# but only includes parties with 2 or more members
Expand Down Expand Up @@ -185,6 +185,14 @@ defmodule TeiserverWeb.Battle.MatchLive.Show do
end)
|> Enum.sort_by(fn v -> v end, &<=/2)

balanced_members =
Enum.map(members, fn x ->
team_id = get_team_id(x.user_id, past_balance.team_players)
Map.put(x, :team_id, team_id)
end)
|> Enum.sort_by(fn m -> rating_logs[m.user.id].value["rating_value"] end, &>=/2)
|> Enum.sort_by(fn m -> m.team_id end, &<=/2)

game_id =
cond do
match.game_id -> match.game_id
Expand All @@ -202,6 +210,7 @@ defmodule TeiserverWeb.Battle.MatchLive.Show do
|> assign(:match, match)
|> assign(:match_name, match_name)
|> assign(:members, members)
|> assign(:balanced_members, balanced_members)
|> assign(:rating_logs, rating_logs)
|> assign(:parties, parties)
|> assign(:past_balance, past_balance)
Expand All @@ -224,6 +233,33 @@ defmodule TeiserverWeb.Battle.MatchLive.Show do
end
end

# Adjust the logs so that we use the prematch values of rating/uncertainty
# Prematch values are more relevant to understanding balance logs
defp get_prematch_log(log) do
%{
"rating_value" => rating_value,
"uncertainty" => uncertainty,
"rating_value_change" => rating_value_change,
"uncertainty_change" => uncertainty_change
} = log.value

old_rating = rating_value - rating_value_change
old_uncertainty = uncertainty - uncertainty_change

Map.put(log, :rating_value, old_rating)
|> Map.put(:uncertainty, old_uncertainty)
end

def get_team_id(player_id, team_players) do
{team_id, _players} =
Enum.find(team_players, fn {_k, player_ids} ->
Enum.any?(player_ids, fn x -> x == player_id end)
end)

# team_id should start at 0 even though first key is 1
team_id - 1
end

defp generate_new_balance_data(match, algorithm) do
# For the section "If balance we made using current ratings", do not fuzz ratings
# This means the rating used is exactly equal to what is stored in database
Expand Down
63 changes: 58 additions & 5 deletions lib/teiserver_web/live/battles/match/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -303,27 +303,80 @@
<tbody>
<tr>
<td>Team 1</td>
<td><%= @past_balance.ratings[1] |> round(2) %></td>
<td>
Rating: <%= @past_balance.ratings[1] |> round(2) %>
</td>
<td>
St Dev: <%= @past_balance.stdevs[1] |> round(2) %>
</td>
</tr>
<tr>
<td>Team 2</td>
<td><%= @past_balance.ratings[2] |> round(2) %></td>
<td>
Rating: <%= @past_balance.ratings[2] |> round(2) %>
</td>
<td>
St Dev: <%= @past_balance.stdevs[2] |> round(2) %>
</td>
</tr>

<tr>
<td>Deviation</td>
<td><%= @past_balance.deviation %></td>
<td colspan="2"><%= @past_balance.deviation %></td>
</tr>
<tr>
<td>Time Taken (ms)</td>
<td><%= @past_balance.time_taken / 1000 %></td>
<td colspan="2"><%= @past_balance.time_taken / 1000 %></td>
</tr>
</tbody>
</table>

<textarea name="" id="" rows={Enum.count(@past_balance.logs)} class="form-control"><%= @past_balance.logs |> Enum.join("\n") %></textarea>

<hr />

<table class="table table-sm">
<thead>
<tr>
<th colspan="2">Name & Party</th>
<th>Team</th>
<th colspan="1">Rating</th>
<th colspan="1">Uncertainty</th>
</tr>
</thead>
<tbody>
<%= for m <- @balanced_members do %>
<% rating = @rating_logs[m.user_id]
{party_colour, party_idx} = Map.get(@parties, m.party_id, {nil, nil}) %>
<tr>
<td>
<%= m.user.name %>
</td>
<%= if party_colour do %>
<td style={"background-color: #{rgba_css(party_colour, 0.3)};"} width="50">
<Fontawesome.icon icon={party_idx} style="solid" size="lg" />
</td>
<% else %>
<td width="50">
&nbsp;
</td>
<% end %>
<td><%= m.team_id + 1 %></td>
<td>
<%= if rating != nil do %>
<%= rating.value["rating_value"] |> round(2) %>
<% end %>
</td>
<td>
<%= if rating != nil do %>
<%= rating.value["uncertainty"] |> round(2) %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
<br />

<h4>If balance we made using current ratings</h4>
<table class="table">
<tbody>
Expand Down
4 changes: 2 additions & 2 deletions test/teiserver/battle/respect_avoids_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ defmodule Teiserver.Battle.RespectAvoidsTest do
"Avoid min time required: 2 h",
"Avoids considered: 0",
"------------------------------------------------------",
"New players: None",
"Solo new players: None",
"------------------------------------------------------",
"Perform brute force with the following players to get the best score.",
"Players: Dixinormus, fbots1998, kyutoryu, HungDaddy, jauggy, Aposis, [DTG]BamBin0, reddragon2010, Noody, SLOPPYGAGGER, MaTThiuS_82, barmalev",
Expand Down Expand Up @@ -250,7 +250,7 @@ defmodule Teiserver.Battle.RespectAvoidsTest do
"Avoid min time required: 2 h",
"Avoids considered: 1",
"------------------------------------------------------",
"New players: None",
"Solo new players: None",
"------------------------------------------------------",
"Perform brute force with the following players to get the best score.",
"Players: fbots1998, kyutoryu, jauggy, reddragon2010, Aposis, [DTG]BamBin0, Dixinormus, Noody, SLOPPYGAGGER, MaTThiuS_82, barmalev, HungDaddy",
Expand Down
15 changes: 15 additions & 0 deletions test/teiserver_web/live/battles/match/show_live_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule TeiserverWeb.Battle.MatchLive.ShowTest do
alias TeiserverWeb.Battle.MatchLive.Show
use ExUnit.Case

test "get team id" do
team_players = %{1 => [1, 4], 2 => [2, 3]}
player_id = 4
result = Show.get_team_id(player_id, team_players)
assert result == 0

player_id = 3
result = Show.get_team_id(player_id, team_players)
assert result == 1
end
end

0 comments on commit f030eb3

Please sign in to comment.