mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
Add rating window and time-based RD inflation (#41)
This commit is contained in:
@@ -3,6 +3,8 @@ defmodule Chesstrainer.Attempts do
|
||||
The Attempts context: a per-user history of finished playthroughs.
|
||||
"""
|
||||
|
||||
import Ecto.Query, warn: false
|
||||
|
||||
alias Chesstrainer.Repo
|
||||
alias Chesstrainer.Accounts.{Scope, User}
|
||||
alias Chesstrainer.Endgames.Endgame
|
||||
@@ -23,4 +25,39 @@ defmodule Chesstrainer.Attempts do
|
||||
|> Attempt.changeset(%{outcome: outcome})
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
The timestamp of the most recent rated attempt for a user/endgame pair, or nil.
|
||||
"""
|
||||
def last_rated_at(user_id, endgame_id) do
|
||||
Attempt
|
||||
|> where([a], a.user_id == ^user_id and a.endgame_id == ^endgame_id)
|
||||
|> rated()
|
||||
|> select([a], max(a.rated_at))
|
||||
|> Repo.one()
|
||||
end
|
||||
|
||||
@doc """
|
||||
The timestamp of the most recent rated attempt for a user, or nil.
|
||||
"""
|
||||
def last_rated_at_for_user(user_id) do
|
||||
Attempt
|
||||
|> where([a], a.user_id == ^user_id)
|
||||
|> rated()
|
||||
|> select([a], max(a.rated_at))
|
||||
|> Repo.one()
|
||||
end
|
||||
|
||||
@doc """
|
||||
The timestamp of the most recent rated attempt on an endgame, or nil.
|
||||
"""
|
||||
def last_rated_at_for_endgame(endgame_id) do
|
||||
Attempt
|
||||
|> where([a], a.endgame_id == ^endgame_id)
|
||||
|> rated()
|
||||
|> select([a], max(a.rated_at))
|
||||
|> Repo.one()
|
||||
end
|
||||
|
||||
defp rated(query), do: where(query, [a], not is_nil(a.rated_at))
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user