tournify

package module
Version: v0.0.0-...-45dd569 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2022 License: MIT Imports: 4 Imported by: 2

README

Tournify

GoDoc Go Report Card Build Status Build status CircleCI codecov

This project aims to support the creation of any tournament.

Current features

  • Group tournament creation
  • Group tournament stats

Planned features

  • Elimination tournaments
  • Double elimination
  • Round robin

Example

To create a group tournament with 2 groups where all teams in each group meet one time simply do the following.

package main

import (
	"fmt"
	"github.com/tournify/tournify"
	"math"
	"math/rand"
)

func main()  {
	teams := []tournify.Team{
		{ID:0},
		{ID:1},
		{ID:2},
		{ID:3},
		{ID:4},
		{ID:5},
		{ID:6},
		{ID:7},
		{ID:8},
		{ID:9},
		{ID:10},
		{ID:11},
		{ID:12},
		{ID:13},
		{ID:14},
		{ID:15},
		{ID:16},
	}

	teamInterfaces := make([]tournify.TeamInterface, len(teams))

	for i := range teams {
		teamInterfaces[i] = &teams[i]
	}

	// The CreateGroupTournamentFromTeams method takes a slice of teams along with the group count and meet count
	tournament := tournify.CreateGroupTournamentFromTeams(teamInterfaces, 2, 1)

	for _, game := range tournament.GetGames() {
		game.SetScore(randomEvenFloat(0.0, 10.0), randomEvenFloat(0.0, 10.0))
		err := tournament.SetGame(game)
		if err != nil {
			panic(err)
		}
	}

	// The print method gives us a string representing the current tournament
	fmt.Println(tournament.Print())
}

func randomEvenFloat(min float64, max float64) float64 {
	return math.RoundToEven(min + rand.Float64() * (max - min))
}

This will print something similar to the following output.

TournamentType: Group

Groups
Group ID: 0
Team ID: 0
Team ID: 1
Team ID: 2
Team ID: 3
Team ID: 4
Team ID: 5
Team ID: 6
Team ID: 7

Group ID: 1
Team ID: 8
Team ID: 9
Team ID: 10
Team ID: 11
Team ID: 12
Team ID: 13
Team ID: 14
Team ID: 15
Team ID: 16


