Compare commits

..

No commits in common. "b623410aff8942ad9226be41e3d1b95bf804267e" and "b4c82d6c8fd6c87d2a9bb1e5ed7e7cea1509b78c" have entirely different histories.

9 changed files with 12 additions and 38 deletions

View File

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

View File

@ -157,14 +157,6 @@ func (game *Game) AddPlayersToGame(player *Player) {
game.players = append(game.players, 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() { func (game *Game) killGame() {
log.Println("Game should be killed") log.Println("Game should be killed")
} }
@ -220,5 +212,5 @@ func (game *Game) playerDisconnected(p *Player) {
} }
func (game *Game) SetWebsocketConnectionFor(ctx context.Context, p *Player, ws *gorillaws.Conn) { func (game *Game) SetWebsocketConnectionFor(ctx context.Context, p *Player, ws *gorillaws.Conn) {
p.SetWebsocketConnectionAndSendBoardState(ctx, ws, &game.board) p.SetWebsocketConnectionAndSendBoardState(ctx, ws, game.board.PGN(), game.board.colorToMove)
} }

View File

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

View File

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

2
go.mod
View File

@ -9,7 +9,7 @@ require (
github.com/stretchr/testify v1.9.0 github.com/stretchr/testify v1.9.0
) )
require github.com/benbjohnson/clock v1.3.5 // indirect require github.com/benbjohnson/clock v1.3.0 // indirect
require ( require (
github.com/bytedance/sonic v1.11.6 // indirect github.com/bytedance/sonic v1.11.6 // indirect

6
go.sum
View File

@ -1,5 +1,5 @@
github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o= github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/benbjohnson/clock v1.3.5/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
@ -71,8 +71,6 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/ratelimit v0.3.1 h1:K4qVE+byfv/B3tC+4nYWP7v/6SimcO7HzHekoMNBma0= go.uber.org/ratelimit v0.3.1 h1:K4qVE+byfv/B3tC+4nYWP7v/6SimcO7HzHekoMNBma0=
go.uber.org/ratelimit v0.3.1/go.mod h1:6euWsTB6U/Nb3X++xEUXA8ciPJvr19Q/0h1+oDcJhRk= go.uber.org/ratelimit v0.3.1/go.mod h1:6euWsTB6U/Nb3X++xEUXA8ciPJvr19Q/0h1+oDcJhRk=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=

View File

@ -31,16 +31,12 @@ func newEmptyLobbyWithPassphrase() *Lobby {
func (l *Lobby) AddPlayerAndStartGameIfFull(player *chess.Player) { func (l *Lobby) AddPlayerAndStartGameIfFull(player *chess.Player) {
l.Game.AddPlayersToGame(player) l.Game.AddPlayersToGame(player)
if l.ContainsTwoPlayers() { if l.IsFull() {
l.Game.StartHandling() l.Game.StartHandling()
} }
} }
func (w *Lobby) AreBothPlayersConnected() bool { func (w *Lobby) IsFull() bool {
return w.Game.AreBothPlayersConnected()
}
func (w *Lobby) ContainsTwoPlayers() bool {
return len(w.Game.GetPlayers()) == 2 return len(w.Game.GetPlayers()) == 2
} }

View File

@ -32,7 +32,7 @@ func (r *LobbyRegistry) CreateNewPrivateLobby() *Lobby {
func (r *LobbyRegistry) GetLobbyForPlayer() *Lobby { func (r *LobbyRegistry) GetLobbyForPlayer() *Lobby {
for _, lobby := range r.lobbies { for _, lobby := range r.lobbies {
if !lobby.ContainsTwoPlayers() { if !lobby.IsFull() {
return lobby return lobby
} }
} }

View File

@ -35,7 +35,7 @@ func (u *Usher) CreateNewPrivateLobby(player *chess.Player) *Lobby {
func (u *Usher) FindExistingPrivateLobby(p utils.Passphrase) *Lobby { func (u *Usher) FindExistingPrivateLobby(p utils.Passphrase) *Lobby {
lobby := GetLobbyRegistry().GetLobbyByPassphrase(p) lobby := GetLobbyRegistry().GetLobbyByPassphrase(p)
if lobby == nil || lobby.AreBothPlayersConnected() { if lobby == nil || lobby.IsFull() {
return nil return nil
} }
return lobby return lobby