model

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Outcome_Win  = 1.0
	Outcome_Loss = 0.0
	Outcome_Draw = 0.5
)
View Source
const (
	StartingLiglicko2Rating     = 1500.0
	StartingLiglicko2Deviation  = 500.0
	StartingLiglicko2Volatility = 0.09
)
View Source
const K = 30
View Source
const MaxSessionKeyAge = time.Hour
View Source
const MinimumStableRatingDeviation = 120.0
View Source
const StartingElo = 1000

Variables

This section is empty.

Functions

func AdminLogout added in v0.2.0

func AdminLogout(db db.Db, id uuid.UUID) error

func CalculateElo deprecated

func CalculateElo(a, b *Player, outcome Outcome) (int, int)

Deprecated: for use with old elo system Updates player A, and B's ELO based on the outcome, see Outcome_XXX. The outcome describes player a, so if it is Outcome_Win then

func CalculateLiglicko2 added in v0.2.0

func CalculateLiglicko2(a, b *Player, outcome Outcome, playedAt time.Time) (float64, float64, error)

func DeleteGame added in v0.2.0

func DeleteGame(db db.Db, adminId uuid.UUID, ikey int64) error

func DeletePlayer added in v0.2.0

func DeletePlayer(db db.Db, playerId, adminId uuid.UUID) error

func GetRemoteAddr added in v0.2.0

func GetRemoteAddr(r *http.Request) string

GetRemoteAddr returns the remote IP address of the request. It prioritizes the X-Forwarded-For header if present, otherwise it uses r.RemoteAddr. If r.RemoteAddr contains a port, it is stripped.

func GetTotalGameCount

func GetTotalGameCount(db db.Db) (int, error)

func GetTotalPlayerCount

func GetTotalPlayerCount(db db.Db) (int, error)

func InsertAdminUser added in v0.2.0

func InsertAdminUser(tx *sqlx.Tx, user AdminUser) error

func InsertAuditLog added in v0.2.0

func InsertAuditLog(tx *sqlx.Tx, auditLog *AuditLog) error

func InsertAuditLogGameAffected added in v0.2.0

func InsertAuditLogGameAffected(tx *sqlx.Tx, gameAffected *AuditLogGameAffected) error

func InsertAuditLogPlayerAffected added in v0.2.0

func InsertAuditLogPlayerAffected(tx *sqlx.Tx, playerAffected *AuditLogPlayerAffected) error

func InsertPlayer

func InsertPlayer(db db.Db, player Player) error

func InsertPlayerTx

func InsertPlayerTx(tx *sqlx.Tx, player Player) error

func Liglicko2InstantFromTime added in v0.2.0

func Liglicko2InstantFromTime(t time.Time) float64

func MergePlayers added in v0.2.0

func MergePlayers(db db.Db, target, dest, adminId uuid.UUID) error

func NextIKey

func NextIKey(db db.Db) (int64, error)

func PlayerEloChart added in v1.0.0

func PlayerEloChart(db db.Db, id uuid.UUID) ([]byte, error)

func RenamePlayer added in v0.2.0

func RenamePlayer(db db.Db, id uuid.UUID, newName string, adminId uuid.UUID) error

func ReplayFrom added in v0.2.0

func ReplayFrom(txx *sqlx.Tx, ikey int64, initialPlayers map[uuid.UUID]*Player) ([]Game, []*Player, error)

func SetGameToDraw added in v0.2.0

func SetGameToDraw(db db.Db, adminId uuid.UUID, ikey int64) error

func SetLeaguePlayers added in v0.2.0

func SetLeaguePlayers(db db.Db, adminId uuid.UUID, players []uuid.UUID) error

func SubmitGame

func SubmitGame(db db.Db, whiteName, blackName string, submitterIsWhite bool, ikey int64, score Score, r *http.Request) (*Game, *Player, *Player, int, int, error)

func SwapGameWinner added in v0.2.0

func SwapGameWinner(db db.Db, adminId uuid.UUID, ikey int64) error

Types

type AdminUser added in v0.2.0

type AdminUser struct {
	Id            uuid.UUID `db:"id"`
	Name          string    `db:"name"`
	OauthId       string    `db:"oauth_id"` // This is the Github login
	Created       time.Time `db:"created"`
	SessionKey    string    `db:"session_key"`
	LastLogin     time.Time `db:"last_login"` // Used to know if the session key is expired
	LastIp        string    `db:"last_ip"`
	LastUserAgent string    `db:"last_user_agent"`
}

func AdminGetFromSessionKey added in v0.2.0

func AdminGetFromSessionKey(db db.Db, key string) (*AdminUser, error)

func AdminLogin added in v0.2.0

func AdminLogin(db db.Db, name, oauthId, lastIp, LastUserAgent string) (*AdminUser, error)

func GetAdminUser added in v0.2.0

func GetAdminUser(db db.Db, id uuid.UUID) (*AdminUser, error)

func GetAdminUsers added in v0.2.0

func GetAdminUsers(db db.Db) ([]AdminUser, error)