Games
Game ID: 0, HomeTeam: 0, AwayTeam: 7, HomeScore: 6.00, AwayScore: 9.00
Game ID: 1, HomeTeam: 1, AwayTeam: 6, HomeScore: 7.00, AwayScore: 4.00
Game ID: 2, HomeTeam: 2, AwayTeam: 5, HomeScore: 4.00, AwayScore: 7.00
Game ID: 3, HomeTeam: 3, AwayTeam: 4, HomeScore: 1.00, AwayScore: 2.00
Game ID: 4, HomeTeam: 0, AwayTeam: 6, HomeScore: 1.00, AwayScore: 3.00
Game ID: 5, HomeTeam: 2, AwayTeam: 5, HomeScore: 5.00, AwayScore: 8.00
Game ID: 6, HomeTeam: 3, AwayTeam: 4, HomeScore: 2.00, AwayScore: 4.00
Game ID: 7, HomeTeam: 7, AwayTeam: 1, HomeScore: 3.00, AwayScore: 5.00
Game ID: 8, HomeTeam: 0, AwayTeam: 5, HomeScore: 3.00, AwayScore: 3.00
Game ID: 9, HomeTeam: 3, AwayTeam: 4, HomeScore: 7.00, AwayScore: 2.00
Game ID: 10, HomeTeam: 7, AwayTeam: 1, HomeScore: 2.00, AwayScore: 4.00
Game ID: 11, HomeTeam: 6, AwayTeam: 2, HomeScore: 6.00, AwayScore: 9.00
Game ID: 12, HomeTeam: 0, AwayTeam: 4, HomeScore: 3.00, AwayScore: 3.00
Game ID: 13, HomeTeam: 7, AwayTeam: 1, HomeScore: 8.00, AwayScore: 2.00
Game ID: 14, HomeTeam: 6, AwayTeam: 2, HomeScore: 9.00, AwayScore: 7.00
Game ID: 15, HomeTeam: 5, AwayTeam: 3, HomeScore: 5.00, AwayScore: 0.00
Game ID: 16, HomeTeam: 0, AwayTeam: 1, HomeScore: 2.00, AwayScore: 6.00
Game ID: 17, HomeTeam: 6, AwayTeam: 2, HomeScore: 10.00, AwayScore: 1.00
Game ID: 18, HomeTeam: 5, AwayTeam: 3, HomeScore: 6.00, AwayScore: 1.00
Game ID: 19, HomeTeam: 4, AwayTeam: 7, HomeScore: 7.00, AwayScore: 3.00
Game ID: 20, HomeTeam: 0, AwayTeam: 2, HomeScore: 2.00, AwayScore: 5.00
Game ID: 21, HomeTeam: 5, AwayTeam: 3, HomeScore: 5.00, AwayScore: 3.00
Game ID: 22, HomeTeam: 4, AwayTeam: 7, HomeScore: 4.00, AwayScore: 5.00
Game ID: 23, HomeTeam: 1, AwayTeam: 6, HomeScore: 3.00, AwayScore: 3.00
Game ID: 24, HomeTeam: 0, AwayTeam: 3, HomeScore: 8.00, AwayScore: 4.00
Game ID: 25, HomeTeam: 4, AwayTeam: 7, HomeScore: 9.00, AwayScore: 3.00
Game ID: 26, HomeTeam: 1, AwayTeam: 6, HomeScore: 9.00, AwayScore: 1.00
Game ID: 27, HomeTeam: 2, AwayTeam: 5, HomeScore: 10.00, AwayScore: 1.00
Game ID: 29, HomeTeam: 9, AwayTeam: 16, HomeScore: 2.00, AwayScore: 7.00
Game ID: 30, HomeTeam: 10, AwayTeam: 15, HomeScore: 2.00, AwayScore: 3.00
Game ID: 31, HomeTeam: 11, AwayTeam: 14, HomeScore: 9.00, AwayScore: 7.00
Game ID: 32, HomeTeam: 12, AwayTeam: 13, HomeScore: 8.00, AwayScore: 7.00
Game ID: 33, HomeTeam: 8, AwayTeam: 16, HomeScore: 2.00, AwayScore: 4.00
Game ID: 34, HomeTeam: 10, AwayTeam: 15, HomeScore: 9.00, AwayScore: 7.00
Game ID: 35, HomeTeam: 11, AwayTeam: 14, HomeScore: 10.00, AwayScore: 9.00
Game ID: 36, HomeTeam: 12, AwayTeam: 13, HomeScore: 1.00, AwayScore: 5.00
Game ID: 38, HomeTeam: 8, AwayTeam: 15, HomeScore: 9.00, AwayScore: 10.00
Game ID: 39, HomeTeam: 11, AwayTeam: 14, HomeScore: 3.00, AwayScore: 7.00
Game ID: 40, HomeTeam: 12, AwayTeam: 13, HomeScore: 7.00, AwayScore: 6.00
Game ID: 42, HomeTeam: 16, AwayTeam: 10, HomeScore: 6.00, AwayScore: 6.00
Game ID: 43, HomeTeam: 8, AwayTeam: 14, HomeScore: 8.00, AwayScore: 4.00
Game ID: 44, HomeTeam: 12, AwayTeam: 13, HomeScore: 1.00, AwayScore: 10.00
Game ID: 46, HomeTeam: 16, AwayTeam: 10, HomeScore: 9.00, AwayScore: 3.00
Game ID: 47, HomeTeam: 15, AwayTeam: 11, HomeScore: 7.00, AwayScore: 6.00
Game ID: 48, HomeTeam: 8, AwayTeam: 13, HomeScore: 1.00, AwayScore: 7.00
Game ID: 50, HomeTeam: 16, AwayTeam: 10, HomeScore: 6.00, AwayScore: 4.00
Game ID: 51, HomeTeam: 15, AwayTeam: 11, HomeScore: 2.00, AwayScore: 5.00
Game ID: 52, HomeTeam: 14, AwayTeam: 12, HomeScore: 2.00, AwayScore: 2.00
Game ID: 53, HomeTeam: 8, AwayTeam: 9, HomeScore: 6.00, AwayScore: 1.00
Game ID: 54, HomeTeam: 16, AwayTeam: 10, HomeScore: 3.00, AwayScore: 4.00
Game ID: 55, HomeTeam: 15, AwayTeam: 11, HomeScore: 4.00, AwayScore: 6.00
Game ID: 56, HomeTeam: 14, AwayTeam: 12, HomeScore: 6.00, AwayScore: 6.00
Game ID: 58, HomeTeam: 8, AwayTeam: 10, HomeScore: 7.00, AwayScore: 8.00
Game ID: 59, HomeTeam: 15, AwayTeam: 11, HomeScore: 0.00, AwayScore: 7.00
Game ID: 60, HomeTeam: 14, AwayTeam: 12, HomeScore: 4.00, AwayScore: 5.00
Game ID: 62, HomeTeam: 9, AwayTeam: 16, HomeScore: 6.00, AwayScore: 4.00
Game ID: 63, HomeTeam: 8, AwayTeam: 11, HomeScore: 0.00, AwayScore: 0.00
Game ID: 64, HomeTeam: 14, AwayTeam: 12, HomeScore: 0.00, AwayScore: 9.00
Game ID: 66, HomeTeam: 9, AwayTeam: 16, HomeScore: 6.00, AwayScore: 6.00
Game ID: 67, HomeTeam: 10, AwayTeam: 15, HomeScore: 8.00, AwayScore: 9.00
Game ID: 68, HomeTeam: 8, AwayTeam: 12, HomeScore: 5.00, AwayScore: 6.00
Game ID: 70, HomeTeam: 9, AwayTeam: 16, HomeScore: 0.00, AwayScore: 8.00
Game ID: 71, HomeTeam: 10, AwayTeam: 15, HomeScore: 2.00, AwayScore: 6.00
Game ID: 72, HomeTeam: 11, AwayTeam: 14, HomeScore: 2.00, AwayScore: 2.00

