Skip to content

Commit

Permalink
Rate early leavers (#534)
Browse files Browse the repository at this point in the history
  • Loading branch information
L-e-x-o-n authored Dec 6, 2024
1 parent 0a33dc1 commit d7df6d3
Showing 1 changed file with 2 additions and 112 deletions.
114 changes: 2 additions & 112 deletions lib/teiserver/game/libs/match_rating_lib.ex
Original file line number Diff line number Diff line change
Expand Up @@ -198,34 +198,14 @@ defmodule Teiserver.Game.MatchRatingLib do
# Run the actual calculation
rate_result = rate_with_ids([winner_ratings, loser_ratings], as_map: true)

status_lookup =
if match.game_type in ["Small Team", "Large Team"] do
match.members
|> Map.new(fn membership ->
{membership.user_id,
MatchLib.calculate_exit_status(membership.left_after, match.game_duration)}
end)
else
%{}
end

# Save the results
win_ratings =
winners
|> Enum.map(fn %{user_id: user_id} ->
rating_update = rate_result[user_id]
user_rating = rating_lookup[user_id] || BalanceLib.default_rating(rating_type_id)

case Map.get(status_lookup, user_id, nil) do
:abandoned ->
do_abandoned_rating(user_id, match, user_rating, rating_update)

:noshow ->
do_noshow_rating(user_id, match, user_rating, rating_update)

_ ->
do_update_rating(user_id, match, user_rating, rating_update)
end
do_update_rating(user_id, match, user_rating, rating_update)
end)

loss_ratings =
Expand Down Expand Up @@ -364,34 +344,14 @@ defmodule Teiserver.Game.MatchRatingLib do
[win_result | _lose_result] = rate_with_ids([winner_ratings | loser_ratings])
win_result = Map.new(win_result)

status_lookup =
if Enum.member?(["Small Team", "Large Team", "Team FFA"], match.game_type) do
match.members
|> Map.new(fn membership ->
{membership.user_id,
MatchLib.calculate_exit_status(membership.left_after, match.game_duration)}
end)
else
%{}
end

# Save the results
win_ratings =
winners
|> Enum.map(fn %{user_id: user_id} ->
rating_update = win_result[user_id]
user_rating = rating_lookup[user_id] || BalanceLib.default_rating(rating_type_id)

case Map.get(status_lookup, user_id, nil) do
:abandoned ->
do_abandoned_rating(user_id, match, user_rating, rating_update)

:noshow ->
do_noshow_rating(user_id, match, user_rating, rating_update)

_ ->
do_update_rating(user_id, match, user_rating, rating_update)
end
do_update_rating(user_id, match, user_rating, rating_update)
end)

# If you lose you just count as losing against the winner
Expand Down Expand Up @@ -481,76 +441,6 @@ defmodule Teiserver.Game.MatchRatingLib do
{new_skill, u}
end

defp do_noshow_rating(user_id, match, user_rating, _rating_update) do
user_rating =
if Map.get(user_rating, :user_id) do
user_rating
else
{:ok, rating} =
Account.create_rating(
Map.merge(user_rating, %{
user_id: user_id,
last_updated: match.finished
})
)

rating
end

rating_type_id = user_rating.rating_type_id

%{
user_id: user_id,
rating_type_id: rating_type_id,
match_id: match.id,
inserted_at: match.finished,
value: %{
reason: "No show",
rating_value: user_rating.rating_value,
skill: user_rating.skill,
uncertainty: user_rating.uncertainty,
rating_value_change: 0,
skill_change: 0,
uncertainty_change: 0
}
}
end

defp do_abandoned_rating(user_id, match, user_rating, _rating_update) do
user_rating =
if Map.get(user_rating, :user_id) do
user_rating
else
{:ok, rating} =
Account.create_rating(
Map.merge(user_rating, %{
user_id: user_id,
last_updated: match.finished
})
)

rating
end

rating_type_id = user_rating.rating_type_id

%{
user_id: user_id,
rating_type_id: rating_type_id,
match_id: match.id,
inserted_at: match.finished,
value: %{
reason: "Abandoned match",
rating_value: user_rating.rating_value,
skill: user_rating.skill,
uncertainty: user_rating.uncertainty,
rating_value_change: 0,
skill_change: 0,
uncertainty_change: 0
}
}
end

@spec do_update_rating(T.userid(), map(), map(), {number(), number()}) :: any
defp do_update_rating(user_id, match, user_rating, rating_update) do
# It's possible they don't yet have a rating
Expand Down

0 comments on commit d7df6d3

Please sign in to comment.