#!/usr/bin/env bash
# game.sh: Blink. The four drive-bay LEDs of a NAS light up in a sequence that grows by one each round; repeat it
# with the keys 1 to 4. Enter starts a round, q quits. The same SEED gives the same sequence.
# Run it: bash game.sh   (it needs Bash 4 or newer, such as TurboShell's bash)
set -uo pipefail

A=$'\e[38;5;214m' G=$'\e[1;38;5;46m' R=$'\e[1;38;5;196m' D=$'\e[38;5;240m' B=$'\e[1m' N=$'\e[0m'
RANDOM=${SEED:-$$} # the seed fixes every later $RANDOM
seq=() round=0 best=0

at() { printf '\e[%d;%dH' "$1" "$2"; }

bay() { # NUMBER COLOUR: one drive bay with its LED in the colour
  local c=$((8 + ($1 - 1) * 17)) i
  at 5 "$c" && printf '%s┌────────────┐%s' "$A" "$N"
  at 6 "$c" && printf '%s│%s   %s██████%s   %s│%s' "$A" "$N" "$2" "$N" "$A" "$N"
  for i in 7 8 9 10; do at "$i" "$c" && printf '%s│%s %s▒▒▒▒▒▒▒▒▒▒%s %s│%s' "$A" "$N" "$D" "$N" "$A" "$N"; done
  at 11 "$c" && printf '%s│%s     %s%d%s      %s│%s' "$A" "$N" "$B" "$1" "$N" "$A" "$N"
  at 12 "$c" && printf '%s└────────────┘%s' "$A" "$N"
}

bays() {
  local i
  for i in 1 2 3 4; do bay "$i" "$D"; done
}

say() { # LINE [LINE]: two lines of text under the bays
  at 15 1 && printf '\e[2K   %s' "$1"
  at 16 1 && printf '\e[2K   %s' "${2:-}"
}

score() {
  at 2 3 && printf '%sBLINK%s   repeat the LEDs with the keys 1 to 4' "$B$A" "$N"
  at 2 56 && printf 'round %2d   best %2d' "$round" "$best"
}

flash() { # NUMBER COLOUR SECONDS: light one LED, then dim it again
  bay "$1" "$2"
  sleep "$3"
  bay "$1" "$D"
}

show() { # the sequence of this round, one LED after another
  local k
  say "Watch the LEDs."
  sleep 0.6
  for k in "${seq[@]:0:round}"; do
    flash "$k" "$G" 0.5
    sleep 0.2
  done
  while IFS= read -rsn1 -t 0.01 _; do :; done # keys typed during the show do not count
}

turn() { # the player repeats the sequence; ends with status 1 at the first wrong key
  local i=0 key
  while ((i < round)); do
    say "Your turn: $i of $round."
    IFS= read -rsn1 key
    [[ $key == q ]] && exit 0
    [[ $key == [1-4] ]] || continue
    if ((key != seq[i])); then
      bay "$key" "$R"
      bay "${seq[i]}" "$G"
      return 1
    fi
    flash "$key" "$A" 0.15
    i=$((i + 1))
  done
}

restore() { printf '\e[0m\e[?25h\e[?1049l'; }
trap restore EXIT
trap 'exit 0' INT TERM
printf '\e[?1049h\e[?25l\e[2J'
at 1 1 && printf '%s╔%s╗%s' "$A" "$(printf '═%.0s' {1..78})" "$N"
at 3 1 && printf '%s╚%s╝%s' "$A" "$(printf '═%.0s' {1..78})" "$N"
at 2 1 && printf '%s║%s' "$A" "$N" && at 2 80 && printf '%s║%s' "$A" "$N"
at 4 4 && printf '%s┌%s┐%s' "$D" "$(printf '─%.0s' {1..71})" "$N" # the case of the NAS around the bays
for i in {5..12}; do at "$i" 4 && printf '%s│%s' "$D" "$N" && at "$i" 76 && printf '%s│%s' "$D" "$N"; done
at 13 4 && printf '%s└%s┘%s' "$D" "$(printf '─%.0s' {1..71})" "$N"
bays
score
say "Watch the LEDs, then press their numbers in the same order." "Enter starts, q quits."
while true; do
  IFS= read -rsn1 key
  [[ $key == q ]] && exit 0
  [[ -z $key ]] || continue
  if ((round == 0)); then # a new game: a new sequence of 99 LEDs
    seq=()
    for _ in {1..99}; do seq+=($((RANDOM % 4 + 1))); done
  fi
  round=$((round + 1))
  score
  show
  if turn; then
    ((round > best)) && best=$round
    score
    say "${G}Round $round is right.$N" "Enter plays round $((round + 1)), q quits."
  else
    say "${R}Wrong LED.$N The green one was next. Score: $((round - 1))." "Enter starts a new game, q quits."
    round=0
  fi
done
