diff --git a/config/config.exs b/config/config.exs index a4f5365..5b21238 100644 --- a/config/config.exs +++ b/config/config.exs @@ -24,6 +24,10 @@ config :chesstrainer, ecto_repos: [Chesstrainer.Repo], generators: [timestamp_type: :utc_datetime] +# Glicko rating system. `system_constant` is the volatility constraint passed +# through to the Glicko package. +config :chesstrainer, :ratings, system_constant: 0.8 + # Configure the endpoint config :chesstrainer, ChesstrainerWeb.Endpoint, url: [host: "localhost"], diff --git a/lib/chesstrainer/accounts/user.ex b/lib/chesstrainer/accounts/user.ex index 39cb3e4..0de131e 100644 --- a/lib/chesstrainer/accounts/user.ex +++ b/lib/chesstrainer/accounts/user.ex @@ -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 diff --git a/lib/chesstrainer/endgames/endgame.ex b/lib/chesstrainer/endgames/endgame.ex index ec7af38..d0ef7bd 100644 --- a/lib/chesstrainer/endgames/endgame.ex +++ b/lib/chesstrainer/endgames/endgame.ex @@ -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 diff --git a/lib/chesstrainer/ratings.ex b/lib/chesstrainer/ratings.ex new file mode 100644 index 0000000..d4a2c31 --- /dev/null +++ b/lib/chesstrainer/ratings.ex @@ -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 diff --git a/lib/chesstrainer_web/live/endgame_live/form.ex b/lib/chesstrainer_web/live/endgame_live/form.ex index 30ce00f..59ff582 100644 --- a/lib/chesstrainer_web/live/endgame_live/form.ex +++ b/lib/chesstrainer_web/live/endgame_live/form.ex @@ -73,7 +73,12 @@ defmodule ChesstrainerWeb.EndgameLive.Form do <.form for={@form} id="endgame-form" phx-change="validate" phx-submit="save"> <.input field={@form[:fen]} type="text" label="Fen" disabled={@live_action == :edit} /> <.input field={@form[:color]} type="text" label="Color" disabled /> - <.input field={@form[:rating]} type="number" label="Rating" /> + <.input + field={@form[:rating]} + type="number" + value={rating_display(@form[:rating])} + label="Rating" + /> <.input field={@form[:message]} type="textarea" label="Message" /> <.input field={@form[:notes]} type="textarea" label="Notes" /> <.input @@ -244,6 +249,12 @@ defmodule ChesstrainerWeb.EndgameLive.Form do end end + # Ratings are stored as floats (Glicko); the admin form presents them as whole numbers. + defp rating_display(%Phoenix.HTML.FormField{value: value}) when is_float(value), + do: round(value) + + defp rating_display(%Phoenix.HTML.FormField{value: value}), do: value + defp return_path("index", _endgame), do: ~p"/admin/endgames" defp return_path("show", endgame), do: ~p"/admin/endgames/#{endgame}" end diff --git a/lib/chesstrainer_web/live/endgame_live/index.ex b/lib/chesstrainer_web/live/endgame_live/index.ex index 628e4ee..2f8377e 100644 --- a/lib/chesstrainer_web/live/endgame_live/index.ex +++ b/lib/chesstrainer_web/live/endgame_live/index.ex @@ -58,7 +58,7 @@ defmodule ChesstrainerWeb.EndgameLive.Index do None - {endgame.rating} + {round(endgame.rating)}
diff --git a/lib/chesstrainer_web/live/endgame_live/play.ex b/lib/chesstrainer_web/live/endgame_live/play.ex index 7c319a5..f1936ca 100644 --- a/lib/chesstrainer_web/live/endgame_live/play.ex +++ b/lib/chesstrainer_web/live/endgame_live/play.ex @@ -4,6 +4,7 @@ defmodule ChesstrainerWeb.EndgameLive.Play do alias Chesstrainer.Attempts alias Chesstrainer.Endgames alias Chesstrainer.Endgames.Tablebase + alias Chesstrainer.Ratings @impl true def render(assigns) do @@ -12,8 +13,12 @@ defmodule ChesstrainerWeb.EndgameLive.Play do <.header> Play endgame {@endgame.key} <:subtitle> - {@endgame.color |> Atom.to_string() |> String.capitalize()} to move · Rating: {@endgame.rating} · Attempts: {@endgame.times_attempted || - 0} · Solved: {@endgame.times_solved || 0} + {@endgame.color |> Atom.to_string() |> String.capitalize()} to move · Rating: {round( + @endgame.rating + )} · Attempts: {@endgame.times_attempted || + 0} · Solved: {@endgame.times_solved || 0} · Your rating: {round( + @user_rating + )}

FEN: {@endgame.fen}