make games rejoinable
This commit is contained in:
parent
b4c82d6c8f
commit
682ce8437b
@ -69,6 +69,7 @@ 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()
|
||||||
@ -78,7 +79,10 @@ 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))
|
||||||
_, found := lobby.GetPlayerByUUID(*req.PlayerID)
|
var found bool
|
||||||
|
if lobby != nil {
|
||||||
|
_, found = lobby.GetPlayerByUUID(*req.PlayerID)
|
||||||
|
}
|
||||||
if found {
|
if found {
|
||||||
c.IndentedJSON(
|
c.IndentedJSON(
|
||||||
http.StatusOK,
|
http.StatusOK,
|
||||||
@ -90,6 +94,7 @@ func JoinPrivateGame(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
c.IndentedJSON(http.StatusNotFound, req)
|
c.IndentedJSON(http.StatusNotFound, req)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,6 +157,14 @@ 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")
|
||||||
}
|
}
|
||||||
|
@ -75,6 +75,8 @@ 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()
|
||||||
}
|
}
|
||||||
@ -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 {
|
func (conn *Connection) Write(msg []byte) error {
|
||||||
conn.wsWriteLock.Lock()
|
conn.wsWriteLock.Lock()
|
||||||
defer conn.wsWriteLock.Unlock()
|
defer conn.wsWriteLock.Unlock()
|
||||||
|
@ -31,12 +31,16 @@ 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.IsFull() {
|
if l.ContainsTwoPlayers() {
|
||||||
l.Game.StartHandling()
|
l.Game.StartHandling()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Lobby) IsFull() bool {
|
func (w *Lobby) AreBothPlayersConnected() bool {
|
||||||
|
return w.Game.AreBothPlayersConnected()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Lobby) ContainsTwoPlayers() bool {
|
||||||
return len(w.Game.GetPlayers()) == 2
|
return len(w.Game.GetPlayers()) == 2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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.IsFull() {
|
if !lobby.ContainsTwoPlayers() {
|
||||||
return lobby
|
return lobby
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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.IsFull() {
|
if lobby == nil || lobby.AreBothPlayersConnected() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return lobby
|
return lobby
|
||||||
|
Loading…
Reference in New Issue
Block a user