Skip to content

Commit

Permalink
chore: fixup
Browse files Browse the repository at this point in the history
Signed-off-by: moul <[email protected]>
  • Loading branch information
moul committed Sep 26, 2023
1 parent 220f7a8 commit 84d10ed
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
28 changes: 25 additions & 3 deletions realm/chess.gno
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ func xNewGame(opponentRaw string, seconds, increment int) string {

opponent := parsePlayer(opponentRaw)
caller := std.GetOrigCaller()
getPlayerStats(opponent).StartedGames++
getPlayerStats(caller).StartedGames++
assertUserNotInLobby(caller)

return newGame(caller, opponent, seconds, increment).json()
Expand Down Expand Up @@ -361,8 +363,18 @@ func MakeMove(gameID, from, to string, promote Piece) string {

caller := std.GetOrigCaller()

stats := getPlayerStats(caller)
stats.Moves++
// FIXME: create better helpers to avoid if/else conditions.
whiteStats := getPlayerStats(g.White)
blackStats := getPlayerStats(g.Black)
var callerStats, opponentStats *playerStats
if caller == g.White {
callerStats = whiteStats
opponentStats = blackStats
} else {
callerStats = blackStats
opponentStats = whiteStats
}
callerStats.Moves++

if (isBlack && g.Black != caller) ||
(!isBlack && g.White != caller) {
Expand All @@ -372,6 +384,9 @@ func MakeMove(gameID, from, to string, promote Piece) string {

// game is time controlled? add move to time control
if g.Time != nil {
whiteStats.TimedoutGames++
blackStats.TimedoutGames++

valid := g.Time.AddMove()
if !valid && len(g.Position.Moves) < 2 {
g.State = GameStateAborted
Expand Down Expand Up @@ -424,13 +439,20 @@ func MakeMove(gameID, from, to string, promote Piece) string {
case o == Checkmate && isBlack:
g.State = GameStateCheckmated
g.Winner = WinnerBlack
blackStats.WonGames++
whiteStats.LostGames++
blackStats.SeriousGames++
whiteStats.SeriousGames++
case o == Checkmate && !isBlack:
g.State = GameStateCheckmated
g.Winner = WinnerWhite
whiteStats.WonGames++
blackStats.LostGames++
blackStats.SeriousGames++
whiteStats.SeriousGames++
case o == Stalemate:
g.State = GameStateStalemate
g.Winner = WinnerDraw

case o == Drawn75Move:
g.State = GameStateDrawn75Move
g.Winner = WinnerDraw
Expand Down
2 changes: 1 addition & 1 deletion realm/chess_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ var commandTests = [...]string{
stats white
# contains addr:g1white moves:4 started:1 won:1 lost:0 timedout:0 resigned:0 drawn:0 serious:1
stats black
# contains addr:g1white moves:3 started:1 won:0 lost:1 timedout:0 resigned:0 drawn:0 serious:1
# contains addr:g1black moves:3 started:1 won:0 lost:1 timedout:0 resigned:0 drawn:0 serious:1
`,
/* XXX: TEMPORARILY DISABLED
` name DrawByAgreement
Expand Down

0 comments on commit 84d10ed

Please sign in to comment.