Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rate early leavers #534

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading