shithub: chessfs

Download patch

ref: 03a1f26a9e4fdd5c7ccaeae9c82e0af4b074db11
parent: a84fb85ea760b4bac5228c3706461a29aff38c82
author: kitzman <[email protected]>
date: Sun Jan 21 08:24:53 EST 2024

chessterm and chessfs bugfixes; rc/spectate

--- a/README
+++ b/README
@@ -14,7 +14,9 @@
 
           rc/play [-r] player directory
 
+          rc/spectate [-r] directory
 
+
      DESCRIPTION
           Chessfs is a filesystem for playing chess games. It posts a
           server named after the argument, which by default is
@@ -72,7 +74,8 @@
           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 sym-
-          bols, in case the used font doesn't have them.
+          bols, in case the used font doesn't have them. Spectate is a
+          similar script, only printing the board updates.
 
      EXAMPLES
           Run the filesystem:
@@ -97,9 +100,6 @@
 
 
      BUGS AND TODO
-          The board does not have the right coordinates for the right
-          player.
-
           Encrypting connections should be an option.
 
           Currently players cannot change the owners of the files.
--- a/chessfs.4
+++ b/chessfs.4
@@ -8,6 +8,8 @@
 
 rc/play [-r] player directory
 
+rc/spectate [-r] directory
+
 .SH DESCRIPTION
 Chessfs is a filesystem for playing chess games. It posts a
 server named after the argument, which by default is "chessfs".
@@ -74,7 +76,8 @@
 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.
+doesn't have them. Spectate is a similar script, only printing the
+board updates.
 .SH EXAMPLES
 Run the filesystem:
 .IP
@@ -101,8 +104,6 @@
 https://shithub.us/kitzman/chess/HEAD/info.html
 
 .SH BUGS AND TODO
-
-The board does not have the right coordinates for the right player.
 
 Encrypting connections should be an option.
 
--- a/cmd/chessterm/chessterm.go
+++ b/cmd/chessterm/chessterm.go
@@ -130,9 +130,9 @@
 //	}
 	switch (int(i) + int(j)) % 2 {
 	case 0:
-		col = squarelcol
-	case 1:
 		col = squaredcol
+	case 1:
+		col = squarelcol
 	}
 
 	if redraw || status.Board[boardSquare] != status.LastStatus.Board[boardSquare] {
--- a/game.go
+++ b/game.go
@@ -170,7 +170,30 @@
 	case White:
 		return g.board.Position().Board().Draw(), nil
 	case Black:
-		return g.board.Position().Board().Flip(chess.UpDown).Draw(), nil
+		d := g.board.Position().Board().Flip(chess.UpDown).Flip(chess.LeftRight).Draw()
+		// quick fix for coordinates
+		var db = []byte(d)
+		for i, b := range(db) {
+			switch b {
+			case 'A': db[i] = 'H'
+			case 'B': db[i] = 'G'
+			case 'C': db[i] = 'F'
+			case 'D': db[i] = 'E'
+			case 'E': db[i] = 'D'
+			case 'F': db[i] = 'C'
+			case 'G': db[i] = 'B'
+			case 'H': db[i] = 'A'
+			case '1': db[i] = '8'
+			case '2': db[i] = '7'
+			case '3': db[i] = '6'
+			case '4': db[i] = '5'
+			case '5': db[i] = '4'
+			case '6': db[i] = '3'
+			case '7': db[i] = '2'
+			case '8': db[i] = '1'
+			}
+		}
+		return string(db), nil
 	}
 	return "", fmt.Errorf("unable to draw board")
 }
--- /dev/null
+++ b/rc/spectate
@@ -1,0 +1,75 @@
+#!/bin/rc
+
+rfork e
+flagfmt='r:transcribe, h:help'
+args='gamedir'
+eval `{aux/getflags $*}
+
+game=$1
+player=()
+lstat=()
+lmsg=()
+uppid=()
+
+if(~ $help 1 || ! ~ $#* 1) {
+	aux/usage
+	exit usage
+}
+
+board=$game/white
+ctl=$game/ctl
+
+fn sighup sigint sigterm{
+	exit
+}
+
+fn upboard {
+	player=`{cat $ctl | grep turn | sed 's/^([a-z]*)''.*/\1/g'}
+	board=$game/$player
+}
+
+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
+}
+
+sleep 0.5
+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
+		upboard
+		prtboard
+		status
+	}
+	if(! ~ $"cstat $"lstat) {
+		lstat=$cstat
+		echo '!!!' $cstat
+	}
+	sleep 0.5
+}
+