Skip to content

Commit

Permalink
GetRandomGames now returns a list of games
Browse files Browse the repository at this point in the history
  • Loading branch information
jensborch committed Nov 21, 2024
1 parent 0b25366 commit 8f567b2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 30 deletions.
31 changes: 16 additions & 15 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,35 +283,36 @@ func addTable2Tournament(ts *httptest.Server, id uint, table uint) func(t *testi
}
}

func randomGame(ts *httptest.Server, id uint) func(t *testing.T) model.GameJson {
return func(t *testing.T) model.GameJson {
func randomGame(ts *httptest.Server, id uint) func(t *testing.T) []model.GameJson {
return func(t *testing.T) []model.GameJson {

resp, _ := http.Get(fmt.Sprintf("%s/api/tournaments/%d/games/random", ts.URL, id))

if resp.StatusCode != 200 {
t.Fatalf("Expected status code 200, got %v", resp.StatusCode)
}

result := model.GameJson{}
result := []model.GameJson{}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
t.Fatalf("Expected a game, got %v", err)
t.Fatalf("Expected an list of tables, got %v", err)
}

if result.LeftScore == 0 {
t.Fatalf("Expected left greater than 0, got %v", result.LeftScore)
if result[0].LeftScore == 0 {
t.Fatalf("Expected left greater than 0, got %v", result[0].LeftScore)
}

if result.RightScore == 0 {
t.Fatalf("Expected left greater than 0, got %v", result.RightScore)
if result[0].RightScore == 0 {
t.Fatalf("Expected left greater than 0, got %v", result[0].RightScore)
}

if result.Table.ID == 0 {
t.Fatalf("Expected table id not equal to 0, got %v", result.Table.ID)
if result[0].Table.ID == 0 {
t.Fatalf("Expected table id not equal to 0, got %v", result[0].Table.ID)
}

/*if len(result.LeftPlayers) != 1 {
t.Fatalf("Expected one left player, got %v", len(result.LeftPlayers))
}*/
//TODO: Fix this
if len(result[0].LeftPlayers) != 2 {
t.Fatalf("Expected one left player, got %v", len(result[0].LeftPlayers))
}

return result
}
Expand Down Expand Up @@ -431,7 +432,7 @@ func Test(t *testing.T) {

random := randomGame(ts, tournament.ID)(t)

postGame(ts, tournament.ID, random.Table.ID, random.RightPlayers, random.LeftPlayers, string(model.RIGHT))(t)
postGame(ts, tournament.ID, random[0].Table.ID, random[0].RightPlayers, random[0].LeftPlayers, string(model.RIGHT))(t)

games := getGame(ts, tournament.ID)(t)

Expand All @@ -443,7 +444,7 @@ func Test(t *testing.T) {
t.Fatalf("Expected a score, got left score %v and right score %d", games[0].LeftScore, games[0].RightScore)
}

postGameNotValid(ts, tournament.ID, random.Table.ID)(t)
postGameNotValid(ts, tournament.ID, random[0].Table.ID)(t)

for _, p := range players {
history := getHistory(ts, tournament.ID, p.Nickname)(t)
Expand Down
2 changes: 1 addition & 1 deletion model/tournament.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type TournamentRepository interface {
FindPlayer(tournamentId string, nickname string) (*TournamentPlayer, Found)
DeactivatePlayer(tournamentId string, nickname string) (*TournamentPlayer, Found)
ActivatePlayer(tournamentId string, nickname string) (*TournamentPlayer, Found)
RandomGame(id string) (*Game, Found)
RandomGames(id string) ([]*Game, Found)
UpdatePlayerRanking(tournamentId string, nickname string, gameScore int, updated time.Time) (*TournamentPlayer, Found)
PlayerHistory(tournamentId string, nickname string, from time.Time) ([]*TournamentPlayerHistory, Found)
History(tournamentId string, from time.Time) ([]*TournamentPlayerHistory, Found)
Expand Down
8 changes: 6 additions & 2 deletions persistence/tournament.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,16 @@ func (r *tournamentRepository) addHistory(player *model.TournamentPlayer) {
}
}

func (r *tournamentRepository) RandomGame(tournamentId string) (*model.Game, model.Found) {
func (r *tournamentRepository) RandomGames(tournamentId string) ([]*model.Game, model.Found) {
if players, found := r.ActivePlayers(tournamentId); found {
if tables, found := r.FindAllTables(tournamentId); found {
gameCombinations := GetGameCombinationsInstance()
gameCombinations.Update(players, tables)
return gameCombinations.Next(), true
games := make([]*model.Game, 0, len(tables))
for t := 0; t < len(tables); t++ {
games = append(games, gameCombinations.Next())
}
return games, true
} else {
return nil, false
}
Expand Down
12 changes: 4 additions & 8 deletions persistence/tournament_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,11 @@ func TestRandomGame(t *testing.T) {

tourRepo.AddTables(tournament.IdAsString(), table)

if game, found := tourRepo.RandomGame(tournament.IdAsString()); !found {
t.Errorf("Failed to generate random game, got %t", found)
} else if game == nil {
t.Errorf("Failed to generate random game, got nil")
if games, found := tourRepo.RandomGames(tournament.IdAsString()); !found {
t.Errorf("Failed to generate random games, got %t", found)
} else if len(games) != 1 {
t.Errorf("Should generate 1 random games, got %d", len(games))
}

/*else if len(games) != 1 {
t.Errorf("Should genrate 1 random games, got %d", len(games))
}*/
}

func TestSaveGame(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions resources/games.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func GetGamesInTournament(param string, db *gorm.DB) func(*gin.Context) {
}
}

// GetRandomGame for a tournament
// @Summary Get random game for a tournament
// GetRandomGames for a tournament
// @Summary Get random games for a tournament
// @Tags actions
// @Accept json
// @Produce json
Expand All @@ -43,10 +43,10 @@ func GetRandomGames(param string, db *gorm.DB) func(*gin.Context) {
defer HandlePanic(c)
id := c.Param(param)
r := persistence.NewTournamentRepository(db)
if game, found := r.RandomGame(id); !found {
if games, found := r.RandomGames(id); !found {
c.JSON(http.StatusNotFound, NewErrorResponse(fmt.Sprintf("Could not find tournament %s", id)))
} else {
c.JSON(http.StatusOK, game)
c.JSON(http.StatusOK, games)
}
}
}
Expand Down

0 comments on commit 8f567b2

Please sign in to comment.