#!/bin/bash
# Rock-paper-scissors implementation for sggs
# Copyright (C) 2021 Martin Bays <mbays@sdf.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of version 3 of the GNU General Public License as
# published by the Free Software Foundation, or any later version.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses/.

FIRST_TO=5

command="$1"
shift
case "$command" in
    describe) echo "Rock-paper-scissors";;
    help) echo "The classic game of psychology and luck.";;
    players) echo 2;;
    setarg) ;;
    init) echo 0 > score1; echo 0 > score2;;
    showstate)
        if [ "$1" = 0 ]; then
            # P=0 means an observer
            echo "Scores: $(<score1) - $(<score2) (first to $FIRST_TO wins)"
            [ -e "last1" ] && echo "P1 played: $(<last1)"
            [ -e "last2" ] && echo "P2 played: $(<last2)"
            exit 0
        fi
        [[ "$1" = 1 || "$1" = 2 ]] || exit 1
        echo "Scores: YOU $(<"score$1") - $(<"score$((3-$1))") THEM (first to $FIRST_TO wins)"
        [ -e "last$1" ] && echo "You played:  $(<"last$1")"
        [ -e "last$((3-$1))" ] && echo "They played: $(<"last$((3-$1))")"
        [ -e "next$1" ] && echo && echo "Your next move: $(<"next$1")"
        [ -e "next$((3-$1))" ] && echo && echo "Their next move: [chosen]"
        ;;
    canmove)
        # Protocol: canmove P
        # Exit 5 if won, 4 if can't move, else (optionally) write move links.
        [ -e won ] && exit 5
        [[ "$1" = 1 || "$1" = 2 ]] || exit 4
        [ -e "next$1" ] && exit 4
        echo "=> move?R Play rock"
        echo "=> move?P Play paper"
        echo "=> move?S Play scissors";;
    move)
        # Protocol: move P MOVE
        # Exit 4 on error, or 1 for line input, or 2 for links, or do move.
        [ -e won ] && echo "Game over!" && exit 4
        [[ "$1" = 1 || "$1" = 2 ]] || exit 4
        [ -e "next$1" ] && echo "Move already selected." && exit 4
        [[ "$2" != [RPS] ]] && echo "R/P/S" && exit 1
        if [ -e "next$((3-$1))" ]; then
            if [ "$1" = 1 ]; then
                c1=$2
                c2=$(<"next2")
            else
                c1=$(<"next1")
                c2=$2
            fi
            if [ "$c1" != "$c2" ]; then
                if [[ "$c1$c2" =~ (RS|SP|PR) ]]; then
                    w=1
                else
                    w=2
                fi
                newscore="$(($(<score$w)+1))"
                [ "$newscore" = "$FIRST_TO" ] && echo $w > won
                echo "$newscore" > score$w
            fi
            mv "next$((3-$1))" "last$((3-$1))"
            echo "$2" > "last$1" 
        else
            echo "$2" > "next$1" 
        fi;;
    resign) [[ "$1" = 1 || "$1" = 2 ]] && echo "$((3-$1))" > won;;
    winner)
        # Protocol: exit 1 if no-one won, else write winner(s)
        [ -e won ] || exit 1
        cat won;;
esac
exit 0