云库可选: 服务支持 cloud 请求参数, 油猴面板加'使用云库'开关(v1.5.0)
This commit is contained in:
1 parent
dad361e4b0
commit
06747bf76c
2 files changed
+30
-14
No files matched your search
+16
-3
@@ -2,7 +2,7 @@
|
|||||||
// @name Xiangqi.com 本地皮卡鱼引擎对接
|
// @name Xiangqi.com 本地皮卡鱼引擎对接
|
||||||
// @name:en Xiangqi.com Local Pikafish Engine
|
// @name:en Xiangqi.com Local Pikafish Engine
|
||||||
// @namespace awesome-xiangqi
|
// @namespace awesome-xiangqi
|
||||||
// @version 1.4.1
|
// @version 1.5.0
|
||||||
// @description 在 play.xiangqi.com 对局中调用本地皮卡鱼引擎(http://127.0.0.1:9377)自动/手动走子
|
// @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
|
// @description:en Hook the site WebSocket and let a local Pikafish engine play for you
|
||||||
// @match https://play.xiangqi.com/*
|
// @match https://play.xiangqi.com/*
|
||||||
@@ -16,7 +16,7 @@
|
|||||||
const ENGINE_URL = "http://127.0.0.1:9377";
|
const ENGINE_URL = "http://127.0.0.1:9377";
|
||||||
const NS = "42/xiangqi,";
|
const NS = "42/xiangqi,";
|
||||||
const FIRST_MOVE_EXTRA_SEC = 5;
|
const FIRST_MOVE_EXTRA_SEC = 5;
|
||||||
const VERSION = "1.4.1";
|
const VERSION = "1.5.0";
|
||||||
const PATCH_MARK = Symbol.for("xiangqi.pikafish.ws.patched");
|
const PATCH_MARK = Symbol.for("xiangqi.pikafish.ws.patched");
|
||||||
const uW = typeof unsafeWindow !== "undefined" ? unsafeWindow : window;
|
const uW = typeof unsafeWindow !== "undefined" ? unsafeWindow : window;
|
||||||
|
|
||||||
@@ -48,6 +48,7 @@
|
|||||||
|
|
||||||
const cfg = {
|
const cfg = {
|
||||||
enabled: storage.get("pf_enabled", "0") === "1",
|
enabled: storage.get("pf_enabled", "0") === "1",
|
||||||
|
useCloud: storage.get("pf_usecloud", "1") === "1",
|
||||||
movetime: Math.max(50, +storage.get("pf_movetime", 800) || 800),
|
movetime: Math.max(50, +storage.get("pf_movetime", 800) || 800),
|
||||||
depth: Math.max(0, +storage.get("pf_depth", 0) || 0),
|
depth: Math.max(0, +storage.get("pf_depth", 0) || 0),
|
||||||
firstDelay: Math.max(0, +storage.get("pf_firstdelay", 0) || 0),
|
firstDelay: Math.max(0, +storage.get("pf_firstdelay", 0) || 0),
|
||||||
@@ -60,6 +61,7 @@
|
|||||||
|
|
||||||
function saveCfg() {
|
function saveCfg() {
|
||||||
storage.set("pf_enabled", cfg.enabled ? "1" : "0");
|
storage.set("pf_enabled", cfg.enabled ? "1" : "0");
|
||||||
|
storage.set("pf_usecloud", cfg.useCloud ? "1" : "0");
|
||||||
storage.set("pf_movetime", cfg.movetime);
|
storage.set("pf_movetime", cfg.movetime);
|
||||||
storage.set("pf_depth", cfg.depth);
|
storage.set("pf_depth", cfg.depth);
|
||||||
storage.set("pf_firstdelay", cfg.firstDelay);
|
storage.set("pf_firstdelay", cfg.firstDelay);
|
||||||
@@ -195,7 +197,7 @@
|
|||||||
|
|
||||||
let liveWS = null;
|
let liveWS = null;
|
||||||
|
|
||||||
async function engineBestmove({ moves, fen, movetime, depth, signal }) {
|
async function engineBestmove({ moves, fen, movetime, depth, useCloud, signal }) {
|
||||||
const timeoutMs = Math.max(15000, movetime * 4 + 2000);
|
const timeoutMs = Math.max(15000, movetime * 4 + 2000);
|
||||||
const ctrl = new AbortController();
|
const ctrl = new AbortController();
|
||||||
const timer = setTimeout(
|
const timer = setTimeout(
|
||||||
@@ -216,6 +218,7 @@
|
|||||||
fen: fen || undefined,
|
fen: fen || undefined,
|
||||||
movetime,
|
movetime,
|
||||||
depth: depth > 0 ? depth : undefined,
|
depth: depth > 0 ? depth : undefined,
|
||||||
|
cloud: useCloud !== false,
|
||||||
}),
|
}),
|
||||||
signal: ctrl.signal,
|
signal: ctrl.signal,
|
||||||
});
|
});
|
||||||
@@ -296,6 +299,7 @@
|
|||||||
fen: G.fen,
|
fen: G.fen,
|
||||||
movetime: cfg.movetime,
|
movetime: cfg.movetime,
|
||||||
depth: cfg.depth,
|
depth: cfg.depth,
|
||||||
|
useCloud: cfg.useCloud,
|
||||||
});
|
});
|
||||||
if (reqId !== G.reqId) return;
|
if (reqId !== G.reqId) return;
|
||||||
if (G.over) throw new Error("对局已结束");
|
if (G.over) throw new Error("对局已结束");
|
||||||
@@ -494,6 +498,7 @@
|
|||||||
<div class="title"><span>🐴 皮卡鱼</span><span class="dot" id="pf-dot" style="background:#666"></span></div>
|
<div class="title"><span>🐴 皮卡鱼</span><span class="dot" id="pf-dot" style="background:#666"></span></div>
|
||||||
<div class="row"><label><input type="checkbox" id="pf-enabled"> 自动走子</label>
|
<div class="row"><label><input type="checkbox" id="pf-enabled"> 自动走子</label>
|
||||||
<span id="pf-side-hint"></span></div>
|
<span id="pf-side-hint"></span></div>
|
||||||
|
<div class="row"><label><input type="checkbox" id="pf-cloud"> 使用云库</label></div>
|
||||||
<div class="row"><span>每步用时(ms)</span><input type="number" id="pf-mt" min="50" step="50"></div>
|
<div class="row"><span>每步用时(ms)</span><input type="number" id="pf-mt" min="50" step="50"></div>
|
||||||
<div class="row"><span>深度(0=用时间)</span><input type="number" id="pf-depth" min="0" max="40" step="1"></div>
|
<div class="row"><span>深度(0=用时间)</span><input type="number" id="pf-depth" min="0" max="40" step="1"></div>
|
||||||
<div class="row"><span>首步延迟(秒+5)</span><input type="number" id="pf-delay" min="0" max="120" step="1"></div>
|
<div class="row"><span>首步延迟(秒+5)</span><input type="number" id="pf-delay" min="0" max="120" step="1"></div>
|
||||||
@@ -519,6 +524,14 @@
|
|||||||
if (cfg.enabled) scheduleTurn(0);
|
if (cfg.enabled) scheduleTurn(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const cloudCb = panel.querySelector("#pf-cloud");
|
||||||
|
cloudCb.checked = cfg.useCloud;
|
||||||
|
cloudCb.addEventListener("change", () => {
|
||||||
|
cfg.useCloud = cloudCb.checked;
|
||||||
|
saveCfg();
|
||||||
|
if (cfg.enabled) scheduleTurn(0);
|
||||||
|
});
|
||||||
|
|
||||||
const mt = panel.querySelector("#pf-mt");
|
const mt = panel.querySelector("#pf-mt");
|
||||||
mt.value = cfg.movetime;
|
mt.value = cfg.movetime;
|
||||||
mt.addEventListener("change", () => {
|
mt.addEventListener("change", () => {
|
||||||
|
|||||||
+14
-11
@@ -31,20 +31,22 @@ class Service:
|
|||||||
await self.engine.quit()
|
await self.engine.quit()
|
||||||
await self.cloud.close()
|
await self.cloud.close()
|
||||||
|
|
||||||
async def bestmove(self, moves_site, fen=None, movetime=None, depth=None):
|
async def bestmove(self, moves_site, fen=None, movetime=None, depth=None, req_cloud=None):
|
||||||
moves_ucci = [site_to_ucci(m) for m in moves_site]
|
moves_ucci = [site_to_ucci(m) for m in moves_site]
|
||||||
fen = fen or fen_from_moves(moves_ucci)
|
fen = fen or fen_from_moves(moves_ucci)
|
||||||
|
|
||||||
# 1. 云库优先
|
# 1. 云库优先(请求可显式关闭; 深度模式强制引擎)
|
||||||
try:
|
use_cloud = req_cloud if req_cloud is not None else True
|
||||||
cloud_hit = await self.cloud.best(fen, moves_count=len(moves_ucci))
|
if use_cloud and not depth:
|
||||||
except Exception as e:
|
try:
|
||||||
log.warning('cloud error: %s', e)
|
cloud_hit = await self.cloud.best(fen, moves_count=len(moves_ucci))
|
||||||
cloud_hit = None
|
except Exception as e:
|
||||||
if cloud_hit and not depth:
|
log.warning('cloud error: %s', e)
|
||||||
mv, score, note = cloud_hit
|
cloud_hit = None
|
||||||
return {'ok': True, 'source': 'cloud', 'move': ucci_to_site(mv),
|
if cloud_hit:
|
||||||
'score': f'cp {score}', 'note': note, 'ms': 0}
|
mv, score, note = cloud_hit
|
||||||
|
return {'ok': True, 'source': 'cloud', 'move': ucci_to_site(mv),
|
||||||
|
'score': f'cp {score}', 'note': note, 'ms': 0}
|
||||||
|
|
||||||
# 2. 皮卡鱼兜底
|
# 2. 皮卡鱼兜底
|
||||||
try:
|
try:
|
||||||
@@ -96,6 +98,7 @@ def make_app(args):
|
|||||||
fen=req.get('fen'),
|
fen=req.get('fen'),
|
||||||
movetime=req.get('movetime'),
|
movetime=req.get('movetime'),
|
||||||
depth=req.get('depth'),
|
depth=req.get('depth'),
|
||||||
|
req_cloud=req.get('cloud'),
|
||||||
)
|
)
|
||||||
return web.json_response(out)
|
return web.json_response(out)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
Reference in new issue
Block a user