diff --git a/xiangqi-engine.user.js b/xiangqi-engine.user.js index d5d7f43..20f8671 100644 --- a/xiangqi-engine.user.js +++ b/xiangqi-engine.user.js @@ -2,7 +2,7 @@ // @name Xiangqi.com 本地皮卡鱼引擎对接 // @name:en Xiangqi.com Local Pikafish Engine // @namespace awesome-xiangqi -// @version 1.6.0 +// @version 1.7.0 // @description 在 play.xiangqi.com 对局中调用本地皮卡鱼引擎(http://127.0.0.1:9377)自动/手动走子 // @description:en Hook the site WebSocket and let a local Pikafish engine play for you // @match https://play.xiangqi.com/* @@ -16,7 +16,7 @@ const ENGINE_URL = "http://127.0.0.1:9377"; const NS = "42/xiangqi,"; const FIRST_MOVE_EXTRA_SEC = 5; - const VERSION = "1.6.0"; + const VERSION = "1.7.0"; const PATCH_MARK = Symbol.for("xiangqi.pikafish.ws.patched"); const uW = typeof unsafeWindow !== "undefined" ? unsafeWindow : window; @@ -53,6 +53,7 @@ movetime: Math.max(50, +storage.get("pf_movetime", 800) || 800), depth: Math.max(0, +storage.get("pf_depth", 0) || 0), firstDelay: Math.max(0, +storage.get("pf_firstdelay", 0) || 0), + moveDelay: Math.max(0, +storage.get("pf_movedelay", 1) || 1), // 每步最小间隔秒(深度/云库秒回时保证节奏) sideOverride: ["auto", "red", "black"].includes( storage.get("pf_side", "auto"), ) @@ -67,6 +68,7 @@ storage.set("pf_movetime", cfg.movetime); storage.set("pf_depth", cfg.depth); storage.set("pf_firstdelay", cfg.firstDelay); + storage.set("pf_movedelay", cfg.moveDelay); storage.set("pf_side", cfg.sideOverride); } @@ -78,6 +80,7 @@ over: false, thinking: false, firstMoveDeadline: 0, + lastMoveAt: 0, reqId: 0, turnTimer: 0, sideCache: { at: 0, side: null }, @@ -293,9 +296,20 @@ } } - const reqId = ++G.reqId; + // 每步最小间隔(深度/云库秒回时保持行棋节奏), 计入 thinking_time G.thinking = true; - setStatus("皮卡鱼思考中…", true); + if (!force && cfg.moveDelay > 0) { + const remainMs = cfg.moveDelay * 1000 - (Date.now() - (G.lastMoveAt || 0)); + if (remainMs > 0) { + setStatus(`行棋节奏延迟: ${Math.ceil(remainMs / 1000)}s`, true); + await new Promise((r) => setTimeout(r, remainMs)); + if (G.over || !isMyTurn()) { + G.thinking = false; + return; + } + } + } + const reqId = ++G.reqId; try { const out = await engineBestmove({ moves: G.moves.slice(), @@ -320,6 +334,7 @@ throw new Error("未找到 socket 连接"); G.moves.push(move); + G.lastMoveAt = Date.now(); setScore( `${out.source === "cloud" ? "☁ 云库" : "♞ 引擎"} · 评分 ${out.score ?? "?"} · ${out.ms ?? "?"}ms`, ); @@ -507,6 +522,7 @@