You can also print statistics for the current tournament if we add the following code at the end of the main function.

	stats, err := tournify.GetGroupTournamentStats(tournament, 3, 0, 1)
	if err != nil {
		panic(err)
	}

	fmt.Println(tournify.PrintGroupTournamentStats(stats))

This will print the stats for the current tournament which will look similar to the following example

Group   Team    Played  Wins    Ties    Losses  +/-     Diff    Points
0       1       7       5       1       1       36/23   13      16
0       5       7       5       1       1       35/26   9       16
0       4       7       4       1       2       31/24   7       13
0       6       7       3       1       3       36/37   -1      10
0       2       7       3       0       4       41/43   -2      9
0       7       7       3       0       4       33/37   -4      9
0       0       7       1       2       4       25/33   -8      5
0       3       7       1       0       6       18/32   -14     3
1       16      9       5       2       2       53/33   20      17
1       11      9       5       2       2       48/38   10      17
1       12      9       5       2       2       45/45   0       17
1       15      9       5       0       4       48/54   -6      15
1       13      9       3       4       2       35/18   17      13
1       10      9       3       1       5       46/56   -10     10
1       -1      9       0       9       0       0/0     0       9
1       8       9       2       2       5       38/40   -2      8
1       9       9       1       5       3       15/31   -16     8
1       14      9       1       3       5       41/54   -13     6

Teams are ordered by the number of points. If a team has an equal number of points as another the diff value is used as a tie-breaker. If the diff value is also the same then the points scored against other teams is used.

Elimination tournaments are also possible. Here is an example of an elimination tournament with 8 teams:

package main