func NewAdminUser added in v0.2.0

func NewAdminUser(name, oauthId, lastIp, LastUserAgent string) *AdminUser

type AuditLog added in v0.2.0

type AuditLog struct {
	Id                   uuid.UUID `db:"id"`
	Created              time.Time `db:"created"`
	DoneBy               uuid.UUID `db:"done_by"`
	OperationName        string    `db:"operation_name"`
	OperationDescription string    `db:"operation_description"`
}

func NewAuditLog added in v0.2.0

func NewAuditLog(DoneBy uuid.UUID, OperationName, OperationDescription string) *AuditLog

type AuditLogGameAffected added in v0.2.0

type AuditLogGameAffected struct {
	AuditLogId   uuid.UUID `db:"audit_log_id"`
	GameIkey     int64     `db:"game_ikey"`
	IsMainTarget bool      `db:"is_main_target"`
	WhiteName    string    `db:"white_name"`
	BlackName    string    `db:"black_name"`
	Played       time.Time `db:"played"`
}

type AuditLogPlayerAffected added in v0.2.0

type AuditLogPlayerAffected struct {
	AuditLogId   uuid.UUID `db:"audit_log_id"`
	PlayerId     uuid.UUID `db:"player_id"`
	IsMainTarget bool      `db:"is_main_target"`
	PlayerName   string    `db:"player_name"`
}

func NewAuditLogPlayerAffected added in v0.2.0

func NewAuditLogPlayerAffected(auditId uuid.UUID, PlayerId uuid.UUID, isMainTarget bool) *AuditLogPlayerAffected

type AuditLogUiFriendly added in v0.2.0

type AuditLogUiFriendly struct {
	AuditLog
	AdminName string `db:"admin_name"`
}

func GetAuditLogsUiFriendly added in v0.2.0

func GetAuditLogsUiFriendly(db db.Db) ([]AuditLogUiFriendly, error)

func GetAuditLogsUiFriendlyForAdmin added in v0.2.0

func GetAuditLogsUiFriendlyForAdmin(db db.Db, id uuid.UUID) ([]AuditLogUiFriendly, error)

func GetAuditLogsUiFriendlyForGame added in v0.2.0

func GetAuditLogsUiFriendlyForGame(db db.Db, ikey int64) ([]AuditLogUiFriendly, error)

func GetAuditLogsUiFriendlyForPlayer added in v0.2.0

func GetAuditLogsUiFriendlyForPlayer(db db.Db, id uuid.UUID) ([]AuditLogUiFriendly, error)

type DetailedAuditLog added in v0.2.0

type DetailedAuditLog struct {
	AuditLog
	AdminName string `db:"admin_name"`
	Players   []AuditLogPlayerAffected
	Games     []AuditLogGameAffected
}

func GetAuditLog added in v0.2.0

func GetAuditLog(tx *sqlx.Tx, id uuid.UUID) (*DetailedAuditLog, error)

type Game

type Game struct {
	PlayerWhite     uuid.UUID `db:"player_white"`
	PlayerBlack     uuid.UUID `db:"player_black"`
	Score           Score     `db:"score"`
	Submitter       uuid.UUID `db:"submitter"`
	Played          time.Time `db:"played"`
	Deleted         bool      `db:"deleted"`
	SubmitIp        string    `db:"submit_ip"`
	SubmitUserAgent string    `db:"submit_user_agent"`
	IKey            int64     `db:"ikey"`

	DEPRECATEDEloGiven int `db:"elo_given"` // Deprecated: for use with old elo system
	DEPRECATEDEloTaken int `db:"elo_taken"` // Deprecated: for use with old elo system

	// Liglicko2White and Liglicko2Black are per-game liglicko2 deltas for each side.
	// They preserve sign, so draws between uneven players can still show non-zero
	// changes.
	Liglicko2White              float64 `db:"liglicko2_white"`
	Liglicko2WhiteOldRating     float64 `db:"liglicko2_white_old_rating"`
	Liglicko2WhiteOldVolatility float64 `db:"liglicko2_white_old_volatility"`
	Liglicko2WhiteOldDeviation  float64 `db:"liglicko2_white_old_deviation"`
	Liglicko2WhiteOldAt         float64 `db:"liglicko2_white_old_at"`

	Liglicko2Black              float64 `db:"liglicko2_black"`
	Liglicko2BlackOldRating     float64 `db:"liglicko2_black_old_rating"`
	Liglicko2BlackOldVolatility float64 `db:"liglicko2_black_old_volatility"`
	Liglicko2BlackOldDeviation  float64 `db:"liglicko2_black_old_deviation"`
	Liglicko2BlackOldAt         float64 `db:"liglicko2_black_old_at"`
}

func CreateGame

func CreateGame(tx *sqlx.Tx, submitter, opponent *Player, submitterIsWhite bool, ikey int64, score Score, r *http.Request) (Game, int, int, error)

func GetGamesByPlayerPairCombs added in v0.2.0

