Compare commits

...

5 Commits

9 changed files with 38 additions and 12 deletions

View File

@ -58,6 +58,7 @@ func GetLobbyForPassphraseHandler(c *gin.Context) {
ID: &lobby.Uuid,
}
c.Header("Access-Control-Allow-Origin", "*")
c.IndentedJSON(http.StatusOK, lobbyInfo)
}
@ -69,6 +70,7 @@ func JoinPrivateGame(c *gin.Context) {
err := c.ShouldBindJSON(&req)
if err != nil || req.Passphrase == nil || *req.Passphrase == "" {
c.IndentedJSON(http.StatusNotFound, req)
return
}
u := lobbies.GetUsher()
@ -78,8 +80,12 @@ func JoinPrivateGame(c *gin.Context) {
req.PlayerID != nil &&
req.LobbyID != nil { //is reconnect
lobby := u.FindExistingPrivateLobby(utils.Passphrase(*req.Passphrase))
_, found := lobby.GetPlayerByUUID(*req.PlayerID)
var found bool
if lobby != nil {
_, found = lobby.GetPlayerByUUID(*req.PlayerID)
}
if found {
c.Header("Access-Control-Allow-Origin", "*")
c.IndentedJSON(
http.StatusOK,
api.PlayerInfo{
@ -90,6 +96,7 @@ func JoinPrivateGame(c *gin.Context) {
return
} else {
c.IndentedJSON(http.StatusNotFound, req)
return
}
}

View File

@ -157,6 +157,14 @@ func (game *Game) AddPlayersToGame(player *Player) {
game.players = append(game.players, player)
}
func (game *Game) AreBothPlayersConnected() bool {
if len(game.GetPlayers()) < 2 {
return false
}
return game.players[0].hasWebsocketConnection() && game.players[1].hasWebsocketConnection()
}
func (game *Game) killGame() {
log.Println("Game should be killed")
}
@ -212,5 +220,5 @@ func (game *Game) playerDisconnected(p *Player) {
}
func (game *Game) SetWebsocketConnectionFor(ctx context.Context, p *Player, ws *gorillaws.Conn) {
p.SetWebsocketConnectionAndSendBoardState(ctx, ws, game.board.PGN(), game.board.colorToMove)
p.SetWebsocketConnectionAndSendBoardState(ctx, ws, &game.board)
}

View File

@ -41,11 +41,10 @@ func (p *Player) SetWebsocketConnection(ctx context.Context, ws *gorillaws.Conn)
func (p *Player) SetWebsocketConnectionAndSendBoardState(
ctx context.Context,
ws *gorillaws.Conn,
boardPosition string,
turnColor types.ChessColor,
board *Board,
) {
p.SetWebsocketConnection(ctx, ws)
p.SendBoardState(types.Move{}, boardPosition, turnColor)
p.SendBoardState(board.getLastMove(), board.PGN(), board.colorToMove)
}
func (p *Player) SetColor(color types.ChessColor) {

View File

@ -75,6 +75,8 @@ func (conn *Connection) SetWebsocketConnection(ws *gorillaws.Conn) {
_, msg, err := conn.ws.ReadMessage()
if err != nil {
log.Println("while reading from websocket: %w", err)
conn.unsetWebsocketConnection()
if conn.disconnectCallback != nil {
conn.disconnectCallback()
}
@ -85,6 +87,10 @@ func (conn *Connection) SetWebsocketConnection(ws *gorillaws.Conn) {
}()
}
func (conn *Connection) unsetWebsocketConnection() {
conn.ws = nil
}
func (conn *Connection) Write(msg []byte) error {
conn.wsWriteLock.Lock()