Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added way to find all game combinations #457

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ func randomGame(ts *httptest.Server, id uint) func(t *testing.T) []model.GameJso
t.Fatalf("Expected one left player, got %v", len(result[0].LeftPlayers))
}

if len(result[0].RightPlayers) != 1 {
t.Fatalf("Expected one right player, got %v", len(result[0].LeftPlayers))
}

return result
}
}
Expand Down
165 changes: 165 additions & 0 deletions persistence/combinations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package persistence

import (
"sort"
"sync"

"github.com/jensborch/go-foosball/model"
)

var (
once sync.Once
instance *GameCombinations
)

type GameCombinations struct {
sync.Mutex
current int
games [][]*model.Game
players []*model.TournamentPlayer
}

func GetGameCombinationsInstance() *GameCombinations {
once.Do(func() {
instance = &GameCombinations{}
})
return instance
}

func (c *GameCombinations) Next() []*model.Game {
c.Lock()
defer c.Unlock()
var result []*model.Game = nil
if len(c.games) != 0 {
if c.current >= len(c.games) {
c.current = 0
}
result = c.games[c.current]
c.current++
}
return result
}

func (c *GameCombinations) Update(players []*model.TournamentPlayer, tables []*model.TournamentTable) {
c.Lock()
defer c.Unlock()
if !isSamePlayers(c.players, players) {
c.players = players
c.games = allGamePlayerCombinations(players, tables)
c.current = 0
}
}

func isSamePlayers(players1, players2 []*model.TournamentPlayer) bool {
if len(players1) != len(players2) {
return false
}

// Sort the arrays
sort.Slice(players1, func(i, j int) bool {
return players1[i].ID < players1[j].ID
})
sort.Slice(players2, func(i, j int) bool {
return players2[i].ID < players2[j].ID
})

for i := range players1 {
if players1[i] != players2[i] {
return false
}
}

return true
}

func generatePlayerPairs(players []*model.TournamentPlayer) [][]*model.TournamentPlayer {
n := len(players)
pairs := make([][]*model.TournamentPlayer, 0, n*(n-1)/2) // Preallocate for efficiency

for i := 0; i < n-1; i++ {
for j := i + 1; j < n; j++ {
pair := []*model.TournamentPlayer{players[i], players[j]}
pairs = append(pairs, pair)
}
}

return pairs
}

func generatePlayerPairsCombinations(players []*model.TournamentPlayer) [][][]*model.TournamentPlayer {
pairs := generatePlayerPairs(players)
n := len(pairs)
combinations := make([][][]*model.TournamentPlayer, 0)

if n > 3 {
for i := 0; i < n-1; i++ {
for j := i + 1; j < n; j++ {
if !overlaps(pairs[i], pairs[j]) {
combination := [][]*model.TournamentPlayer{
{pairs[i][0], pairs[i][1]},
{pairs[j][0], pairs[j][1]},
}
combinations = append(combinations, combination)
}
}
}
} else {
for i := 0; i < n; i++ {
combination := [][]*model.TournamentPlayer{
{pairs[i][0], pairs[i][1]},
}
combinations = append(combinations, combination)
}
}
return combinations
}

func overlaps(pair1, pair2 []*model.TournamentPlayer) bool {
for _, p1 := range pair1 {
for _, p2 := range pair2 {
if p1.Player.Nickname == p2.Player.Nickname {
return true
}
}
}
return false
}

func allGamePlayerCombinations(players []*model.TournamentPlayer, tables []*model.TournamentTable) [][]*model.Game {
var games [][]*model.Game

playerCombinations := generatePlayerPairsCombinations(players)
n := len(playerCombinations)
//tablesCount := int(math.Floor(float64(len(tables)*4) / float64(len(players))))

for c := 0; c < n; c++ {
round := make([]*model.Game, 0)
for t, table := range tables {
tableSize := len(playerCombinations[c])
i := c + t
if i >= n {
break
}

if tableSize == 2 {
game := model.Game{
TournamentTable: *table,
RightPlayerOne: *playerCombinations[i][0][0],
RightPlayerTwo: *playerCombinations[i][0][1],
LeftPlayerOne: *playerCombinations[i][1][0],
LeftPlayerTwo: *playerCombinations[i][1][1],
}
round = append(round, &game)
} else {
game := model.Game{
TournamentTable: *table,
RightPlayerOne: *playerCombinations[i][0][0],
LeftPlayerOne: *playerCombinations[i][0][1],
}
round = append(round, &game)
}
}
games = append(games, round)
}
return games
}
188 changes: 188 additions & 0 deletions persistence/combinations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package persistence

import (
"sync"
"testing"

"github.com/jensborch/go-foosball/model"
)

func TestGetGameCombinationsInstance(t *testing.T) {
var wg sync.WaitGroup
instance1 := GetGameCombinationsInstance()
if instance1 == nil {
t.Error("Expected non-nil instance")
}

// Test that GetInstance always returns the same instance
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
instance2 := GetGameCombinationsInstance()
if instance1 != instance2 {
t.Error("Expected the same instance")
}
}()
}
wg.Wait()
}