import (
	"fmt"
	"github.com/tournify/tournify"
	"math"
	"math/rand"
	"time"
)

func main() {
	teams := []tournify.Team{
		{ID: 0},
		{ID: 1},
		{ID: 2},
		{ID: 3},
		{ID: 4},
		{ID: 5},
		{ID: 6},
		{ID: 7},
	}

	teamInterfaces := make([]tournify.TeamInterface, len(teams))

	for i := range teams {
		teamInterfaces[i] = &teams[i]
	}

	tournament := tournify.CreateEliminationTournamentFromTeams(teamInterfaces)

	for _, game := range tournament.GetGames() {
		err := tournament.SetGameScore(game, randomEvenFloat(0.0, 100.0), randomEvenFloat(0.0, 100.0))
		if err != nil {
			panic(err)
		}
	}

	// The print method gives us a string representing the current tournament
	fmt.Println(tournament.Print())

	// Loop eliminated teams
	fmt.Println("Eliminated teams")
	for _, team := range tournament.GetEliminatedTeams() {
		fmt.Printf("Team ID: %d\n", team.GetID())
	}

	fmt.Println()

	// Loop remaining teams
	fmt.Println("Remaining teams")
	for _, team := range tournament.GetRemainingTeams() {
		fmt.Printf("Team ID: %d\n", team.GetID())
	}
}

func randomEvenFloat(min float64, max float64) float64 {
	rand.Seed(time.Now().UnixNano())
	return math.RoundToEven(min + rand.Float64()*(max-min))
}

The output from the above code will be something like the following:

TournamentType: Elimination

Teams
Team ID: 0
Team ID: 1
Team ID: 2
Team ID: 3
Team ID: 4
Team ID: 5
Team ID: 6
Team ID: 7

Games
Game ID: 0, HomeTeam: 0, AwayTeam: 1, HomeScore: 85.00, AwayScore: 55.00
Game ID: 1, HomeTeam: 2, AwayTeam: 3, HomeScore: 14.00, AwayScore: 10.00
Game ID: 2, HomeTeam: 4, AwayTeam: 5, HomeScore: 53.00, AwayScore: 49.00
Game ID: 3, HomeTeam: 6, AwayTeam: 7, HomeScore: 95.00, AwayScore: 91.00
Game ID: 4, HomeTeam: 0, AwayTeam: 2, HomeScore: 34.00, AwayScore: 31.00
Game ID: 5, HomeTeam: 4, AwayTeam: 6, HomeScore: 0.00, AwayScore: 70.00
Game ID: 6, HomeTeam: 0, AwayTeam: 6, HomeScore: 13.00, AwayScore: 12.00

Eliminated teams
Team ID: 1
Team ID: 2
Team ID: 3
Team ID: 4
Team ID: 5
Team ID: 7

Remaining teams
Team ID: 0
Team ID: 6

Contributing

Please see contributing.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DivideRoundDown

func DivideRoundDown(a int, b int) int

DivideRoundDown takes two ints, divides them and rounds the result up to the nearest int

func DivideRoundUp

func DivideRoundUp(a int, b int) int

DivideRoundUp takes two ints, divides them and rounds the result up to the nearest int

func NumberOfGamesForEliminationTournament

func NumberOfGamesForEliminationTournament(teamCount int) int

NumberOfGamesForEliminationTournament Calculates the number of games in a elimination tournament based on the number of teams

func NumberOfGamesForGroupTournament

func NumberOfGamesForGroupTournament(teamCount int, groupCount int, meetCount int) int

NumberOfGamesForGroupTournament Calculates the number of games in a group tournament based on number of teams, groups and unique encounters. This is unreliable with uneven numbers.

func PrintGroupTournamentStats

func PrintGroupTournamentStats(teamStats []TeamStatsInterface) string

PrintGroupTournamentStats takes an array of team stats and returns them as a single string with a new line for each team

func WinnerChanged

func WinnerChanged(oldGame GameInterface, newGame GameInterface) bool

