Skip to content

Commit

Permalink
Fix: Removed the game ID that is no longer needed from PlayCard and T…
Browse files Browse the repository at this point in the history
…ransmitIntelligence
  • Loading branch information
KazeNoYumeX committed Mar 21, 2024
1 parent bc71d39 commit 1e928d8
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 135 deletions.
8 changes: 3 additions & 5 deletions Backend/service/delivery/http/v1/player_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ func (p *PlayerHandler) PlayCard(c *gin.Context) {
"next_player": game.CurrentPlayerID,
}

c.JSON(http.StatusOK, gin.H{
"result": true,
})
c.JSON(http.StatusOK, gin.H{})
}

// TransmitIntelligence godoc
Expand Down Expand Up @@ -104,13 +102,13 @@ func (p *PlayerHandler) TransmitIntelligence(c *gin.Context) {
}

// Check card_id exists in player_cards
exist, err := p.playerService.CheckPlayerCardExist(c, playerId, player.GameID, req.CardID)
exist, err := p.playerService.CheckPlayerCardExist(c, playerId, req.CardID)
if err != nil || !exist {
c.JSON(http.StatusInternalServerError, gin.H{"message": "Card not found"})
return
}

ret, err := p.playerService.TransmitIntelligenceCard(c, playerId, player.GameID, req.CardID)
ret, err := p.playerService.TransmitIntelligenceCard(c, playerId, req.CardID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
return
Expand Down
8 changes: 4 additions & 4 deletions Backend/service/repository/mysql/player_card_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ func (p PlayerCardRepository) DeletePlayerCard(ctx context.Context, id uint) err
return nil
}

func (p PlayerCardRepository) DeletePlayerCardByPlayerIdAndCardId(ctx context.Context, playerId uint, gameId uint, cardId uint) (bool, error) {
func (p PlayerCardRepository) DeletePlayerCardByPlayerIdAndCardId(ctx context.Context, playerId uint, cardId uint) (bool, error) {
card := new(repository.PlayerCard)

result := p.db.Delete(&card, "player_id = ? AND game_id = ? AND card_id = ?", playerId, gameId, cardId)
result := p.db.Delete(&card, "player_id = ? AND card_id = ?", playerId, cardId)
if result.Error != nil {
return false, result.Error
}

return true, nil
}

func (p *PlayerCardRepository) ExistPlayerCardByPlayerIdAndCardId(ctx context.Context, playerId uint, gameId uint, cardId uint) (bool, error) {
func (p *PlayerCardRepository) ExistPlayerCardByPlayerIdAndCardId(ctx context.Context, playerId uint, cardId uint) (bool, error) {
var card repository.PlayerCard
result := p.db.First(&card, "player_id = ? AND game_id = ? AND card_id = ?", playerId, gameId, cardId)
result := p.db.First(&card, "player_id = ? AND card_id = ?", playerId, cardId)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return false, nil
Expand Down
4 changes: 2 additions & 2 deletions Backend/service/repository/player_card_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type PlayerCardRepository interface {
GetPlayerCardsByGameId(ctx context.Context, id uint) ([]*PlayerCard, error)
CreatePlayerCard(ctx context.Context, card *PlayerCard) (*PlayerCard, error)
DeletePlayerCard(ctx context.Context, id uint) error
DeletePlayerCardByPlayerIdAndCardId(ctx context.Context, playerId uint, gameId uint, cardId uint) (bool, error)
ExistPlayerCardByPlayerIdAndCardId(ctx context.Context, playerId uint, gameId uint, cardId uint) (bool, error)
DeletePlayerCardByPlayerIdAndCardId(ctx context.Context, playerId uint, cardId uint) (bool, error)
ExistPlayerCardByPlayerIdAndCardId(ctx context.Context, playerId uint, cardId uint) (bool, error)
GetPlayerCards(ctx context.Context, playerCard *PlayerCard) (*[]PlayerCard, error)
}
8 changes: 4 additions & 4 deletions Backend/service/service/player_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ func (p *PlayerService) CanPlayCard(c context.Context, player *repository.Player
return true, nil
}

func (p *PlayerService) CheckPlayerCardExist(c context.Context, playerId uint, gameId uint, cardId uint) (bool, error) {
exist, err := p.PlayerCardRepo.ExistPlayerCardByPlayerIdAndCardId(c, playerId, gameId, cardId)
func (p *PlayerService) CheckPlayerCardExist(c context.Context, playerId uint, cardId uint) (bool, error) {
exist, err := p.PlayerCardRepo.ExistPlayerCardByPlayerIdAndCardId(c, playerId, cardId)

if err != nil {
return false, err
Expand Down Expand Up @@ -174,7 +174,7 @@ func (p *PlayerService) PlayCard(c context.Context, playerId uint, cardId uint)
return game, &handCard.Card, nil
}

func (p *PlayerService) TransmitIntelligenceCard(c context.Context, playerId uint, gameId uint, cardId uint) (bool, error) {
func (p *PlayerService) TransmitIntelligenceCard(c context.Context, playerId uint, cardId uint) (bool, error) {
player, err := p.PlayerRepo.GetPlayerWithGamePlayersAndPlayerCardsCard(c, playerId)
if err != nil {
return false, err
Expand All @@ -190,7 +190,7 @@ func (p *PlayerService) TransmitIntelligenceCard(c context.Context, playerId uin
return false, err
}

ret, err := p.PlayerCardRepo.DeletePlayerCardByPlayerIdAndCardId(c, playerId, gameId, cardId)
ret, err := p.PlayerCardRepo.DeletePlayerCardByPlayerIdAndCardId(c, playerId, cardId)
if err != nil {
return false, err
}
Expand Down
8 changes: 4 additions & 4 deletions Backend/tests/e2e/game_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

func (suite *IntegrationTestSuite) TestStartGameE2E() {
// Set the cards
seeders.SeederCards(suite.db)
seeders.OnlyCardsRun(suite.db)

api := "/api/v1/games"

Expand Down Expand Up @@ -50,7 +50,7 @@ func (suite *IntegrationTestSuite) TestStartGameE2E() {
})

suite.T().Run("it can fail when player count less than 3", func(t *testing.T) {
num := rand.Intn(3)
num := rand.Intn(2) + 1

var players []request.PlayerInfo
for i := 0; i < num; i++ {
Expand Down Expand Up @@ -135,13 +135,13 @@ func (suite *IntegrationTestSuite) TestStartGameE2E() {
assert.Equal(t, url, resBody["url"])

// Assert Game and Players
game := new(repository.Game)
game := &repository.Game{}
_ = suite.db.First(&game, "id = ?", 1)

assert.Equal(t, roomID, game.RoomID)
assert.Equal(t, enums.ActionCardStage, game.Status)

gamePlayers := new([]repository.Player)
gamePlayers := &[]repository.Player{}
_ = suite.db.Find(&gamePlayers, "game_id = ?", game.ID)

assert.Equal(t, num, len(*gamePlayers))
Expand Down
Loading

0 comments on commit 1e928d8

Please sign in to comment.