Skip to content

Commit

Permalink
Update based on Lexon feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jauggy committed Dec 24, 2024
1 parent 38ceb96 commit e8fa023
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
24 changes: 12 additions & 12 deletions lib/teiserver/game/libs/match_rating_lib.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Teiserver.Game.MatchRatingLib do
alias Teiserver.{Account, Coordinator, Config, Game, Battle}
alias Teiserver.Data.Types, as: T
alias Teiserver.Repo
alias Teiserver.Battle.{BalanceLib, MatchLib}
alias Teiserver.Battle.BalanceLib
require Logger

@rated_match_types [
Expand Down Expand Up @@ -44,14 +44,14 @@ defmodule Teiserver.Game.MatchRatingLib do

@spec rate_match(non_neg_integer() | Teiserver.Battle.Match.t(), boolean()) ::
:ok | {:error, atom}
def rate_match(match_id, override) when is_integer(match_id) do
def rate_match(match_id, rerate?) when is_integer(match_id) do
Battle.get_match(match_id, preload: [:members])
|> rate_match(override)
|> rate_match(rerate?)
end

def rate_match(nil, _), do: {:error, :no_match}

def rate_match(match, override) do
def rate_match(match, rerate?) do
logs = Game.list_rating_logs(search: [match_id: match.id], limit: 1, select: [:id])

sizes =
Expand Down Expand Up @@ -85,9 +85,9 @@ defmodule Teiserver.Game.MatchRatingLib do
Map.get(match.tags, "game/modoptions/ranked_game", "1") == "0" ->
{:error, :unranked_tag}

# If override is set to true we skip the next few checks
override ->
do_rate_match(match, override?: true)
# If rerate? is set to true we skip the next few checks
rerate? ->
do_rate_match(match, rerate?: true)

not Enum.empty?(logs) ->
{:error, :already_rated}
Expand Down Expand Up @@ -437,7 +437,7 @@ defmodule Teiserver.Game.MatchRatingLib do

@spec do_update_rating(T.userid(), map(), map(), {number(), number()}, any()) :: any
defp do_update_rating(user_id, match, user_rating, rating_update, opts) do
override? = Keyword.get(opts, :override?, false)
rerate? = Keyword.get(opts, :rerate?, false)
# It's possible they don't yet have a rating
user_rating =
if Map.get(user_rating, :user_id) do
Expand All @@ -460,7 +460,7 @@ defmodule Teiserver.Game.MatchRatingLib do
# This is the player's first match
user_rating.num_matches == nil -> 1
# We are re-rating a previously rated match, so num_matches unchanged
override? -> user_rating.num_matches
rerate? -> user_rating.num_matches
# Otherwise increment by one
true -> user_rating.num_matches + 1
end
Expand Down Expand Up @@ -732,11 +732,11 @@ defmodule Teiserver.Game.MatchRatingLib do
end

# Saves ratings logs to database
# If override? then delete existing logs of that match before we insert
# If rerate? then delete existing logs of that match before we insert
defp save_rating_logs(match_id, win_ratings, loss_ratings, opts) do
override? = Keyword.get(opts, :override?, false)
rerate? = Keyword.get(opts, :rerate?, false)

if(override?) do
if(rerate?) do
Ecto.Multi.new()
|> Ecto.Multi.run(:delete_existing, fn repo, _ ->
query = """
Expand Down
7 changes: 7 additions & 0 deletions test/teiserver/game/libs/match_rating_lib_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ defmodule Teiserver.Game.MatchRatingLibTest do
# Check num_matches has increased
assert ratings[user1.id].num_matches == 2
assert ratings[user1.id].num_matches == 2

# Rerate the same match
MatchRatingLib.re_rate_specific_matches([match.id])

# Check num_matches unchanged
assert ratings[user1.id].num_matches == 2
assert ratings[user1.id].num_matches == 2
end

defp get_ratings(userids, rating_type_id) do
Expand Down

0 comments on commit e8fa023

Please sign in to comment.