Skip to content

Commit

Permalink
fix failing tests and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PapePathe committed Dec 24, 2023
1 parent 0283306 commit 9b6f0b1
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 37 deletions.
6 changes: 3 additions & 3 deletions gametake/gametake_tout.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ func (t Tout) EvaluateCard(card cards.Card) (int, bool) {
func (t Tout) Winner(a cards.Card, b cards.Card) cards.Card {
aValue, bValue := t.EvaluateCardForWin(a), t.EvaluateCardForWin(b)

if a.Couleur == b.Couleur && aValue > bValue {
return a
if a.Couleur == b.Couleur && aValue < bValue {
return b
}

return b
return a
}

func (t Tout) EvaluateCardForWin(card cards.Card) int {
Expand Down
9 changes: 6 additions & 3 deletions gametake/gametake_tout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ func TestWinnerTOUT(t *testing.T) {
assert.Equal(t, cards.HuitCoeur, winner)

winner = TOUT.Winner(cards.RoiPique, cards.HuitCoeur)
assert.Equal(t, cards.HuitCoeur, winner)
assert.Equal(t, cards.RoiPique, winner)

winner = TOUT.Winner(cards.RoiPique, cards.DixCarreau)
assert.Equal(t, cards.RoiPique, winner)

winner = TOUT.Winner(cards.RoiPique, cards.DamePique)
assert.Equal(t, cards.RoiPique, winner)
Expand All @@ -193,8 +196,8 @@ func TestWinnerTOUT(t *testing.T) {
assert.Equal(t, cards.ValetCarreau, winner)

winner = TOUT.Winner(cards.ValetTrefle, cards.NeufCarreau)
assert.Equal(t, cards.NeufCarreau, winner)
assert.Equal(t, cards.ValetTrefle, winner)

winner = TOUT.Winner(cards.NeufCarreau, cards.ValetTrefle)
assert.Equal(t, cards.ValetTrefle, winner)
assert.Equal(t, cards.NeufCarreau, winner)
}
2 changes: 1 addition & 1 deletion pkg/game/deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (d *Deck) NextPlayer() int {
return d.players[d.cardscount]
}

return 0
return d.winner
}

func (d *Deck) Score() (int, int) {
Expand Down
17 changes: 8 additions & 9 deletions pkg/game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func NewGame() *Game {
ring: [4]int{0, 1, 2, 3},
Takechannel: make(chan player.PlayEvent, 100),
PlayEventDetailsChannel: make(chan player.PlayEventDetails, 320),
quitChannel: make(chan bool),
quitChannel: make(chan bool, 1),
}

return &newGame
Expand All @@ -76,8 +76,10 @@ func (g *Game) StartPlayChannel() {
select {
case pc := <-g.Takechannel:
log.Trace().Interface("player sent a card", pc)
err := g.PlayCardNext(pc.PlayerID, pc.Card)
log.Err(err).Str("Error playing card", pc.Card.String())
if g.nombrePli < 8 {
err := g.PlayCardNext(pc.PlayerID, pc.Card)
log.Err(err).Str("Error playing card", pc.Card.String())
}
case evt := <-g.PlayEventDetailsChannel:
publisher := broker.NewPublisher([]string{"localhost:19092"}, true)
msg, _ := evt.AsKafkaMessage("play.event.details")
Expand Down Expand Up @@ -137,19 +139,15 @@ func (g *Game) PlayCardNext(playerID int, c cards.Card) error {
g.scoreTeamB += b

g.nombrePli++

if g.nombrePli < 7 {
g.Decks[g.nombrePli] = NewDeck(g.ring, g.take)
}

g.broadcastGameDeck()
} else {
g.broadcastGameDeck()
}
log.Trace().Interface("Game ring", g.ring).Int("Nombre de plis", g.nombrePli).Msg("")

if g.nombrePli > 7 {
g.quitChannel <- true
} else {
g.broadcastGameDeck()
}

return nil
Expand Down Expand Up @@ -319,5 +317,6 @@ func (g *Game) DispatchCards() error {
}
}

g.broadcastGameDeck()
return nil
}
34 changes: 34 additions & 0 deletions pkg/game/game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package game
import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -375,10 +376,31 @@ func TestPlayCardNext(t *testing.T) {
})

fmt.Println("take ", game1.GetTake().Name(), "deck", pli, "winner:", winner)

for i := 1; i < 8; i++ {
for _, pid := range game1.ring {
p := game1.GetPlayers()[pid]
err := game1.PlayCardNext(p.GetID(), p.GetPlayingHand().Cards[i])
require.NoError(t, err)
}
}
})
}
}

