shithub: chessfs

Download patch

ref: 470f19bc73180395683aa1e14c96ea3c2d3a828f
parent: eb8f0cbd6b03c83d0ec53c73ce721dae347217de
author: kitzman <kitzman@disroot.org>
date: Fri Jan 26 09:22:30 EST 2024

added rc/replay

--- a/README
+++ b/README
@@ -19,7 +19,9 @@
 
           rc/spectate [-r] directory
 
+          rc/replay [-skfdch] chessdirectory
 
+
      DESCRIPTION
           Chessfs is a filesystem for playing chess games. It posts a
           server named after the argument, which by default is
@@ -52,6 +54,20 @@
           position, depending on the player. Writing to the file makes
           a move on behalf of the corresponding player.
 
+
+
+
+
+
+
+
+
+
+
+     CHESSFS(4)                                             CHESSFS(4)
+
+
+
           draw    Offer a draw, or accept a draw if the other player
                   offered one.
           resign  Resign the game.
@@ -91,8 +107,11 @@
           game status, and waits for your or your opponent's move. The
           -r flag transcribes the UTF-8 piece symbols into other sym-
           bols, in case the used font doesn't have them. Spectate is a
-          similar script, only printing the board updates.
+          similar script, only printing the board updates. Replay can
+          be used to replay games, with -s seconds between moves. The
+          PGN is read from standard input.
 
+
      EXAMPLES
           Run the filesystem:
 
@@ -100,6 +119,20 @@
                mount /srv/chessfs /n/chess
 
           Create a new game, set parameters, and play:
+
+
+
+
+
+
+
+
+
+
+
+     CHESSFS(4)                                             CHESSFS(4)
+
+
 
                cd /n/chess
                cd games/`{cat clone}
--- a/chessfs.4
+++ b/chessfs.4
@@ -12,6 +12,8 @@
 
 rc/spectate [-r] directory
 
+rc/replay [-skfdch] chessdirectory
+
 .SH DESCRIPTION
 Chessfs is a filesystem for playing chess games. It posts a
 server named after the argument, which by default is "chessfs".
@@ -107,7 +109,10 @@
 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. Spectate is a similar script, only printing the
-board updates.
+board updates. Replay can be used to replay games, with
+.BI -s
+seconds between moves. The PGN is read from standard input.
+
 .SH EXAMPLES
 Run the filesystem:
 .IP
--- /dev/null
+++ b/rc/replay
@@ -1,0 +1,90 @@
+#!/bin/rc
+
+rfork e
+flagfmt='s sleepamt, k skip, f fen, d delay, c:close, h:help'
+args='chessdir'
+eval `{aux/getflags $*}
+
+chessdir=$1
+gameid=()
+gamedir=()
+player=white
+
+if(~ $help 1 || ! ~ $#* 1) {
+	aux/usage
+	exit usage
+}
+
+if(~ $#sleepamt 0)
+	sleepamt=1.5
+if(~ $#delay 0)
+	delay=10
+
+fn sighup sigint sigterm{
+	exit
+}
+
+# create game
+gameid=`{cat $chessdir/clone}
+gamedir=$chessdir/games/$gameid
+if(~ $#gameid 0)
+	exit clone
+
+echo 'created game' $gameid
+
+# load fen if necessary
+if(! ~ $#fen 0) {
+	echo $"fen >$chessdir/fen
+	if(! ~ $#status 0) {
+		echo 'unable to load fen'
+		exit fen
+	}
+	echo 'loaded FEN'
+}
+
+# start player process
+echo 'starting game in 5 seconds'
+sleep $delay
+
+oifs=$ifs
+line=()
+finished=()
+echo start >$gamedir/ctl
+echo 'game started'
+
+while(line=`{read | tr -d \xa} && ! ~ $line 1.*) {}
+while(~ $#finished 0) {
+	for(i in `{seq 1 $#line}) {
+		move=$line($i)
+		if(! ~ $move *.) {
+			echo $player 'plays' $move
+			echo $move >$gamedir/$player
+			switch($player) {
+			case white
+				player=black
+			case black
+				player=white
+			}
+			sleep $sleepamt
+		}
+	}
+	line=`{read | tr -d \xa}
+	if(! ~ $#status 0)
+		finished=yes
+}
+
+echo 'replay finished'
+
+ifs=$oifs
+rm /env/oifs
+rm /env/chessdir
+rm /env/gameid
+rm /env/gamedir
+rm /env/player
+rm /env/line
+rm /env/finished
+
+if(! ~ $#close 0) {
+	echo 'closing game'
+	echo close >$gamedir/ctl
+}