Make Lobby a map. Kind of useless...

This commit is contained in:
Marco 2023-04-18 22:47:51 +02:00
parent 9c8f46c9a4
commit 447ea90230
4 changed files with 29 additions and 18 deletions

10
main.go
View File

@ -40,10 +40,12 @@ func main() {
} }
router := gin.Default() router := gin.Default()
router.GET("/albums", getAlbums) router.GET("/api/albums", getAlbums)
router.GET("/albums/:id", getAlbumByID) router.GET("/api/albums/:id", getAlbumByID)
router.POST("/albums", postAlbums) router.POST("/api/albums", postAlbums)
router.GET("/random", playRandomGame)
router.GET("/api/random", playRandomGame)
http.HandleFunc("/api/ws", server.PlayHandler)
if hostname == "mbook" { if hostname == "mbook" {
log.Println("Starting service WITHOUT TLS") log.Println("Starting service WITHOUT TLS")

View File

@ -102,3 +102,7 @@ func addPlayersToGame(players [2]Player) {
go game.handle() go game.handle()
} }
func removePlayersFromLobby(players [2]Player) {
panic("not yet implemented")
}

View File

@ -1,10 +1,10 @@
package server package server
import "log" import (
"github.com/google/uuid"
)
type Lobby struct { type Lobby map[uuid.UUID]Player
players []Player
}
var lobbyInstance *Lobby = nil var lobbyInstance *Lobby = nil
@ -17,17 +17,23 @@ func GetLobby() *Lobby {
} }
func newLobby() *Lobby { func newLobby() *Lobby {
return &Lobby{} var lobby Lobby
return &lobby
} }
func (lobby *Lobby) RegisterPlayer(player *Player) { func (lobby Lobby) RegisterPlayer(player *Player) {
lobby.players = append(lobby.players, *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 { for _, player := range lobby {
player1 := lobby.players[len(lobby.players)-2] players[index] = player
player2 := lobby.players[len(lobby.players)-1] index += 1
addPlayersToGame([2]Player{player1, player2}) }
addPlayersToGame(players)
removePlayersFromLobby(players)
} }
} }

View File

@ -14,7 +14,7 @@ var player_number = 0
var upgrader = websocket.Upgrader{} var upgrader = websocket.Upgrader{}
func PlayHandler(w http.ResponseWriter, r *http.Request) { 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.")) w.Write([]byte("Hello, this is your websocket connection speaking."))
// Allow connections from any origin // Allow connections from any origin
upgrader.CheckOrigin = func(r *http.Request) bool { return true } 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) log.Print("Error during connection upgrading:", err)
return return
} }
} }
func WriteMessageToPlayer(player *Player, msg []byte, msgType int) error { func WriteMessageToPlayer(player *Player, msg []byte, msgType int) error {