diff --git a/.github/workflows/realm.yml b/.github/workflows/realm.yml index 20eff82..498e717 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 -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 diff --git a/realm/chess.gno b/realm/chess.gno index 9703658..106aa73 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.Error()) + } + 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 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) + } }