Change lobby handling and websocket handling.
This commit is contained in:
parent
eb946b4267
commit
5b6cb566ec
53
main.go
53
main.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"local/m/mchess_server/server"
|
"local/m/mchess_server/server"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -25,7 +26,7 @@ func main() {
|
|||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
|
|
||||||
router.GET("/api/random", registerForRandomGame)
|
router.GET("/api/random", registerForRandomGame)
|
||||||
router.POST("/api/ws", registerWebSocketConnection)
|
router.GET("/api/ws", registerWebSocketConnection)
|
||||||
|
|
||||||
if hostname == "mbook" {
|
if hostname == "mbook" {
|
||||||
log.Println("Starting service WITHOUT TLS")
|
log.Println("Starting service WITHOUT TLS")
|
||||||
@ -40,6 +41,8 @@ func registerForRandomGame(c *gin.Context) {
|
|||||||
player := server.NewPlayer(uuid.New())
|
player := server.NewPlayer(uuid.New())
|
||||||
server.GetLobby().RegisterPlayer(player)
|
server.GetLobby().RegisterPlayer(player)
|
||||||
|
|
||||||
|
log.Println("responding with player id ", player.Uuid)
|
||||||
|
|
||||||
c.IndentedJSON(http.StatusOK, server.PlayerInfo{
|
c.IndentedJSON(http.StatusOK, server.PlayerInfo{
|
||||||
PlayerID: player.Uuid,
|
PlayerID: player.Uuid,
|
||||||
})
|
})
|
||||||
@ -47,23 +50,37 @@ func registerForRandomGame(c *gin.Context) {
|
|||||||
|
|
||||||
func registerWebSocketConnection(c *gin.Context) {
|
func registerWebSocketConnection(c *gin.Context) {
|
||||||
// w http.ResponseWriter, r *http.Request
|
// w http.ResponseWriter, r *http.Request
|
||||||
|
webSocketConn, err := websocket.Accept(c.Writer, c.Request, nil)
|
||||||
var playerInfo server.PlayerInfo
|
|
||||||
|
|
||||||
err := c.BindJSON(&playerInfo)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("BindJSON has an error: (%e)", err)
|
log.Println(err)
|
||||||
}
|
return
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,6 @@ func NewGame() *Game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (game *Game) handle() {
|
func (game *Game) handle() {
|
||||||
|
|
||||||
defer log.Println("Game ", game.id, ": handle() ended")
|
defer log.Println("Game ", game.id, ": handle() ended")
|
||||||
|
|
||||||
gameState := PlayerToMove
|
gameState := PlayerToMove
|
||||||
@ -88,7 +87,13 @@ func (game *Game) handle() {
|
|||||||
func (game *Game) addPlayersToGame(players [2]Player) {
|
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())
|
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
|
game.players = players
|
||||||
|
|
||||||
|
go game.handle()
|
||||||
}
|
}
|
||||||
|
|
||||||
func removePlayersFromLobby(players [2]Player) {
|
func removePlayersFromLobby(players [2]Player) {
|
||||||
|
@ -24,20 +24,20 @@ func newLobby() Lobby {
|
|||||||
func (lobby Lobby) RegisterPlayer(player *Player) {
|
func (lobby Lobby) RegisterPlayer(player *Player) {
|
||||||
lobby[player.Uuid] = *player
|
lobby[player.Uuid] = *player
|
||||||
|
|
||||||
if len(lobby)%2 == 0 {
|
var players []Player
|
||||||
var players [2]Player
|
|
||||||
var index int = 0
|
|
||||||
|
|
||||||
for _, player := range lobby {
|
for _, player := range lobby {
|
||||||
players[index] = player
|
if !player.InGame {
|
||||||
index += 1
|
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]))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Player struct {
|
type Player struct {
|
||||||
Uuid uuid.UUID
|
Uuid uuid.UUID
|
||||||
Conn websocket.Conn
|
Conn *websocket.Conn
|
||||||
|
InGame bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type PlayerInfo struct {
|
type PlayerInfo struct {
|
||||||
|
Loading…
Reference in New Issue
Block a user