mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 03:32:06 +00:00
Rate users and endgames with Glicko (#40)
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
defmodule Chesstrainer.RatingsTest do
|
||||
use Chesstrainer.DataCase, async: true
|
||||
|
||||
import Chesstrainer.AccountsFixtures
|
||||
import Chesstrainer.EndgamesFixtures
|
||||
|
||||
alias Chesstrainer.Accounts.Scope
|
||||
alias Chesstrainer.Accounts.User
|
||||
alias Chesstrainer.Attempts
|
||||
alias Chesstrainer.Attempts.Attempt
|
||||
alias Chesstrainer.Endgames.Endgame
|
||||
alias Chesstrainer.Ratings
|
||||
|
||||
describe "rate_attempt/1" do
|
||||
test "a solve raises the user's rating and lowers the endgame's" do
|
||||
user = user_fixture()
|
||||
endgame = endgame_fixture(rating: 1800)
|
||||
{:ok, attempt} = Attempts.record_attempt(Scope.for_user(user), endgame, :solved)
|
||||
|
||||
assert {:ok, %{user: rated_user, endgame: rated_endgame, attempt: rated_attempt}} =
|
||||
Ratings.rate_attempt(attempt)
|
||||
|
||||
assert rated_user.rating > 1500.0
|
||||
assert rated_endgame.rating < 1800.0
|
||||
assert is_float(rated_user.rating)
|
||||
assert is_float(rated_user.rating_deviation)
|
||||
assert is_float(rated_endgame.rating)
|
||||
assert is_float(rated_endgame.rating_deviation)
|
||||
refute is_nil(rated_attempt.rated_at)
|
||||
end
|
||||
|
||||
test "a fail lowers the user's rating and raises the endgame's" do
|
||||
user = user_fixture()
|
||||
endgame = endgame_fixture(rating: 1800)
|
||||
{:ok, attempt} = Attempts.record_attempt(Scope.for_user(user), endgame, :failed)
|
||||
|
||||
assert {:ok, %{user: rated_user, endgame: rated_endgame}} = Ratings.rate_attempt(attempt)
|
||||
|
||||
assert rated_user.rating < 1500.0
|
||||
assert rated_endgame.rating > 1800.0
|
||||
end
|
||||
|
||||
test "persists both ratings and stamps the attempt as rated" do
|
||||
user = user_fixture()
|
||||
endgame = endgame_fixture(rating: 1800)
|
||||
{:ok, attempt} = Attempts.record_attempt(Scope.for_user(user), endgame, :solved)
|
||||
|
||||
{:ok, _} = Ratings.rate_attempt(attempt)
|
||||
|
||||
assert Repo.get!(User, user.id).rating > 1500.0
|
||||
assert Repo.get!(Endgame, endgame.id).rating < 1800.0
|
||||
assert Repo.get!(Attempt, attempt.id).rated_at
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -4,6 +4,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
|
||||
import Phoenix.LiveViewTest
|
||||
import Chesstrainer.EndgamesFixtures
|
||||
|
||||
alias Chesstrainer.Accounts.User
|
||||
alias Chesstrainer.Attempts.Attempt
|
||||
alias Chesstrainer.Endgames
|
||||
alias Chesstrainer.Endgames.Tablebase
|
||||
@@ -72,7 +73,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
|
||||
color: :black,
|
||||
key: key,
|
||||
result: :win,
|
||||
rating: 1500
|
||||
rating: 1500.0
|
||||
})
|
||||
end
|
||||
|
||||
@@ -460,19 +461,48 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
|
||||
assert attempt.user_id == user.id
|
||||
assert attempt.outcome == :failed
|
||||
end
|
||||
|
||||
test "rates the user and the endgame on a solve", %{conn: conn, user: user} do
|
||||
endgame = endgame_fixture(rating: 1800)
|
||||
fen_after = "8/8/8/8/8/8/8/8 b - - 0 1"
|
||||
|
||||
seed_tablebase(endgame.fen, moves: grading_moves())
|
||||
seed_tablebase(fen_after, checkmate: true, moves: [])
|
||||
|
||||
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
|
||||
|
||||
render_hook(view, "endgame_move_played", %{
|
||||
"from" => "d1",
|
||||
"to" => "d7",
|
||||
"san" => "Qd7+",
|
||||
"prev_fen" => endgame.fen,
|
||||
"fen" => fen_after,
|
||||
"endgame_id" => endgame.id
|
||||
})
|
||||
|
||||
rated_user = Repo.get!(User, user.id)
|
||||
|
||||
assert render(view) =~ "Your rating: #{round(rated_user.rating)}"
|
||||
assert rated_user.rating > 1500.0
|
||||
assert Endgames.get_endgame!(endgame.id).rating < 1800.0
|
||||
end
|
||||
end
|
||||
|
||||
describe "per-user attempt records without a signed-in user" do
|
||||
test "records nothing for an anonymous visitor", %{conn: conn} do
|
||||
endgame = endgame_fixture()
|
||||
test "records nothing and leaves ratings untouched", %{conn: conn} do
|
||||
endgame = endgame_fixture(rating: 1800)
|
||||
seed_tablebase(endgame.fen, moves: grading_moves())
|
||||
|
||||
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
|
||||
|
||||
play_move(view, endgame, "d1", "d4", "Qd4")
|
||||
|
||||
assert render(view) =~ "Failed"
|
||||
html = render(view)
|
||||
|
||||
assert html =~ "Failed"
|
||||
refute html =~ "Your rating"
|
||||
assert Repo.aggregate(Attempt, :count) == 0
|
||||
assert Endgames.get_endgame!(endgame.id).rating == 1800.0
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user