Types

type Game

type Game struct {
	ID        int
	ParentIDs []int
	Scores    []ScoreInterface
	Teams     []TeamInterface
}

Game is a default struct used as an example of how structs can be implemented for tournify

func (*Game) GetAwayScore

func (g *Game) GetAwayScore() ScoreInterface

GetAwayScore returns the second score in the Scores slice

func (*Game) GetAwayTeam

func (g *Game) GetAwayTeam() TeamInterface

GetAwayTeam returns the second team in the Teams slice

func (*Game) GetHomeScore

func (g *Game) GetHomeScore() ScoreInterface

GetHomeScore returns the first score in the Scores slice

func (*Game) GetHomeTeam

func (g *Game) GetHomeTeam() TeamInterface

GetHomeTeam returns the first team in the Teams slice

func (*Game) GetID

func (g *Game) GetID() int

GetID returns the id of the game

func (*Game) GetParentIDs

func (g *Game) GetParentIDs() []int

GetParentIDs gets the ids of any games that caused this game to be generated, typically this is used in Elimination games

func (*Game) GetScores

func (g *Game) GetScores() []ScoreInterface

GetScores returns a slice of Score

func (*Game) GetTeams

func (g *Game) GetTeams() []TeamInterface

GetTeams returns a slice of Team

func (*Game) Print

func (g *Game) Print() string

Print writes game details to stdout

func (*Game) SetAwayTeam

func (g *Game) SetAwayTeam(t TeamInterface)

SetAwayTeam sets the second team of the game and adds a placeholder home team if it's not already there

func (*Game) SetHomeTeam

func (g *Game) SetHomeTeam(t TeamInterface)

SetHomeTeam sets the first team of the game

func (*Game) SetScore

func (g *Game) SetScore(homeScore float64, awayScore float64)

SetScore sets home and away scores for home and away teams, this function is needed for games with a home and away team.

type GameInterface

type GameInterface interface {
	GetID() int
	GetParentIDs() []int
	GetHomeTeam() TeamInterface
	GetAwayTeam() TeamInterface
	SetHomeTeam(t TeamInterface)
	SetAwayTeam(t TeamInterface)
	GetHomeScore() ScoreInterface
	GetAwayScore() ScoreInterface
	GetTeams() []TeamInterface   // For games that can have any number of teams
	GetScores() []ScoreInterface // For games that can have any number of scores
	SetScore(homeScore float64, awayScore float64)
	Print() string
}

GameInterface defines the needed methods for games used in the library. A Game is a flexible entity and conforms to what you might typically find in Soccer where you have a home and away team and a score for each team but the interface also tries to allow for other types of games where the number of teams and scores is not limited to 2

type Group

type Group struct {
	ID    int
	Teams []TeamInterface
	Games []GameInterface
}

Group is for group tournaments only

func (*Group) AppendGame

func (t *Group) AppendGame(game GameInterface)

AppendGame takes a single game and appends it to the Games slice

func (*Group) AppendGames

func (t *Group) AppendGames(games []GameInterface)

AppendGames adds a slice of games to the Games slice

func (*Group) AppendTeam

func (t *Group) AppendTeam(team TeamInterface)

AppendTeam takes a single team and appends it to the Teams slice

func (*Group) AppendTeams

func (t *Group) AppendTeams(teams []TeamInterface)

AppendTeams adds a slice of teams to the Teams slice

func (*Group) GetGames

func (t *Group) GetGames() *[]GameInterface

GetGames returns the slice of games belonging to the group

func (*Group) GetID

func (t *Group) GetID() int

GetID returns the id of the group

func (*Group) GetTeams

func (t *Group) GetTeams() *[]TeamInterface

GetTeams returns a slice of teams belonging to the group

func (*Group) Print

func (t *Group) Print() string

Print writes group details to stdout

func (*Group) RemoveGame

func (t *Group) RemoveGame(game GameInterface)

RemoveGame takes a single game and removes it to the Games slice

type GroupInterface

