From 447ea90230bdef14502f62781a1c3fabfa44c574 Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 18 Apr 2023 22:47:51 +0200 Subject: [PATCH] Make Lobby a map. Kind of useless... --- main.go | 10 ++++++---- server/game.go | 4 ++++ server/lobby.go | 30 ++++++++++++++++++------------ server/server.go | 3 +-- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index be8050c..8270ac5 100644 --- a/main.go +++ b/main.go @@ -40,10 +40,12 @@ func main() { } router := gin.Default() - router.GET("/albums", getAlbums) - router.GET("/albums/:id", getAlbumByID) - router.POST("/albums", postAlbums) - router.GET("/random", playRandomGame) + router.GET("/api/albums", getAlbums) + router.GET("/api/albums/:id", getAlbumByID) + router.POST("/api/albums", postAlbums) + + router.GET("/api/random", playRandomGame) + http.HandleFunc("/api/ws", server.PlayHandler) if hostname == "mbook" { log.Println("Starting service WITHOUT TLS") diff --git a/server/game.go b/server/game.go index 8eaedf0..c9da184 100644 --- a/server/game.go +++ b/server/game.go @@ -102,3 +102,7 @@ func addPlayersToGame(players [2]Player) { go game.handle() } + +func removePlayersFromLobby(players [2]Player) { + panic("not yet implemented") +} diff --git a/server/lobby.go b/server/lobby.go index 49ef1f6..dc59113 100644 --- a/server/lobby.go +++ b/server/lobby.go @@ -1,10 +1,10 @@ package server -import "log" +import ( + "github.com/google/uuid" +) -type Lobby struct { - players []Player -} +type Lobby map[uuid.UUID]Player var lobbyInstance *Lobby = nil @@ -17,17 +17,23 @@ func GetLobby() *Lobby { } func newLobby() *Lobby { - return &Lobby{} + var lobby Lobby + return &lobby } -func (lobby *Lobby) RegisterPlayer(player *Player) { - lobby.players = append(lobby.players, *player) +func (lobby Lobby) RegisterPlayer(player *Player) { + lobby[player.uuid] = *player - log.Println(lobby) + if len(lobby)%2 == 0 { + var players [2]Player + var index int = 0 - if len(lobby.players)%2 == 0 { - player1 := lobby.players[len(lobby.players)-2] - player2 := lobby.players[len(lobby.players)-1] - addPlayersToGame([2]Player{player1, player2}) + for _, player := range lobby { + players[index] = player + index += 1 + } + + addPlayersToGame(players) + removePlayersFromLobby(players) } } diff --git a/server/server.go b/server/server.go index a63dec9..8b8404b 100644 --- a/server/server.go +++ b/server/server.go @@ -14,7 +14,7 @@ var player_number = 0 var upgrader = websocket.Upgrader{} func PlayHandler(w http.ResponseWriter, r *http.Request) { - log.Println("SocketHandler invoked") + log.Println("PlayHandler invoked") w.Write([]byte("Hello, this is your websocket connection speaking.")) // Allow connections from any origin upgrader.CheckOrigin = func(r *http.Request) bool { return true } @@ -24,7 +24,6 @@ func PlayHandler(w http.ResponseWriter, r *http.Request) { log.Print("Error during connection upgrading:", err) return } - } func WriteMessageToPlayer(player *Player, msg []byte, msgType int) error {