func GetGamesByPlayerPairCombs(db db.Db, playerAIds []uuid.UUID, playerBIds []uuid.UUID) ([]Game, error)

type GameWithDetails added in v0.2.0

type GameWithDetails struct {
	Game
	WhiteName     string `db:"white_name"`
	BlackName     string `db:"black_name"`
	SubmitterName string `db:"submitter_name"`
}

func GetGameWithDetails added in v0.2.0

func GetGameWithDetails(db db.Db, ikey int64) (GameWithDetails, error)

func (GameWithDetails) WinnerName added in v0.2.0

func (g GameWithDetails) WinnerName() string

type GameWithOutcome added in v0.2.0

type GameWithOutcome struct {
	Ikey            int64
	PlayerName      string
	OpponentName    string
	Outcome         string
	Played          time.Time
	Deleted         bool
	EloChange       int
	Liglicko2Change float64
}

func GetGamesWithOutcomes added in v0.2.0

func GetGamesWithOutcomes(db db.Db) ([]GameWithOutcome, error)

type GameWithPlayerNames

type GameWithPlayerNames struct {
	Game
	WhiteName string `db:"white_name"`
	BlackName string `db:"black_name"`
}

func GetGamesByPlayer

func GetGamesByPlayer(db db.Db, playerId uuid.UUID) ([]GameWithPlayerNames, error)

func (*GameWithPlayerNames) MapGameToGameWithOutcome added in v0.2.0

func (g *GameWithPlayerNames) MapGameToGameWithOutcome(playerId uuid.UUID) GameWithOutcome

type GamesUiFriendly added in v0.2.0

type GamesUiFriendly struct {
	Wins, Draws, Losses         int
	TotalGames                  int
	WinRate, LossRate, DrawRate float64
	WhiteWinRate, BlackWinRate  float64
	Games                       []GameWithOutcome
}

func MapGamesToUserFriendly added in v0.2.0

func MapGamesToUserFriendly(playerId uuid.UUID, games []GameWithPlayerNames) GamesUiFriendly

type LeaguePlayerUiFriendly added in v0.2.0

type LeaguePlayerUiFriendly struct {
	InLeague bool `db:"in_league"`
	Player
}

func GetUiFriendlyLeaguePlayers added in v0.2.0

func GetUiFriendlyLeaguePlayers(db db.Db) ([]LeaguePlayerUiFriendly, error)

type Liglicko2Rating added in v0.2.0

type Liglicko2Rating = liglicko2core.Rating

type Outcome

type Outcome float64

Used for the calculations

type Player

type Player struct {
	Id             uuid.UUID `db:"id"`
	Name           string    `db:"name"`
	NameNormalised string    `db:"name_normalised"`
	DEPRECATEDElo  int       `db:"elo"` // Deprecated: for use with old elo system
	// Liglicko2Rating is the player's current liglicko2 rating scalar.
	Liglicko2Rating float64 `db:"liglicko2_rating"`
	// Liglicko2Deviation is the player's current liglicko2 rating deviation (RD).
	Liglicko2Deviation float64 `db:"liglicko2_deviation"`
	// Liglicko2Volatility is the player's current liglicko2 volatility (sigma).
	Liglicko2Volatility float64 `db:"liglicko2_volatility"`
	// Liglicko2At is the liglicko2 timestamp used by the algorithm.
	// It is stored as "rating periods since Unix epoch", where this app currently
	// defines 1 rating period as 1 day.
	Liglicko2At float64   `db:"liglicko2_at"`
	JoinTime    time.Time `db:"join_time"`
	Deleted     bool      `db:"deleted"`
}

func GetLeaguePlayers added in v0.2.0

func GetLeaguePlayers(tx *sqlx.Tx) ([]Player, error)

func GetPlayer

func GetPlayer(db db.Db, id uuid.UUID) (Player, error)

func GetPlayerTx added in v0.2.0

func GetPlayerTx(tx *sqlx.Tx, id uuid.UUID) (Player, error)

func GetPlayers

func GetPlayers(db db.Db) ([]Player, error)

func GetPlayersByElo

func GetPlayersByElo(db db.Db, showDeleted bool) ([]Player, error)

func NewPlayer

func NewPlayer(name string) Player

func SearchPlayerByName

func SearchPlayerByName(db db.Db, name string) ([]Player, error)

func (*Player) ApplyRating added in v0.2.0

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

type PlayerWithGameCount added in v0.2.0

type PlayerWithGameCount struct {
	Player
	GameCount int `db:"game_count"`
}

func GetPlayersByEloWithGameCount added in v0.2.0

func GetPlayersByEloWithGameCount(db db.Db) ([]PlayerWithGameCount, error)

type Score

type Score string
const (
	Score_Win  Score = "1-0"
	Score_Loss Score = "0-1"
	Score_Draw Score = "1/2-1/2"
)

func (Score) Outcome added in v0.2.0

func (s Score) Outcome() Outcome

func (*Score) Switch added in v0.2.0

func (s *Score) Switch()

Jump to

Keyboard shortcuts

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