rsps-scripts

Automation scripts for private servers
Log | Files | Refs

functions.sh (3706B)


      1 #!/usr/bin/env bash
      2 
      3 # currentpos: hyprctl cursorpos
      4 # currentpos: slurp
      5 
      6 set -uo pipefail
      7 
      8 function msleep() {
      9 	local ms
     10 	case "$#" in
     11 		1) ms=$1 ;;
     12 		2) ms=$(shuf -i "$1-$2" -n 1) ;;
     13 		*) return 1 ;;
     14 	esac
     15 	sleep "$(printf '%s' "$ms" | awk '{printf "%.3f", $1 / 1000}')"
     16 	read -r -t "$((ms / 1000)).$((ms % 1000))" <<< ""
     17 }
     18 
     19 function hctl() {
     20 	hyprctl "$@" >/dev/null
     21 }
     22 
     23 function left_click() {
     24 	msleep 50 100
     25 	hctl dispatch 'hl.dsp.send_key_state({ mods = 0, key = "mouse:272", state = "down" })' >/dev/null
     26 	hctl dispatch 'hl.dsp.send_key_state({ mods = 0, key = "mouse:272", state = "up" })' >/dev/null
     27 }
     28 
     29 function right_click() {
     30 	msleep 50 100
     31 	hctl dispatch 'hl.dsp.send_key_state({ mods = 0, key = "mouse:273", state = "down" })' >/dev/null
     32 	hctl dispatch 'hl.dsp.send_key_state({ mods = 0, key = "mouse:273", state = "up" })' >/dev/null
     33 	msleep 250 350
     34 }
     35 
     36 function current_mouse() {
     37 	hyprctl cursorpos
     38 }
     39 
     40 function close_bank() {
     41 	msleep 100 200
     42 	move_mouse 1086 323 14 14
     43 	msleep 100 200
     44 	left_click
     45 	msleep 200 300
     46 }
     47 
     48 function inventory_slot() {
     49     local slot=$1
     50     local start_x=1751
     51     local start_y=794
     52     local col_spacing=42
     53     local row_spacing=36
     54     local cols=4
     55     local rows=7
     56     if [[ ! $slot =~ ^[0-9]+$ ]] || (( slot < 1 || slot > cols * rows )); then
     57         echo "inventory_slot: slot must be 1-$((cols * rows))" >&2
     58         return 1
     59     fi
     60     local index=$((slot - 1))
     61     local row=$((index / cols))
     62     local col=$((index % cols))
     63     local x=$((start_x + col * col_spacing))
     64     local y=$((start_y + row * row_spacing))
     65     move_mouse "$x" "$y" 16 16
     66 }
     67 
     68 function move_mouse() {
     69 	# debug line: printf 'args(%d):' "$#"; printf ' [%q]' "$@"; printf '\n' >&2
     70 	local relative=0
     71 	if [[ "${1:-}" == "-r" ]]; then
     72 		relative=1
     73 		shift
     74 	fi
     75 
     76 	local x y width height
     77 	case "$#" in
     78 		2)
     79 			x="$1"
     80 			y="$2"
     81 			;;
     82 
     83 		4)
     84 			x="$1"
     85 			y="$2"
     86 			width="$3"
     87 			height="$4"
     88 
     89 			# arithmetic instead of shuf so negative relative offsets work
     90 			x=$((x + RANDOM % (width + 1)))
     91 			y=$((y + RANDOM % (height + 1)))
     92 			;;
     93 
     94 		*)
     95 			echo "Usage: move_mouse [-r] x y [width height]" >&2
     96 			return 1
     97 			;;
     98 	esac
     99 
    100 	if ((relative)); then
    101 		local cx cy
    102 		# hyprctl cursorpos prints "150, 150"
    103 		IFS=', ' read -r cx cy < <(hyprctl cursorpos)
    104 		x=$((cx + x))
    105 		y=$((cy + y))
    106 	fi
    107 
    108 	ydotool mousemove -a -x "$x" -y "$y"
    109 }
    110 
    111 function cleanup() {
    112 	hctl eval 'hl.unbind("SUPER + F12")' >/dev/null
    113 	hctl eval 'hl.unbind("F11")' 2>/dev/null
    114 	hctl eval 'hl.unbind("F10")' 2>/dev/null
    115 	rm -f "$pipe"
    116 	# restore cursor hiding
    117 	hctl eval 'hl.config({ ["cursor.inactive_timeout"] = 3 })'
    118 
    119 	# stop ydotoold if we started it
    120 	if [[ -n "${YDOTOOLD_PID:-}" ]]; then
    121 		kill "$YDOTOOLD_PID" 2>/dev/null
    122 		wait "$YDOTOOLD_PID" 2>/dev/null
    123 	fi
    124 }
    125 
    126 function startup() {
    127 	# cleanup on term or int
    128 	trap 'cleanup; exit 0' TERM INT
    129 	# pipe setup
    130 	pipe="/tmp/rsps.pipe"
    131 	mkfifo "$pipe"
    132 
    133 	# start ydotoold in background (skip if already running)
    134 	if pgrep -x ydotoold >/dev/null; then
    135 		printf ' - ydotoold already running, skipping start\n'
    136 		YDOTOOLD_PID=""
    137 	else
    138 		ydotoold >/dev/null 2>&1 &
    139 		YDOTOOLD_PID=$!
    140 		sleep 0.3
    141 	fi
    142 	# fullscreen client
    143 	target_class="org-eclipse-jdt-internal-jarinjarloader-JarRsrcLoader"
    144 	hctl dispatch "hl.dsp.window.fullscreen({ window = 'class:$target_class', action = 'set' })"
    145 	# disable mouse cursor hiding
    146 	hctl eval 'hl.config({ ["cursor.inactive_timeout"] = 0 })'
    147 	printf 'Press Super+F12 to quit.\n'
    148 	hctl eval "hl.bind(\"SUPER + F12\", hl.dsp.exec_cmd(\"kill $BASHPID\"))" >/dev/null
    149 	hctl eval "hl.bind(\"F11\", hl.dsp.exec_cmd(\"echo go > $pipe\"))"
    150 	hctl eval "hl.bind(\"F10\", hl.dsp.exec_cmd(\"echo second > $pipe\"))"
    151 }
    152 
    153 function complete() {
    154 	printf '\nFinished.\n'
    155 	cleanup
    156 }