From ba93264fabd2dd3a8ab03b7e685644fde1819654 Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Mon, 25 Sep 2023 01:32:35 +0200 Subject: [PATCH 1/5] feat: auto-resign from open games when joining lobby --- realm/chess.gno | 11 +++++++++-- realm/lobby.gno | 11 ++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/realm/chess.gno b/realm/chess.gno index 9703658..91e3afd 100644 --- a/realm/chess.gno +++ b/realm/chess.gno @@ -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: @@ -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 diff --git a/realm/lobby.gno b/realm/lobby.gno index 8f5db18..894f11b 100644 --- a/realm/lobby.gno +++ b/realm/lobby.gno @@ -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 From 9b2eea9b2895c5b67390aaaade379f971687472a Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Mon, 25 Sep 2023 01:44:12 +0200 Subject: [PATCH 2/5] update workflow --- .github/workflows/realm.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/realm.yml b/.github/workflows/realm.yml index 20eff82..b560fb3 100644 --- a/.github/workflows/realm.yml +++ b/.github/workflows/realm.yml @@ -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 . From a426fc852c8c6a5e8dbad6d5e295f5eed80c24f3 Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Mon, 25 Sep 2023 01:49:49 +0200 Subject: [PATCH 3/5] string -> error --- .github/workflows/realm.yml | 2 +- realm/chess.gno | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/realm.yml b/.github/workflows/realm.yml index b560fb3..bf3d3fe 100644 --- a/.github/workflows/realm.yml +++ b/.github/workflows/realm.yml @@ -21,4 +21,4 @@ jobs: with: go-version: 'stable' - run: go mod download -x - - run: go run github.com/gnolang/gno/gnovm/cmd/gno test -verbose . + - run: go run github.com/gnolang/gno/gnovm/cmd/gno test -verbose ./realm diff --git a/realm/chess.gno b/realm/chess.gno index 91e3afd..106aa73 100644 --- a/realm/chess.gno +++ b/realm/chess.gno @@ -515,7 +515,7 @@ func Resign(gameID string) string { g := getGame(gameID, true) err := resign(g) if err != nil { - panic(err.String()) + panic(err.Error()) } return g.json() } From e7071991adbbe37a212d86ae4ee110dd1df8fb66 Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Mon, 25 Sep 2023 01:54:54 +0200 Subject: [PATCH 4/5] dont run perft tests normally --- .github/workflows/realm.yml | 2 +- .github/workflows/rules.yml | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/rules.yml diff --git a/.github/workflows/realm.yml b/.github/workflows/realm.yml index bf3d3fe..498e717 100644 --- a/.github/workflows/realm.yml +++ b/.github/workflows/realm.yml @@ -21,4 +21,4 @@ jobs: with: go-version: 'stable' - run: go mod download -x - - run: go run github.com/gnolang/gno/gnovm/cmd/gno test -verbose ./realm + - run: go run github.com/gnolang/gno/gnovm/cmd/gno test -verbose -run 'Test([^P]|P[^e])' ./realm diff --git a/.github/workflows/rules.yml b/.github/workflows/rules.yml new file mode 100644 index 0000000..ab4b62a --- /dev/null +++ b/.github/workflows/rules.yml @@ -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 From b40f9c7c71b67da94d627f61f1cddf6627c43f4f Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Mon, 25 Sep 2023 02:00:32 +0200 Subject: [PATCH 5/5] fix test --- realm/lobby_test.gno | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/realm/lobby_test.gno b/realm/lobby_test.gno index 95a2c58..48a6c0f 100644 --- a/realm/lobby_test.gno +++ b/realm/lobby_test.gno @@ -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, @@ -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) + } }