Marco
eb946b4267
1. Use maintained websocket framework 2. Introduce function that allows to register a websocket connection per player
44 lines
665 B
Go
44 lines
665 B
Go
package server
|
|
|
|
import (
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type Lobby map[uuid.UUID]Player
|
|
|
|
var lobbyInstance Lobby = nil
|
|
|
|
func GetLobby() Lobby {
|
|
if lobbyInstance == nil {
|
|
lobbyInstance = newLobby()
|
|
}
|
|
|
|
return lobbyInstance
|
|
}
|
|
|
|
func newLobby() Lobby {
|
|
lobby := make(map[uuid.UUID]Player, 0)
|
|
return lobby
|
|
}
|
|
|
|
func (lobby Lobby) RegisterPlayer(player *Player) {
|
|
lobby[player.Uuid] = *player
|
|
|
|
if len(lobby)%2 == 0 {
|
|
var players [2]Player
|
|
var index int = 0
|
|
|
|
for _, player := range lobby {
|
|
players[index] = player
|
|
index += 1
|
|
}
|
|
|
|
game := NewGame()
|
|
|
|
game.addPlayersToGame(players)
|
|
|
|
delete(lobby, players[0].Uuid)
|
|
delete(lobby, players[1].Uuid)
|
|
}
|
|
}
|