2017-11-16 11:44:01 +00:00
|
|
|
defmodule Glicko.Result do
|
2020-01-19 13:58:40 +00:00
|
|
|
@moduledoc """
|
|
|
|
Provides convenience functions for handling a result against an opponent.
|
2017-11-16 11:44:01 +00:00
|
|
|
|
2020-01-19 13:58:40 +00:00
|
|
|
## Usage
|
2017-11-16 11:44:01 +00:00
|
|
|
|
2020-01-19 13:58:40 +00:00
|
|
|
iex> opponent = Player.new_v2
|
|
|
|
iex> Result.new(opponent, 1.0)
|
2020-02-05 04:15:20 +00:00
|
|
|
%Result{opponent_rating: 0.0, opponent_rating_deviation: 2.014761872416068, score: 1.0}
|
2020-01-19 13:58:40 +00:00
|
|
|
iex> Result.new(opponent, :draw) # With shortcut
|
2020-02-05 04:15:20 +00:00
|
|
|
%Result{opponent_rating: 0.0, opponent_rating_deviation: 2.014761872416068, score: 0.5}
|
2017-11-16 11:44:01 +00:00
|
|
|
|
2020-01-19 13:58:40 +00:00
|
|
|
"""
|
2017-11-16 11:44:01 +00:00
|
|
|
|
2020-01-19 13:58:40 +00:00
|
|
|
alias Glicko.Player
|
2017-11-16 11:44:01 +00:00
|
|
|
|
2020-02-05 04:15:20 +00:00
|
|
|
@type t :: %__MODULE__{
|
|
|
|
opponent_rating: Player.rating(),
|
|
|
|
opponent_rating_deviation: Player.rating_deviation(),
|
|
|
|
score: score
|
|
|
|
}
|
2017-11-16 11:44:01 +00:00
|
|
|
|
2020-01-19 13:58:40 +00:00
|
|
|
@type score :: float
|
|
|
|
@type score_shortcut :: :loss | :draw | :win
|
2017-11-16 11:44:01 +00:00
|
|
|
|
2020-01-19 13:58:40 +00:00
|
|
|
@score_shortcut_map %{loss: 0.0, draw: 0.5, win: 1.0}
|
|
|
|
@score_shortcuts Map.keys(@score_shortcut_map)
|
2017-11-16 11:44:01 +00:00
|
|
|
|
2020-02-05 04:15:20 +00:00
|
|
|
defstruct [:opponent_rating, :opponent_rating_deviation, :score]
|
|
|
|
|
2020-01-19 13:58:40 +00:00
|
|
|
@doc """
|
|
|
|
Creates a new result from an opponent rating, opponent rating deviation and score.
|
2017-11-17 00:08:25 +00:00
|
|
|
|
2020-01-19 13:58:40 +00:00
|
|
|
Values provided for the opponent rating and opponent rating deviation must be *v2* based.
|
2017-11-29 01:53:31 +00:00
|
|
|
|
2020-01-19 13:58:40 +00:00
|
|
|
Supports passing either `:loss`, `:draw`, or `:win` as shortcuts.
|
|
|
|
"""
|
|
|
|
@spec new(Player.rating(), Player.rating_deviation(), score | score_shortcut) :: t
|
|
|
|
def new(opponent_rating, opponent_rating_deviation, score) when is_number(score) do
|
2020-02-05 04:15:20 +00:00
|
|
|
%__MODULE__{
|
|
|
|
opponent_rating: opponent_rating,
|
|
|
|
opponent_rating_deviation: opponent_rating_deviation,
|
|
|
|
score: score
|
|
|
|
}
|
2020-01-19 13:58:40 +00:00
|
|
|
end
|
2017-11-16 11:44:01 +00:00
|
|
|
|
2020-01-19 13:58:40 +00:00
|
|
|
def new(opponent_rating, opponent_rating_deviation, score_type)
|
|
|
|
when is_atom(score_type) and score_type in @score_shortcuts do
|
2020-02-05 04:15:20 +00:00
|
|
|
score = Map.fetch!(@score_shortcut_map, score_type)
|
|
|
|
new(opponent_rating, opponent_rating_deviation, score)
|
2020-01-19 13:58:40 +00:00
|
|
|
end
|
2017-11-16 11:44:01 +00:00
|
|
|
|
2020-01-19 13:58:40 +00:00
|
|
|
@doc """
|
|
|
|
Creates a new result from an opponent and score.
|
2017-11-17 00:08:25 +00:00
|
|
|
|
2020-01-19 13:58:40 +00:00
|
|
|
Supports passing either `:loss`, `:draw`, or `:win` as shortcuts.
|
|
|
|
"""
|
|
|
|
@spec new(opponent :: Player.t(), score :: score | score_shortcut) :: t
|
|
|
|
def new(opponent, score) do
|
|
|
|
new(Player.rating(opponent, :v2), Player.rating_deviation(opponent, :v2), score)
|
|
|
|
end
|
2017-11-17 00:08:25 +00:00
|
|
|
|
2020-01-19 13:58:40 +00:00
|
|
|
@doc """
|
|
|
|
Convenience function for accessing an opponent's rating.
|
|
|
|
"""
|
2020-02-23 22:14:58 +00:00
|
|
|
@spec opponent_rating(result :: t()) :: Player.rating()
|
2020-02-05 04:15:20 +00:00
|
|
|
def opponent_rating(%__MODULE__{} = result), do: result.opponent_rating
|
2017-11-17 00:08:25 +00:00
|
|
|
|
2020-01-19 13:58:40 +00:00
|
|
|
@doc """
|
|
|
|
Convenience function for accessing an opponent's rating deviation.
|
|
|
|
"""
|
2020-02-23 22:14:58 +00:00
|
|
|
@spec opponent_rating_deviation(result :: t()) :: Player.rating_deviation()
|
2020-02-05 04:15:20 +00:00
|
|
|
def opponent_rating_deviation(%__MODULE__{} = result), do: result.opponent_rating_deviation
|
2017-11-17 00:08:25 +00:00
|
|
|
|
2020-01-19 13:58:40 +00:00
|
|
|
@doc """
|
|
|
|
Convenience function for accessing the score.
|
|
|
|
"""
|
2020-02-23 22:14:58 +00:00
|
|
|
@spec score(result :: t()) :: score
|
2020-02-05 04:15:20 +00:00
|
|
|
def score(%__MODULE__{} = result), do: result.score
|
2017-11-16 11:44:01 +00:00
|
|
|
end
|