shithub: chessfs

Download patch

ref: 715de9d3e1e26dbee3490ac3ad53f6b2069536da
parent: bb0853c09800c2e32c6474b0ba6550012bed869e
author: kitzman <kitzman@disroot.org>
date: Thu Jan 18 03:40:26 EST 2024

fixed bugs; added play script

--- a/README
+++ b/README
@@ -10,7 +10,9 @@
      SYNOPSIS
           chessfs [-verbose] [-addr addr] [-srv srv] [-group gid]
 
+          rc/play [-r] player directory
 
+
      DESCRIPTION
           Chessfs is a filesystem for playing chess games. It posts a
           server named after the argument, which by default is
@@ -41,7 +43,7 @@
 
           The board files, white and black, show the current pieces'
           position, depending on the player. Writing to the file makes
-          a move on behalf of the specified player.
+          a move on behalf of the corresponding player.
 
           draw    Offer a draw, or accept a draw if the other player
                   offered one.
@@ -52,6 +54,12 @@
           For reading the game in the PGN notation, the moves files
           can be read.
 
+          The package comes with two scripts. Play is an interactive
+          script which prints the board and game status, and waits for
+          your or your opponent's move. The -r flag transcribes the
+          UTF-8 piece symbols into other symbols, in case the used
+          font doesn't have them.
+
      EXAMPLES
           Run the filesystem:
 
@@ -74,7 +82,7 @@
           https://shithub.us/kitzman/chess/HEAD/info.html
 
 
-     BUGS AND TODOS
+     BUGS AND TODO
           The board does not have the right coordinates for the right
           player.
 
--- a/chessfs.4
+++ b/chessfs.4
@@ -4,6 +4,8 @@
 .SH SYNOPSIS
 chessfs [-verbose] [-addr addr] [-srv srv] [-group gid]
 
+rc/play [-r] player directory
+
 .SH DESCRIPTION
 Chessfs is a filesystem for playing chess games. It posts a
 server named after the argument, which by default is "chessfs".
@@ -39,7 +41,7 @@
 .PP
 The board files, white and black, show the current pieces' position,
 depending on the player. Writing to the file makes a move on behalf
-of the specified player.
+of the corresponding player.
 .TF "\fLresign\fR"
 .TP
 .BI "draw"
@@ -54,6 +56,12 @@
 .RE
 .PP
 For reading the game in the PGN notation, the moves files can be read.
+.PP
+The package comes with two scripts. Play is an interactive script
+which prints the board and game status, and waits for your or
+your opponent's move. The -r flag transcribes
+the UTF-8 piece symbols into other symbols, in case the used font
+doesn't have them.
 .SH EXAMPLES
 Run the filesystem:
 .IP
@@ -79,7 +87,7 @@
 https://git.disroot.org/kitzman/chessfs
 https://shithub.us/kitzman/chess/HEAD/info.html
 
-.SH BUGS AND TODOS
+.SH BUGS AND TODO
 
 The board does not have the right coordinates for the right player.
 
--- a/chessfs.go
+++ b/chessfs.go
@@ -302,16 +302,28 @@
 
 	switch cmdline {
 	case "draw":
+		turn := f.game.GetTurn()
+		if turn != f.player {
+			return 0, fmt.Errorf("not your turn")
+		}
 		err := f.game.OfferDraw(f.player)
 		if err != nil {
 			return 0, err
 		}
 	case "resign":
+		turn := f.game.GetTurn()
+		if turn != f.player {
+			return 0, fmt.Errorf("not your turn")
+		}
 		err := f.game.Resign(f.player)
 		if err != nil {
 			return 0, err
 		}
 	default:
+		turn := f.game.GetTurn()
+		if turn != f.player {
+			return 0, fmt.Errorf("not your turn")
+		}
 		err := f.game.Move(cmdline)
 		if err != nil {
 			return 0, err
--- a/game.go
+++ b/game.go
@@ -251,11 +251,11 @@
 
 	switch player {
 	case White:
-		g.state = WhiteWon
+		g.state = BlackWon
 		g.board.Resign(chess.White)
 		break
 	case Black:
-		g.state = BlackWon
+		g.state = WhiteWon
 		g.board.Resign(chess.Black)
 		break
 	}
--- /dev/null
+++ b/rc/play
@@ -1,0 +1,112 @@
+#!/bin/rc
+
+rfork e
+flagfmt='r:transcribe, h:help'
+args='player gamedir'
+eval `{aux/getflags $*}
+
+player=$1
+game=$2
+lstat=()
+lmsg=()
+uppid=()
+
+if(~ $help 1 || ! ~ $#* 2 || ! ~ $player (white black)) {
+	aux/usage
+	exit usage
+}
+
+board=$game/$player
+ctl=$game/ctl
+
+fn prtboard {
+	if(~ $#transcribe 0) {
+		cat $board
+	}
+	if not {
+		cat $board | \
+		sed 's/♙/p/g' | sed 's/♟/q/g' | \
+		sed 's/♖/ᴙ/g' | sed 's/♜/ᴚ/g' | \
+		sed 's/♘/N/g' | sed 's/♞/ᴎ/g' | \
+		sed 's/♗/ᴇ/g' | sed 's/♝/ᴈ/g' | \
+		sed 's/♕/S/g' | sed 's/♛/Z/g' | \
+		sed 's/♔/ᴍ/g' | sed 's/♚/ᴡ/g'
+	}
+}
+
+fn status {
+	gst=`{cat $ctl | sed 1q}
+	msg=`{cat $ctl | sed 2q | tail -n 1}
+	wtm=`{cat $ctl | sed 3q | tail -n 1 | awk '{print $3}'}
+	wtb=`{cat $ctl | sed 4q | tail -n 1 | awk '{print $3}'}
+
+	echo $msg '			W' $wtm 'B' $wtb
+}
+
+fn shutdown {
+	echo 'shutting down play script'
+	echo kill >/proc/$uppid/note
+	echo 'exiting'
+	exit
+}
+fn sighup sigint sigterm{
+	shutdown
+}
+
+oifs=$ifs
+
+while() {
+	if(! test -f $ctl) {
+		echo 'ctl file' $ctl 'not found'
+		exit
+	}
+	cstat=`{cat $ctl | sed 1q}
+	msg=`{cat $ctl | sed 2q | tail -n 1}
+	if(! ~ $"msg $"lmsg) {
+		lmsg=$msg
+		prtboard
+		status
+	}
+	if(! ~ $"cstat $"lstat) {
+		lstat=$cstat
+		echo '!!!' $cstat
+	}
+	sleep 0.5
+} &
+uppid=$apid
+echo $uppid
+
+sleep 0.5
+while(echo -n '> '; ifs=() line=`{read | tr -d \xa}) {
+	ifs=$oifs
+	if(! ~ $line '') {
+		switch($line) {
+		case start
+			echo start >$ctl
+		case time*
+			if(! ~ $line(1) '') {
+				echo time $line(1) >$ctl
+			}
+			if not {
+				echo malformed time
+			}
+		case /h
+			echo '/h		help'
+			echo '/p		print board'
+			echo '/q		quit'
+			echo 'start		start game'
+			echo 'time		set time'
+			echo '*			move/draw/resign'
+			prtt=()
+		case /p
+			prtboard
+			status
+		case /q
+			shutdown
+		case *
+			echo $line >$board
+			sleep 0.5
+		}
+	}
+}
+shutdown