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.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")

View File

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

View File

@ -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)
}
}

View File

@ -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 {