Skip to content

Commit

Permalink
feat: make Resign call abort if len(moves) < 2
Browse files Browse the repository at this point in the history
  • Loading branch information
thehowl committed Sep 26, 2023
1 parent df9744d commit ca259bb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
16 changes: 13 additions & 3 deletions realm/chess.gno
Original file line number Diff line number Diff line change
Expand Up @@ -492,21 +492,28 @@ func Abort(gameID string) string {
std.AssertOriginCall()

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

func abort(g *Game) error {
if len(g.Position.Moves) >= 2 {
panic("game can no longer be aborted; if you wish to quit, resign")
return errors.New("game can no longer be aborted; if you wish to quit, resign")
}

caller := std.GetOrigCaller()
if caller != g.White && caller != g.Black {
panic("you are not involved in this game")
return errors.New("you are not involved in this game")
}
g.State = GameStateAborted
g.Concluder = &caller
g.DrawOfferer = nil
g.Winner = WinnerNone

return g.json()
return nil
}

func Resign(gameID string) string {
Expand All @@ -521,6 +528,9 @@ func Resign(gameID string) string {
}

func resign(g *Game) error {
if len(g.Position.Moves) < 2 {
return abort(g)
}
caller := std.GetOrigCaller()
switch caller {
case g.Black:
Expand Down
8 changes: 4 additions & 4 deletions realm/lobby_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ func TestLobbyJoin_HasOpenGames(t *testing.T) {

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

0 comments on commit ca259bb

Please sign in to comment.