Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (game *Game) GetNextIndex() int {
return i
}
}
return 0
return -1
}

func (game *Game) HasPlayer() bool {
Expand Down
2 changes: 1 addition & 1 deletion game/game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 3 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
41 changes: 24 additions & 17 deletions server/handlers.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}