shithub: chessfs

ref: 470f19bc73180395683aa1e14c96ea3c2d3a828f
dir: /rc/play/

View raw version
#!/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