model

package
v0.0.0-...-33d7a3f Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Match

type Match struct {
	bun.BaseModel `bun:"table:matches,alias:m"`
	ID            int64             `bun:"id,pk,autoincrement"`
	PlayersByRank map[Rank][]Player // the players ordered by their rank
	Players       []Player          `json:"players"`
	Date          time.Time         `bun:"date"   json:"date"`   // the date of the team
	Field         string            `bun:"field"  json:"field"`  // field in where the match was played
	Team1         Team              `bun:"team1"  json:"team1"`  // the first team
	Team2         Team              `bun:"team2"  json:"team2"`  // the second team
	MatchResult   Result            `bun:"result" json:"result"` // the match result
}

func (*Match) GenerateTeams

func (m *Match) GenerateTeams()

GenerateTeams is the complete algorithm to create evenly teams based on the player's ranking where the first picker is random and each player are distributed based on their ranks

func (*Match) Init

func (m *Match) Init(players []Player)

Init initializes the match wit the players

func (*Match) SaveResult

func (m *Match) SaveResult(result Result) []Player

SaveResult will return a list of players with the actualized stats looking at a MatchResult

type Player

type Player struct {
	bun.BaseModel `bun:"table:players,alias:p"`
	ID            int64    `bun:"id,pk,autoincrement"`
	Nickname      string   `bun:"nickname,unique" json:"nickname"`
	Name          string   `bun:"name" json:"name"`
	Rank          Rank     `bun:"rank" json:"rank"`
	Description   string   `bun:"description" json:"description"`
	Age           int      `bun:"age" json:"age"`
	Position      Position `bun:"position" json:"position"`
	TotalGoals    int      `bun:"totalGoals" json:"totalGoals"`
	GamesPlayed   int      `bun:"gamesPlayed" json:"gamesPlayed"`
	GamesWon      int      `bun:"gamesWon" json:"gamesWon"`
	GamesLost     int      `bun:"gamesLost" json:"gamesLost"`
	Diff          int      `bun:"diff" json:"diff"`
	Elo           float64  `bun:"elo" json:"elo"`
	Points        int      `bun:"points" json:"points"`
}

func (*Player) CalculateELO

func (p *Player) CalculateELO() float64

CalculateELO calculates the player's ELO considering their points (age, goals, gamesWon, etc) is a simple formula, maybe improve it in a future

func (*Player) CalculatePoints

func (p *Player) CalculatePoints(goals int) int

CalculatePoints calculates the player's points

func (*Player) Init

func (p *Player) Init(nickname string, name string, description string, age int, rank Rank, position Position, totalGoals int, gamesPlayed int, gamesWon int, gamesLost int) error

Init creates the player with their values

func (*Player) UpdatePlayer

func (p *Player) UpdatePlayer(goals int, won bool)

UpdatePlayer update the entire player struct checking the goals and if the player won or lose

type Position

type Position string
const (
	Volante   Position = "volante"
	Delantero Position = "delantero"
	Defensor  Position = "defensor"
)

Position ------------------------------------------

func (*Position) UnmarshalJSON

func (r *Position) UnmarshalJSON(data []byte) error

UnmarshalJSON give us the capability of unmarshal a non-string field into or Player struct

type Rank

type Rank int
const (
	Five  Rank = 5
	Four  Rank = 4
	Three Rank = 3
	Two   Rank = 2
	One   Rank = 1
)

Rank ------------------------------------------

func (*Rank) UnmarshalJSON

func (r *Rank) UnmarshalJSON(data []byte) error

UnmarshalJSON give us the capability of unmarshal a non-int field into or Player struct

type Result

type Result struct {
	PlayerGoals map[string]int `json:"playersGoals"` // the goals that each player made (e.g: {1 : 3} - where 1 references: "Messi" that scored 3 goals)
	Winner      Team           `json:"winner"`       // winner of the match
	Loser       Team           `json:"loser"`        // loser of the match
}

Result of a match

func (*Result) ThePlayerWon

func (r *Result) ThePlayerWon(player Player) bool

ThePlayerWon check if the player is in the winner team or not TODO, improve this. Time complexity of O(n) but the maximum n will be 7

type Team

type Team struct {
	Name            string   `json:"name"`
	TotalPlayers    int      `json:"totalPlayers"`
	Players         []Player `json:"players"`
	Points          float64  `json:"points"`
	ChanceOfWinning string   `json:"chanceOfWinning"`
}

func (*Team) Init

func (t *Team) Init(name string, totalPlayers int, players []Player)

func (*Team) ToJSON

func (t *Team) ToJSON() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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