type GroupInterface interface {
	GetID() int
	GetTeams() *[]TeamInterface
	GetGames() *[]GameInterface
	AppendGames(games []GameInterface)
	AppendGame(game GameInterface)
	RemoveGame(game GameInterface)
	AppendTeams(teams []TeamInterface)
	AppendTeam(team TeamInterface)
	Print() string
}

GroupInterface defines the interface of tournament groups used for group tournaments

type Score

type Score struct {
	ID     int
	Points float64 // We want to support any type of game where points can be very high or even just decimals
}

Score is a default struct used as an example of how structs can be implemented for tournify

func (*Score) GetID

func (s *Score) GetID() int

GetID returns the id of the score

func (*Score) GetPoints

func (s *Score) GetPoints() float64

GetPoints returns the point value of the score

func (*Score) SetPoints

func (s *Score) SetPoints(points float64)

SetPoints allows you to set points for the score

type ScoreInterface

type ScoreInterface interface {
	GetID() int
	GetPoints() float64
	SetPoints(points float64)
}

ScoreInterface defines how scores should be defined. Scores hold points defined as a float. Using a float64 should allow this library to be used for any type of game.

type Team

type Team struct {
	ID         int
	Eliminated int // Increment by 1 every time this team is eliminated
}

Team is a default struct used as an example of how structs can be implemented for tournify

func (*Team) GetEliminatedCount

func (t *Team) GetEliminatedCount() int

GetEliminatedCount gets the number of times the team has been eliminated in a knockout tournament

func (*Team) GetID

func (t *Team) GetID() int

GetID returns the id of the score

func (*Team) Print

func (t *Team) Print() string

Print writes team details to stdout

func (*Team) SetEliminatedCount

func (t *Team) SetEliminatedCount(i int)

SetEliminatedCount sets the number of times the team has been eliminated in a knockout tournament

type TeamInterface

type TeamInterface interface {
	GetID() int
	GetEliminatedCount() int
	SetEliminatedCount(i int)
	Print() string
}

TeamInterface defines the methods for teams. Teams are used to create tournaments and generate games. Teams can have games

func GetLoserTeam

func GetLoserTeam(g GameInterface) TeamInterface

func GetWinnerTeam

func GetWinnerTeam(g GameInterface) TeamInterface

type TeamStats

type TeamStats struct {
	Tournament    TournamentInterface
	Group         GroupInterface
	Team          TeamInterface
	Played        int
	Wins          int
	Losses        int
	Ties          int
	PointsFor     float64
	PointsAgainst float64
	Points        int
}

TeamStats is a default struct used as an example of how structs can be implemented for tournify

func (*TeamStats) AddPoints

func (t *TeamStats) AddPoints(points int)

AddPoints adds the specified number of points to Points

func (*TeamStats) GetDiff

func (t *TeamStats) GetDiff() float64

GetDiff returns the difference of PointsFor and PointsAgainst

func (*TeamStats) GetGroup

func (t *TeamStats) GetGroup() GroupInterface

GetGroup returns the Group that the statistics were generated for, stats are directly related to a team and the group they are in.

func (*TeamStats) GetLosses

func (t *TeamStats) GetLosses() int

GetLosses returns the number of lost games

func (*TeamStats) GetPlayed

func (t *TeamStats) GetPlayed() int

GetPlayed returns the number of games played

func (*TeamStats) GetPoints

func (t *TeamStats) GetPoints() int

GetPoints returns the number of points the team has based on wins, losses or ties

func (*TeamStats) GetPointsAgainst

func (t *TeamStats) GetPointsAgainst() float64

GetPointsAgainst returns the number of goals or points that other teams have made against this team

func (*TeamStats) GetPointsFor

func (t *TeamStats) GetPointsFor() float64

GetPointsFor returns the number of goals or points that this team has made

func (*TeamStats) GetTeam

func (t *TeamStats) GetTeam() TeamInterface

GetTeam returns the Team that the statistics were generated for, stats are directly related to a team and the group they are in.

