Skip to content

Commit

Permalink
feat: auto-resign from open games when joining lobby
Browse files Browse the repository at this point in the history
  • Loading branch information
thehowl committed Sep 24, 2023
1 parent b2e9b19 commit ba93264
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
11 changes: 9 additions & 2 deletions realm/chess.gno
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,14 @@ func Resign(gameID string) string {
std.AssertOriginCall()

g := getGame(gameID, true)
err := resign(g)
if err != nil {
panic(err.String())
}
return g.json()
}

func resign(g *Game) error {
caller := std.GetOrigCaller()
switch caller {
case g.Black:
Expand All @@ -523,12 +530,12 @@ func Resign(gameID string) string {
g.State = GameStateResigned
g.Winner = WinnerBlack
default:
panic("you are not involved in this game")
return errors.New("you are not involved in this game")
}
g.DrawOfferer = nil
g.saveResult()

return g.json()
return nil
}

// DrawOffer creates a draw offer in the current game, if one doesn't already
Expand Down
11 changes: 10 additions & 1 deletion realm/lobby.gno
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,16 @@ func LobbyJoin(seconds, increment int) {
// Ensure that user's previous games are finished (or timed out).
caller := std.GetOrigCaller()

assertGamesFinished(getUserGames(caller))
games := getUserGames(caller)
// XXX: temporary to avoid ever prohibiting a user from joining the lobby.
// when possible, change back to assertGamesFinished(games)
for _, g := range games {
if !g.State.IsFinished() {
if err := resign(g); err != nil {
panic("internal error (could not resign game " + g.ID + "): " + err.Error())
}
}
}
assertUserNotInLobby(caller)

// remove caller from lobbyPlayer2Game, so LobbyGameFound
Expand Down

0 comments on commit ba93264

Please sign in to comment.