func TestPlayingWithMachines(t *testing.T) {
t.Parallel()

game1 := setupGameWithMachines(4)

err := game1.AddTake(game1.GetPlayers()[0].GetID(), gametake.TOUT)
require.NoError(t, err)
assert.True(t, game1.takesComplete())
go game1.StartPlayChannel()

time.Sleep(time.Second * 10)
}

func TestTakesComplete(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -429,6 +451,18 @@ func TestTakesComplete(t *testing.T) {
})
}

func setupGameWithMachines(playersCount int) *Game {
game1 := NewGame()
for i := 0; i < playersCount; i++ {
err := game1.AddPlayer(player.NewMachinePlayer(time.Millisecond))
if err != nil {
fmt.Println("ERROR SETTING UP A TEST GAME")
}
}

return game1
}

func setupGame(playersCount int) *Game {
game1 := NewGame()
for i := 0; i < playersCount; i++ {
Expand Down
14 changes: 14 additions & 0 deletions pkg/player/machine_deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (m machineDeck) FindWinner() cards.Card {
if c.IsNotEmpty() && winningCard.IsNotEmpty() {
winner := m.gametake.Winner(winningCard, c)

log.Trace().Str("previous winner:", winningCard.String()).Str("winner", winner.String()).Str("candidate:", c.String()).Msg("")
if winner == c {
winningCard = c
}
Expand All @@ -31,6 +32,19 @@ func (m machineDeck) FindWinner() cards.Card {
return winningCard
}

func (m machineDeck) RemainingCardsCount() int {
count := 0

for _, c := range m.hand.Cards {
if c.IsNotEmpty() {
count++
}
}

return count

}

func (m machineDeck) WinningOrLowestCard() cards.Card {
found, pCards := m.hand.HasColor(m.cards[0].Couleur)

Expand Down
50 changes: 48 additions & 2 deletions pkg/player/machine_deck_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,53 @@
package player

import "testing"
import (
"testing"

func TestFindWinner(t *testing.T) {
"github.com/stretchr/testify/assert"
"pathe.co/zinx/gametake"
"pathe.co/zinx/pkg/cards"
)

func TestFindWinnerOneCardOnDeck(t *testing.T) {
t.Parallel()

m := machineDeck{
cards: [4]cards.Card{cards.ValetCarreau},
gametake: gametake.TOUT,
hand: PlayingHand{Cards: []cards.Card{}},
}
assert.Equal(t, cards.ValetCarreau, m.FindWinner())
}

func TestFindWinnerTwoCardsOnDeck(t *testing.T) {
t.Parallel()

m := machineDeck{
cards: [4]cards.Card{cards.SeptCarreau, cards.NeufCarreau},
gametake: gametake.TOUT,
hand: PlayingHand{Cards: []cards.Card{}},
}
assert.Equal(t, cards.NeufCarreau, m.FindWinner())
}

func TestFindWinnerThreeCardsOnDeck(t *testing.T) {
t.Parallel()

m := machineDeck{
cards: [4]cards.Card{cards.SeptCarreau, cards.NeufCarreau, cards.ValetCoeur},
gametake: gametake.TOUT,
hand: PlayingHand{Cards: []cards.Card{}},
}
assert.Equal(t, cards.NeufCarreau, m.FindWinner())
}

func TestFindWinnerFourCardsOnDeck(t *testing.T) {
t.Parallel()

m := machineDeck{
cards: [4]cards.Card{cards.SeptCarreau, cards.NeufCarreau, cards.ValetCoeur, cards.ValetCarreau},
gametake: gametake.TOUT,
hand: PlayingHand{Cards: []cards.Card{}},
}
assert.Equal(t, cards.ValetCarreau, m.FindWinner())
}
35 changes: 19 additions & 16 deletions pkg/player/machine_player.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,26 @@ func init() {
}

type MachinePlayer struct {
Hand Hand `json:"hand"`
PlayingHand PlayingHand `json:"playingHand"`
Take *gametake.GameTake `json:"take"`
ID int `json:"id"`
Hand Hand `json:"hand"`
PlayingHand PlayingHand `json:"playingHand"`
Take *gametake.GameTake `json:"take"`
ID int `json:"id"`
waitDuration time.Duration
}

func NewMachinePlayer() *MachinePlayer {
func NewMachinePlayer(wDuration time.Duration) *MachinePlayer {
return &MachinePlayer{
Hand: Hand{Cards: [5]cards.Card{}},
PlayingHand: PlayingHand{Cards: []cards.Card{}},
Take: nil,
ID: 0,
Hand: Hand{Cards: [5]cards.Card{}},
PlayingHand: PlayingHand{Cards: []cards.Card{}},
Take: nil,
ID: 0,
waitDuration: wDuration,
}
}

func (p *MachinePlayer) BroadCastGameDeck(e ReceiveDeckMsg) {
if e.NextPlayer == p.ID {
time.Sleep(time.Second)
time.Sleep(p.waitDuration)
log.Debug().Int("Machine Player ID", p.ID).Str("Hand", p.PlayingHand.String()).Msg("It's my turn to play")
log.Trace().Msg(fmt.Sprintf("Deck: %s %s %s %s", e.Deck[0], e.Deck[1], e.Deck[2], e.Deck[3]))

Expand All @@ -47,12 +49,13 @@ func (p *MachinePlayer) BroadCastGameDeck(e ReceiveDeckMsg) {
c = mDeck.WinningOrLowestCard()
log.Debug().Int("Machine Player ID", p.ID).Msg("==> Found card")
} else {
c = mDeck.LowestCard()
log.Debug().Int("Machine Player ID", p.ID).Str("Card", c.String()).Msg("Going to play the lowest card since player does not have card")
}

if !c.IsNotEmpty() {
panic(c)
if mDeck.RemainingCardsCount() == 1 {
log.Trace().Msg("Player has only one card remaining on hand")
}

c = mDeck.LowestCard()
log.Debug().Int("Machine Player ID", p.ID).Str("Card", c.String()).Msg("Going to play the lowest card since player does not have card that can win")
}

e.PlayChannel <- PlayEvent{Card: c, PlayerID: p.GetID()}
Expand All @@ -63,7 +66,7 @@ func (p *MachinePlayer) BroadCastGameDeck(e ReceiveDeckMsg) {
Gametake: e.Take,
}

time.Sleep(time.Second)
time.Sleep(p.waitDuration)
}
}

Expand Down
6 changes: 3 additions & 3 deletions znet/server_socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ func (s *SocketHandler) StartPlayerRegistration(c *websocket.Conn) {
log.Error().Err(err).Msg("error writing message to socket")
}

s.g.AddPlayer(player.NewMachinePlayer())
s.g.AddPlayer(player.NewMachinePlayer())
s.g.AddPlayer(player.NewMachinePlayer())
s.g.AddPlayer(player.NewMachinePlayer(time.Second))
s.g.AddPlayer(player.NewMachinePlayer(time.Second))
s.g.AddPlayer(player.NewMachinePlayer(time.Second))

go s.g.StartPlayChannel()
}
Expand Down
8 changes: 8 additions & 0 deletions znet/server_socket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"pathe.co/zinx/gametake"
"pathe.co/zinx/pkg/cards"
"pathe.co/zinx/pkg/game"
"pathe.co/zinx/pkg/player"
)

func TestIndexPage(t *testing.T) {
Expand Down Expand Up @@ -130,8 +131,15 @@ func TestNewSocketHandler(t *testing.T) {
_, rawmsg, err = conn.ReadMessage()
require.NoError(t, err)

var deckMsg = player.ReceiveDeckMsg{}
err = json.Unmarshal(rawmsg, &deckMsg)
require.NoError(t, err)
assert.Equal(t, player.ReceiveDeck, deckMsg.ID)

_, rawmsg, err = conn.ReadMessage()
var playMsg game.ReceivePlayingHandMsg

fmt.Println(string(rawmsg))
err = json.Unmarshal(rawmsg, &playMsg)
require.NoError(t, err)
assert.Equal(t, game.ReceivePlayingHand, playMsg.ID)
Expand Down

0 comments on commit 9b6f0b1

Please sign in to comment.