func (*TeamStats) GetTies

func (t *TeamStats) GetTies() int

GetTies returns the number of games resulting in a tied game

func (*TeamStats) GetWins

func (t *TeamStats) GetWins() int

GetWins returns the number of won games

func (*TeamStats) Print

func (t *TeamStats) Print() string

Print prints the stats as a single string

type TeamStatsInterface

type TeamStatsInterface interface {
	GetGroup() GroupInterface
	GetTeam() TeamInterface
	GetPlayed() int
	GetWins() int
	GetLosses() int
	GetTies() int
	GetPointsFor() float64
	GetPointsAgainst() float64
	GetDiff() float64
	GetPoints() int
	AddPoints(points int)
	Print() string
}

TeamStatsInterface is used to show team statistics. Currently this is specifically made for group tournaments where there is a need to rank teams.

func GetGroupStats

func GetGroupStats(group GroupInterface, winPoints int, lossPoints int, tiePoints int) []TeamStatsInterface

GetGroupStats returns the current stats for all the teams in a single group

func GetGroupTournamentStats

func GetGroupTournamentStats(t TournamentInterface, winPoints int, lossPoints int, tiePoints int) ([]TeamStatsInterface, error)

GetGroupTournamentStats takes 4 inputs. The first input is the tournament itself. The other three input defines how many points a team should get for a win, loss or tie. The standard is 3, 0, 1 but it can vary depending on the tournament.

func SortTournamentStats

func SortTournamentStats(stats []TeamStatsInterface) []TeamStatsInterface

SortTournamentStats sorts the statistics by points, diff and finally scored goals against other teams

type Tournament

type Tournament struct {
	Type   TournamentType // Is it elimination or group or ladder or poker? What is a type?
	Teams  []TeamInterface
	Groups []GroupInterface
	Games  []GameInterface
}

Tournament is a default struct used as an example of how structs can be implemented for tournify

func (*Tournament) AppendGame

func (t *Tournament) AppendGame(game GameInterface)

AppendGame appends a game to the tournament game slice

func (*Tournament) GetEliminatedTeams

func (t *Tournament) GetEliminatedTeams() []TeamInterface

GetEliminatedTeams gets all teams that have been eliminated at least one time in an elimination tournament

func (*Tournament) GetGameByID

func (t *Tournament) GetGameByID(id int) GameInterface

GetGameByID takes an int and returns a game with that id if it exists in the tournament

func (*Tournament) GetGameDepth

func (t *Tournament) GetGameDepth(game GameInterface) int

GetGameDepth gets the depth of the game in a tournament such as an elimination tournament. It is the same as counting how many games each team had to win in order to get to this game (a team which is by itself in a game automatically wins).

func (*Tournament) GetGameFirstAncestorID

func (t *Tournament) GetGameFirstAncestorID(game GameInterface) int

GetGameFirstAncestorID gets the lowest game id with a depth of 0 which this game is a descendant of

func (*Tournament) GetGameLastDescendantID

func (t *Tournament) GetGameLastDescendantID(game GameInterface) int

GetGameLastDescendantID gets the id of the last game that has been generated off of the provided game

func (*Tournament) GetGames

func (t *Tournament) GetGames() []GameInterface

GetGames returns the game slice

func (*Tournament) GetGamesAtDepth

func (t *Tournament) GetGamesAtDepth(depth int) (games []GameInterface)

GetGamesAtDepth takes an int for depth and returns any games at the depth as a slice

func (*Tournament) GetGroups

func (t *Tournament) GetGroups() []GroupInterface

GetGroups returns the group slice

func (*Tournament) GetRemainingTeams

func (t *Tournament) GetRemainingTeams() []TeamInterface

GetRemainingTeams gets all teams that have not been eliminated in an elimination tournament

func (*Tournament) GetTeams

func (t *Tournament) GetTeams() []TeamInterface

GetTeams returns the team slice

func (*Tournament) GetType

func (t *Tournament) GetType() int

