From e221c9d374321f714d4fa1e7765cdc518e18e84e Mon Sep 17 00:00:00 2001 From: Luis Guilherme Pelin Martins Date: Mon, 8 Apr 2024 23:41:03 -0300 Subject: [PATCH 1/2] FEA Implement support for multiple concurrent games * Introduce an array to hold multiple Game instances * Check for an available game slot when a new player connects - If no available slot, start a new game and add it to the array - If an available slot exists, assign the player to that game * Each game instance runs independently with its own lifecycle and communication channel * Remove the unused HandleGetSit handler This commit introduces the capability to host multiple concurrent Pongo games. When a new player connects via WebSocket, the server checks for an available slot in the existing games. If no slot is available, a new game instance is started and added to the games array. This allows multiple games to run simultaneously, each with its own lifecycle and communication channel. The HandleGetSit handler has been removed as it is no longer needed in this implementation. Co-authored-by: Comai --- game/game.go | 2 +- game/game_test.go | 2 +- main.go | 8 +++----- server/handlers.go | 41 ++++++++++++++++++++++++----------------- 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/game/game.go b/game/game.go index 0e824c7..c578265 100644 --- a/game/game.go +++ b/game/game.go @@ -78,7 +78,7 @@ func (game *Game) GetNextIndex() int { return i } } - return 0 + return -1 } func (game *Game) HasPlayer() bool { diff --git a/game/game_test.go b/game/game_test.go index 3357d4a..c7f395e 100644 --- a/game/game_test.go +++ b/game/game_test.go @@ -36,7 +36,7 @@ func TestGame_GetNextIndex(t *testing.T) { {[4]*Player{{Id: "player1"}, nil, nil}, 1}, {[4]*Player{{Id: "player1"}, {Id: "player2"}, nil}, 2}, {[4]*Player{{Id: "player1"}, {Id: "player2"}, {Id: "player3"}}, 3}, - {[4]*Player{{Id: "player1"}, {Id: "player2"}, {Id: "player3"}, {Id: "player4"}}, 0}, + {[4]*Player{{Id: "player1"}, {Id: "player2"}, {Id: "player3"}, {Id: "player4"}}, -1}, } for _, tc := range testCases { diff --git a/main.go b/main.go index f6ca861..1c66afe 100644 --- a/main.go +++ b/main.go @@ -12,13 +12,11 @@ import ( var port = ":3001" func main() { - g := game.StartGame() - go g.ReadGameChannel() + var games []*game.Game + games = append(games, game.StartGame()) websocketServer := server.New() fmt.Println("Server started on port", port) - http.HandleFunc("/", websocketServer.HandleGetSit(g)) - http.Handle("/subscribe", websocket.Handler(websocketServer.HandleSubscribe(g))) - + http.Handle("/subscribe", websocket.Handler(websocketServer.HandleSubscribe(games))) panic(http.ListenAndServe(port, nil)) } diff --git a/server/handlers.go b/server/handlers.go index e8ef298..39f803f 100644 --- a/server/handlers.go +++ b/server/handlers.go @@ -1,33 +1,40 @@ package server import ( - "fmt" - "io" - "net/http" - "github.com/lguibr/pongo/game" "golang.org/x/net/websocket" ) -func (s *Server) HandleSubscribe(g *game.Game) func(ws *websocket.Conn) { +func (s *Server) HandleSubscribe(games []*game.Game) func(ws *websocket.Conn) { return func(ws *websocket.Conn) { + var currentGameIndex int = -1 + var currentGame *game.Game + + for _, game := range games { + if game != nil && game.GetNextIndex() != -1 { + currentGame = game + break + } + } + noAvailableGame := currentGameIndex == -1 + + if noAvailableGame { + currentGame := game.StartGame() + games = append(games, currentGame) + } else { + currentGame = games[currentGameIndex] + } + + go currentGame.ReadGameChannel() + //INFO Open WebSocket connection s.OpenConnection(ws) //INFO Start Game lifecycle - go g.LifeCycle(ws, func() { s.CloseConnection(ws) }) + go currentGame.LifeCycle(ws, func() { + s.CloseConnection(ws) + }) //INFO Keep WebSocket connection open s.KeepConnection(ws) } } - -func (s *Server) HandleGetSit(g *game.Game) func(w http.ResponseWriter, r *http.Request) { - return func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - _, err := io.WriteString(w, string(g.ToJson())) - if err != nil { - fmt.Println("Error writing to client: ", err) - } - } -} From 550090b9d33d90bc0b80f063703b5750b8207bc9 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Tue, 9 Apr 2024 02:46:15 +0000 Subject: [PATCH 2/2] chore: Updated coverage badge. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e3d46c5..dc73818 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # PonGo -![Coverage](https://img.shields.io/badge/Coverage-59.0%25-yellow) +![Coverage](https://img.shields.io/badge/Coverage-59.2%25-yellow) ![Unit-tests](https://img.shields.io/github/actions/workflow/status/lguibr/pongo/test.yml?label=UnitTests) ![Building](https://img.shields.io/github/actions/workflow/status/lguibr/pongo/build.yml?label=Build) ![Lint](https://img.shields.io/github/actions/workflow/status/lguibr/pongo/lint.yml?label=Lint)