Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: auto-resign from open games when joining lobby #123

Merged
merged 5 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/realm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ jobs:
- uses: actions/setup-go@v4
with:
go-version: 'stable'
- run: go install github.com/gnolang/gno/gnovm/cmd/gno
- run: gno test --verbose ./realm
- run: go mod download -x
- run: go run github.com/gnolang/gno/gnovm/cmd/gno test -verbose -run 'Test([^P]|P[^e])' ./realm
22 changes: 22 additions & 0 deletions .github/workflows/rules.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: rules

on:
pull_request:
paths:
- "realm/rules.gno"
push:
branches:
- master
- main

jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: 'stable'
- run: go mod download -x
- run: go run github.com/gnolang/gno/gnovm/cmd/gno test -verbose -run 'TestPerft' ./realm
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.Error())
}
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
15 changes: 7 additions & 8 deletions realm/lobby_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ func TestLobbyGameFound(t *testing.T) {

func TestLobbyJoin_HasOpenGames(t *testing.T) {
cleanup()
std.TestSetOrigCaller(white)
g := &Game{
ID: "123",
White: white,
Expand All @@ -176,12 +175,12 @@ func TestLobbyJoin_HasOpenGames(t *testing.T) {
addToUser2Games(white, g)
addToUser2Games(black, g)

defer func() {
e := recover()
if !strings.Contains(fmt.Sprint(e), "game 123 is not yet finished") {
t.Errorf("expecting 'game is not finished' panic, got %v", e)
}
}()

std.TestSetOrigCaller(white)
LobbyJoin(10*60, 5)
if g.State != GameStateResigned {
t.Errorf("state wrong: want %d got %d", GameStateResigned, g.State)
}
if g.Winner != WinnerBlack {
t.Errorf("winner wrong: want %q got %q", "black", g.Winner)
}
}