先设置sudo ydotool免密执行。
首先使用另一个设备连接上电脑的ssh。在电脑的tty下运行./start-random-display,后面等待的十秒内,走离电脑屏幕。待十秒后随机桌面启动后,在ssh连接的终端中运行./manage-game start,自动启动Steam并进入游戏(一分钟多一点)。然后可以回到电脑旁。
完成之后,在ssh终端中使用./manage-game exit退出,再./close-display退出桌面,准备进行下一轮。
| #!/bin/bash | |
| # 获取当前用户 ID 和运行目录 | |
| uid=$(id -u) | |
| run_dir="/run/user/$uid" | |
| sig_file="/tmp/his.txt" | |
| # 检查是否存在非 .lock 的 wayland 文件 | |
| if [ -n "$(ls "$run_dir"/wayland-* 2>/dev/null | rg -v 'lock')" ]; then | |
| #echo "检测到 Wayland (Hyprland)..." | |
| # 从你指定的 /tmp/his.txt 读取签名 | |
| if [ -f "$sig_file" ]; then | |
| latest_sig=$(cat "$sig_file" | tr -d '[:space:]') | |
| # 提取第一个有效的 wayland display 名字 | |
| w_display=$(ls "$run_dir"/wayland-* | rg -v 'lock' | head -n 1 | xargs basename) | |
| #echo "正在使用签名 [$latest_sig] 退出 Hyprland..." | |
| # 注入变量并执行 | |
| WAYLAND_DISPLAY="$w_display" \ | |
| HYPRLAND_INSTANCE_SIGNATURE="$latest_sig" \ | |
| hyprctl dispatch exit | |
| else | |
| echo "错误: 找不到签名文件 $sig_file,请检查 exec-once 是否正确写入。" >&2 | |
| exit 1 | |
| fi | |
| else | |
| #echo "未检测到 Wayland,正在终止 Xorg..." | |
| [ "$uid" -ne 0 ] && pkill Xorg || pkill Xorg | |
| fi | |
| echo '退出完成' |
| #!/bin/bash | |
| currdir="$(dirname "$0")" | |
| #"$currdir"/palworld-automation exit | |
| nvidia-smi | rg Palworld-Win64 | awk '{print $5}' | xargs -n1 -d\\n kill | |
| #sleep 15 | |
| sleep 3 | |
| "$currdir"/exit-steam |
| #!/bin/bash | |
| pkill --signal KILL steam |
| #!/bin/bash | |
| # 获取当前脚本所在目录 | |
| currdir="$(dirname "$(readlink -f "$0")")" | |
| # 获取当前用户 ID | |
| uid=$(id -u) | |
| # 检测 Wayland 变量 | |
| wayland_files=$(fd '^wayland' /run/user/1000 2>/dev/null | rg -v 'lock') | |
| # 逻辑判断函数:构建并执行命令 | |
| run_cmd() { | |
| local cmd_name=$1 | |
| # 检查是否存在 wayland-* 文件 | |
| if [ -e "${wayland_files[0]}" ]; then | |
| # 如果存在,统计数量 | |
| count=${#wayland_files[@]} | |
| if [ "$count" -gt 1 ]; then | |
| echo "错误: 检测到多个 Wayland 会话 ($count),无法确定目标变量。" >&2 | |
| exit 1 | |
| fi | |
| # 获取文件名(只取最后一部分作为变量值) | |
| w_display=$(basename "${wayland_files[0]}") | |
| #echo "检测到 Wayland 环境: $w_display" | |
| DISPLAY=:0 WAYLAND_DISPLAY="$w_display" "$currdir/$cmd_name" | |
| else | |
| # 纯 X11 环境 | |
| #echo "未检测到 Wayland,使用 X11 模式" | |
| DISPLAY=:0 xrandr --output DP-2 --mode '2560x1440' --rate 165 --primary | |
| DISPLAY=:0 "$currdir/$cmd_name" | |
| fi | |
| } | |
| # CLI 参数处理 | |
| case "$1" in | |
| "start") | |
| run_cmd "start-steam-and-game" | |
| ;; | |
| "exit") | |
| run_cmd "exit-game-and-steam" | |
| ;; | |
| *) | |
| echo "用法: $0 {start|exit}" | |
| exit 1 | |
| ;; | |
| esac |
| #!/usr/bin/env ruby | |
| # --- 核心动作函数 --- | |
| def 移动并点击(x, y) | |
| # 基础算法: (x / 2560.0 * 1920.0 / 2.0) | |
| ratio = 1920.0 / 2560.0 / 2.0 | |
| target_x = x * ratio | |
| target_y = y * ratio | |
| # 如果参数中包含 -x,则在此基础上乘上 1.3333333333 | |
| if ARGV.include?("-x") | |
| target_x *= 1.3333333333 | |
| target_y *= 1.3333333333 | |
| end | |
| # 最终取整 | |
| final_x = target_x.round | |
| final_y = target_y.round | |
| `sudo ydotool mousemove -a -x #{final_x} -y #{final_y}` | |
| sleep 0.5 | |
| `sudo ydotool click 0xC0` | |
| end | |
| def 按下Esc键 | |
| `sudo ydotool key 1:1 1:0` | |
| sleep 0.5 | |
| end | |
| # --- 业务逻辑封装 --- | |
| def 主界面关通知 | |
| 移动并点击(1341, 1242) | |
| end | |
| def 主菜单开始游戏 | |
| 移动并点击(1300, 864) | |
| end | |
| def 第一个存档 | |
| 移动并点击(1740, 334) | |
| end | |
| def 存档开始游戏 | |
| 移动并点击(1844, 1305) | |
| end | |
| def 游戏内打开菜单 | |
| 按下Esc键 | |
| end | |
| def 返回标题 | |
| 移动并点击(729, 1166) | |
| end | |
| def 返回确认 | |
| 移动并点击(1158, 949) | |
| end | |
| def 退出游戏 | |
| 移动并点击(1393, 1300) | |
| end | |
| def 退出确认 | |
| 移动并点击(1166, 938) | |
| end | |
| # --- 流程控制函数 --- | |
| def 启动流程 | |
| 主界面关通知 | |
| sleep 1 | |
| 主菜单开始游戏 | |
| sleep 1 | |
| 第一个存档 | |
| sleep 1 | |
| 存档开始游戏 | |
| sleep 1 | |
| end | |
| def 退出流程 | |
| 游戏内打开菜单 | |
| sleep 1 | |
| 返回标题 | |
| sleep 1 | |
| 返回确认 | |
| sleep 15 | |
| 退出游戏 | |
| sleep 1 | |
| 退出确认 | |
| end | |
| # --- 命令行参数解析 --- | |
| # 获取第一个参数 | |
| command = ARGV[0] | |
| case command | |
| when "start" | |
| 启动流程 | |
| when "exit" | |
| 退出流程 | |
| else | |
| puts "使用说明: ruby #{__FILE__} [start|exit]" | |
| end |
| #!/bin/bash | |
| set -e | |
| # 生成随机数 0 或 1 | |
| # $RANDOM 会返回 0 到 32767 之间的整数 | |
| result=$(( $RANDOM % 2 )) | |
| #result=1 | |
| { | |
| echo "脚本已启动(当前用户 UID: $(id -u))" | |
| echo "请等待 10 秒钟..." | |
| } | wall | |
| # 等待 10 秒 | |
| sleep 10 | |
| echo 'Starting your desktop...' | wall | |
| cd | |
| if [ $result -eq 0 ]; then | |
| #echo "结果为 0,正在启动 start..." | |
| startx | |
| else | |
| #echo "结果为 1,正在启动 start-hyprland..." | |
| start-hyprland | |
| fi |
| #!/bin/bash | |
| set -e | |
| currdir="$(dirname "$0")" | |
| game_id=1623730 | |
| extra_args="" | |
| if [ -z "$WAYLAND_DISPLAY" ]; then | |
| extra_args="-x" | |
| fi | |
| steam-np steam://rungameid/$game_id & | |
| notify-send 'Wait for game to launch' -t 2000 | |
| sleep 70 | |
| notify-send 'Start automation' -t 2000 | |
| "$currdir"/palworld-automation start $extra_args |