Change lobby handling and websocket handling.

This commit is contained in:
Marco 2023-05-28 17:43:05 +02:00
parent eb946b4267
commit 5b6cb566ec
4 changed files with 57 additions and 34 deletions

53
main.go
View File

@ -1,6 +1,7 @@
package main
import (
"context"
"local/m/mchess_server/server"
"log"
"net/http"
@ -25,7 +26,7 @@ func main() {
router := gin.Default()
router.GET("/api/random", registerForRandomGame)
router.POST("/api/ws", registerWebSocketConnection)
router.GET("/api/ws", registerWebSocketConnection)
if hostname == "mbook" {
log.Println("Starting service WITHOUT TLS")
@ -40,6 +41,8 @@ func registerForRandomGame(c *gin.Context) {
player := server.NewPlayer(uuid.New())
server.GetLobby().RegisterPlayer(player)
log.Println("responding with player id ", player.Uuid)
c.IndentedJSON(http.StatusOK, server.PlayerInfo{
PlayerID: player.Uuid,
})
@ -47,23 +50,37 @@ func registerForRandomGame(c *gin.Context) {
func registerWebSocketConnection(c *gin.Context) {
// w http.ResponseWriter, r *http.Request
var playerInfo server.PlayerInfo
err := c.BindJSON(&playerInfo)
webSocketConn, err := websocket.Accept(c.Writer, c.Request, nil)
if err != nil {
log.Printf("BindJSON has an error: (%e)", err)
}
log.Println("playerID:", playerInfo.PlayerID)
player, found := server.GetLobby()[playerInfo.PlayerID]
if found {
webSocketConn, err := websocket.Accept(c.Writer, c.Request, nil)
if err != nil {
log.Println(err)
return
}
player.Conn = *webSocketConn
log.Println(err)
return
}
go waitForAndHandlePlayerID(c, *webSocketConn)
}
func waitForAndHandlePlayerID(ctx context.Context, conn websocket.Conn) {
// var playerInfo server.PlayerInfo
msgType, id, err := conn.Read(ctx)
log.Println("read from websocket: ", msgType, id, err)
log.Println("id as string", string(id))
uuid, err := uuid.ParseBytes(id)
if err != nil {
log.Println(err)
conn.Close(websocket.StatusCode(400), err.Error())
return
}
lobby := server.GetLobby()
player, found := lobby[uuid]
if !found {
conn.Close(websocket.StatusCode(400), "player not found")
return
}
if player.Conn != nil {
player.Conn.Close(websocket.StatusCode(400), "closing existing connection")
}
player.Conn = &conn
}

View File

@ -26,7 +26,6 @@ func NewGame() *Game {
}
func (game *Game) handle() {
defer log.Println("Game ", game.id, ": handle() ended")
gameState := PlayerToMove
@ -88,7 +87,13 @@ func (game *Game) handle() {
func (game *Game) addPlayersToGame(players [2]Player) {
log.Printf("Adding players %s and %s to new game", players[0].Uuid.String(), players[1].Uuid.String())
for _, player := range players {
player.InGame = true
}
game.players = players
go game.handle()
}
func removePlayersFromLobby(players [2]Player) {

View File

@ -24,20 +24,20 @@ func newLobby() Lobby {
func (lobby Lobby) RegisterPlayer(player *Player) {
lobby[player.Uuid] = *player
if len(lobby)%2 == 0 {
var players [2]Player
var index int = 0
var players []Player
for _, player := range lobby {
players[index] = player
index += 1
for _, player := range lobby {
if !player.InGame {
players = append(players, player)
}
game := NewGame()
game.addPlayersToGame(players)
delete(lobby, players[0].Uuid)
delete(lobby, players[1].Uuid)
}
if len(players) < 2 {
return
}
game := NewGame()
game.addPlayersToGame([2]Player(players[:2]))
}

View File

@ -6,8 +6,9 @@ import (
)
type Player struct {
Uuid uuid.UUID
Conn websocket.Conn
Uuid uuid.UUID
Conn *websocket.Conn
InGame bool
}
type PlayerInfo struct {