func TestGameCombinationsEven(t *testing.T) {
tables, players := testData([]string{"P1", "P2", "P3", "P4"}, []string{"T1"})

gameCombinations := GetGameCombinationsInstance()

gameCombinations.Update(players, tables)

tests := []struct {
want []string
}{
{want: []string{"P1", "P2", "P3", "P4"}},
{want: []string{"P1", "P3", "P2", "P4"}},
{want: []string{"P1", "P4", "P2", "P3"}},
{want: []string{"P1", "P2", "P3", "P4"}},
{want: []string{"P1", "P3", "P2", "P4"}},
{want: []string{"P1", "P4", "P2", "P3"}},
}

for i, tc := range tests {
game := gameCombinations.Next()[0]

if game == nil {
t.Errorf("Test %d: Expected non nil game", i+1)
} else {

if game.TournamentTable.Table.Name != "T1" {
t.Errorf("Test %d: Expected game to have table T1, but got %s", i+1, game.TournamentTable.Table.Name)
}

if game.RightPlayerOne.Player.Nickname != tc.want[0] {
t.Errorf("Test %d: Expected right player 1 nickname to be %s, but got %s", i+1, tc.want[0], game.RightPlayerOne.Player.Nickname)
}

if game.RightPlayerTwo.Player.Nickname != tc.want[1] {
t.Errorf("Test %d: Expected right player 2 nickname to be %s, but got %s", i+1, tc.want[1], game.RightPlayerTwo.Player.Nickname)
}

if game.LeftPlayerOne.Player.Nickname != tc.want[2] {
t.Errorf("Test %d: Expected left player 1 nickname to be %s, but got %s", i+1, tc.want[2], game.LeftPlayerOne.Player.Nickname)
}

if game.LeftPlayerTwo.Player.Nickname != tc.want[3] {
t.Errorf("Test %d: Expected left player 2 nickname to be %s, but got %s", i+1, tc.want[3], game.LeftPlayerTwo.Player.Nickname)
}
}
}
}

func TestGameCombinationsUnevenSmall(t *testing.T) {
tables, players := testData([]string{"P1", "P2", "P3"}, []string{"T1"})

gameCombinations := GetGameCombinationsInstance()

gameCombinations.Update(players, tables)

tests := []struct {
want []string
}{
{want: []string{"P1", "P2"}},
{want: []string{"P1", "P3"}},
{want: []string{"P2", "P3"}},
{want: []string{"P1", "P2"}},
}

for i, tc := range tests {
game := gameCombinations.Next()[0]

if game == nil {
t.Errorf("Test %d: Expected non nil game", i+1)
} else {

if game.TournamentTable.Table.Name != "T1" {
t.Errorf("Test %d: Expected game to have table T1, but got %s", i+1, game.TournamentTable.Table.Name)
}

if game.RightPlayerOne.Player.Nickname != tc.want[0] {
t.Errorf("Test %d: Expected right player 1 nickname to be %s, but got %s", i+1, tc.want[0], game.RightPlayerOne.Player.Nickname)
}

if game.RightPlayerTwo.Player.Nickname != "" {
t.Errorf("Test %d: Expected right player 2 be empty, but got %s", i+1, game.RightPlayerTwo.Player.Nickname)
}

if game.LeftPlayerOne.Player.Nickname != tc.want[1] {
t.Errorf("Test %d: Expected left player 1 nickname to be %s, but got %s", i+1, tc.want[1], game.LeftPlayerOne.Player.Nickname)
}

if game.LeftPlayerTwo.Player.Nickname != "" {
t.Errorf("Test %d: Expected left player 2 nickname to be empty, but got %s", i+1, game.LeftPlayerTwo.Player.Nickname)
}
}
}
}

func TestGameCombinationsMultiTables(t *testing.T) {
tables, players := testData([]string{"P1", "P2", "P3", "P4", "P5", "P6", "P7"}, []string{"T1", "T2", "T3"})

gameCombinations := GetGameCombinationsInstance()

gameCombinations.Update(players, tables)

tests := []struct {
want [][]string
}{
{want: [][]string{{"P1", "P2", "P3", "P4"}, {"P6", "", "P7", ""}}},
}

for i, tc := range tests {
for j := range tc.want {
game := gameCombinations.Next()

for tableNumber, g := range game {

if g.TournamentTable.Table.Name != tables[tableNumber].Table.Name {
t.Errorf("Test %d:%d: Expected game to have table %s, but got %s", i+1, j+1, tables[tableNumber].Table.Name, g.TournamentTable.Table.Name)
}

/*if g.RightPlayerOne.Player.Nickname != p[0] {
t.Errorf("Test %d:%d: Expected right player 1 nickname to be %s, but got %s", i+1, j+1, p[0], g.RightPlayerOne.Player.Nickname)
}

if g.RightPlayerTwo.Player.Nickname != p[1] {
t.Errorf("Test %d:%d: Expected right player 2 nickname to be %s, but got %s", i+1, j+1, p[1], g.RightPlayerTwo.Player.Nickname)
}

if g.LeftPlayerOne.Player.Nickname != p[2] {
t.Errorf("Test %d:%d: Expected left player 1 nickname to be %s, but got %s", i+1, j+1, p[2], g.LeftPlayerOne.Player.Nickname)
}

if g.LeftPlayerTwo.Player.Nickname != p[3] {
t.Errorf("Test %d:%d: Expected left player 2 nickname to be %s, but got %s", i+1, j+1, p[3], g.LeftPlayerTwo.Player.Nickname)
}*/
}
}
}
}

func testData(names []string, tablesName []string) ([]*model.TournamentTable, []*model.TournamentPlayer) {
tables := make([]*model.TournamentTable, len(tablesName))
for i, n := range tablesName {
tables[i] = &model.TournamentTable{
Table: model.Table{
Name: n,
},
}
}

players := make([]*model.TournamentPlayer, len(names))
for i, n := range names {
players[i] = &model.TournamentPlayer{
Player: model.Player{
Nickname: n,
},
}
}

return tables, players
}
Loading
Loading