Compare commits
5 Commits
b4c82d6c8f
...
b623410aff
Author | SHA1 | Date | |
---|---|---|---|
b623410aff | |||
75fd0cc400 | |||
cd2ab106a2 | |||
90dca37d44 | |||
682ce8437b |
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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()
|
||||