Skip to content

Commit

Permalink
feat: auto-resign from open games when joining lobby (#123)
Browse files Browse the repository at this point in the history
* feat: auto-resign from open games when joining lobby

* update workflow

* string -> error

* dont run perft tests normally

* fix test
  • Loading branch information
thehowl authored Sep 25, 2023
1 parent eddf3b4 commit 4c3da92
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
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)
}
}

0 comments on commit 4c3da92

Please sign in to comment.