1#!/usr/bin/env bash 2# Agent-facing probe for Space Browser. 3# 4# The browser never paints its own cursor, and the world->screen transform only 5# exists inside the page. So every coordinate below is converted by the page 6# itself (window.__game, see client/src/main.ts) and nothing here does the maths. 7# A point this script prints is the point the game will act on. 8# 9# ./tools/agent.sh open open the game (only if it is not already up) 10# ./tools/agent.sh reload reload the page: a brand new player 11# ./tools/agent.sh state you, held, camera, canvas, ships, bodies, ore 12# ./tools/agent.sh targets [kind] every clickable thing, nearest first 13# ./tools/agent.sh move --target ore:41 | --cell 60,30 | --world 120,-40 | --px 636,288 14# ./tools/agent.sh click --target ore:41 15# ./tools/agent.sh drag --target ore:41 --to-cell 60,30 16# ./tools/agent.sh world the same state straight from the server, no browser 17# ./tools/agent.sh shot out.png screenshot, plus the canvas box it was taken with 18# ./tools/agent.sh peer a second tab: a second player, for PvP 19# ./tools/agent.sh tab <n> switch tab 20set -euo pipefail 21 22URL=${URL:-http://localhost:5173} 23API=${API:-http://localhost:8090} 24cd "$(dirname "$0")/.." 25 26ab() { agent-browser "$@"; } 27# eval prints the value as JSON: unwrap it so bash sees plain text. 28js() { 29 local out 30 out=$(ab eval "$1") 31 printf '%s\n' "$out" | jq -r . 2>/dev/null || printf '%s\n' "$out" 32} 33 34ready() { [ "$(ab eval 'typeof window.__game === "object" && window.__game.state ? 1 : 0' 2>/dev/null)" = "1" ]; } 35 36ensure() { 37 ready || ab open "$URL" >/dev/null 2>&1 || true 38 for _ in $(seq 40); do 39 ready && return 0 40 sleep 0.25 41 done 42 echo "agent.sh: window.__game never appeared: is ./dev-start.sh running at $URL?" >&2 43 exit 1 44} 45 46# Resolve one point to "px py", in page coordinates. 47coords() { 48 local t="" c="" w="" p="" 49 while [ $# -gt 0 ]; do 50 case "$1" in 51 --target) t=$2; shift 2 ;; 52 --cell) c=$2; shift 2 ;; 53 --world) w=$2; shift 2 ;; 54 --px) p=$2; shift 2 ;; 55 *) echo "agent.sh: unknown flag $1" >&2; exit 2 ;; 56 esac 57 done 58 [ -n "$t$c$w$p" ] || { echo "agent.sh: give --target, --cell, --world or --px" >&2; exit 2; } 59 local out 60 out=$(js "(() => { 61 const g = window.__game, s = { t: '$t', c: '$c', w: '$w', p: '$p' }; 62 let px, py; 63 if (s.t) { 64 const [kind, id] = s.t.split(':'); 65 const h = g.targets().find((x) => x.kind === kind && x.id === Number(id)); 66 if (!h) return 'ERR nothing named ' + s.t + ' on screen; run: agent.sh targets'; 67 px = h.cellPx; py = h.cellPy; // the centre of its cell, not its exact pixel 68 } else if (s.c) { 69 const [x, y] = s.c.split(',').map(Number), r = g.canvas(); 70 px = r.left + (x + 0.5) * r.cell; py = r.top + (y + 0.5) * r.cell; 71 } else if (s.w) { 72 const [x, y] = s.w.split(',').map(Number), q = g.toScreen(x, y); 73 px = q.px; py = q.py; 74 } else { 75 [px, py] = s.p.split(',').map(Number); 76 } 77 return Math.round(px) + ' ' + Math.round(py); 78 })()") 79 case "$out" in ERR*) echo "agent.sh: ${out#ERR }" >&2; exit 1 ;; esac 80 echo "$out" 81} 82 83# point <spec...>: sets PX and PY, or dies with the reason. 84point() { 85 local p 86 p=$(coords "$@") 87 PX=${p% *} 88 PY=${p#* } 89} 90 91cmd=${1:-help} 92shift || true 93case "$cmd" in 94 open) 95 ensure 96 echo "open $(js 'location.href')" 97 js "JSON.stringify(window.__game.canvas())" 98 ;; 99 reload) 100 ab open "$URL" >/dev/null 101 ensure 102 echo "reloaded $URL" 103 ;; 104 state) 105 ensure 106 js "JSON.stringify({ you: __game.state.you, held: __game.state.held, cam: __game.cam, canvas: __game.canvas(), ships: __game.state.players, bodies: __game.state.bodies, ore: __game.state.ore, loose: __game.state.loose })" 107 ;; 108 targets) 109 ensure 110 js "JSON.stringify(__game.targets().filter((t) => !'${1:-}' || t.kind === '${1:-}').map((t) => ({ kind: t.kind, id: t.id, owner: t.owner, cell: t.cellX + ',' + t.cellY, px: Math.round(t.cellPx) + ',' + Math.round(t.cellPy), world: Math.round(t.x) + ',' + Math.round(t.y), d: Math.round(t.d) })))" 111 ;; 112 move) 113 ensure 114 point "$@" 115 ab mouse move "$PX" "$PY" >/dev/null 116 echo "moved to $PX $PY" 117 ;; 118 click) 119 ensure 120 point "$@" 121 ab mouse move "$PX" "$PY" >/dev/null 122 ab mouse down >/dev/null 123 ab mouse up >/dev/null 124 echo "clicked $PX $PY" 125 ;; 126 drag) 127 ensure 128 from=() 129 to=() 130 while [ $# -gt 0 ]; do 131 case "$1" in 132 --to-cell) to+=(--cell "$2"); shift 2 ;; 133 --to-world) to+=(--world "$2"); shift 2 ;; 134 --to-target) to+=(--target "$2"); shift 2 ;; 135 --to-px) to+=(--px "$2"); shift 2 ;; 136 *) from+=("$1"); shift ;; 137 esac 138 done 139 [ ${#to[@]} -gt 0 ] || { echo "agent.sh: drag needs --to-cell, --to-world, --to-target or --to-px" >&2; exit 2; } 140 point "${from[@]}" 141 fx=$PX fy=$PY 142 point "${to[@]}" 143 tx=$PX ty=$PY 144 ab mouse move "$fx" "$fy" >/dev/null 145 ab mouse down >/dev/null 146 for i in 1 2 3; do ab mouse move $((fx + (tx - fx) * i / 3)) $((fy + (ty - fy) * i / 3)) >/dev/null; done 147 ab mouse up >/dev/null 148 echo "dragged $fx $fy -> $tx $ty" 149 ;; 150 world) 151 curl -sf "$API/debug/state" | jq -c . 152 ;; 153 shot) 154 ensure 155 # agent-browser resolves a relative path against its own cwd, so a shot lands 156 # where the caller asked only when it is absolute. 157 f=${1:-shot.png} 158 case "$f" in /*) ;; *) f=$PWD/$f ;; esac 159 ab screenshot "$f" >/dev/null 160 echo "$f" 161 js "JSON.stringify({ cam: __game.cam, canvas: __game.canvas() })" 162 ;; 163 peer) 164 ab tab new "$URL" >/dev/null 165 ensure 166 echo "second player in a new tab" 167 ab tab list 168 ;; 169 tab) 170 id=$1 171 case "$id" in ''|*[!0-9]*) ;; *) id=t$id ;; esac # 2 and t2 are the same tab 172 ab tab "$id" >/dev/null 173 ensure 174 echo "tab $id" 175 ;; 176 *) 177 sed -n '2,20p' "$0" | sed 's/^# \{0,1\}//' 178 ;; 179esac