Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CCLa2PaEeCj8njQZSWGqHx
45 lines
2.6 KiB
Bash
Executable File
45 lines
2.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# A way back for a player whose browser lost their seat: the seat gets a new
|
|
# token, the old one stops working, and the link printed carries the new one.
|
|
# Whether the one asking holds that seat is the keeper's call; send the link
|
|
# only to them. Runs ON the droplet, through the game server's keeper door.
|
|
# __SLUG__-reseat.sh find NAME every table not yet over where a person of that name sits, for one who lost the codes too
|
|
# __SLUG__-reseat.sh ROOM who sits at the table, and when each person last moved or spoke
|
|
# __SLUG__-reseat.sh ROOM SEAT re-seat SEAT and print the link
|
|
set -euo pipefail
|
|
ROOM="${1:?usage: __SLUG__-reseat.sh find NAME | ROOM [SEAT]}"
|
|
DOOR="http://127.0.0.1:__PORT__/keeper/rooms/${ROOM^^}"
|
|
[ -s /etc/__SLUG__/keeper.key ] || { echo "No keeper's key at /etc/__SLUG__/keeper.key; a deploy makes it." >&2; exit 1; }
|
|
# The key goes as a header read from a file descriptor, never on a command line another user could see.
|
|
KEY="@/dev/fd/3"
|
|
exec 3< <(printf 'x-keeper-key: %s\n' "$(cat /etc/__SLUG__/keeper.key)")
|
|
if [ "$ROOM" = find ]; then
|
|
curl -s -H "$KEY" --get --data-urlencode "name=${2:?a name to look for}" "http://127.0.0.1:__PORT__/keeper/seats" | python3 -c '
|
|
import datetime, json, sys
|
|
r = json.load(sys.stdin)
|
|
if "error" in r: sys.exit(r["error"])
|
|
if not r["tables"]: print("no table still in play has a seat under that name")
|
|
for t in r["tables"]:
|
|
when = datetime.datetime.fromtimestamp(t["updatedAt"] / 1000, datetime.timezone.utc).strftime("%Y-%m-%d %H:%M UTC")
|
|
print(" %s seat %-3s %s with %s, last moved %s" % (t["roomId"], t["seat"], "(a bot stands in)" if t["standIn"] else "", ", ".join(t["players"]) or "nobody yet", when))
|
|
'
|
|
elif [ $# -lt 2 ]; then
|
|
curl -s -H "$KEY" "$DOOR" | python3 -c '
|
|
import datetime, json, sys
|
|
r = json.load(sys.stdin)
|
|
if "error" in r: sys.exit(r["error"])
|
|
print("room %s, %s" % (r["roomId"], "in play" if r["started"] else "not begun"))
|
|
for s in r["seats"]:
|
|
when = "bot" if s["bot"] else ("a bot stands in; " if s.get("standIn") else "") + ("never moved or spoke" if s["lastActed"] is None else "last acted " + datetime.datetime.fromtimestamp(s["lastActed"] / 1000, datetime.timezone.utc).strftime("%Y-%m-%d %H:%M UTC"))
|
|
print(" %-3s %-24s %s" % (s["id"], s["name"], when))
|
|
'
|
|
else
|
|
curl -s -X POST -H "$KEY" -H 'content-type: application/json' -d "{\"seat\":\"$2\"}" "$DOOR/reseat" | python3 -c '
|
|
import json, sys
|
|
r = json.load(sys.stdin)
|
|
if "error" in r: sys.exit(r["error"])
|
|
print("%s has a new token; their old one opens nothing now. Send them this link:" % r["name"])
|
|
print(r["link"])
|
|
'
|
|
fi
|