mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
Rate users and endgames with Glicko (#40)
This commit is contained in:
@@ -11,6 +11,8 @@ defmodule Chesstrainer.Accounts.User do
|
||||
field :hashed_password, :string, redact: true
|
||||
field :role, Ecto.Enum, values: [:member, :moderator, :admin, :super_admin], default: :member
|
||||
field :plan, Ecto.Enum, values: [:free, :premium], default: :free
|
||||
field :rating, :float, default: 1500.0
|
||||
field :rating_deviation, :float, default: 350.0
|
||||
field :confirmed_at, :utc_datetime
|
||||
field :authenticated_at, :utc_datetime, virtual: true
|
||||
|
||||
|
||||
@@ -17,8 +17,8 @@ defmodule Chesstrainer.Endgames.Endgame do
|
||||
field :message, :string
|
||||
field :notes, :string
|
||||
field :result, Ecto.Enum, values: [:win, :loss, :draw]
|
||||
field :rating, :integer, default: 1500
|
||||
field :rating_deviation, :integer
|
||||
field :rating, :float, default: 1500.0
|
||||
field :rating_deviation, :float, default: 350.0
|
||||
field :times_attempted, :integer
|
||||
field :times_solved, :integer
|
||||
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
defmodule Chesstrainer.Ratings do
|
||||
@moduledoc """
|
||||
Applies Glicko rating updates to a finished attempt.
|
||||
|
||||
Both sides are rated: the user as the player and the endgame as the
|
||||
opponent, then the reverse so the endgame's difficulty tracks its results.
|
||||
"""
|
||||
|
||||
alias Chesstrainer.Repo
|
||||
alias Chesstrainer.Accounts.User
|
||||
alias Chesstrainer.Endgames.Endgame
|
||||
alias Chesstrainer.Attempts.Attempt
|
||||
|
||||
@default_system_constant 0.8
|
||||
|
||||
@doc """
|
||||
Rates both sides of a finished attempt and stamps it as rated.
|
||||
|
||||
A solve scores 1 for the user and 0 for the endgame; a failure scores 0 for
|
||||
the user and 1 for the endgame. Returns the updated user, endgame and attempt.
|
||||
"""
|
||||
def rate_attempt(%Attempt{outcome: outcome} = attempt) do
|
||||
user = Repo.get!(User, attempt.user_id)
|
||||
endgame = Repo.get!(Endgame, attempt.endgame_id)
|
||||
|
||||
score = score_for(outcome)
|
||||
user_player = player(user)
|
||||
endgame_player = player(endgame)
|
||||
|
||||
new_user =
|
||||
Glicko.new_rating(user_player, [Glicko.Result.new(endgame_player, score)], opts())
|
||||
|
||||
new_endgame =
|
||||
Glicko.new_rating(endgame_player, [Glicko.Result.new(user_player, 1.0 - score)], opts())
|
||||
|
||||
Repo.transaction(fn ->
|
||||
user =
|
||||
user
|
||||
|> Ecto.Changeset.change(
|
||||
rating: new_user.rating,
|
||||
rating_deviation: new_user.rating_deviation
|
||||
)
|
||||
|> Repo.update!()
|
||||
|
||||
endgame =
|
||||
endgame
|
||||
|> Ecto.Changeset.change(
|
||||
rating: new_endgame.rating,
|
||||
rating_deviation: new_endgame.rating_deviation
|
||||
)
|
||||
|> Repo.update!()
|
||||
|
||||
attempt =
|
||||
attempt
|
||||
|> Ecto.Changeset.change(rated_at: DateTime.utc_now(:second))
|
||||
|> Repo.update!()
|
||||
|
||||
%{user: user, endgame: endgame, attempt: attempt}
|
||||
end)
|
||||
end
|
||||
|
||||
defp player(%{rating: rating, rating_deviation: rating_deviation}) do
|
||||
Glicko.Player.new_v1(rating: rating, rating_deviation: rating_deviation)
|
||||
end
|
||||
|
||||
defp score_for(:solved), do: 1.0
|
||||
defp score_for(:failed), do: 0.0
|
||||
|
||||
defp opts do
|
||||
ratings = Application.get_env(:chesstrainer, :ratings, [])
|
||||
[system_constant: Keyword.get(ratings, :system_constant, @default_system_constant)]
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user