From df1d14c24cb6056f3d8f1c58d8bee924340aa609 Mon Sep 17 00:00:00 2001 From: Danilo Jakob Date: Tue, 19 Nov 2024 22:11:12 +0100 Subject: [PATCH] feat(server): updated module name to adhere to golang standard --- server/cmd/main.go | 49 +++ server/cmd/{triffgonix => }/main_test.go | 0 server/cmd/triffgonix/main.go | 50 --- server/go.mod | 4 +- server/internal/{triffgonix => }/api/api.go | 24 +- .../internal/{triffgonix => }/api/api_test.go | 4 +- .../internal/{triffgonix => }/api/dto/data.go | 4 +- .../{triffgonix => }/api/socket/data.go | 0 .../{triffgonix => }/api/socket/hub.go | 6 +- .../{triffgonix => }/api/socket/hub_test.go | 2 +- .../{triffgonix => }/dart/engine/engine.go | 4 +- .../dart/engine/engine_test.go | 2 +- .../dart/engine/x01/x01engine.go | 7 +- .../dart/engine/x01/x01engine_test.go | 383 ++++++++++++++++++ .../{triffgonix => }/database/database.go | 2 +- .../database/database_test.go | 2 +- .../internal/{triffgonix => }/domain/data.go | 4 +- .../internal/{triffgonix => }/models/model.go | 0 .../dart/engine/x01/x01engine_test.go | 383 ------------------ 19 files changed, 466 insertions(+), 464 deletions(-) create mode 100644 server/cmd/main.go rename server/cmd/{triffgonix => }/main_test.go (100%) delete mode 100644 server/cmd/triffgonix/main.go rename server/internal/{triffgonix => }/api/api.go (82%) rename server/internal/{triffgonix => }/api/api_test.go (87%) rename server/internal/{triffgonix => }/api/dto/data.go (91%) rename server/internal/{triffgonix => }/api/socket/data.go (100%) rename server/internal/{triffgonix => }/api/socket/hub.go (94%) rename server/internal/{triffgonix => }/api/socket/hub_test.go (96%) rename server/internal/{triffgonix => }/dart/engine/engine.go (97%) rename server/internal/{triffgonix => }/dart/engine/engine_test.go (98%) rename server/internal/{triffgonix => }/dart/engine/x01/x01engine.go (94%) create mode 100644 server/internal/dart/engine/x01/x01engine_test.go rename server/internal/{triffgonix => }/database/database.go (94%) rename server/internal/{triffgonix => }/database/database_test.go (91%) rename server/internal/{triffgonix => }/domain/data.go (96%) rename server/internal/{triffgonix => }/models/model.go (100%) delete mode 100644 server/internal/triffgonix/dart/engine/x01/x01engine_test.go diff --git a/server/cmd/main.go b/server/cmd/main.go new file mode 100644 index 0000000..df62fa4 --- /dev/null +++ b/server/cmd/main.go @@ -0,0 +1,49 @@ +package main + +import ( + "github.com/DaniloMurer/triffgonix/server/internal/api" + "github.com/DaniloMurer/triffgonix/server/internal/database" + "github.com/gin-contrib/cors" + "github.com/gin-gonic/gin" +) + +// sets up the router +func setupRouter() *gin.Engine { + database.AutoMigrate() + + router := gin.Default() + + corsConfig := cors.DefaultConfig() + corsConfig.AllowAllOrigins = true + corsConfig.AllowCredentials = true + corsConfig.AllowHeaders = []string{"Content-Type", "Accept", "Authorization", "Origin"} + + router.Use(cors.New(corsConfig)) + + router.GET("/", func(c *gin.Context) { + c.String(200, "Welcome") + }) + + group := router.Group("/api") + { + group.POST("/user", api.CreatePlayer) + group.GET("/user", api.GetPlayers) + group.POST("/game", api.CreateGame) + group.GET("/game", api.GetGames) + } + socketGroup := router.Group("/ws") + { + socketGroup.GET("/dart/:gameId", api.HandleDartWebSocket) + socketGroup.GET("/dart", api.HandleGeneralWebsocket) + } + + return router +} + +func main() { + router := setupRouter() + err := router.Run("0.0.0.0:8080") + if err != nil { + panic("Error while starting server") + } +} diff --git a/server/cmd/triffgonix/main_test.go b/server/cmd/main_test.go similarity index 100% rename from server/cmd/triffgonix/main_test.go rename to server/cmd/main_test.go diff --git a/server/cmd/triffgonix/main.go b/server/cmd/triffgonix/main.go deleted file mode 100644 index 3fecf79..0000000 --- a/server/cmd/triffgonix/main.go +++ /dev/null @@ -1,50 +0,0 @@ -package main - -import ( - "server/internal/triffgonix/api" - "server/internal/triffgonix/database" - - "github.com/gin-contrib/cors" - "github.com/gin-gonic/gin" -) - -// sets up the router -func setupRouter() *gin.Engine { - database.AutoMigrate() - - router := gin.Default() - - corsConfig := cors.DefaultConfig() - corsConfig.AllowAllOrigins = true - corsConfig.AllowCredentials = true - corsConfig.AllowHeaders = []string{"Content-Type", "Accept", "Authorization", "Origin"} - - router.Use(cors.New(corsConfig)) - - router.GET("/", func(c *gin.Context) { - c.String(200, "Welcome") - }) - - group := router.Group("/api") - { - group.POST("/user", api.CreatePlayer) - group.GET("/user", api.GetPlayers) - group.POST("/game", api.CreateGame) - group.GET("/game", api.GetGames) - } - socketGroup := router.Group("/ws") - { - socketGroup.GET("/dart/:gameId", api.HandleDartWebSocket) - socketGroup.GET("/dart", api.HandleGeneralWebsocket) - } - - return router -} - -func main() { - router := setupRouter() - err := router.Run("0.0.0.0:8080") - if err != nil { - panic("Error while starting server") - } -} diff --git a/server/go.mod b/server/go.mod index acdb89a..baffcc4 100644 --- a/server/go.mod +++ b/server/go.mod @@ -1,4 +1,4 @@ -module server +module github.com/DaniloMurer/triffgonix/server go 1.23.0 @@ -9,6 +9,7 @@ require github.com/gin-contrib/cors v1.7.2 require gorm.io/gorm v1.25.11 require ( + github.com/google/uuid v1.6.0 github.com/gorilla/websocket v1.5.3 github.com/stretchr/testify v1.9.0 gorm.io/driver/sqlite v1.5.6 @@ -26,7 +27,6 @@ require ( github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.20.0 // indirect github.com/goccy/go-json v0.10.2 // indirect - github.com/google/uuid v1.6.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/json-iterator/go v1.1.12 // indirect diff --git a/server/internal/triffgonix/api/api.go b/server/internal/api/api.go similarity index 82% rename from server/internal/triffgonix/api/api.go rename to server/internal/api/api.go index 2406bf1..bd3b7bc 100644 --- a/server/internal/triffgonix/api/api.go +++ b/server/internal/api/api.go @@ -1,14 +1,14 @@ package api import ( + "github.com/DaniloMurer/triffgonix/server/internal/api/dto" + socket2 "github.com/DaniloMurer/triffgonix/server/internal/api/socket" + "github.com/DaniloMurer/triffgonix/server/internal/dart/engine" + "github.com/DaniloMurer/triffgonix/server/internal/dart/engine/x01" + "github.com/DaniloMurer/triffgonix/server/internal/database" + "github.com/DaniloMurer/triffgonix/server/internal/models" + "github.com/DaniloMurer/triffgonix/server/pkg/logging" "net/http" - "server/internal/triffgonix/api/dto" - "server/internal/triffgonix/api/socket" - "server/internal/triffgonix/dart/engine" - "server/internal/triffgonix/dart/engine/x01" - "server/internal/triffgonix/database" - "server/internal/triffgonix/models" - "server/pkg/logging" "strconv" "github.com/gin-gonic/gin" @@ -17,7 +17,7 @@ import ( var ( upgrader = websocket.Upgrader{} - hubs = map[string]socket.Hub{} + hubs = map[string]socket2.Hub{} generalConnections []*websocket.Conn ) @@ -36,14 +36,14 @@ func HandleDartWebSocket(c *gin.Context) { } gameId := c.Param("gameId") // get message from socket - var message socket.IncomingMessage + var message socket2.IncomingMessage err = conn.ReadJSON(&message) if err != nil { logger.Error("error while reading from socket connection: %v", err) return } switch *message.Type { - case socket.Handshake: + case socket2.Handshake: hub, exists := hubs[gameId] if exists { hub.RegisterNewClient(conn) @@ -120,7 +120,7 @@ func CreateGame(c *gin.Context) { Players: &players, Engine: x01.New(newGame.StartingScore), } - newHub := socket.Hub{Id: savedGame.Id, Clients: map[*socket.Client]bool{}, Game: game} + newHub := socket2.Hub{Id: savedGame.Id, Clients: map[*socket2.Client]bool{}, Game: game} hubs[strconv.FormatUint(uint64(savedGame.Id), 10)] = newHub broadcastNewGame(savedGame) c.JSON(http.StatusCreated, &savedGame) @@ -134,7 +134,7 @@ func GetGames(c *gin.Context) { func broadcastNewGame(newGame *models.Game) { game := dto.Game{} game.FromEntity(newGame) - message := socket.OutgoingMessage{Type: socket.NewGame, Content: game} + message := socket2.OutgoingMessage{Type: socket2.NewGame, Content: game} for _, conn := range generalConnections { err := conn.WriteJSON(message) if err != nil { diff --git a/server/internal/triffgonix/api/api_test.go b/server/internal/api/api_test.go similarity index 87% rename from server/internal/triffgonix/api/api_test.go rename to server/internal/api/api_test.go index ccf4f59..d2e0a34 100644 --- a/server/internal/triffgonix/api/api_test.go +++ b/server/internal/api/api_test.go @@ -1,10 +1,10 @@ package api import ( + "github.com/DaniloMurer/triffgonix/server/internal/database" + "github.com/DaniloMurer/triffgonix/server/internal/models" "github.com/google/uuid" "net/http/httptest" - "server/internal/triffgonix/database" - "server/internal/triffgonix/models" "testing" "github.com/gin-gonic/gin" diff --git a/server/internal/triffgonix/api/dto/data.go b/server/internal/api/dto/data.go similarity index 91% rename from server/internal/triffgonix/api/dto/data.go rename to server/internal/api/dto/data.go index 521f755..6d0fabb 100644 --- a/server/internal/triffgonix/api/dto/data.go +++ b/server/internal/api/dto/data.go @@ -1,8 +1,8 @@ package dto import ( - "server/internal/triffgonix/domain" - "server/internal/triffgonix/models" + "github.com/DaniloMurer/triffgonix/server/internal/domain" + "github.com/DaniloMurer/triffgonix/server/internal/models" ) type Players struct { diff --git a/server/internal/triffgonix/api/socket/data.go b/server/internal/api/socket/data.go similarity index 100% rename from server/internal/triffgonix/api/socket/data.go rename to server/internal/api/socket/data.go diff --git a/server/internal/triffgonix/api/socket/hub.go b/server/internal/api/socket/hub.go similarity index 94% rename from server/internal/triffgonix/api/socket/hub.go rename to server/internal/api/socket/hub.go index df9d99d..f5d82de 100644 --- a/server/internal/triffgonix/api/socket/hub.go +++ b/server/internal/api/socket/hub.go @@ -1,10 +1,10 @@ package socket import ( + "github.com/DaniloMurer/triffgonix/server/internal/dart/engine" + "github.com/DaniloMurer/triffgonix/server/internal/domain" + "github.com/DaniloMurer/triffgonix/server/pkg/logging" "github.com/gorilla/websocket" - "server/internal/triffgonix/dart/engine" - "server/internal/triffgonix/domain" - "server/pkg/logging" ) var logger = logging.NewLogger() diff --git a/server/internal/triffgonix/api/socket/hub_test.go b/server/internal/api/socket/hub_test.go similarity index 96% rename from server/internal/triffgonix/api/socket/hub_test.go rename to server/internal/api/socket/hub_test.go index ae12f7a..31690bb 100644 --- a/server/internal/triffgonix/api/socket/hub_test.go +++ b/server/internal/api/socket/hub_test.go @@ -1,7 +1,7 @@ package socket import ( - "server/pkg/logging" + "github.com/DaniloMurer/triffgonix/server/pkg/logging" "testing" ) diff --git a/server/internal/triffgonix/dart/engine/engine.go b/server/internal/dart/engine/engine.go similarity index 97% rename from server/internal/triffgonix/dart/engine/engine.go rename to server/internal/dart/engine/engine.go index 493970d..9db86be 100644 --- a/server/internal/triffgonix/dart/engine/engine.go +++ b/server/internal/dart/engine/engine.go @@ -1,8 +1,8 @@ package engine import ( - "server/internal/triffgonix/api/dto" - "server/internal/triffgonix/domain" + "github.com/DaniloMurer/triffgonix/server/internal/api/dto" + "github.com/DaniloMurer/triffgonix/server/internal/domain" ) type Engine interface { diff --git a/server/internal/triffgonix/dart/engine/engine_test.go b/server/internal/dart/engine/engine_test.go similarity index 98% rename from server/internal/triffgonix/dart/engine/engine_test.go rename to server/internal/dart/engine/engine_test.go index 3791185..68bf076 100644 --- a/server/internal/triffgonix/dart/engine/engine_test.go +++ b/server/internal/dart/engine/engine_test.go @@ -1,7 +1,7 @@ package engine import ( - "server/internal/triffgonix/domain" + "github.com/DaniloMurer/triffgonix/server/internal/domain" "testing" ) diff --git a/server/internal/triffgonix/dart/engine/x01/x01engine.go b/server/internal/dart/engine/x01/x01engine.go similarity index 94% rename from server/internal/triffgonix/dart/engine/x01/x01engine.go rename to server/internal/dart/engine/x01/x01engine.go index 96a6327..358b6ba 100644 --- a/server/internal/triffgonix/dart/engine/x01/x01engine.go +++ b/server/internal/dart/engine/x01/x01engine.go @@ -1,9 +1,10 @@ +// Package x01 is the implementation of the X01 dart game mode package x01 import ( - "server/internal/triffgonix/dart/engine" - "server/internal/triffgonix/domain" - "server/pkg/logging" + "github.com/DaniloMurer/triffgonix/server/internal/dart/engine" + "github.com/DaniloMurer/triffgonix/server/internal/domain" + "github.com/DaniloMurer/triffgonix/server/pkg/logging" ) var logger = logging.NewLogger() diff --git a/server/internal/dart/engine/x01/x01engine_test.go b/server/internal/dart/engine/x01/x01engine_test.go new file mode 100644 index 0000000..25a4599 --- /dev/null +++ b/server/internal/dart/engine/x01/x01engine_test.go @@ -0,0 +1,383 @@ +package x01 + +import ( + "fmt" + "github.com/DaniloMurer/triffgonix/server/internal/dart/engine" + "github.com/DaniloMurer/triffgonix/server/internal/domain" + "testing" +) + +func TestX01Engine_RegisterThrow(t *testing.T) { + player := engine.Player{Value: &domain.Player{Id: 1}, Turns: []engine.Turn{}} + + game := engine.Game{Engine: New(301), Players: &engine.Players{}} + + throw := domain.Throw{Id: 1, Points: 5, Multiplicator: 1, PlayerId: player.Value.Id} + game.Players.Add(&player) + + game.Engine.RegisterThrow(&throw, game.Players) + if playerThrows := game.Engine.GetPlayerThrows(&player); len(*playerThrows) == 0 { + t.Fatalf(`ERROR: Throws for given player should be 1, instead it's zero`) + } + + if len(game.Players.CurrentPlayer.Turns) != 1 { + t.Fatalf("ERROR: expected only on turn, since there was till space for adding a throw. instead a new turn was created") + } +} + +func TestPlayers_Add(t *testing.T) { + player := engine.Player{Value: &domain.Player{PlayerName: "1", Id: 1}, Turns: []engine.Turn{}} + player2 := engine.Player{Value: &domain.Player{PlayerName: "2", Id: 2}, Turns: []engine.Turn{}} + game := engine.Game{Players: &engine.Players{}, Engine: New(301)} + + game.Players.Add(&player) + game.Players.Add(&player2) + + // testing adding players to linked list correctly + if game.Players.Head.Value.Id != player.Value.Id { + t.Fatalf(`ERROR: expected id of head should be 1, instead got: %d`, game.Players.Head.Value.Id) + } + if game.Players.CurrentPlayer.Value.Id != player.Value.Id { + t.Fatalf(`ERROR: expected the id of the current player to be 1, instead got: %d`, game.Players.CurrentPlayer.Value.Id) + } + if game.Players.Tail.Value.Id != player2.Value.Id { + t.Fatalf(`ERROR: expected the id of the tail to be 2, instead got: %d`, game.Players.Tail.Value.Id) + } + // testing if the bidirectional linking of the list is correct + if game.Players.Head.Next.Value.Id != player2.Value.Id { + t.Fatalf(`ERROR: expected the id of the next player after head to be 2, instead got: %d`, game.Players.Head.Next.Value.Id) + } + if game.Players.Head.Previous != nil { + t.Fatalf(`ERROR: expected the previous player from head to be nil, instead got: %v`, game.Players.Head.Previous) + } + if game.Players.Tail.Previous.Value.Id != player.Value.Id { + t.Fatalf(`ERROR: expected the prevous player from the tail to have id 1, instead got: %d`, game.Players.Tail.Previous.Value.Id) + } + if game.Players.Tail.Next != nil { + t.Fatalf(`ERROR: expected the next player from the tail to be nil, instead got: %v`, game.Players.Tail.Next) + } +} + +func TestX01Engine_RegisterThrow_SwitchPlayer(t *testing.T) { + player := engine.Player{Value: &domain.Player{PlayerName: "1", Id: 1}, Turns: []engine.Turn{}} + player2 := engine.Player{Value: &domain.Player{PlayerName: "2", Id: 2}, Turns: []engine.Turn{}} + game := engine.Game{Players: &engine.Players{}, Engine: New(301)} + + game.Players.Add(&player) + game.Players.Add(&player2) + + // testing turn switching after three throws + throw := domain.Throw{Id: 1, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} + throw2 := domain.Throw{Id: 2, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} + throw3 := domain.Throw{Id: 3, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} + + game.Engine.RegisterThrow(&throw, game.Players) + game.Engine.RegisterThrow(&throw2, game.Players) + game.Engine.RegisterThrow(&throw3, game.Players) + + if game.Players.CurrentPlayer.Value.Id != player2.Value.Id { + t.Fatalf(`ERROR: expected the player to change to player2 after three throws. instead got player with id: %d`, game.Players.CurrentPlayer.Value.Id) + } + + game.Engine.RegisterThrow(&throw, game.Players) + game.Engine.RegisterThrow(&throw2, game.Players) + game.Engine.RegisterThrow(&throw3, game.Players) + + if game.Players.CurrentPlayer.Value.Id != player.Value.Id { + t.Fatalf(`ERROR: expected the player to change to player1 after three throws. instead got player with id %d`, game.Players.CurrentPlayer.Value.Id) + } + + game.Engine.RegisterThrow(&throw, game.Players) + game.Engine.RegisterThrow(&throw2, game.Players) + game.Engine.RegisterThrow(&throw3, game.Players) + + if game.Players.CurrentPlayer.Value.Id != player2.Value.Id { + t.Fatalf(`ERROR: expected the player to change to player2 after three throws. instead got player with id: %d`, game.Players.CurrentPlayer.Value.Id) + } +} + +func TestX01Engine_CalculatePlayerScore(t *testing.T) { + player := engine.Player{Value: &domain.Player{PlayerName: "1", Id: 1}, Turns: []engine.Turn{}} + player2 := engine.Player{Value: &domain.Player{PlayerName: "2", Id: 2}, Turns: []engine.Turn{}} + game := engine.Game{Players: &engine.Players{}, Engine: New(301)} + + game.Players.Add(&player) + game.Players.Add(&player2) + + throw := domain.Throw{Id: 1, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} + throw2 := domain.Throw{Id: 2, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} + throw3 := domain.Throw{Id: 3, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} + + game.Engine.RegisterThrow(&throw, game.Players) + game.Engine.RegisterThrow(&throw2, game.Players) + game.Engine.RegisterThrow(&throw3, game.Players) + + if player.Value.Score != 298 { + t.Fatalf(`ERROR: expected player score after three 1 throws to be 298. instead got: %d`, game.Players.GetPreviousPlayer().Value.Score) + } + + throw4 := domain.Throw{Id: 4, Points: 2, Multiplicator: 1, PlayerId: player2.Value.Id} + throw5 := domain.Throw{Id: 5, Points: 2, Multiplicator: 1, PlayerId: player2.Value.Id} + throw6 := domain.Throw{Id: 6, Points: 3, Multiplicator: 3, PlayerId: player2.Value.Id} + + game.Engine.RegisterThrow(&throw4, game.Players) + game.Engine.RegisterThrow(&throw5, game.Players) + game.Engine.RegisterThrow(&throw6, game.Players) + + if player2.Value.Score != 288 { + t.Fatalf(`ERROR: expected player score after three 1 throws to be 298. instead got: %d`, game.Players.GetPreviousPlayer().Value.Score) + } +} + +func TestX01Engine_RegisterThrow_CorrectTurnsAmount(t *testing.T) { + player := engine.Player{Value: &domain.Player{PlayerName: "1", Id: 1}, Turns: []engine.Turn{}} + player2 := engine.Player{Value: &domain.Player{PlayerName: "2", Id: 2}, Turns: []engine.Turn{}} + game := engine.Game{Players: &engine.Players{}, Engine: New(301)} + + game.Players.Add(&player) + game.Players.Add(&player2) + + throw := domain.Throw{Id: 1, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} + throw2 := domain.Throw{Id: 2, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} + throw3 := domain.Throw{Id: 3, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} + + game.Engine.RegisterThrow(&throw, game.Players) + game.Engine.RegisterThrow(&throw2, game.Players) + game.Engine.RegisterThrow(&throw3, game.Players) + + var playerTurns []engine.Turn + for _, turn := range game.Players.GetPreviousPlayer().Turns { + playerTurns = append(playerTurns, turn) + } + + if len(playerTurns) != 1 { + t.Fatalf(`ERROR: expected the player 1 to have one turn that blongs to him, instead got %d`, len(playerTurns)) + } + + game.Engine.RegisterThrow(&throw, game.Players) + game.Engine.RegisterThrow(&throw2, game.Players) + game.Engine.RegisterThrow(&throw3, game.Players) + + var player2Turns []engine.Turn + for _, turn := range game.Players.GetPreviousPlayer().Turns { + player2Turns = append(player2Turns, turn) + } + + if len(player2Turns) != 1 { + t.Fatalf(`ERROR: expected the player 2 to have one turn that blongs to him, instead got %d`, len(player2Turns)) + } + + game.Engine.RegisterThrow(&throw, game.Players) + game.Engine.RegisterThrow(&throw2, game.Players) + game.Engine.RegisterThrow(&throw3, game.Players) + + var player1Turns2 []engine.Turn + for _, turn := range game.Players.GetPreviousPlayer().Turns { + player1Turns2 = append(player1Turns2, turn) + } + + if len(player1Turns2) != 2 { + t.Fatalf(`ERROR: expected the player 1 to have two turn that blongs to him, instead got %d`, len(player1Turns2)) + } +} + +func TestX01Engine_HasAnyPlayerWon(t *testing.T) { + player := engine.Player{Value: &domain.Player{PlayerName: "1", Id: 1, Score: 301}, Turns: []engine.Turn{}} + player2 := engine.Player{Value: &domain.Player{PlayerName: "2", Id: 2, Score: 0}, Turns: []engine.Turn{}} + game := engine.Game{Players: &engine.Players{}, Engine: New(301)} + + game.Players.Add(&player) + game.Players.Add(&player2) + + winningPlayer := game.Engine.HasAnyPlayerWon(game.Players) + if winningPlayer.Value.Id != player2.Value.Id { + t.Fatalf(`ERROR: expected player 2 to win. instead player with id: %d won`, winningPlayer.Value.Id) + } +} + +func TestX01Engine_HasAnyPlayerWon_AfterThrows(t *testing.T) { + player := engine.Player{Value: &domain.Player{PlayerName: "1", Id: 1, Score: 34}, Turns: []engine.Turn{}} + player2 := engine.Player{Value: &domain.Player{PlayerName: "2", Id: 2, Score: 2}, Turns: []engine.Turn{}} + game := engine.Game{Players: &engine.Players{}, Engine: New(301)} + + game.Players.Add(&player) + game.Players.Add(&player2) + + throw := domain.Throw{Id: 1, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} + throw2 := domain.Throw{Id: 2, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} + throw3 := domain.Throw{Id: 3, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} + + game.Engine.RegisterThrow(&throw, game.Players) + if game.Engine.HasAnyPlayerWon(game.Players) != nil { + t.Fatal("ERROR: player 1 wasn't supposed to win") + } + game.Engine.RegisterThrow(&throw2, game.Players) + if game.Engine.HasAnyPlayerWon(game.Players) != nil { + t.Fatal("ERROR: player 1 wasn't supposed to win") + } + game.Engine.RegisterThrow(&throw3, game.Players) + if game.Engine.HasAnyPlayerWon(game.Players) != nil { + t.Fatal("ERROR: player 1 wasn't supposed to win") + } + + throw4 := domain.Throw{Id: 4, Points: 255, Multiplicator: 1, PlayerId: player2.Value.Id} + throw5 := domain.Throw{Id: 5, Points: 37, Multiplicator: 1, PlayerId: player2.Value.Id} + throw6 := domain.Throw{Id: 6, Points: 3, Multiplicator: 3, PlayerId: player2.Value.Id} + + game.Engine.RegisterThrow(&throw4, game.Players) + if game.Engine.HasAnyPlayerWon(game.Players) != nil { + t.Fatal("ERROR: player 2 was not supposed to win yet") + } + game.Engine.RegisterThrow(&throw5, game.Players) + if game.Engine.HasAnyPlayerWon(game.Players) != nil { + t.Fatal("ERROR: player 2 was not supposed to win yet") + } + game.Engine.RegisterThrow(&throw6, game.Players) + if game.Engine.HasAnyPlayerWon(game.Players) == nil { + t.Fatal("ERROR: player 2 was supposed to win") + } +} + +func TestX01Engine_UndoThrow(t *testing.T) { + player := engine.Player{Value: &domain.Player{PlayerName: "1", Id: 1, Score: 34}, Turns: []engine.Turn{}} + game := engine.Game{Players: &engine.Players{}, Engine: New(301)} + + game.Players.Add(&player) + + throw := domain.Throw{Id: 1, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} + + game.Engine.RegisterThrow(&throw, game.Players) + playerThrows := game.Engine.GetPlayerThrows(&player) + if len(*playerThrows) != 1 { + t.Fatal("ERROR: expected player throws to contain one throw") + } + + game.Engine.UndoLastThrow(game.Players) + playerThrows2 := game.Engine.GetPlayerThrows(&player) + + fmt.Printf("trace: playerThrows2 - %+v\n", playerThrows2) + if len(*playerThrows2) != 0 { + t.Fatal("ERROR: expected player throws to contain no throws after undo") + } +} + +func TestX01Engine_UndoThrow_WhenSecondPlayerTurn(t *testing.T) { + player := engine.Player{Value: &domain.Player{PlayerName: "1", Id: 1, Score: 34}, Turns: []engine.Turn{}} + player2 := engine.Player{Value: &domain.Player{PlayerName: "2", Id: 2, Score: 2}, Turns: []engine.Turn{}} + game := engine.Game{Players: &engine.Players{}, Engine: New(301)} + + game.Players.Add(&player) + game.Players.Add(&player2) + + throw := domain.Throw{Id: 1, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} + throw2 := domain.Throw{Id: 2, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} + throw3 := domain.Throw{Id: 3, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} + + game.Engine.RegisterThrow(&throw, game.Players) + game.Engine.RegisterThrow(&throw2, game.Players) + game.Engine.RegisterThrow(&throw3, game.Players) + + if len(*game.Engine.GetPlayerThrows(&player)) != 3 { + t.Fatal("ERROR: expected the first player to have three throws registred") + } + + game.Engine.RegisterThrow(&throw, game.Players) + + if len(*game.Engine.GetPlayerThrows(&player2)) != 1 { + t.Fatal("ERROR: expected the second player to have one throw registred") + } + + game.Engine.UndoLastThrow(game.Players) + + if len(*game.Engine.GetPlayerThrows(&player2)) != 0 { + t.Fatal("ERROR: expected the second player to have zero throws registred after undo") + } + + game.Engine.UndoLastThrow(game.Players) + + if len(*game.Engine.GetPlayerThrows(&player)) != 2 { + t.Fatal("ERROR: expected the first player to have two throws registred after undo") + } + + if game.Players.CurrentPlayer.Value.Id != player.Value.Id { + t.Fatal("ERROR: expected the first player to be current player after undo") + } +} + +func TestX01Engine_OverThrow_Scenario(t *testing.T) { + player := engine.Player{Value: &domain.Player{PlayerName: "1", Id: 1, Score: 0}, Turns: []engine.Turn{}} + player2 := engine.Player{Value: &domain.Player{PlayerName: "2", Id: 2, Score: 0}, Turns: []engine.Turn{}} + game := engine.Game{Players: &engine.Players{}, Engine: New(301)} + + game.Players.Add(&player) + game.Players.Add(&player2) + + throw := domain.Throw{Id: 1, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} + throw2 := domain.Throw{Id: 2, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} + throw3 := domain.Throw{Id: 3, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} + throw4 := domain.Throw{Id: 4, Points: 302, Multiplicator: 1, PlayerId: player.Value.Id} + game.Engine.RegisterThrow(&throw, game.Players) + game.Engine.RegisterThrow(&throw2, game.Players) + game.Engine.RegisterThrow(&throw3, game.Players) + + if game.Players.CurrentPlayer.Value.Id != player2.Value.Id { + t.Fatalf("ERROR: expected player two to be the current player") + } + game.Engine.RegisterThrow(&throw4, game.Players) + if player2.Turns[0].Sum() != 0 { + t.Fatalf("ERROR: expected player two on a overthrow to have a zero points in turn. instead got: %d", + player2.Turns[0].Sum()) + } + if game.Players.CurrentPlayer.Value.Id != player.Value.Id { + t.Fatalf("ERROR: expected player one to be current player after overthrow") + } + + throw5 := domain.Throw{Id: 1, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} + throw6 := domain.Throw{Id: 2, Points: 302, Multiplicator: 1, PlayerId: player.Value.Id} + + game.Engine.RegisterThrow(&throw5, game.Players) + game.Engine.RegisterThrow(&throw6, game.Players) + if player.Turns[1].Sum() != 1 { + t.Fatalf("ERROR: expected player one on a overthrow to have one point in turn. instead got: %d", player.Turns[1].Sum()) + } + if game.Players.CurrentPlayer.Value.Id != player2.Value.Id { + t.Fatalf("ERROR: expected player two to be the current player") + } +} + +func TestX01Engine_OverThrow_NoSpaceInTurnScenario(t *testing.T) { + player := engine.Player{Value: &domain.Player{PlayerName: "1", Id: 1, Score: 0}, Turns: []engine.Turn{}} + player2 := engine.Player{Value: &domain.Player{PlayerName: "2", Id: 2, Score: 0}, Turns: []engine.Turn{}} + game := engine.Game{Players: &engine.Players{}, Engine: New(301)} + + game.Players.Add(&player) + game.Players.Add(&player2) + + throw := domain.Throw{Id: 1, Points: 298, Multiplicator: 1, PlayerId: player.Value.Id} + throw2 := domain.Throw{Id: 2, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} + throw3 := domain.Throw{Id: 3, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} + + game.Engine.RegisterThrow(&throw, game.Players) + game.Engine.RegisterThrow(&throw2, game.Players) + game.Engine.RegisterThrow(&throw3, game.Players) + + throw4 := domain.Throw{Id: 4, Points: 1, Multiplicator: 1, PlayerId: player2.Value.Id} + throw5 := domain.Throw{Id: 3, Points: 1, Multiplicator: 1, PlayerId: player2.Value.Id} + throw6 := domain.Throw{Id: 3, Points: 1, Multiplicator: 1, PlayerId: player2.Value.Id} + + game.Engine.RegisterThrow(&throw4, game.Players) + game.Engine.RegisterThrow(&throw5, game.Players) + game.Engine.RegisterThrow(&throw6, game.Players) + + throw7 := domain.Throw{Id: 3, Points: 4, Multiplicator: 1, PlayerId: player.Value.Id} + game.Engine.RegisterThrow(&throw7, game.Players) + + if game.Players.CurrentPlayer.Value.Id != player2.Value.Id { + t.Fatalf("ERROR: expected player one to be the current player") + } + + if player.Turns[1].Sum() != 0 { + t.Fatalf("ERROR: expected player one on a overthrow to have a zero points in turn. instead got: %d", + player.Turns[1].Sum()) + } +} diff --git a/server/internal/triffgonix/database/database.go b/server/internal/database/database.go similarity index 94% rename from server/internal/triffgonix/database/database.go rename to server/internal/database/database.go index d713f0d..99a9ba0 100644 --- a/server/internal/triffgonix/database/database.go +++ b/server/internal/database/database.go @@ -1,9 +1,9 @@ package database import ( + "github.com/DaniloMurer/triffgonix/server/internal/models" "gorm.io/driver/sqlite" "gorm.io/gorm" - "server/internal/triffgonix/models" ) var database *gorm.DB diff --git a/server/internal/triffgonix/database/database_test.go b/server/internal/database/database_test.go similarity index 91% rename from server/internal/triffgonix/database/database_test.go rename to server/internal/database/database_test.go index 05f65f3..2655a0f 100644 --- a/server/internal/triffgonix/database/database_test.go +++ b/server/internal/database/database_test.go @@ -1,8 +1,8 @@ package database import ( + "github.com/DaniloMurer/triffgonix/server/internal/models" "github.com/google/uuid" - "server/internal/triffgonix/models" "testing" "github.com/stretchr/testify/assert" diff --git a/server/internal/triffgonix/domain/data.go b/server/internal/domain/data.go similarity index 96% rename from server/internal/triffgonix/domain/data.go rename to server/internal/domain/data.go index 4270f7d..9be254d 100644 --- a/server/internal/triffgonix/domain/data.go +++ b/server/internal/domain/data.go @@ -1,6 +1,8 @@ package domain -import "server/internal/triffgonix/models" +import ( + "github.com/DaniloMurer/triffgonix/server/internal/models" +) type Player struct { Id uint `json:"id"` diff --git a/server/internal/triffgonix/models/model.go b/server/internal/models/model.go similarity index 100% rename from server/internal/triffgonix/models/model.go rename to server/internal/models/model.go diff --git a/server/internal/triffgonix/dart/engine/x01/x01engine_test.go b/server/internal/triffgonix/dart/engine/x01/x01engine_test.go deleted file mode 100644 index 18e087e..0000000 --- a/server/internal/triffgonix/dart/engine/x01/x01engine_test.go +++ /dev/null @@ -1,383 +0,0 @@ -package x01 - -import ( - "fmt" - "server/internal/triffgonix/dart/engine" - "server/internal/triffgonix/domain" - "testing" -) - -func TestX01Engine_RegisterThrow(t *testing.T) { - player := engine.Player{Value: &domain.Player{Id: 1}, Turns: []engine.Turn{}} - - game := engine.Game{Engine: New(301), Players: &engine.Players{}} - - throw := domain.Throw{Id: 1, Points: 5, Multiplicator: 1, PlayerId: player.Value.Id} - game.Players.Add(&player) - - game.Engine.RegisterThrow(&throw, game.Players) - if playerThrows := game.Engine.GetPlayerThrows(&player); len(*playerThrows) == 0 { - t.Fatalf(`ERROR: Throws for given player should be 1, instead it's zero`) - } - - if len(game.Players.CurrentPlayer.Turns) != 1 { - t.Fatalf("ERROR: expected only on turn, since there was till space for adding a throw. instead a new turn was created") - } -} - -func TestPlayers_Add(t *testing.T) { - player := engine.Player{Value: &domain.Player{PlayerName: "1", Id: 1}, Turns: []engine.Turn{}} - player2 := engine.Player{Value: &domain.Player{PlayerName: "2", Id: 2}, Turns: []engine.Turn{}} - game := engine.Game{Players: &engine.Players{}, Engine: New(301)} - - game.Players.Add(&player) - game.Players.Add(&player2) - - // testing adding players to linked list correctly - if game.Players.Head.Value.Id != player.Value.Id { - t.Fatalf(`ERROR: expected id of head should be 1, instead got: %d`, game.Players.Head.Value.Id) - } - if game.Players.CurrentPlayer.Value.Id != player.Value.Id { - t.Fatalf(`ERROR: expected the id of the current player to be 1, instead got: %d`, game.Players.CurrentPlayer.Value.Id) - } - if game.Players.Tail.Value.Id != player2.Value.Id { - t.Fatalf(`ERROR: expected the id of the tail to be 2, instead got: %d`, game.Players.Tail.Value.Id) - } - // testing if the bidirectional linking of the list is correct - if game.Players.Head.Next.Value.Id != player2.Value.Id { - t.Fatalf(`ERROR: expected the id of the next player after head to be 2, instead got: %d`, game.Players.Head.Next.Value.Id) - } - if game.Players.Head.Previous != nil { - t.Fatalf(`ERROR: expected the previous player from head to be nil, instead got: %v`, game.Players.Head.Previous) - } - if game.Players.Tail.Previous.Value.Id != player.Value.Id { - t.Fatalf(`ERROR: expected the prevous player from the tail to have id 1, instead got: %d`, game.Players.Tail.Previous.Value.Id) - } - if game.Players.Tail.Next != nil { - t.Fatalf(`ERROR: expected the next player from the tail to be nil, instead got: %v`, game.Players.Tail.Next) - } -} - -func TestX01Engine_RegisterThrow_SwitchPlayer(t *testing.T) { - player := engine.Player{Value: &domain.Player{PlayerName: "1", Id: 1}, Turns: []engine.Turn{}} - player2 := engine.Player{Value: &domain.Player{PlayerName: "2", Id: 2}, Turns: []engine.Turn{}} - game := engine.Game{Players: &engine.Players{}, Engine: New(301)} - - game.Players.Add(&player) - game.Players.Add(&player2) - - // testing turn switching after three throws - throw := domain.Throw{Id: 1, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} - throw2 := domain.Throw{Id: 2, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} - throw3 := domain.Throw{Id: 3, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} - - game.Engine.RegisterThrow(&throw, game.Players) - game.Engine.RegisterThrow(&throw2, game.Players) - game.Engine.RegisterThrow(&throw3, game.Players) - - if game.Players.CurrentPlayer.Value.Id != player2.Value.Id { - t.Fatalf(`ERROR: expected the player to change to player2 after three throws. instead got player with id: %d`, game.Players.CurrentPlayer.Value.Id) - } - - game.Engine.RegisterThrow(&throw, game.Players) - game.Engine.RegisterThrow(&throw2, game.Players) - game.Engine.RegisterThrow(&throw3, game.Players) - - if game.Players.CurrentPlayer.Value.Id != player.Value.Id { - t.Fatalf(`ERROR: expected the player to change to player1 after three throws. instead got player with id %d`, game.Players.CurrentPlayer.Value.Id) - } - - game.Engine.RegisterThrow(&throw, game.Players) - game.Engine.RegisterThrow(&throw2, game.Players) - game.Engine.RegisterThrow(&throw3, game.Players) - - if game.Players.CurrentPlayer.Value.Id != player2.Value.Id { - t.Fatalf(`ERROR: expected the player to change to player2 after three throws. instead got player with id: %d`, game.Players.CurrentPlayer.Value.Id) - } -} - -func TestX01Engine_CalculatePlayerScore(t *testing.T) { - player := engine.Player{Value: &domain.Player{PlayerName: "1", Id: 1}, Turns: []engine.Turn{}} - player2 := engine.Player{Value: &domain.Player{PlayerName: "2", Id: 2}, Turns: []engine.Turn{}} - game := engine.Game{Players: &engine.Players{}, Engine: New(301)} - - game.Players.Add(&player) - game.Players.Add(&player2) - - throw := domain.Throw{Id: 1, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} - throw2 := domain.Throw{Id: 2, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} - throw3 := domain.Throw{Id: 3, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} - - game.Engine.RegisterThrow(&throw, game.Players) - game.Engine.RegisterThrow(&throw2, game.Players) - game.Engine.RegisterThrow(&throw3, game.Players) - - if player.Value.Score != 298 { - t.Fatalf(`ERROR: expected player score after three 1 throws to be 298. instead got: %d`, game.Players.GetPreviousPlayer().Value.Score) - } - - throw4 := domain.Throw{Id: 4, Points: 2, Multiplicator: 1, PlayerId: player2.Value.Id} - throw5 := domain.Throw{Id: 5, Points: 2, Multiplicator: 1, PlayerId: player2.Value.Id} - throw6 := domain.Throw{Id: 6, Points: 3, Multiplicator: 3, PlayerId: player2.Value.Id} - - game.Engine.RegisterThrow(&throw4, game.Players) - game.Engine.RegisterThrow(&throw5, game.Players) - game.Engine.RegisterThrow(&throw6, game.Players) - - if player2.Value.Score != 288 { - t.Fatalf(`ERROR: expected player score after three 1 throws to be 298. instead got: %d`, game.Players.GetPreviousPlayer().Value.Score) - } -} - -func TestX01Engine_RegisterThrow_CorrectTurnsAmount(t *testing.T) { - player := engine.Player{Value: &domain.Player{PlayerName: "1", Id: 1}, Turns: []engine.Turn{}} - player2 := engine.Player{Value: &domain.Player{PlayerName: "2", Id: 2}, Turns: []engine.Turn{}} - game := engine.Game{Players: &engine.Players{}, Engine: New(301)} - - game.Players.Add(&player) - game.Players.Add(&player2) - - throw := domain.Throw{Id: 1, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} - throw2 := domain.Throw{Id: 2, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} - throw3 := domain.Throw{Id: 3, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} - - game.Engine.RegisterThrow(&throw, game.Players) - game.Engine.RegisterThrow(&throw2, game.Players) - game.Engine.RegisterThrow(&throw3, game.Players) - - var playerTurns []engine.Turn - for _, turn := range game.Players.GetPreviousPlayer().Turns { - playerTurns = append(playerTurns, turn) - } - - if len(playerTurns) != 1 { - t.Fatalf(`ERROR: expected the player 1 to have one turn that blongs to him, instead got %d`, len(playerTurns)) - } - - game.Engine.RegisterThrow(&throw, game.Players) - game.Engine.RegisterThrow(&throw2, game.Players) - game.Engine.RegisterThrow(&throw3, game.Players) - - var player2Turns []engine.Turn - for _, turn := range game.Players.GetPreviousPlayer().Turns { - player2Turns = append(player2Turns, turn) - } - - if len(player2Turns) != 1 { - t.Fatalf(`ERROR: expected the player 2 to have one turn that blongs to him, instead got %d`, len(player2Turns)) - } - - game.Engine.RegisterThrow(&throw, game.Players) - game.Engine.RegisterThrow(&throw2, game.Players) - game.Engine.RegisterThrow(&throw3, game.Players) - - var player1Turns2 []engine.Turn - for _, turn := range game.Players.GetPreviousPlayer().Turns { - player1Turns2 = append(player1Turns2, turn) - } - - if len(player1Turns2) != 2 { - t.Fatalf(`ERROR: expected the player 1 to have two turn that blongs to him, instead got %d`, len(player1Turns2)) - } -} - -func TestX01Engine_HasAnyPlayerWon(t *testing.T) { - player := engine.Player{Value: &domain.Player{PlayerName: "1", Id: 1, Score: 301}, Turns: []engine.Turn{}} - player2 := engine.Player{Value: &domain.Player{PlayerName: "2", Id: 2, Score: 0}, Turns: []engine.Turn{}} - game := engine.Game{Players: &engine.Players{}, Engine: New(301)} - - game.Players.Add(&player) - game.Players.Add(&player2) - - winningPlayer := game.Engine.HasAnyPlayerWon(game.Players) - if winningPlayer.Value.Id != player2.Value.Id { - t.Fatalf(`ERROR: expected player 2 to win. instead player with id: %d won`, winningPlayer.Value.Id) - } -} - -func TestX01Engine_HasAnyPlayerWon_AfterThrows(t *testing.T) { - player := engine.Player{Value: &domain.Player{PlayerName: "1", Id: 1, Score: 34}, Turns: []engine.Turn{}} - player2 := engine.Player{Value: &domain.Player{PlayerName: "2", Id: 2, Score: 2}, Turns: []engine.Turn{}} - game := engine.Game{Players: &engine.Players{}, Engine: New(301)} - - game.Players.Add(&player) - game.Players.Add(&player2) - - throw := domain.Throw{Id: 1, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} - throw2 := domain.Throw{Id: 2, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} - throw3 := domain.Throw{Id: 3, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} - - game.Engine.RegisterThrow(&throw, game.Players) - if game.Engine.HasAnyPlayerWon(game.Players) != nil { - t.Fatal("ERROR: player 1 wasn't supposed to win") - } - game.Engine.RegisterThrow(&throw2, game.Players) - if game.Engine.HasAnyPlayerWon(game.Players) != nil { - t.Fatal("ERROR: player 1 wasn't supposed to win") - } - game.Engine.RegisterThrow(&throw3, game.Players) - if game.Engine.HasAnyPlayerWon(game.Players) != nil { - t.Fatal("ERROR: player 1 wasn't supposed to win") - } - - throw4 := domain.Throw{Id: 4, Points: 255, Multiplicator: 1, PlayerId: player2.Value.Id} - throw5 := domain.Throw{Id: 5, Points: 37, Multiplicator: 1, PlayerId: player2.Value.Id} - throw6 := domain.Throw{Id: 6, Points: 3, Multiplicator: 3, PlayerId: player2.Value.Id} - - game.Engine.RegisterThrow(&throw4, game.Players) - if game.Engine.HasAnyPlayerWon(game.Players) != nil { - t.Fatal("ERROR: player 2 was not supposed to win yet") - } - game.Engine.RegisterThrow(&throw5, game.Players) - if game.Engine.HasAnyPlayerWon(game.Players) != nil { - t.Fatal("ERROR: player 2 was not supposed to win yet") - } - game.Engine.RegisterThrow(&throw6, game.Players) - if game.Engine.HasAnyPlayerWon(game.Players) == nil { - t.Fatal("ERROR: player 2 was supposed to win") - } -} - -func TestX01Engine_UndoThrow(t *testing.T) { - player := engine.Player{Value: &domain.Player{PlayerName: "1", Id: 1, Score: 34}, Turns: []engine.Turn{}} - game := engine.Game{Players: &engine.Players{}, Engine: New(301)} - - game.Players.Add(&player) - - throw := domain.Throw{Id: 1, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} - - game.Engine.RegisterThrow(&throw, game.Players) - playerThrows := game.Engine.GetPlayerThrows(&player) - if len(*playerThrows) != 1 { - t.Fatal("ERROR: expected player throws to contain one throw") - } - - game.Engine.UndoLastThrow(game.Players) - playerThrows2 := game.Engine.GetPlayerThrows(&player) - - fmt.Printf("trace: playerThrows2 - %+v\n", playerThrows2) - if len(*playerThrows2) != 0 { - t.Fatal("ERROR: expected player throws to contain no throws after undo") - } -} - -func TestX01Engine_UndoThrow_WhenSecondPlayerTurn(t *testing.T) { - player := engine.Player{Value: &domain.Player{PlayerName: "1", Id: 1, Score: 34}, Turns: []engine.Turn{}} - player2 := engine.Player{Value: &domain.Player{PlayerName: "2", Id: 2, Score: 2}, Turns: []engine.Turn{}} - game := engine.Game{Players: &engine.Players{}, Engine: New(301)} - - game.Players.Add(&player) - game.Players.Add(&player2) - - throw := domain.Throw{Id: 1, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} - throw2 := domain.Throw{Id: 2, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} - throw3 := domain.Throw{Id: 3, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} - - game.Engine.RegisterThrow(&throw, game.Players) - game.Engine.RegisterThrow(&throw2, game.Players) - game.Engine.RegisterThrow(&throw3, game.Players) - - if len(*game.Engine.GetPlayerThrows(&player)) != 3 { - t.Fatal("ERROR: expected the first player to have three throws registred") - } - - game.Engine.RegisterThrow(&throw, game.Players) - - if len(*game.Engine.GetPlayerThrows(&player2)) != 1 { - t.Fatal("ERROR: expected the second player to have one throw registred") - } - - game.Engine.UndoLastThrow(game.Players) - - if len(*game.Engine.GetPlayerThrows(&player2)) != 0 { - t.Fatal("ERROR: expected the second player to have zero throws registred after undo") - } - - game.Engine.UndoLastThrow(game.Players) - - if len(*game.Engine.GetPlayerThrows(&player)) != 2 { - t.Fatal("ERROR: expected the first player to have two throws registred after undo") - } - - if game.Players.CurrentPlayer.Value.Id != player.Value.Id { - t.Fatal("ERROR: expected the first player to be current player after undo") - } -} - -func TestX01Engine_OverThrow_Scenario(t *testing.T) { - player := engine.Player{Value: &domain.Player{PlayerName: "1", Id: 1, Score: 0}, Turns: []engine.Turn{}} - player2 := engine.Player{Value: &domain.Player{PlayerName: "2", Id: 2, Score: 0}, Turns: []engine.Turn{}} - game := engine.Game{Players: &engine.Players{}, Engine: New(301)} - - game.Players.Add(&player) - game.Players.Add(&player2) - - throw := domain.Throw{Id: 1, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} - throw2 := domain.Throw{Id: 2, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} - throw3 := domain.Throw{Id: 3, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} - throw4 := domain.Throw{Id: 4, Points: 302, Multiplicator: 1, PlayerId: player.Value.Id} - game.Engine.RegisterThrow(&throw, game.Players) - game.Engine.RegisterThrow(&throw2, game.Players) - game.Engine.RegisterThrow(&throw3, game.Players) - - if game.Players.CurrentPlayer.Value.Id != player2.Value.Id { - t.Fatalf("ERROR: expected player two to be the current player") - } - game.Engine.RegisterThrow(&throw4, game.Players) - if player2.Turns[0].Sum() != 0 { - t.Fatalf("ERROR: expected player two on a overthrow to have a zero points in turn. instead got: %d", - player2.Turns[0].Sum()) - } - if game.Players.CurrentPlayer.Value.Id != player.Value.Id { - t.Fatalf("ERROR: expected player one to be current player after overthrow") - } - - throw5 := domain.Throw{Id: 1, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} - throw6 := domain.Throw{Id: 2, Points: 302, Multiplicator: 1, PlayerId: player.Value.Id} - - game.Engine.RegisterThrow(&throw5, game.Players) - game.Engine.RegisterThrow(&throw6, game.Players) - if player.Turns[1].Sum() != 1 { - t.Fatalf("ERROR: expected player one on a overthrow to have one point in turn. instead got: %d", player.Turns[1].Sum()) - } - if game.Players.CurrentPlayer.Value.Id != player2.Value.Id { - t.Fatalf("ERROR: expected player two to be the current player") - } -} - -func TestX01Engine_OverThrow_NoSpaceInTurnScenario(t *testing.T) { - player := engine.Player{Value: &domain.Player{PlayerName: "1", Id: 1, Score: 0}, Turns: []engine.Turn{}} - player2 := engine.Player{Value: &domain.Player{PlayerName: "2", Id: 2, Score: 0}, Turns: []engine.Turn{}} - game := engine.Game{Players: &engine.Players{}, Engine: New(301)} - - game.Players.Add(&player) - game.Players.Add(&player2) - - throw := domain.Throw{Id: 1, Points: 298, Multiplicator: 1, PlayerId: player.Value.Id} - throw2 := domain.Throw{Id: 2, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} - throw3 := domain.Throw{Id: 3, Points: 1, Multiplicator: 1, PlayerId: player.Value.Id} - - game.Engine.RegisterThrow(&throw, game.Players) - game.Engine.RegisterThrow(&throw2, game.Players) - game.Engine.RegisterThrow(&throw3, game.Players) - - throw4 := domain.Throw{Id: 4, Points: 1, Multiplicator: 1, PlayerId: player2.Value.Id} - throw5 := domain.Throw{Id: 3, Points: 1, Multiplicator: 1, PlayerId: player2.Value.Id} - throw6 := domain.Throw{Id: 3, Points: 1, Multiplicator: 1, PlayerId: player2.Value.Id} - - game.Engine.RegisterThrow(&throw4, game.Players) - game.Engine.RegisterThrow(&throw5, game.Players) - game.Engine.RegisterThrow(&throw6, game.Players) - - throw7 := domain.Throw{Id: 3, Points: 4, Multiplicator: 1, PlayerId: player.Value.Id} - game.Engine.RegisterThrow(&throw7, game.Players) - - if game.Players.CurrentPlayer.Value.Id != player2.Value.Id { - t.Fatalf("ERROR: expected player one to be the current player") - } - - if player.Turns[1].Sum() != 0 { - t.Fatalf("ERROR: expected player one on a overthrow to have a zero points in turn. instead got: %d", - player.Turns[1].Sum()) - } -}