Playground
games.gno
package kewrchess
import (
"errors"
"time"
"gno.land/p/g1g7dna0gp4nec5rza4q25htj0cjgswrxefp37ep/chess"
)
// Timeout duration (seconds) for challenge proposals and turn timeouts
var ChallengeWindowSeconds int64 = 180 // 3-minute challenge window for settlement proposals
var TurnTimeoutSeconds int64 = 120 // 2-minute move timeout when opponent is AFK (Abandonment)
// getVerifiedCaller validates the format and availability of the caller address.
func getVerifiedCaller(caller address) (address, error) {
if caller == "" {
return "", errors.New("alamat caller tidak boleh kosong")
}
return caller, nil
}
// Stores all chess games permanently on the blockchain
var games []*Game
var nextGameID int
// On-chain matchmaking queue (FIFO)
var matchmakingQueue []address
// init automatically runs when contract is initialized
func init() {
// Initial demo game setup: White player in Game #0
pemberiTantangan := address("g1g7dna0gp4nec5rza4q25htj0cjgswrxefp37ep")
penerimaTantangan := address("g1kewrchessplayerblack777777777777777") // Simulated opponent
newGame := NewGame(nextGameID, pemberiTantangan, penerimaTantangan)
games = append(games, newGame)
nextGameID++
}
// CreateGame creates a new direct game challenge
func CreateGame(caller address, opponent address) error {
vCaller, err := getVerifiedCaller(caller)
if err != nil {
return err
}
if vCaller == opponent {
return errors.New("tidak bisa bermain melawan diri sendiri")
}
newGame := NewGame(nextGameID, vCaller, opponent)
games = append(games, newGame)
nextGameID++
return nil
}
// JoinQueue registers a player into the on-chain matchmaking queue.
// If the queue is empty, the player is added to the queue.
// If another player is already waiting, both players are immediately matched into a new game.
func JoinQueue(caller address) error {
vCaller, err := getVerifiedCaller(caller)
if err != nil {
return err
}
// Check if player is already in queue
for _, addr := range matchmakingQueue {
if addr == vCaller {
return errors.New("anda sudah berada di dalam antrean matchmaking")
}
}
// If queue is empty, add caller to queue
if len(matchmakingQueue) == 0 {
matchmakingQueue = append(matchmakingQueue, vCaller)
return nil
}
// If a player is waiting, take the first player (FIFO)
opponent := matchmakingQueue[0]
matchmakingQueue = matchmakingQueue[1:]
// Create new game: waiting player becomes White, caller becomes Black
newGame := NewGame(nextGameID, opponent, vCaller)
games = append(games, newGame)
nextGameID++
return nil
}
// LeaveQueue cancels and removes a player from the on-chain matchmaking queue
func LeaveQueue(caller address) error {
vCaller, err := getVerifiedCaller(caller)
if err != nil {
return err
}
for i, addr := range matchmakingQueue {
if addr == vCaller {
matchmakingQueue = append(matchmakingQueue[:i], matchmakingQueue[i+1:]...)
return nil
}
}
return errors.New("anda tidak sedang berada di dalam antrean matchmaking")
}
func makeMoveInternal(caller address, gameID int, fromRow, fromCol, toRow, toCol int, promoPiece string) error {
vCaller, err := getVerifiedCaller(caller)
if err != nil {
return err
}
if gameID < 0 || gameID >= len(games) {
return errors.New("game tidak ditemukan")
}
g := games[gameID]
if g.status != "PLAYING" {
return errors.New("game sudah selesai")
}
if g.isWhiteTurn && vCaller != g.white {
return errors.New("bukan giliran Anda (Putih)")
}
if !g.isWhiteTurn && vCaller != g.black {
return errors.New("bukan giliran Anda (Hitam)")
}
if fromRow < 0 || fromRow > 7 || fromCol < 0 || fromCol > 7 ||
toRow < 0 || toRow > 7 || toCol < 0 || toCol > 7 {
return errors.New("koordinat di luar papan")
}
piece := g.board[fromRow][fromCol]
if piece == "." {
return errors.New("tidak ada bidak di posisi asal")
}
// Delegate move validation to package p/demo/chess
if !chess.IsValidMove(g.board, fromRow, fromCol, toRow, toCol, g.isWhiteTurn, g.castling, g.enPassantCol) {
return errors.New("langkah catur tidak valid")
}
isWhite := chess.IsWhitePiece(piece)
// Validate pawn promotion
isPromotion := (piece == "P" && toRow == 0) || (piece == "p" && toRow == 7)
var finalPiece string = piece
if isPromotion {
promoted, err := chess.NormalizePromotionPiece(promoPiece, isWhite)
if err != nil {
return err
}
finalPiece = promoted
}
isCastling := (piece == "K" || piece == "k") && chess.Abs(toCol-fromCol) == 2
isEnPassant := (piece == "P" || piece == "p") && toCol == g.enPassantCol && fromCol != toCol && g.board[toRow][toCol] == "."
// Verify move does not leave own king in check
origDest := g.board[toRow][toCol]
g.board[toRow][toCol] = piece
g.board[fromRow][fromCol] = "."
var epRow int
var epPiece string
if isEnPassant {
if isWhite {
epRow = 3
} else {
epRow = 4
}
epPiece = g.board[epRow][toCol]
g.board[epRow][toCol] = "."
}
inCheck := chess.IsKingInCheck(g.board, isWhite)
// Revert simulation
g.board[fromRow][fromCol] = piece
g.board[toRow][toCol] = origDest
if isEnPassant {
g.board[epRow][toCol] = epPiece
}
if inCheck {
return errors.New("langkah ilegal: tidak boleh membiarkan Raja Anda terkena skak")
}
// Execute move
g.board[toRow][toCol] = finalPiece
g.board[fromRow][fromCol] = "."
// Execute En Passant capture
if isEnPassant {
if isWhite {
g.board[3][toCol] = "."
} else {
g.board[4][toCol] = "."
}
}
// Move Rook during castling
if isCastling {
if isWhite {
if toCol == 6 { // Kingside
g.board[7][5] = "R"
g.board[7][7] = "."
} else if toCol == 2 { // Queenside
g.board[7][3] = "R"
g.board[7][0] = "."
}
} else {
if toCol == 6 { // Kingside
g.board[0][5] = "r"
g.board[0][7] = "."
} else if toCol == 2 { // Queenside
g.board[0][3] = "r"
g.board[0][0] = "."
}
}
}
// Update castling rights
if piece == "K" {
g.castling.WhiteKingMoved = true
} else if piece == "k" {
g.castling.BlackKingMoved = true
} else if piece == "R" {
if fromRow == 7 && fromCol == 0 {
g.castling.WhiteLeftRookMoved = true
} else if fromRow == 7 && fromCol == 7 {
g.castling.WhiteRightRookMoved = true
}
} else if piece == "r" {
if fromRow == 0 && fromCol == 0 {
g.castling.BlackLeftRookMoved = true
} else if fromRow == 0 && fromCol == 7 {
g.castling.BlackRightRookMoved = true
}
}
// Update en passant vulnerability
if (piece == "P" || piece == "p") && chess.Abs(toRow-fromRow) == 2 {
g.enPassantCol = fromCol
} else {
g.enPassantCol = -1
}
// Detect Checkmate or Stalemate for opponent
opponentIsWhite := !isWhite
if !chess.HasAnyLegalMoves(g.board, opponentIsWhite, g.castling, g.enPassantCol) {
if chess.IsKingInCheck(g.board, opponentIsWhite) {
if isWhite {
g.status = "WHITE_WON"
} else {
g.status = "BLACK_WON"
}
} else {
g.status = "DRAW"
}
}
g.isWhiteTurn = !g.isWhiteTurn
g.drawOfferedBy = ""
g.turnDeadline = 0
g.turnChallengedBy = ""
return nil
}
// MakeMove moves a piece (defaults to Queen on promotion)
func MakeMove(caller address, gameID int, fromRow, fromCol, toRow, toCol int) error {
return makeMoveInternal(caller, gameID, fromRow, fromCol, toRow, toCol, "Q")
}
// MakeMovePromote moves a piece with a specified promotion piece (Q, R, B, N)
func MakeMovePromote(caller address, gameID int, fromRow, fromCol, toRow, toCol int, promoPiece string) error {
return makeMoveInternal(caller, gameID, fromRow, fromCol, toRow, toCol, promoPiece)
}
// Resign resigns the game for the calling player
func Resign(caller address, gameID int) error {
vCaller, err := getVerifiedCaller(caller)
if err != nil {
return err
}
if gameID < 0 || gameID >= len(games) {
return errors.New("game tidak ditemukan")
}
g := games[gameID]
if g.status != "PLAYING" && g.status != "PROPOSED" {
return errors.New("game sudah selesai")
}
if vCaller != g.white && vCaller != g.black {
return errors.New("hanya pemain dalam game ini yang dapat menyerah")
}
g.resignedBy = vCaller
if vCaller == g.white {
g.status = "BLACK_WON"
} else {
g.status = "WHITE_WON"
}
g.settledBy = vCaller
g.drawOfferedBy = ""
g.turnDeadline = 0
g.turnChallengedBy = ""
g.challengeDeadline = 0
return nil
}
// OfferDraw offers a draw to the opponent
func OfferDraw(caller address, gameID int) error {
vCaller, err := getVerifiedCaller(caller)
if err != nil {
return err
}
if gameID < 0 || gameID >= len(games) {
return errors.New("game tidak ditemukan")
}
g := games[gameID]
if g.status != "PLAYING" {
return errors.New("game sudah selesai")
}
if vCaller != g.white && vCaller != g.black {
return errors.New("hanya pemain dalam game ini yang dapat menawarkan remis")
}
if g.drawOfferedBy == vCaller {
return errors.New("anda sudah mengajukan tawaran remis")
}
if g.drawOfferedBy != "" && g.drawOfferedBy != vCaller {
g.status = "DRAW"
g.drawOfferedBy = ""
return nil
}
g.drawOfferedBy = vCaller
return nil
}
// AcceptDraw accepts a draw offer from the opponent
func AcceptDraw(caller address, gameID int) error {
vCaller, err := getVerifiedCaller(caller)
if err != nil {
return err
}
if gameID < 0 || gameID >= len(games) {
return errors.New("game tidak ditemukan")
}
g := games[gameID]
if g.status != "PLAYING" {
return errors.New("game sudah selesai")
}
if vCaller != g.white && vCaller != g.black {
return errors.New("hanya pemain dalam game ini yang dapat menerima remis")
}
if g.drawOfferedBy == "" {
return errors.New("tidak ada tawaran remis yang sedang aktif")
}
if g.drawOfferedBy == vCaller {
return errors.New("tidak bisa menerima tawaran remis yang Anda ajukan sendiri")
}
g.status = "DRAW"
g.drawOfferedBy = ""
return nil
}
// DeclineDraw declines a draw offer from the opponent
func DeclineDraw(caller address, gameID int) error {
vCaller, err := getVerifiedCaller(caller)
if err != nil {
return err
}
if gameID < 0 || gameID >= len(games) {
return errors.New("game tidak ditemukan")
}
g := games[gameID]
if g.status != "PLAYING" {
return errors.New("game sudah selesai")
}
if vCaller != g.white && vCaller != g.black {
return errors.New("hanya pemain dalam game ini yang dapat menolak remis")
}
if g.drawOfferedBy == "" {
return errors.New("tidak ada tawaran remis yang sedang aktif")
}
if g.drawOfferedBy == vCaller {
return errors.New("tidak bisa menolak tawaran remis yang Anda ajukan sendiri")
}
g.drawOfferedBy = ""
return nil
}
// ProposeSettlement submits a victory claim for off-chain chess results.
// Prevents fraudulent opponent moves: proposal opens a challenge period.
// Opponent can confirm immediately (ConfirmSettlement), or dispute if cheated (DisputeSettlement).
// If opponent does not respond, winner can finalize after ChallengeWindowSeconds expires.
func ProposeSettlement(caller address, gameID int, movesUCI string) error {
vCaller, err := getVerifiedCaller(caller)
if err != nil {
return err
}
if gameID < 0 || gameID >= len(games) {
return errors.New("game tidak ditemukan")
}
g := games[gameID]
if vCaller != g.white && vCaller != g.black {
return errors.New("hanya pemain dalam game ini yang dapat mengajukan settlement")
}
if g.settledBy != "" {
return errors.New("game ini sudah diselesaikan di blockchain")
}
if g.status != "PLAYING" {
return errors.New("game ini sedang dalam status " + g.status)
}
// Replay chess moves from the beginning using package gno.land/p/demo/chess
finalBoard, _, status, err := chess.ReplayUCI(movesUCI)
if err != nil {
return errors.New("verifikasi langkah catur gagal: " + err.Error())
}
if status == "PLAYING" {
return errors.New("rangkaian langkah catur belum menghasilkan skakmat atau remis")
}
// Verify winner claim rights
if status == "WHITE_WON" && vCaller != g.white {
return errors.New("hanya pemain Putih yang berhak klaim kemenangan untuk White")
}
if status == "BLACK_WON" && vCaller != g.black {
return errors.New("hanya pemain Hitam yang berhak klaim kemenangan untuk Black")
}
g.board = finalBoard
g.proposedBy = vCaller
g.pendingStatus = status
g.challengeDeadline = time.Now().Unix() + ChallengeWindowSeconds
g.status = "PROPOSED"
g.drawOfferedBy = ""
g.turnDeadline = 0
g.turnChallengedBy = ""
return nil
}
// ConfirmSettlement allows opponent to immediately approve final result without waiting for challenge window
func ConfirmSettlement(caller address, gameID int) error {
vCaller, err := getVerifiedCaller(caller)
if err != nil {
return err
}
if gameID < 0 || gameID >= len(games) {
return errors.New("game tidak ditemukan")
}
g := games[gameID]
if g.status != "PROPOSED" {
return errors.New("tidak ada proposal settlement yang sedang aktif")
}
if vCaller != g.white && vCaller != g.black {
return errors.New("hanya pemain dalam game ini yang dapat mengonfirmasi")
}
if vCaller == g.proposedBy {
return errors.New("tidak bisa mengonfirmasi proposal yang Anda ajukan sendiri")
}
g.status = g.pendingStatus
g.settledBy = g.proposedBy
g.challengeDeadline = 0
return nil
}
// FinalizeSettlement officially finalizes the game after the challenge period has passed
func FinalizeSettlement(caller address, gameID int) error {
vCaller, err := getVerifiedCaller(caller)
if err != nil {
return err
}
if gameID < 0 || gameID >= len(games) {
return errors.New("game tidak ditemukan")
}
g := games[gameID]
if g.status != "PROPOSED" {
return errors.New("tidak ada proposal settlement yang sedang aktif")
}
if vCaller != g.white && vCaller != g.black {
return errors.New("hanya pemain dalam game ini yang dapat memfinalisasi")
}
now := time.Now().Unix()
if now < g.challengeDeadline {
return errors.New("jendela sanggahan (challenge period) belum berakhir")
}
g.status = g.pendingStatus
g.settledBy = g.proposedBy
g.challengeDeadline = 0
return nil
}
// DisputeSettlement allows opponent to dispute a fraudulent settlement proposal
func DisputeSettlement(caller address, gameID int) error {
vCaller, err := getVerifiedCaller(caller)
if err != nil {
return err
}
if gameID < 0 || gameID >= len(games) {
return errors.New("game tidak ditemukan")
}
g := games[gameID]
if g.status != "PROPOSED" {
return errors.New("tidak ada proposal settlement yang sedang aktif")
}
if vCaller != g.white && vCaller != g.black {
return errors.New("hanya pemain dalam game ini yang dapat menyanggah")
}
if vCaller == g.proposedBy {
return errors.New("tidak bisa menyanggah proposal yang Anda ajukan sendiri")
}
// Cancel fraudulent proposal and revert game status back to PLAYING
g.status = "PLAYING"
g.proposedBy = ""
g.pendingStatus = ""
g.challengeDeadline = 0
return nil
}
// SettleGame proposes settlement and finalizes immediately if challenge window is 0
func SettleGame(caller address, gameID int, movesUCI string) error {
err := ProposeSettlement(caller, gameID, movesUCI)
if err != nil {
return err
}
if ChallengeWindowSeconds <= 0 {
return FinalizeSettlement(caller, gameID)
}
return nil
}
// StartTurnTimer is initiated when opponent goes AFK or closes browser (Rage Quit) during an off-chain game.
// Player submits current move history (currentMovesUCI).
// Smart contract validates the board and ensures it is the OPPONENT'S TURN.
func StartTurnTimer(caller address, gameID int, currentMovesUCI string) error {
vCaller, err := getVerifiedCaller(caller)
if err != nil {
return err
}
if gameID < 0 || gameID >= len(games) {
return errors.New("game tidak ditemukan")
}
g := games[gameID]
if g.status != "PLAYING" {
return errors.New("game sudah selesai")
}
if vCaller != g.white && vCaller != g.black {
return errors.New("hanya pemain dalam game ini yang dapat menuntut timer giliran")
}
currentBoard, nextIsWhiteTurn, status, err := chess.ReplayUCI(currentMovesUCI)
if err != nil {
return errors.New("verifikasi langkah posisi saat ini gagal: " + err.Error())
}
if status != "PLAYING" {
return errors.New("posisi catur ini sudah selesai (skakmat/remis), gunakan SettleGame")
}
// Validate that next turn is the opponent's turn, not the caller's
if vCaller == g.white && nextIsWhiteTurn {
return errors.New("saat ini adalah giliran Anda sendiri (Putih), lakukan langkah Anda terlebih dahulu")
}
if vCaller == g.black && !nextIsWhiteTurn {
return errors.New("saat ini adalah giliran Anda sendiri (Hitam), lakukan langkah Anda terlebih dahulu")
}
g.board = currentBoard
g.isWhiteTurn = nextIsWhiteTurn
g.turnChallengedBy = vCaller
g.turnDeadline = time.Now().Unix() + TurnTimeoutSeconds
return nil
}
// ClaimTimeout allows the challenger to win the game after the opponent goes AFK/rage quits
// and turnDeadline has passed without a response move from the opponent.
func ClaimTimeout(caller address, gameID int) error {
vCaller, err := getVerifiedCaller(caller)
if err != nil {
return err
}
if gameID < 0 || gameID >= len(games) {
return errors.New("game tidak ditemukan")
}
g := games[gameID]
if g.status != "PLAYING" {
return errors.New("game sudah selesai")
}
if g.turnDeadline == 0 {
return errors.New("tidak ada batas waktu giliran yang sedang berjalan")
}
now := time.Now().Unix()
if now < g.turnDeadline {
return errors.New("batas waktu giliran lawan belum habis")
}
if vCaller != g.turnChallengedBy {
return errors.New("hanya pemain yang menuntut timer yang dapat mengklaim kemenangan timeout")
}
if vCaller == g.white {
g.status = "WHITE_WON"
} else {
g.status = "BLACK_WON"
}
g.settledBy = vCaller
g.turnDeadline = 0
g.turnChallengedBy = ""
return nil
}
// GetGamesCount returns the total number of games
func GetGamesCount() int {
return len(games)
}
// GetBoard returns the 8x8 board for a given game ID
func GetBoard(gameID int) [8][8]string {
if gameID < 0 || gameID >= len(games) {
panic("game tidak ditemukan")
}
return [8][8]string(games[gameID].board)
}
// GetWhitePlayer returns the White player address for a given game ID
func GetWhitePlayer(gameID int) address {
if gameID < 0 || gameID >= len(games) {
panic("game tidak ditemukan")
}
return games[gameID].white
}
// GetBlackPlayer returns the Black player address for a given game ID
func GetBlackPlayer(gameID int) address {
if gameID < 0 || gameID >= len(games) {
panic("game tidak ditemukan")
}
return games[gameID].black
}
// IsWhiteTurn returns whether it is White's turn for a given game ID
func IsWhiteTurn(gameID int) bool {
if gameID < 0 || gameID >= len(games) {
panic("game tidak ditemukan")
}
return games[gameID].isWhiteTurn
}
// GetGameStatus returns the game status for a given game ID
func GetGameStatus(gameID int) string {
if gameID < 0 || gameID >= len(games) {
panic("game tidak ditemukan")
}
return games[gameID].status
}
// GetDrawOfferedBy returns the address that offered a draw, or empty address if none
func GetDrawOfferedBy(gameID int) address {
if gameID < 0 || gameID >= len(games) {
panic("game tidak ditemukan")
}
return games[gameID].drawOfferedBy
}
// GetResignedBy returns the address of the player who resigned, or empty address if none
func GetResignedBy(gameID int) address {
if gameID < 0 || gameID >= len(games) {
panic("game tidak ditemukan")
}
return games[gameID].resignedBy
}
// GetQueueCount returns the number of players waiting in the matchmaking queue
func GetQueueCount() int {
return len(matchmakingQueue)
}
// IsInQueue checks if an address is currently waiting in the matchmaking queue
func IsInQueue(addr address) bool {
for _, a := range matchmakingQueue {
if a == addr {
return true
}
}
return false
}
// GetActiveGameForPlayer searches for the latest active game ("PLAYING") for a given address.
// Returns -1 if no active game found.
func GetActiveGameForPlayer(addr address) int {
for i := len(games) - 1; i >= 0; i-- {
g := games[i]
if (g.white == addr || g.black == addr) && g.status == "PLAYING" {
return g.id
}
}
return -1
}
// GetLatestGameForPlayer returns the latest game ID involving the player, or -1 if none.
func GetLatestGameForPlayer(addr address) int {
for i := len(games) - 1; i >= 0; i-- {
g := games[i]
if g.white == addr || g.black == addr {
return g.id
}
}
return -1
}
// GetSettledBy returns the address that finalized the game settlement on-chain
func GetSettledBy(gameID int) address {
if gameID < 0 || gameID >= len(games) {
panic("game tidak ditemukan")
}
return games[gameID].settledBy
}
// GetProposedBy returns the address that proposed settlement
func GetProposedBy(gameID int) address {
if gameID < 0 || gameID >= len(games) {
panic("game tidak ditemukan")
}
return games[gameID].proposedBy
}
// GetPendingStatus returns the pending status of settlement
func GetPendingStatus(gameID int) string {
if gameID < 0 || gameID >= len(games) {
panic("game tidak ditemukan")
}
return games[gameID].pendingStatus
}
// GetChallengeDeadline returns the deadline unix timestamp for challenging settlement
func GetChallengeDeadline(gameID int) int64 {
if gameID < 0 || gameID >= len(games) {
panic("game tidak ditemukan")
}
return games[gameID].challengeDeadline
}
// GetTurnDeadline returns the deadline unix timestamp for turn timeout
func GetTurnDeadline(gameID int) int64 {
if gameID < 0 || gameID >= len(games) {
panic("game tidak ditemukan")
}
return games[gameID].turnDeadline
}
// GetTurnChallengedBy returns the address that initiated the turn timer
func GetTurnChallengedBy(gameID int) address {
if gameID < 0 || gameID >= len(games) {
panic("game tidak ditemukan")
}
return games[gameID].turnChallengedBy
}
games_test.gno
package kewrchess
import (
"testing"
)
func TestGameInitAndRender(t *testing.T) {
if GetGamesCount() != 1 {
t.Fatalf("expected 1 game in init, got %d", GetGamesCount())
}
rendered := Render("")
if len(rendered) == 0 {
t.Fatal("rendered output is empty")
}
}
func TestResign(t *testing.T) {
white := GetWhitePlayer(0)
err := Resign(white, 0)
if err != nil {
t.Fatalf("unexpected error on resign: %v", err)
}
if GetGameStatus(0) != "BLACK_WON" {
t.Fatalf("expected status BLACK_WON, got %s", GetGameStatus(0))
}
}
func TestNewGameAndMoves(t *testing.T) {
p1 := address("g1p177777777777777777777777777777777777777")
p2 := address("g1p277777777777777777777777777777777777777")
err := CreateGame(p1, p2)
if err != nil {
t.Fatalf("failed to create game: %v", err)
}
gameID := GetGamesCount() - 1
// White moves pawn e2 to e4 (from 6,4 to 4,4)
err = MakeMove(p1, gameID, 6, 4, 4, 4)
if err != nil {
t.Fatalf("failed to make move e2e4: %v", err)
}
if IsWhiteTurn(gameID) {
t.Fatal("expected Black's turn after White move")
}
// Black moves pawn e7 to e5 (from 1,4 to 3,4)
err = MakeMove(p2, gameID, 1, 4, 3, 4)
if err != nil {
t.Fatalf("failed to make move e7e5: %v", err)
}
// Test draw offer
err = OfferDraw(p1, gameID)
if err != nil {
t.Fatalf("p1 failed to offer draw: %v", err)
}
if GetDrawOfferedBy(gameID) != p1 {
t.Fatalf("expected draw offered by p1, got %s", GetDrawOfferedBy(gameID))
}
// p2 accepts draw
err = AcceptDraw(p2, gameID)
if err != nil {
t.Fatalf("p2 failed to accept draw: %v", err)
}
if GetGameStatus(gameID) != "DRAW" {
t.Fatalf("expected status DRAW, got %s", GetGameStatus(gameID))
}
}
func TestMatchmakingQueue(t *testing.T) {
p1 := address("g1player11111111111111111111111111111111111")
p2 := address("g1player22222222222222222222222222222222222")
// 1. Initial queue count must be 0
if GetQueueCount() != 0 {
t.Fatalf("expected queue count 0, got %d", GetQueueCount())
}
// 2. Player 1 joins queue
err := JoinQueue(p1)
if err != nil {
t.Fatalf("p1 failed to join queue: %v", err)
}
if GetQueueCount() != 1 {
t.Fatalf("expected queue count 1, got %d", GetQueueCount())
}
if !IsInQueue(p1) {
t.Fatal("expected p1 to be in queue")
}
// 3. Joining twice must be rejected
err = JoinQueue(p1)
if err == nil {
t.Fatal("expected error when joining queue twice")
}
// 4. Test LeaveQueue
err = LeaveQueue(p1)
if err != nil {
t.Fatalf("p1 failed to leave queue: %v", err)
}
if GetQueueCount() != 0 {
t.Fatalf("expected queue count 0 after leave, got %d", GetQueueCount())
}
if IsInQueue(p1) {
t.Fatal("expected p1 to not be in queue after leave")
}
// 5. Leaving when not in queue should return an error
err = LeaveQueue(p1)
if err == nil {
t.Fatal("expected error when leaving queue while not in it")
}
// 6. Test Matchmaking: P1 joins, then P2 joins -> match automatically created
initialGameCount := GetGamesCount()
err = JoinQueue(p1)
if err != nil {
t.Fatalf("p1 failed to rejoin queue: %v", err)
}
err = JoinQueue(p2)
if err != nil {
t.Fatalf("p2 failed to join queue: %v", err)
}
// Queue should be empty again after matching
if GetQueueCount() != 0 {
t.Fatalf("expected queue count 0 after match, got %d", GetQueueCount())
}
// New game should be created
if GetGamesCount() != initialGameCount+1 {
t.Fatalf("expected game count %d, got %d", initialGameCount+1, GetGamesCount())
}
newGameID := GetGamesCount() - 1
if GetWhitePlayer(newGameID) != p1 {
t.Fatalf("expected White to be p1, got %s", GetWhitePlayer(newGameID))
}
if GetBlackPlayer(newGameID) != p2 {
t.Fatalf("expected Black to be p2, got %s", GetBlackPlayer(newGameID))
}
if GetGameStatus(newGameID) != "PLAYING" {
t.Fatalf("expected status PLAYING, got %s", GetGameStatus(newGameID))
}
// Verify GetActiveGameForPlayer
if GetActiveGameForPlayer(p1) != newGameID {
t.Fatalf("expected active game for p1 to be %d, got %d", newGameID, GetActiveGameForPlayer(p1))
}
if GetActiveGameForPlayer(p2) != newGameID {
t.Fatalf("expected active game for p2 to be %d, got %d", newGameID, GetActiveGameForPlayer(p2))
}
}
func TestSettleGame(t *testing.T) {
// For testing instant auto-settlement
oldWindow := ChallengeWindowSeconds
ChallengeWindowSeconds = 0
defer func() { ChallengeWindowSeconds = oldWindow }()
p1 := address("g1winner111111111111111111111111111111111")
p2 := address("g1loser2222222222222222222222222222222222")
err := CreateGame(p1, p2)
if err != nil {
t.Fatalf("failed to create game: %v", err)
}
gameID := GetGamesCount() - 1
scholarsMate := "e2e4 e7e5 d1h5 b8c6 f1c4 g8f6 h5f7"
// 1. Third party caller should not be able to settle
p3 := address("g1intruder3333333333333333333333333333333")
err = SettleGame(p3, gameID, scholarsMate)
if err == nil {
t.Fatal("expected error when non-player tries to settle")
}
// 2. Incomplete game (not checkmate) should fail
err = SettleGame(p1, gameID, "e2e4 e7e5")
if err == nil {
t.Fatal("expected error when moves do not end in checkmate or draw")
}
// 3. Illegal move sequence should fail
err = SettleGame(p1, gameID, "e2e5")
if err == nil {
t.Fatal("expected error for illegal move sequence")
}
// 4. White won checkmate, but Black tries to claim
err = SettleGame(p2, gameID, scholarsMate)
if err == nil {
t.Fatal("expected error when losing player tries to claim victory")
}
// 5. Legitimate Winner (p1 / White) settles with valid Scholar's Mate checkmate
err = SettleGame(p1, gameID, scholarsMate)
if err != nil {
t.Fatalf("failed to settle game: %v", err)
}
if GetGameStatus(gameID) != "WHITE_WON" {
t.Fatalf("expected status WHITE_WON, got %s", GetGameStatus(gameID))
}
if GetSettledBy(gameID) != p1 {
t.Fatalf("expected settledBy p1, got %s", GetSettledBy(gameID))
}
// 6. Settling again should fail
err = SettleGame(p1, gameID, scholarsMate)
if err == nil {
t.Fatal("expected error when trying to settle already settled game")
}
}
func TestTwoPhaseSettlementAndDispute(t *testing.T) {
p1 := address("g1proposer111111111111111111111111111111")
p2 := address("g1victim22222222222222222222222222222222")
err := CreateGame(p1, p2)
if err != nil {
t.Fatalf("failed to create game: %v", err)
}
gameID := GetGamesCount() - 1
scholarsMate := "e2e4 e7e5 d1h5 b8c6 f1c4 g8f6 h5f7"
// 1. Proposer submits settlement proposal
err = ProposeSettlement(p1, gameID, scholarsMate)
if err != nil {
t.Fatalf("p1 failed to propose settlement: %v", err)
}
if GetGameStatus(gameID) != "PROPOSED" {
t.Fatalf("expected status PROPOSED, got %s", GetGameStatus(gameID))
}
if GetProposedBy(gameID) != p1 {
t.Fatalf("expected proposedBy p1, got %s", GetProposedBy(gameID))
}
// 2. Proposer cannot confirm their own proposal
err = ConfirmSettlement(p1, gameID)
if err == nil {
t.Fatal("expected error when proposer tries to confirm their own proposal")
}
// 3. Victim disputes fraudulent proposal -> status reverts to PLAYING
err = DisputeSettlement(p2, gameID)
if err != nil {
t.Fatalf("victim failed to dispute proposal: %v", err)
}
if GetGameStatus(gameID) != "PLAYING" {
t.Fatalf("expected status PLAYING after dispute, got %s", GetGameStatus(gameID))
}
if GetProposedBy(gameID) != "" {
t.Fatal("expected proposedBy to be cleared after dispute")
}
// 4. Repropose and victim legitimately confirms -> instant settlement
err = ProposeSettlement(p1, gameID, scholarsMate)
if err != nil {
t.Fatalf("failed to repropose settlement: %v", err)
}
err = ConfirmSettlement(p2, gameID)
if err != nil {
t.Fatalf("victim failed to confirm settlement: %v", err)
}
if GetGameStatus(gameID) != "WHITE_WON" {
t.Fatalf("expected status WHITE_WON after confirmation, got %s", GetGameStatus(gameID))
}
}
func TestTurnTimeoutAbandonment(t *testing.T) {
oldTimeout := TurnTimeoutSeconds
TurnTimeoutSeconds = 0
defer func() { TurnTimeoutSeconds = oldTimeout }()
p1 := address("g1activeplayer11111111111111111111111111")
p2 := address("g1ragequitter22222222222222222222222222")
err := CreateGame(p1, p2)
if err != nil {
t.Fatalf("failed to create game: %v", err)
}
gameID := GetGamesCount() - 1
// White played e2e4 offchain, Black rage-quitted on move 1!
currentMoves := "e2e4"
// 1. Black cannot start turn timer when it is Black's turn
err = StartTurnTimer(p2, gameID, currentMoves)
if err == nil {
t.Fatal("expected error when AFK player tries to start timer against opponent")
}
// 2. White legitimately starts turn timer for AFK Black
err = StartTurnTimer(p1, gameID, currentMoves)
if err != nil {
t.Fatalf("white failed to start turn timer: %v", err)
}
if GetTurnChallengedBy(gameID) != p1 {
t.Fatalf("expected turnChallengedBy p1, got %s", GetTurnChallengedBy(gameID))
}
// 3. Other player or Black cannot claim timeout
err = ClaimTimeout(p2, gameID)
if err == nil {
t.Fatal("expected error when non-challenger tries to claim timeout")
}
// 4. White claims timeout (since TurnTimeoutSeconds=0) -> White wins by Abandonment!
err = ClaimTimeout(p1, gameID)
if err != nil {
t.Fatalf("white failed to claim timeout: %v", err)
}
if GetGameStatus(gameID) != "WHITE_WON" {
t.Fatalf("expected status WHITE_WON from abandonment, got %s", GetGameStatus(gameID))
}
if GetSettledBy(gameID) != p1 {
t.Fatalf("expected settledBy p1, got %s", GetSettledBy(gameID))
}
}
gnomod.toml
module = "gno.land/r/g1g7dna0gp4nec5rza4q25htj0cjgswrxefp37ep/kewrchess" gno = "0.9" [addpkg] creator = "g1g7dna0gp4nec5rza4q25htj0cjgswrxefp37ep" height = 54060 max_deposit = "100000000ugnot"
render.gno
package kewrchess
import (
"gno.land/p/g1g7dna0gp4nec5rza4q25htj0cjgswrxefp37ep/chess"
"gno.land/p/nt/ufmt/v0"
)
func Render(path string) string {
output := "# ♟️ KewrChess - Gno.land Edition\n\n"
output += "---\n\n"
// On-chain Matchmaking Queue Status
if len(matchmakingQueue) > 0 {
output += ufmt.Sprintf("### ⏳ Matchmaking Queue (%d players waiting)\n\n", len(matchmakingQueue))
for i, qAddr := range matchmakingQueue {
output += ufmt.Sprintf("- Position #%d: `%s`\n", i+1, qAddr)
}
output += "\n---\n\n"
}
if len(games) == 0 {
output += "Never Sold His Soul wkwk!\n\n"
output += "No active chess games found.\n"
return output
}
for _, g := range games {
output += ufmt.Sprintf("### ⚔️ Game #%d\n", g.id)
statusText := "`PLAYING` 🟢"
if g.status == "WHITE_WON" {
if g.resignedBy != "" {
statusText = "`WHITE WON (Black Resigned)` 🏳️ 🏆"
} else {
statusText = "`WHITE WON` 🏆"
}
} else if g.status == "BLACK_WON" {
if g.resignedBy != "" {
statusText = "`BLACK WON (White Resigned)` 🏳️ 🏆"
} else {
statusText = "`BLACK WON` 🏆"
}
} else if g.status == "DRAW" {
statusText = "`DRAW` 🤝"
}
output += ufmt.Sprintf("> **Status**: %s\n", statusText)
if g.settledBy != "" {
output += ufmt.Sprintf("> ⛓️ **On-Chain Settlement**: Settled by `%s`\n", g.settledBy)
}
if g.status == "PLAYING" && g.drawOfferedBy != "" {
offeredColor := "Black"
if g.drawOfferedBy == g.white {
offeredColor = "White"
}
output += ufmt.Sprintf("> 🤝 **Draw Offer**: Offered by %s (`%s`)\n", offeredColor, g.drawOfferedBy)
}
turnText := "Black (⚫)"
if g.isWhiteTurn {
turnText = "White (⚪)"
}
output += ufmt.Sprintf("> **Turn**: %s\n>\n", turnText)
output += ufmt.Sprintf("> * ⚪ **White**: `%s`\n", g.white)
output += ufmt.Sprintf("> * ⚫ **Black**: `%s`\n\n", g.black)
output += "| | | | | | | | |\n"
output += "| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n"
for r := 0; r < 8; r++ {
output += "|"
for c := 0; c < 8; c++ {
piece := g.board[r][c]
if piece == "." {
square := "⬜"
if (r+c)%2 != 0 {
square = "🟩"
}
output += ufmt.Sprintf(" <span style=\"font-size:32px;\">%s</span> |", square)
} else {
emoji := chess.GetPieceEmoji(piece)
output += ufmt.Sprintf(" <span style=\"font-size:32px;\">%s</span> |", emoji)
}
}
output += "\n"
}
output += "\n---\n\n"
}
output += "### 💡 How to Play:\n"
output += "```bash\n"
output += "# Join automatic matchmaking queue (Find Match on-chain):\n"
output += "gno call gno.land/r/g1g7dna0gp4nec5rza4q25htj0cjgswrxefp37ep/kewrchess JoinQueue \\\n"
output += " --args \"[your_address]\"\n\n"
output += "# Leave / cancel matchmaking queue:\n"
output += "gno call gno.land/r/g1g7dna0gp4nec5rza4q25htj0cjgswrxefp37ep/kewrchess LeaveQueue \\\n"
output += " --args \"[your_address]\"\n\n"
output += "# Standard move (default auto-promotion to Queen):\n"
output += "gno call gno.land/r/g1g7dna0gp4nec5rza4q25htj0cjgswrxefp37ep/kewrchess MakeMove \\\n"
output += " --args \"[your_address]\" \\\n"
output += " --args [gameID] \\\n"
output += " --args [fromRow] --args [fromCol] \\\n"
output += " --args [toRow] --args [toCol]\n\n"
output += "# Move with promotion choice (Q = Queen, N = Knight, R = Rook, B = Bishop):\n"
output += "gno call gno.land/r/g1g7dna0gp4nec5rza4q25htj0cjgswrxefp37ep/kewrchess MakeMovePromote \\\n"
output += " --args \"[your_address]\" \\\n"
output += " --args [gameID] \\\n"
output += " --args [fromRow] --args [fromCol] \\\n"
output += " --args [toRow] --args [toCol] \\\n"
output += " --args \"N\"\n\n"
output += "# Resign:\n"
output += "gno call gno.land/r/g1g7dna0gp4nec5rza4q25htj0cjgswrxefp37ep/kewrchess Resign \\\n"
output += " --args \"[your_address]\" \\\n"
output += " --args [gameID]\n\n"
output += "# Offer a draw:\n"
output += "gno call gno.land/r/g1g7dna0gp4nec5rza4q25htj0cjgswrxefp37ep/kewrchess OfferDraw \\\n"
output += " --args \"[your_address]\" \\\n"
output += " --args [gameID]\n\n"
output += "# Accept draw offer:\n"
output += "gno call gno.land/r/g1g7dna0gp4nec5rza4q25htj0cjgswrxefp37ep/kewrchess AcceptDraw \\\n"
output += " --args \"[your_address]\" \\\n"
output += " --args [gameID]\n\n"
output += "# Decline draw offer:\n"
output += "gno call gno.land/r/g1g7dna0gp4nec5rza4q25htj0cjgswrxefp37ep/kewrchess DeclineDraw \\\n"
output += " --args \"[your_address]\" \\\n"
output += " --args [gameID]\n\n"
output += "# Final settlement (Officially settle match result on-chain via move verification):\n"
output += "gno call gno.land/r/g1g7dna0gp4nec5rza4q25htj0cjgswrxefp37ep/kewrchess SettleGame \\\n"
output += " --args \"[your_address]\" \\\n"
output += " --args [gameID] \\\n"
output += " --args \"e2e4 e7e5 d1h5 b8c6 f1c4 g8f6 h5f7\" # Full UCI chess move history\n\n"
output += "# Confirm settlement result by opponent (Instant approval):\n"
output += "gno call gno.land/r/g1g7dna0gp4nec5rza4q25htj0cjgswrxefp37ep/kewrchess ConfirmSettlement \\\n"
output += " --args \"[your_address]\" \\\n"
output += " --args [gameID]\n\n"
output += "# Dispute fraudulent settlement proposal:\n"
output += "gno call gno.land/r/g1g7dna0gp4nec5rza4q25htj0cjgswrxefp37ep/kewrchess DisputeSettlement \\\n"
output += " --args \"[your_address]\" \\\n"
output += " --args [gameID]\n\n"
output += "# Start turn timer if opponent is AFK / rage quits:\n"
output += "gno call gno.land/r/g1g7dna0gp4nec5rza4q25htj0cjgswrxefp37ep/kewrchess StartTurnTimer \\\n"
output += " --args \"[your_address]\" \\\n"
output += " --args [gameID] \\\n"
output += " --args \"[current_moves_uci]\"\n\n"
output += "# Claim win due to opponent abandonment / timeout:\n"
output += "gno call gno.land/r/g1g7dna0gp4nec5rza4q25htj0cjgswrxefp37ep/kewrchess ClaimTimeout \\\n"
output += " --args \"[your_address]\" \\\n"
output += " --args [gameID]\n"
output += "```\n"
return output
}
types.gno
package kewrchess
import (
"gno.land/p/g1g7dna0gp4nec5rza4q25htj0cjgswrxefp37ep/chess"
)
// Game represents an on-chain chess game session
type Game struct {
id int
white address // Wallet address of White player
black address // Wallet address of Black player
board chess.Board // 8x8 chess board from package chess
isWhiteTurn bool // True = White's turn, False = Black's turn
status string // "PLAYING", "WHITE_WON", "BLACK_WON", "DRAW"
castling chess.CastlingRights // Castling rights for White & Black
enPassantCol int // Pawn column vulnerable to en passant (-1 if none)
drawOfferedBy address // Address of player offering a draw ("" if none)
resignedBy address // Address of player who resigned ("" if none)
settledBy address // Address of player who recorded final settlement on blockchain ("" if not yet)
// Anti-Cheat Protection: Proposal & Challenge Period
proposedBy address // Player submitting victory claim
pendingStatus string // Proposed settlement status ("WHITE_WON", "BLACK_WON", "DRAW")
challengeDeadline int64 // Unix timestamp deadline for challenging settlement proposal
// Anti-RageQuit Protection: Timeout & Abandonment
turnDeadline int64 // Unix timestamp deadline for opponent's turn response
turnChallengedBy address // Player demanding turn response due to AFK opponent
}
// NewGame initializes a new game with the standard chess starting layout
func NewGame(id int, white, black address) *Game {
return &Game{
id: id,
white: white,
black: black,
board: chess.NewInitialBoard(),
isWhiteTurn: true,
status: "PLAYING",
castling: chess.CastlingRights{},
enPassantCol: -1,
drawOfferedBy: "",
resignedBy: "",
settledBy: "",
proposedBy: "",
pendingStatus: "",
challengeDeadline: 0,
turnDeadline: 0,
turnChallengedBy: "",
}
}