GetType returns the type of tournament as an int

func (*Tournament) GetTypeString

func (t *Tournament) GetTypeString() string

GetTypeString returns the type of tournament as a string

func (*Tournament) IsDepthFull

func (t *Tournament) IsDepthFull(depth int) bool

IsDepthFull checks if the expected numbers of games have been filled for a depth in an elimination tournament

func (*Tournament) MakeTeamEliminated

func (t *Tournament) MakeTeamEliminated(team TeamInterface)

func (*Tournament) MakeTeamRemain

func (t *Tournament) MakeTeamRemain(team TeamInterface)

func (*Tournament) Print

func (t *Tournament) Print() string

Print writes the full tournament details to a string

func (*Tournament) SetGame

func (t *Tournament) SetGame(game GameInterface) error

SetGame overwrites any game with the same id

func (*Tournament) SetGameScore

func (t *Tournament) SetGameScore(game GameInterface, homeScore float64, awayScore float64) error

func (*Tournament) SetGames

func (t *Tournament) SetGames(games []GameInterface)

SetGames sets the tournaments games slice

type TournamentInterface

type TournamentInterface interface {
	GetType() int
	GetTypeString() string
	GetTeams() []TeamInterface
	GetEliminatedTeams() []TeamInterface // For elimination style tournaments
	GetRemainingTeams() []TeamInterface  // For elimination style tournaments
	GetGroups() []GroupInterface
	GetGames() []GameInterface
	AppendGame(game GameInterface)
	SetGame(game GameInterface) error
	SetGameScore(game GameInterface, homeScore float64, awayScore float64) error
	Print() string
}

TournamentInterface defines the methods needed to handle tournaments.

func CreateEliminationTournamentFromTeams

func CreateEliminationTournamentFromTeams(teams []TeamInterface) TournamentInterface

CreateEliminationTournamentFromTeams takes a slice of teams and generates a elimination tournament The ID used for games are very important for elimination tournaments as it is used to determine the home or away team in later games

func CreateGroupTournamentFromGroups

func CreateGroupTournamentFromGroups(groups []GroupInterface, meetCount int) TournamentInterface

CreateGroupTournamentFromGroups takes a slice of groups that contain teams and returns a group tournament TODO simplify and break down this function in to smaller chunks? TODO this method currently uses cross matching for games but other types of matching could be supported

func CreateGroupTournamentFromTeams

func CreateGroupTournamentFromTeams(teams []TeamInterface, groupCount int, meetCount int) TournamentInterface

CreateGroupTournamentFromTeams takes a slice of teams and generates a group tournament

func CreateTournament

func CreateTournament(teamCount int, meetCount int, groupCount int, tournamentType int) TournamentInterface

CreateTournament creates a tournament with the simplest input. It is recommended to create a slice with specific use via CreateTournamentFromTeams as this method will generate it's own Teams as a sort of placeholder.

func CreateTournamentFromTeams

func CreateTournamentFromTeams(teams []TeamInterface, meetCount int, groupCount int, tournamentType int) TournamentInterface

CreateTournamentFromTeams takes a slice of teams and generates a tournament of the specified type

type TournamentType

type TournamentType int

TournamentType defines the type of tournament TODO is this a good way of defining tournament types?

How can this be extended by others who want to build on this code without modifying the library?
const (
	// TournamentTypeGroup is for group tournaments
	TournamentTypeGroup TournamentType = 0
	// TournamentTypeSeries is similar to group tournaments but only has one group, usually this would be used when playing in a soccer league for example
	TournamentTypeSeries TournamentType = 1
	// TournamentTypeElimination is for elimination or knockout tournaments
	TournamentTypeElimination TournamentType = 2
	// TournamentTypeDoubleElimination is the same as TournamentTypeElimination
	// but teams to get knocked out early get a second chance to come back and win
	TournamentTypeDoubleElimination TournamentType = 3
)

func (TournamentType) String

func (tournamentType TournamentType) String() string

Jump to

Keyboard shortcuts

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