93 lines
5.1 KiB
PowerShell
93 lines
5.1 KiB
PowerShell
# 注册无人值守用的两个计划任务 (在跑游戏的那个账号下, 普通 PowerShell 即可):
|
|
# LcMCPKeepDisplay 每 1 分钟跑 keep_display.ps1: 会话掉到 Disc 时重挂 console, 防止截图全黑
|
|
# LcMCPDailyCycle 定时跑 auto_cycle.py: 刷材料 -> 镜牢 -> 升级/同步, 报告写到 ~\lc_mcp\cycles
|
|
#
|
|
# 用法:
|
|
# powershell -ExecutionPolicy Bypass -File scripts\install_unattended.ps1
|
|
# 可选: -At 05:30 每日运行时间。游戏每日重置为 06:00 (KST), 即北京时间 05:00
|
|
# -EveryHours 2.5 改成"每 N 小时重复一轮"以持续刷取; 0 = 只按 -At 每天一次
|
|
# 任务时限包含 daily 45 分钟、每局镜牢 100 分钟、各一次重试及 30 分钟收尾
|
|
# -Mirror 1 每轮镜牢局数, 0 = 不打镜牢
|
|
# -Target 34 把队内人格升到的等级
|
|
# -Python <路径> 默认 vendor\aalc\.venv\Scripts\pythonw.exe (无窗口, 不抢游戏焦点)
|
|
# -NoWatchdog 只装每日任务
|
|
# -Uninstall
|
|
#
|
|
# 前提: LcMCPServer 已用 install_logon_task.ps1 装好并在运行。
|
|
# 先手动跑一轮确认无误再装: python scripts\auto_cycle.py --mirror 1
|
|
param(
|
|
[string]$At = "05:30",
|
|
[double]$EveryHours = 0,
|
|
[int]$Mirror = 1,
|
|
[int]$Target = 34,
|
|
[string]$Python = "",
|
|
[switch]$NoWatchdog,
|
|
[switch]$Uninstall
|
|
)
|
|
$ErrorActionPreference = "Stop"
|
|
$Repo = Split-Path -Parent $PSScriptRoot
|
|
$Watchdog = "LcMCPKeepDisplay"
|
|
$Daily = "LcMCPDailyCycle"
|
|
|
|
if ($Uninstall) {
|
|
foreach ($name in @($Watchdog, $Daily)) {
|
|
Unregister-ScheduledTask -TaskName $name -Confirm:$false -ErrorAction SilentlyContinue
|
|
Write-Output "removed $name"
|
|
}
|
|
exit 0
|
|
}
|
|
|
|
if (-not $Python) { $Python = Join-Path $Repo "vendor\aalc\.venv\Scripts\pythonw.exe" }
|
|
if (-not (Test-Path $Python)) {
|
|
throw "找不到 Python: $Python`n先在 vendor\aalc 里执行 uv sync, 或用 -Python 指定"
|
|
}
|
|
if (-not (Get-ScheduledTask -TaskName "LcMCPServer" -ErrorAction SilentlyContinue)) {
|
|
Write-Warning "没有找到 LcMCPServer 计划任务, 每日任务需要它: 先运行 scripts\install_logon_task.ps1"
|
|
}
|
|
|
|
$user = "$env:USERDOMAIN\$env:USERNAME"
|
|
# Limited: keep_display.ps1 自己用 RunAs 静默提权跑 tscon (需要 ConsentPromptBehaviorAdmin=0)
|
|
$principal = New-ScheduledTaskPrincipal -UserId $user -LogonType Interactive -RunLevel Limited
|
|
|
|
if (-not $NoWatchdog) {
|
|
$ps = "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe"
|
|
$script = Join-Path $PSScriptRoot "keep_display.ps1"
|
|
$action = New-ScheduledTaskAction -Execute $ps -WorkingDirectory $Repo `
|
|
-Argument "-NoProfile -NonInteractive -WindowStyle Hidden -ExecutionPolicy Bypass -File `"$script`""
|
|
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(1) `
|
|
-RepetitionInterval (New-TimeSpan -Minutes 1) -RepetitionDuration (New-TimeSpan -Days 3650)
|
|
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries `
|
|
-ExecutionTimeLimit (New-TimeSpan -Minutes 3) -MultipleInstances IgnoreNew
|
|
Register-ScheduledTask -TaskName $Watchdog -Action $action -Trigger $trigger -Principal $principal `
|
|
-Settings $settings -Force | Out-Null
|
|
Write-Output "registered $Watchdog (every 1 min) -> $script"
|
|
}
|
|
|
|
$cycle = Join-Path $PSScriptRoot "auto_cycle.py"
|
|
$action = New-ScheduledTaskAction -Execute $Python -WorkingDirectory $Repo `
|
|
-Argument "-X utf8 `"$cycle`" --mirror $Mirror --target $Target"
|
|
# 与 auto_cycle.py 的 DAILY_TIMEOUT_S / MIRROR_TIMEOUT_S / STALL_RETRIES 对齐。
|
|
# 每个任务最多两次尝试, 另留 30 分钟给启动、轮询和升级; 避免外层先杀掉报告进程。
|
|
$limit = New-TimeSpan -Minutes (30 + 2 * (45 + 100 * [Math]::Max($Mirror, 0)))
|
|
if ($EveryHours -gt 0) {
|
|
# 持续刷取: 每 N 小时重复一轮。开始时间取"现在", 装完即进入节奏。
|
|
# 注意 New-TimeSpan -Hours 的参数是 [int], 传 2.5 会被截断成 2 小时, 所以用 -Minutes。
|
|
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(1) `
|
|
-RepetitionInterval (New-TimeSpan -Minutes ($EveryHours * 60)) `
|
|
-RepetitionDuration (New-TimeSpan -Days 3650)
|
|
$desc = "every $EveryHours h"
|
|
# 重复周期必须大于单轮上限, 否则会在上一轮还没结束时又起一轮
|
|
if (($EveryHours * 60) -lt $limit.TotalMinutes) {
|
|
Write-Warning "EveryHours=$EveryHours ($($EveryHours * 60) 分钟) 短于单轮上限 $limit, 可能重叠; MultipleInstances=IgnoreNew 会跳过, 但建议调大"
|
|
}
|
|
} else {
|
|
$trigger = New-ScheduledTaskTrigger -Daily -At $At
|
|
$desc = "daily at $At"
|
|
}
|
|
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries `
|
|
-StartWhenAvailable -ExecutionTimeLimit $limit -MultipleInstances IgnoreNew
|
|
Register-ScheduledTask -TaskName $Daily -Action $action -Trigger $trigger -Principal $principal `
|
|
-Settings $settings -Force | Out-Null
|
|
Write-Output "registered $Daily ($desc, mirror=$Mirror, target=Lv.$Target, limit=$limit)"
|
|
Write-Output "reports -> $env:USERPROFILE\lc_mcp\cycles\cycle_*.json"
|