Skip to content

Commit

Permalink
Feat: Added game seeder
Browse files Browse the repository at this point in the history
  • Loading branch information
KazeNoYumeX committed Mar 21, 2024
1 parent 385f948 commit bc71d39
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Backend/cmd/migrate/game_card_seeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ import (

func main() {
db := config.NewDatabase()
seeders.Run(db)
seeders.OnlyCardsRun(db)
}
14 changes: 14 additions & 0 deletions Backend/cmd/migrate/game_seeder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build migrate

package main

import (
"github.com/Game-as-a-Service/The-Message/config"
"github.com/Game-as-a-Service/The-Message/database/seeders"
_ "github.com/joho/godotenv/autoload"
)

func main() {
db := config.NewDatabase()
seeders.Run(db)
}
5 changes: 5 additions & 0 deletions Backend/database/seeders/database_seeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ import (

func Run(db *gorm.DB) {
SeederCards(db)
SeederGameWithPlayers(db)
}

func OnlyCardsRun(db *gorm.DB) {
SeederCards(db)
}
46 changes: 46 additions & 0 deletions Backend/database/seeders/game_seeder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package seeders

import (
"math/rand"

"github.com/Game-as-a-Service/The-Message/enums"
"github.com/Game-as-a-Service/The-Message/service/repository"
"github.com/go-faker/faker/v4"
"gorm.io/gorm"
)

func SeederGameWithPlayers(db *gorm.DB) {
// Get all cards
var cards []*repository.Card
_ = db.Find(&cards)

// Fake game data
game := &repository.Game{
RoomID: faker.UUIDDigit(),
Status: enums.ActionCardStage,
Players: []repository.Player{},
}

// Fake player count random 1~3
playerCount := rand.Intn(3) + 1

// Fake players data
for i := 0; i < playerCount; i++ {
player := repository.Player{
UserID: faker.UUIDDigit(),
Name: faker.FirstName(),
PlayerCards: []repository.PlayerCard{},
}

// Each player gets 3 cards
for j := 0; j < 3; j++ {
player.PlayerCards = append(player.PlayerCards, repository.PlayerCard{
CardID: cards[i*3+j].ID,
Type: "hand",
})
}
game.Players = append(game.Players, player)
}

_ = db.Create(&game)
}
2 changes: 1 addition & 1 deletion Backend/tests/e2e/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (suite *IntegrationTestSuite) SetupTest() {
//Fixme Run db refresh and seeders
config.RunRefresh()
db := config.NewDatabase()
seeders.Run(db)
seeders.OnlyCardsRun(db)
}

func (suite *IntegrationTestSuite) TearDownTest() {
Expand Down

0 comments on commit bc71d39

Please sign in to comment.