ratingutil

package
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2020 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	PeriodDay   time.Duration = 24 * time.Hour
	PeriodWeek                = 7 * PeriodDay
	PeriodMonth               = 30 * PeriodDay
	PeriodYear                = 365 * PeriodDay
)

RatingPeriod constants can multiple float64

PeriodDay * 3.0  => 3 days

Variables

This section is empty.

Functions

func AsRoundrobin

func AsRoundrobin(ratings map[Element]rating.Rating, scores map[Element]float64) error

AsRoundrobin considers Multiplayer Matches to be a round-trip tournament ApplyStrategy

Types

type ApplyStrategy

type ApplyStrategy func(map[Element]rating.Rating, map[Element]float64) error

ApplyStrategy is an alias for a function. This function shows how to reflect in a multiplayer game when reflecting the result of Match in Rating.

type Clock

type Clock interface {
	Now() time.Time
}

Clock is a clock used in this package. The default is to use time.Now()

type Config

type Config struct {
	//Service Clock
	Clock
	Tau float64

	//RatingPeriod is the fixed interval of Rating.
	//All matches played between this interval are considered to occur simultaneously and are calculated.
	//In RatingPeriod, the period in which the players play about 15 times is good.
	RatingPeriod time.Duration

	//It will return to the initial deviation if you have not played for about this period.
	//This period is a guideline, and the time to return to the actual initial deviation is determined by the player's Volatility here.
	//And initial Volatility is calculated based on this period.
	PeriodToResetDeviation time.Duration

	//Fighting prosperity strategy uses round robin by default
	DefaultApplyStrategy ApplyStrategy
}

Config is service configuration

func NewConfig

func NewConfig() *Config

NewConfig is default configuration

func (*Config) InitialVolatility

func (c *Config) InitialVolatility() float64

InitialVolatility calculates the initial rating fluctuation according to the setting

func (*Config) WithApplyStrategy

func (c *Config) WithApplyStrategy(strategy ApplyStrategy) *Config

WithApplyStrategy is set DefaultApplyStrategy to config

func (*Config) WithClock

func (c *Config) WithClock(clock Clock) *Config

WithClock is set clock to config

func (*Config) WithRatingPeriod

func (c *Config) WithRatingPeriod(period time.Duration) *Config

WithRatingPeriod is set RatingPeriod to config

func (*Config) WithTau

func (c *Config) WithTau(tau float64) *Config

WithTau is set Tau to config

type Element

type Element interface {
	Name() string
	Rating() rating.Rating
	ApplyMatch(rating.Rating, float64) error
	Prepare(time.Time, *Config) error
}

Element is an interface of Team/Player

type Match

type Match struct {
	// contains filtered or unexported fields
}

Match is a model that represents multiple Team / Player battles

func (*Match) Add

func (m *Match) Add(element Element, score float64) error

Add function adds a Score.

func (*Match) Apply

func (m *Match) Apply(scoresAt time.Time, config *Config) error

Apply function determines the current score and reflects it on Team / Player's Rating.

func (*Match) Ratings

func (m *Match) Ratings() map[Element]rating.Rating

Ratings return match joined Team/Players current Rating

func (*Match) Reset

func (m *Match) Reset()

Reset returns to the zero score

func (*Match) Scores

func (m *Match) Scores() map[Element]float64

Scores return copy internal scores

func (*Match) String

func (m *Match) String() string

func (*Match) WinProbs

func (m *Match) WinProbs() map[Element]float64

WinProbs returns the probability that each Team / Player will be first

type Player

type Player struct {
	// contains filtered or unexported fields
}

Player is rating resouse

func (*Player) ApplyMatch

func (p *Player) ApplyMatch(opponent rating.Rating, score float64) error

ApplyMatch reflects match results between players.

func (*Player) Name

func (p *Player) Name() string

Name is player name

func (*Player) Prepare

func (p *Player) Prepare(outcomeAt time.Time, config *Config) error

Prepare does the work before operating the player according to the configs

func (*Player) Rating

func (p *Player) Rating() rating.Rating

Rating returns the estimated strength of the current player

func (*Player) String

func (p *Player) String() string

String is implements fmt.Stringer

type Players

type Players []*Player

Players is an array of players

type Service

type Service struct {
	*Config
}

Service is usecase

Example
package main

import (
	"fmt"

	"github.com/mashiike/rating"
	"github.com/mashiike/rating/ratingutil"
)

func main() {

	//for example, Tag battle
	svc := ratingutil.New(ratingutil.NewConfig())
	team1 := svc.NewTeam(
		"bovidae",
		ratingutil.Players{
			svc.NewPlayer(
				"sheep",
				rating.New(1700.0, 50.0, svc.Config.InitialVolatility()),
				svc.Config.Now(),
			),
			svc.NewDefaultPlayer("goat"),
		},
	)
	team2 := svc.NewTeam(
		"equidae",
		ratingutil.Players{
			svc.NewPlayer(
				"donkey",
				rating.New(1400.0, 50.0, svc.Config.InitialVolatility()),
				svc.Config.Now(),
			),
			svc.NewDefaultPlayer("zebra"),
		},
	)
	match, _ := svc.NewMatch(team1, team2)

	fmt.Println(match)
	match.Add(team1, 1.0)
	match.Add(team2, 0.0)
	err := svc.Apply(match)
	if err != nil {
		fmt.Println(err.Error())
	}
	fmt.Println("=== after match ===")
	fmt.Println(match)

}
Output:

[ bovidae:{ sheep:1700.0p-138.6 goat:1500.0p-700.0 }(0.66)  equidae:{ donkey:1400.0p-138.6 zebra:1500.0p-700.0 }(0.34) ]
=== after match ===
[ bovidae:{ sheep:1705.2p-137.2 goat:1654.5p-530.9 }(0.81)  equidae:{ donkey:1393.7p-137.0 zebra:1364.1p-536.3 }(0.19) ]

func New

func New(config *Config) *Service

New creates a service class for this package.

func (*Service) Apply

func (s *Service) Apply(match *Match) error

Apply is a function for apply Match for Team/Player outcome.

func (*Service) ApplyWithTime

func (s *Service) ApplyWithTime(match *Match, outcomeAt time.Time) error

ApplyWithTime is a function for apply Match for Team/Player outcome.

func (*Service) NewDefaultPlayer

func (s *Service) NewDefaultPlayer(name string) *Player

NewDefaultPlayer is factory of default player

func (*Service) NewMatch

func (s *Service) NewMatch(elements ...Element) (*Match, error)

NewMatch creates a new Match model from a given Team / Player

func (*Service) NewPlayer

func (s *Service) NewPlayer(name string, fixed rating.Rating, fixedAt time.Time) *Player

NewPlayer is constractor of *Player

func (*Service) NewTeam

func (s *Service) NewTeam(name string, members Players) *Team

NewTeam is constractor of *Team

type Team

type Team struct {
	// contains filtered or unexported fields
}

Team is multiple player http://rhetoricstudios.com/downloads/AbstractingGlicko2ForTeamGames.pdf

func (*Team) ApplyMatch

func (t *Team) ApplyMatch(opponent rating.Rating, score float64) error

ApplyMatch reflects match results between teams.

func (*Team) Name

func (t *Team) Name() string

Name is team name

func (*Team) Prepare

func (t *Team) Prepare(outcomeAt time.Time, config *Config) error

Prepare does the work before operating the team according to the configs

func (*Team) Rating

func (t *Team) Rating() rating.Rating

Rating return estimated team rating

func (*Team) String

func (t *Team) String() string

String is implements fmt.Stringer

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL