Skip to content

Commit

Permalink
Update after geekingfrog review
Browse files Browse the repository at this point in the history
Remove context
  • Loading branch information
jauggy committed Jun 9, 2024
1 parent e261714 commit d7a6c3a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 39 deletions.
19 changes: 6 additions & 13 deletions lib/teiserver/battle/libs/balance_lib.ex
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,12 @@ defmodule Teiserver.Battle.BalanceLib do
groups
|> Enum.map(fn group ->
# Iterate over our map
{_ignored, better_map} =
Enum.map_reduce(group, %{}, fn {user_id, value}, acc ->
fixed_value =
cond do
# We're missing data so need to fetch it
is_number(value) -> get_user_rating_rank_old(user_id, value)
true -> value
end

{nil, Map.put(acc, user_id, fixed_value)}
end)

better_map
Map.new(group, fn {user_id, value} ->
cond do
is_number(value) -> {user_id, get_user_rating_rank_old(user_id, value)}
true -> {user_id, value}
end
end)
end)
end

Expand Down
1 change: 0 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ defmodule Teiserver.MixProject do
{:libcluster, "~> 3.3"},
{:tzdata, "~> 1.1"},
{:ex_ulid, "~> 0.1.0"},
{:mock, "~> 0.3.0", only: :test},

# Teiserver libs
{:openskill, git: "https://github.com/StanczakDominik/openskill.ex.git", branch: "master"},
Expand Down
63 changes: 38 additions & 25 deletions test/teiserver/battle/balance_lib_internal_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,70 @@ defmodule Teiserver.Battle.BalanceLibInternalTest do
Can run all balance tests via
mix test --only balance_test
"""
use ExUnit.Case
import Mock
use Teiserver.DataCase, async: true
@moduletag :balance_test
alias Teiserver.Battle.BalanceLib

setup_with_mocks([
{Teiserver.Account, [:passthrough],
[get_user_by_id: fn member_id -> get_user_by_id_mock(member_id) end]}
]) do
:ok
end

test "Able to standardise groups with incomplete data" do
[user1, user2, user3, user4, user5] = create_test_users()

groups = [
%{1 => 19, 2 => 20},
%{3 => 18},
%{4 => 15},
%{5 => 11}
%{user1.id => 19, user2.id => 20},
%{user3.id => 18},
%{user4.id => 15},
%{user5.id => 11}
]

fixed_groups = BalanceLib.standardise_groups(groups)

assert fixed_groups == [
%{
1 => %{name: "User_1", rank: 0, rating: 19},
2 => %{name: "User_2", rank: 0, rating: 20}
user1.id => %{name: "User_1", rank: 0, rating: 19},
user2.id => %{name: "User_2", rank: 0, rating: 20}
},
%{3 => %{name: "User_3", rank: 0, rating: 18}},
%{4 => %{name: "User_4", rank: 0, rating: 15}},
%{5 => %{name: "User_5", rank: 0, rating: 11}}
%{user3.id => %{name: "User_3", rank: 0, rating: 18}},
%{user4.id => %{name: "User_4", rank: 0, rating: 15}},
%{user5.id => %{name: "User_5", rank: 0, rating: 11}}
]

# loser_picks algo will hit the databases so let's just test with split_one_chevs
result = BalanceLib.create_balance(fixed_groups, 2, algorithm: "split_one_chevs")
assert result != nil
end

test "Handle groups with incomplete data in create_balance" do
test "Handle groups with incomplete data in create_balance loser_pics" do
[user1, user2, user3, user4, user5] = create_test_users()

groups = [
%{user1.id => 19, user2.id => 20},
%{user3.id => 18},
%{user4.id => 15},
%{user5.id => 11}
]

# loser_picks algo will hit the databases so let's just test with split_one_chevs
result = BalanceLib.create_balance(groups, 2, algorithm: "loser_picks")
assert result != nil
end

test "Handle groups with incomplete data in create_balance split_one_chevs" do
[user1, user2, user3, user4, user5] = create_test_users()

groups = [
%{1 => 19, 2 => 20},
%{3 => 18},
%{4 => 15},
%{5 => 11}
%{user1.id => 19, user2.id => 20},
%{user3.id => 18},
%{user4.id => 15},
%{user5.id => 11}
]

# loser_picks algo will hit the databases so let's just test with split_one_chevs
result = BalanceLib.create_balance(groups, 2, algorithm: "split_one_chevs")
assert result != nil
end

defp get_user_by_id_mock(user_id) do
%{rank: 0, name: "User_#{user_id}"}
defp create_test_users do
Enum.map(1..5, fn k ->
Teiserver.TeiserverTestLib.new_user("User_#{k}")
end)
end
end

0 comments on commit d7a6c3a

Please sign in to comment.