Skip to content

Commit

Permalink
Fix error on empty balance
Browse files Browse the repository at this point in the history
  • Loading branch information
jauggy committed Feb 26, 2025
1 parent 4307777 commit 9d14aca
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/teiserver_web/controllers/api/spads_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ defmodule TeiserverWeb.API.SpadsController do
opts
})

if balance_result do
if is_non_empty_balance_result?(balance_result) do
# Get some counts for later
team_count =
balance_result.team_sizes
Expand Down Expand Up @@ -215,6 +215,14 @@ defmodule TeiserverWeb.API.SpadsController do
end
end

def is_non_empty_balance_result?(balance_result) do
cond do
balance_result == nil -> false
balance_result.team_sizes == %{} -> false
true -> true
end
end

@spec get_member_lobby(non_neg_integer()) :: T.lobby() | nil
defp get_member_lobby(userid) do
case Account.get_client_by_id(userid) do
Expand Down
21 changes: 21 additions & 0 deletions test/teiserver_web/controllers/api/spads_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule TeiserverWeb.API.SpadsControllerTest do
alias Teiserver.Coordinator
alias Teiserver.Account.ClientLib
alias Teiserver.Client
alias TeiserverWeb.API.SpadsController

import Teiserver.TeiserverTestLib,
only: [
Expand Down Expand Up @@ -104,5 +105,25 @@ defmodule TeiserverWeb.API.SpadsControllerTest do

assert data == %{}
end

test "can detect empty balance result" do
# This is the default balance result when no players
# Defined inside balance_lib.ex
balance_result = %{
logs: [],
time_taken: 0,
captains: %{},
deviation: 0,
ratings: %{},
team_groups: %{},
team_players: %{},
team_sizes: %{},
means: %{},
stdevs: %{},
has_parties?: false
}

assert SpadsController.is_non_empty_balance_result?(balance_result) == false
end
end
end

0 comments on commit 9d14aca

Please sign in to comment.