@wsl233/dsh-gomoku 0.3.4 → 0.3.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -7
- package/client.js +1 -1
- package/index.js +161 -22
- package/lan-server.js +175 -7
- package/package.json +1 -1
- package/preview.html +1 -1
- package/ui/gomoku-ui.js +600 -42
- package/ui/gomoku.css +176 -5
- package/ui/lan.html +78 -2
package/README.md
CHANGED
|
@@ -2,16 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
DeepSeek Harness(DSH)的五子棋插件:左侧栏「五子棋」入口,主页面提供棋盘、对手选择和完整对局操作。
|
|
4
4
|
|
|
5
|
-
当前版本:0.3.
|
|
5
|
+
当前版本:0.3.10。支持本地双人、大肥鱼算法(简单 / 中等 / 困难)、局域网双人、DSH 模型服务驱动的 DeepSeek 对战、提示、复盘(含胜率曲线)、棋谱导出/导入、对局保留、全屏和实时胜率。
|
|
6
6
|
|
|
7
7
|
## 安装
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
发布后的安装方式:
|
|
10
10
|
|
|
11
11
|
```bash
|
|
12
12
|
npm install -g @wsl233/dsh-gomoku
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
+
当前本地版本 0.3.10 尚未发布到 npm;发布前请继续用下面的本地安装命令。
|
|
16
|
+
|
|
15
17
|
也可以在 DSH 插件管理器里安装 `@wsl233/dsh-gomoku`。本地开发时使用:
|
|
16
18
|
|
|
17
19
|
```text
|
|
@@ -23,10 +25,10 @@ plugin_manager install_bundle target="file:C:/Users/rache/Desktop/DENNIS/wuziqi/
|
|
|
23
25
|
## 使用
|
|
24
26
|
|
|
25
27
|
- **本地双人**:同一台电脑轮流落子,支持撤销、新局、提示、复盘。
|
|
26
|
-
- **与大肥鱼对战**:你执黑先手,AI
|
|
27
|
-
-
|
|
28
|
-
- **与 DeepSeek 对战**:复用 DSH 已配置的模型服务,浏览器不接触 API Key
|
|
29
|
-
- **提示 / 复盘 /
|
|
28
|
+
- **与大肥鱼对战**:你执黑先手,AI 执白;算法对战可选简单 / 中等 / 困难,DeepSeek 对战不使用难度档。
|
|
29
|
+
- **局域网对战**:点击后本机启动服务端并生成加入链接与二维码;同一局域网的另一台电脑打开链接即可执白加入。浏览器端不显示换对手的「+」和模式菜单;新局、悔棋需对方同意;房主可关闭双方提示;棋谱只能在开局前或一局结束后导入并同步双方;未带房间号的链接会先显示房间号输入页。房主可在联机设置弹窗里「退出联机对战」,退出后重新点右上角 + 即可换对手;房主必须保持 DSH 打开,端口需在防火墙放行。
|
|
30
|
+
- **与 DeepSeek 对战**:复用 DSH 已配置的模型服务,浏览器不接触 API Key;请求使用紧凑棋谱和短提示词。模型没有返回可用落点、请求超时或被中断时,本手不会自动落子,而是弹出提示,让你选择「重试 DeepSeek」或「切换回算法对战」。宿主对单次请求的等待上限为 10 分钟,最大输出 token 为 8192;提示词会直接给出棋盘、候选点与优先级,要求模型少推理、直接输出坐标。棋盘下方可展开「DeepSeek 过程」,查看模型、提示词、推理与原始输出;成功和失败都会记录。
|
|
31
|
+
- **提示 / 复盘 / 导出导入**:提示用算法搜索最佳落子;复盘可逐手回看,底部同时绘制黑棋胜率随手数变化的曲线(0–100 刻度、50% 参考线、当前手标记、悬停提示与点击跳转);导出只生成一个 `GM1.` 开头的字符串并自动复制,导入时粘贴该字符串即可。
|
|
30
32
|
- **对局保留**:离开 DSH 五子棋面板再回来,本地 / 人机棋局会从 localStorage 恢复。
|
|
31
33
|
- **全屏**:只保留棋盘界面,隐藏 DSH 侧边栏。
|
|
32
34
|
|
|
@@ -34,7 +36,7 @@ plugin_manager install_bundle target="file:C:/Users/rache/Desktop/DENNIS/wuziqi/
|
|
|
34
36
|
|
|
35
37
|
服务端由 `lan-server.js` 提供,端口 8790–8799,同时提供加入页、静态 UI 和 `/ws` WebSocket;二维码由 `vendor/qrcode.mjs` 本地生成,不依赖 npm 包。
|
|
36
38
|
|
|
37
|
-
加入链接形如 `http://<本机局域网 IP>:8790/lan.html?room=1234
|
|
39
|
+
加入链接形如 `http://<本机局域网 IP>:8790/lan.html?room=1234`;如果只打开 `lan.html` 而不带房间号,会显示 4 位房间号输入页。如果对方打不开,请确认两台电脑在同一局域网,并在防火墙中允许 Node/DSH 监听该端口。
|
|
38
40
|
|
|
39
41
|
## 开发
|
|
40
42
|
|
package/client.js
CHANGED
|
@@ -27,7 +27,7 @@ if (!window.__dshGomokuClientLoaded) {
|
|
|
27
27
|
const PANEL_ID = 'gomoku';
|
|
28
28
|
const UI_BASE = '/dsh-gomoku';
|
|
29
29
|
const STYLE_ID = 'dsh-gomoku-style';
|
|
30
|
-
const UI_VERSION = '0.2.0-
|
|
30
|
+
const UI_VERSION = '0.2.0-32';
|
|
31
31
|
const withVersion = (src) => src + '?v=' + UI_VERSION;
|
|
32
32
|
|
|
33
33
|
let scriptPromise = null;
|
package/index.js
CHANGED
|
@@ -21,6 +21,8 @@ const ROUTE_PREFIX = '/dsh-gomoku';
|
|
|
21
21
|
const ROOT = fileURLToPath(new URL('.', import.meta.url));
|
|
22
22
|
const UI_DIR = resolve(ROOT, 'ui');
|
|
23
23
|
const ASSET_DIR = resolve(ROOT, 'assets');
|
|
24
|
+
const DEEPSEEK_TIMEOUT_MS = 10 * 60 * 1000;
|
|
25
|
+
const DEEPSEEK_MAX_TOKENS = 8192;
|
|
24
26
|
|
|
25
27
|
let lanServer = null;
|
|
26
28
|
let lanServerPromise = null;
|
|
@@ -222,7 +224,14 @@ async function handleRequest(webCtx, req, res) {
|
|
|
222
224
|
sendJson(res, { ok: true, ...result }, method);
|
|
223
225
|
} catch (error) {
|
|
224
226
|
console.warn('[dsh-gomoku] DeepSeek move failed:', error);
|
|
225
|
-
sendJson(res, {
|
|
227
|
+
sendJson(res, {
|
|
228
|
+
ok: false,
|
|
229
|
+
error: 'deepseek-failed',
|
|
230
|
+
message: describeDeepseekError(error),
|
|
231
|
+
prompt: error && error.prompt || '',
|
|
232
|
+
text: error && error.text || '',
|
|
233
|
+
reasoning: error && error.reasoning || '',
|
|
234
|
+
}, method, 504);
|
|
226
235
|
}
|
|
227
236
|
return;
|
|
228
237
|
}
|
|
@@ -231,7 +240,7 @@ async function handleRequest(webCtx, req, res) {
|
|
|
231
240
|
sendJson(res, {
|
|
232
241
|
ok: true,
|
|
233
242
|
name,
|
|
234
|
-
version: '0.
|
|
243
|
+
version: '0.3.8',
|
|
235
244
|
phase: 'play',
|
|
236
245
|
capabilities: {
|
|
237
246
|
lan: true,
|
|
@@ -326,6 +335,110 @@ function normalizeBoard(payload) {
|
|
|
326
335
|
return payload.board.map((value) => (value === 1 || value === 2 ? value : 0));
|
|
327
336
|
}
|
|
328
337
|
|
|
338
|
+
const PROMPT_DIRECTIONS = [[1, 0], [0, 1], [1, 1], [1, -1]];
|
|
339
|
+
|
|
340
|
+
function promptFiveAt(board, x, y, color) {
|
|
341
|
+
for (const [dx, dy] of PROMPT_DIRECTIONS) {
|
|
342
|
+
let count = 1;
|
|
343
|
+
for (const sign of [-1, 1]) {
|
|
344
|
+
let cx = x + dx * sign;
|
|
345
|
+
let cy = y + dy * sign;
|
|
346
|
+
while (cx >= 0 && cx < 19 && cy >= 0 && cy < 19 && board[cy * 19 + cx] === color) {
|
|
347
|
+
count += 1;
|
|
348
|
+
cx += dx * sign;
|
|
349
|
+
cy += dy * sign;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
if (count >= 5) return true;
|
|
353
|
+
}
|
|
354
|
+
return false;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function buildMovePrompt(board, moves, side) {
|
|
358
|
+
const color = side === 1 ? 1 : 2;
|
|
359
|
+
const opponent = color === 1 ? 2 : 1;
|
|
360
|
+
const colorName = color === 1 ? '黑' : '白';
|
|
361
|
+
const stones = board.filter(Boolean).length;
|
|
362
|
+
const candidates = [];
|
|
363
|
+
if (stones === 0) {
|
|
364
|
+
candidates.push({ x: 9, y: 9, ownWin: false, block: false, near: 0, label: '天元' });
|
|
365
|
+
} else {
|
|
366
|
+
for (let y = 0; y < 19; y += 1) {
|
|
367
|
+
for (let x = 0; x < 19; x += 1) {
|
|
368
|
+
if (board[y * 19 + x]) continue;
|
|
369
|
+
let near = 0;
|
|
370
|
+
for (let yy = Math.max(0, y - 2); yy <= Math.min(18, y + 2); yy += 1) {
|
|
371
|
+
for (let xx = Math.max(0, x - 2); xx <= Math.min(18, x + 2); xx += 1) {
|
|
372
|
+
if (board[yy * 19 + xx]) near += 1;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
if (!near && stones) continue;
|
|
376
|
+
board[y * 19 + x] = color;
|
|
377
|
+
const ownWin = promptFiveAt(board, x, y, color);
|
|
378
|
+
board[y * 19 + x] = 0;
|
|
379
|
+
board[y * 19 + x] = opponent;
|
|
380
|
+
const block = promptFiveAt(board, x, y, opponent);
|
|
381
|
+
board[y * 19 + x] = 0;
|
|
382
|
+
const center = 18 - (Math.abs(x - 9) + Math.abs(y - 9));
|
|
383
|
+
candidates.push({ x, y, ownWin, block, near, center, label: ownWin ? '立即成五' : (block ? '必须堵' : '') });
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
candidates.sort((a, b) => {
|
|
388
|
+
if (a.ownWin !== b.ownWin) return a.ownWin ? -1 : 1;
|
|
389
|
+
if (a.block !== b.block) return a.block ? -1 : 1;
|
|
390
|
+
if (a.near !== b.near) return b.near - a.near;
|
|
391
|
+
return b.center - a.center;
|
|
392
|
+
});
|
|
393
|
+
const top = candidates.slice(0, 12);
|
|
394
|
+
const rows = [];
|
|
395
|
+
const header = 'y\\x ' + Array.from({ length: 19 }, (_, i) => String(i).padStart(2, ' ') + ' ').join('');
|
|
396
|
+
rows.push(header);
|
|
397
|
+
for (let y = 0; y < 19; y += 1) {
|
|
398
|
+
let row = String(y).padStart(2, ' ') + ' ';
|
|
399
|
+
for (let x = 0; x < 19; x += 1) {
|
|
400
|
+
const value = board[y * 19 + x];
|
|
401
|
+
row += (value === 1 ? 'B' : value === 2 ? 'W' : '.') + ' ';
|
|
402
|
+
}
|
|
403
|
+
rows.push(row);
|
|
404
|
+
}
|
|
405
|
+
const last = moves.length ? moves[moves.length - 1] : null;
|
|
406
|
+
const candidateLines = top.map((item, index) => {
|
|
407
|
+
const tags = [];
|
|
408
|
+
if (item.ownWin) tags.push('立即成五');
|
|
409
|
+
if (item.block) tags.push('必须堵');
|
|
410
|
+
if (!tags.length) tags.push('邻近' + item.near + '子');
|
|
411
|
+
return `${index + 1}. (${item.x},${item.y}) ${tags.join('/')}`;
|
|
412
|
+
});
|
|
413
|
+
const ownWins = top.filter((item) => item.ownWin).length;
|
|
414
|
+
const blocks = top.filter((item) => item.block).length;
|
|
415
|
+
const prompt = [
|
|
416
|
+
'你是五子棋落子选择器,执' + colorName + '方。19 路棋盘,横竖斜连续 5 子获胜;x 是列,y 是行,范围 0-18。',
|
|
417
|
+
'棋盘记号:B=黑,W=白,.=空;左侧数字为 y 行号,顶部为 x 列号。',
|
|
418
|
+
'当前共 ' + stones + ' 手' + (last ? ',最后一手:(' + last.x + ',' + last.y + ')' : ',空盘') + '。',
|
|
419
|
+
'棋盘:',
|
|
420
|
+
rows.join('\n'),
|
|
421
|
+
'候选空位(已按优先级排序):',
|
|
422
|
+
candidateLines.length ? candidateLines.join('\n') : '(无可用空位)',
|
|
423
|
+
'决策规则,按顺序执行:',
|
|
424
|
+
'1. 只要有“立即成五”,直接选择第一个,立刻获胜。',
|
|
425
|
+
'2. 否则若有“必须堵”,选择能同时制造最大威胁的堵点;只有一个时必须选它。',
|
|
426
|
+
'3. 否则只从前 5 个候选中选择,优先靠近最后一手、靠近中心、能形成活三或冲四的点。',
|
|
427
|
+
'为了尽快给出结果:不要复述棋盘,不要解释规则,不要输出分析过程;只输出一个坐标,格式 x,y。',
|
|
428
|
+
'只输出一行,例如 9,8。',
|
|
429
|
+
].join('\n');
|
|
430
|
+
return { prompt, candidates: top, ownWins, blocks };
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
function describeDeepseekError(error) {
|
|
434
|
+
const raw = String(error && error.message ? error.message : error);
|
|
435
|
+
const name = String(error && error.name || '');
|
|
436
|
+
if (name === 'AbortError' || name === 'TimeoutError' || /abort|timeout|timed out|中断|超时/i.test(raw)) {
|
|
437
|
+
return 'DeepSeek 请求在 10 分钟内没有完成,已被中断;本次没有落子。';
|
|
438
|
+
}
|
|
439
|
+
return 'DeepSeek 请求失败:' + raw;
|
|
440
|
+
}
|
|
441
|
+
|
|
329
442
|
async function requestDeepseekMove(llm, payload) {
|
|
330
443
|
const board = normalizeBoard(payload);
|
|
331
444
|
const side = payload.side === 2 ? 2 : 1;
|
|
@@ -350,59 +463,85 @@ async function requestDeepseekMove(llm, payload) {
|
|
|
350
463
|
const y = Number(item[1]);
|
|
351
464
|
const color = item[2] === 2 ? 2 : 1;
|
|
352
465
|
if (Number.isInteger(x) && Number.isInteger(y) && x >= 0 && x < 19 && y >= 0 && y < 19) {
|
|
353
|
-
moves.push(
|
|
466
|
+
moves.push({ x, y, color });
|
|
354
467
|
}
|
|
355
468
|
}
|
|
356
469
|
}
|
|
357
|
-
const
|
|
358
|
-
const prompt =
|
|
359
|
-
'19 路五子棋。坐标 x,y 为 0-18,你执' + (side === 1 ? '黑' : '白') + '(1黑 2白)。',
|
|
360
|
-
'已落子(按顺序):' + history,
|
|
361
|
-
'只输出一个空位,格式 x,y,不要任何解释。',
|
|
362
|
-
].join('\n');
|
|
470
|
+
const promptPlan = buildMovePrompt(board, moves, side);
|
|
471
|
+
const prompt = promptPlan.prompt;
|
|
363
472
|
|
|
364
473
|
const options = {
|
|
365
474
|
provider,
|
|
366
475
|
model,
|
|
367
476
|
messages: [{ role: 'user', content: [{ type: 'text', text: prompt }] }],
|
|
368
|
-
temperature: 0
|
|
369
|
-
maxTokens:
|
|
370
|
-
signal: AbortSignal.timeout(
|
|
477
|
+
temperature: 0,
|
|
478
|
+
maxTokens: DEEPSEEK_MAX_TOKENS,
|
|
479
|
+
signal: AbortSignal.timeout(DEEPSEEK_TIMEOUT_MS),
|
|
371
480
|
};
|
|
372
481
|
|
|
373
482
|
let text = '';
|
|
483
|
+
let reasoning = '';
|
|
374
484
|
let failure = null;
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
485
|
+
try {
|
|
486
|
+
for await (const chunk of llm.stream(options)) {
|
|
487
|
+
if (chunk && chunk.type === 'text-delta' && typeof chunk.text === 'string') text += chunk.text;
|
|
488
|
+
if (chunk && chunk.type === 'reasoning-delta' && typeof chunk.text === 'string') reasoning += chunk.text;
|
|
489
|
+
if (chunk && chunk.type === 'finish' && chunk.kind === 'error') failure = chunk.failure || chunk;
|
|
490
|
+
}
|
|
491
|
+
} catch (error) {
|
|
492
|
+
if (error && typeof error === 'object') {
|
|
493
|
+
error.prompt = prompt;
|
|
494
|
+
error.text = text;
|
|
495
|
+
error.reasoning = reasoning;
|
|
496
|
+
}
|
|
497
|
+
throw error;
|
|
498
|
+
}
|
|
499
|
+
if (failure) {
|
|
500
|
+
const error = new Error(failure.message || failure.code || '模型调用失败');
|
|
501
|
+
error.prompt = prompt;
|
|
502
|
+
error.text = text;
|
|
503
|
+
error.reasoning = reasoning;
|
|
504
|
+
throw error;
|
|
505
|
+
}
|
|
506
|
+
const parsed = parseMoveText(text, board) || parseMoveText(reasoning, board);
|
|
507
|
+
let message = '';
|
|
508
|
+
if (!parsed) {
|
|
509
|
+
message = (!text.trim() && !reasoning.trim())
|
|
510
|
+
? '模型没有返回任何文本,可能把输出额度用在了内部推理上,或请求在输出前被中断。'
|
|
511
|
+
: '模型输出了内容,但没有给出一个可用的空位坐标。';
|
|
378
512
|
}
|
|
379
|
-
if (failure) throw new Error(failure.message || failure.code || '模型调用失败');
|
|
380
|
-
const parsed = parseMoveText(text, board);
|
|
381
|
-
if (!parsed) throw new Error('模型未返回合法落子');
|
|
382
513
|
return {
|
|
383
514
|
move: parsed,
|
|
384
515
|
provider,
|
|
385
516
|
model,
|
|
386
|
-
text: text.trim().slice(0,
|
|
517
|
+
text: text.trim().slice(0, 600),
|
|
518
|
+
reasoning: reasoning.trim().slice(0, 600),
|
|
387
519
|
prompt,
|
|
388
520
|
elapsedMs: Date.now() - startedAt,
|
|
521
|
+
message,
|
|
389
522
|
};
|
|
390
523
|
}
|
|
391
524
|
|
|
392
525
|
function parseMoveText(text, board) {
|
|
393
526
|
if (!text) return null;
|
|
527
|
+
const normalized = String(text)
|
|
528
|
+
.replace(/[,、]/g, ',')
|
|
529
|
+
.replace(/[:=]/g, ':')
|
|
530
|
+
.replace(/[()()【】\[\]]/g, ' ');
|
|
394
531
|
const candidates = [];
|
|
395
|
-
const objectMatch =
|
|
532
|
+
const objectMatch = normalized.match(/\{[^}]*\}/g) || [];
|
|
396
533
|
for (const raw of objectMatch) {
|
|
397
534
|
try {
|
|
398
535
|
const value = JSON.parse(raw);
|
|
399
536
|
if (value && Number.isInteger(value.x) && Number.isInteger(value.y)) candidates.push(value);
|
|
400
537
|
} catch { /* ignore */ }
|
|
401
538
|
}
|
|
402
|
-
const numberMatch =
|
|
539
|
+
const numberMatch = normalized.match(/["']?x["']?\s*[:=]\s*(\d+)[\s,]+["']?y["']?\s*[:=]\s*(\d+)/i);
|
|
403
540
|
if (numberMatch) candidates.push({ x: Number(numberMatch[1]), y: Number(numberMatch[2]) });
|
|
404
|
-
const pairMatch =
|
|
541
|
+
const pairMatch = normalized.match(/(?:^|[^\d])(\d{1,2})\s*,\s*(\d{1,2})(?:[^\d]|$)/);
|
|
405
542
|
if (pairMatch) candidates.push({ x: Number(pairMatch[1]), y: Number(pairMatch[2]) });
|
|
543
|
+
const chineseMatch = normalized.match(/第?\s*(\d{1,2})\s*行[^\d]{0,8}(\d{1,2})\s*列/);
|
|
544
|
+
if (chineseMatch) candidates.push({ x: Number(chineseMatch[2]) - 1, y: Number(chineseMatch[1]) - 1 });
|
|
406
545
|
for (const move of candidates) {
|
|
407
546
|
if (move.x >= 0 && move.x < 19 && move.y >= 0 && move.y < 19 && board[move.y * 19 + move.x] === 0) {
|
|
408
547
|
return { x: move.x, y: move.y };
|
package/lan-server.js
CHANGED
|
@@ -319,6 +319,10 @@ function roomState(room, youColor) {
|
|
|
319
319
|
winner: room.game.winner,
|
|
320
320
|
winLine: room.game.winLine,
|
|
321
321
|
round: room.round,
|
|
322
|
+
allowHint: room.allowHint !== false,
|
|
323
|
+
pending: room.pending
|
|
324
|
+
? { id: room.pending.id, action: room.pending.action, from: room.pending.from }
|
|
325
|
+
: null,
|
|
322
326
|
};
|
|
323
327
|
}
|
|
324
328
|
|
|
@@ -358,6 +362,9 @@ function handleDisconnect(client) {
|
|
|
358
362
|
room.players[client.color] = null;
|
|
359
363
|
if (room.names) room.names[client.color] = '';
|
|
360
364
|
}
|
|
365
|
+
if (room.pending && (room.pending.from === client.color || !room.players[1] || !room.players[2])) {
|
|
366
|
+
room.pending = null;
|
|
367
|
+
}
|
|
361
368
|
if (!room.players[1] && !room.players[2]) {
|
|
362
369
|
closeRoom(room);
|
|
363
370
|
return;
|
|
@@ -382,6 +389,8 @@ function handleJoin(client, role, code) {
|
|
|
382
389
|
game: freshGame(),
|
|
383
390
|
players: { 1: null, 2: null },
|
|
384
391
|
names: { 1: '', 2: '' },
|
|
392
|
+
allowHint: true,
|
|
393
|
+
pending: null,
|
|
385
394
|
status: 'waiting',
|
|
386
395
|
round: 1,
|
|
387
396
|
updatedAt: nowMs(),
|
|
@@ -459,19 +468,170 @@ function handleName(client, message) {
|
|
|
459
468
|
broadcast(room);
|
|
460
469
|
}
|
|
461
470
|
|
|
462
|
-
function
|
|
471
|
+
function resetGame(room, notice) {
|
|
472
|
+
room.game = freshGame();
|
|
473
|
+
room.round++;
|
|
474
|
+
room.status = room.players[2] ? 'playing' : 'waiting';
|
|
475
|
+
room.pending = null;
|
|
476
|
+
broadcast(room);
|
|
477
|
+
if (notice) {
|
|
478
|
+
for (const color of [BLACK, WHITE]) {
|
|
479
|
+
const player = room.players[color];
|
|
480
|
+
if (player) player.send({ type: 'notice', protocol: PROTOCOL, text: notice });
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
function handleRequest(client, message) {
|
|
463
486
|
const room = client.room;
|
|
464
487
|
if (!room) return sendError(client, 'not-in-room', '你还没有加入房间');
|
|
465
|
-
if (room.
|
|
466
|
-
return sendError(client, '
|
|
488
|
+
if (!room.players[1] || !room.players[2]) {
|
|
489
|
+
return sendError(client, 'no-opponent', '等待对手加入后再操作');
|
|
467
490
|
}
|
|
468
|
-
room.
|
|
469
|
-
|
|
491
|
+
if (room.pending) return sendError(client, 'request-pending', '已经有一个待确认的请求');
|
|
492
|
+
const action = message.action === 'undo' ? 'undo' : (message.action === 'reset' ? 'reset' : '');
|
|
493
|
+
if (!action) return sendError(client, 'bad-request', '未知的请求类型');
|
|
494
|
+
if (action === 'undo' && room.game.moves.length === 0) {
|
|
495
|
+
return sendError(client, 'nothing-to-undo', '还没有可以撤销的棋步');
|
|
496
|
+
}
|
|
497
|
+
const id = String(nowMs()) + '-' + String(Math.floor(Math.random() * 1000));
|
|
498
|
+
room.pending = { id, action, from: client.color, at: nowMs() };
|
|
499
|
+
room.updatedAt = nowMs();
|
|
500
|
+
broadcast(room);
|
|
501
|
+
const other = room.players[3 - client.color];
|
|
502
|
+
if (other) {
|
|
503
|
+
other.send({
|
|
504
|
+
type: 'notice',
|
|
505
|
+
protocol: PROTOCOL,
|
|
506
|
+
text: action === 'reset' ? '对手请求开始新一局,等待你确认。' : '对手请求悔棋,等待你确认。',
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
client.send({
|
|
510
|
+
type: 'notice',
|
|
511
|
+
protocol: PROTOCOL,
|
|
512
|
+
text: action === 'reset' ? '已请求新一局,等待对手确认。' : '已请求悔棋,等待对手确认。',
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
function handleRespond(client, message) {
|
|
517
|
+
const room = client.room;
|
|
518
|
+
if (!room) return sendError(client, 'not-in-room', '你还没有加入房间');
|
|
519
|
+
const pending = room.pending;
|
|
520
|
+
if (!pending) return sendError(client, 'no-request', '没有待确认的请求');
|
|
521
|
+
if (pending.from === client.color) return sendError(client, 'self-respond', '不能确认自己的请求');
|
|
522
|
+
if (message.id && String(message.id) !== pending.id) {
|
|
523
|
+
return sendError(client, 'stale-request', '这个请求已经失效');
|
|
524
|
+
}
|
|
525
|
+
const requester = room.players[pending.from];
|
|
526
|
+
const accepted = message.accept === true;
|
|
527
|
+
if (!accepted) {
|
|
528
|
+
room.pending = null;
|
|
529
|
+
broadcast(room);
|
|
530
|
+
if (requester) {
|
|
531
|
+
requester.send({
|
|
532
|
+
type: 'notice',
|
|
533
|
+
protocol: PROTOCOL,
|
|
534
|
+
text: pending.action === 'reset' ? '对手拒绝开始新一局。' : '对手拒绝了悔棋请求。',
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
client.send({ type: 'notice', protocol: PROTOCOL, text: '已拒绝对手的请求。' });
|
|
538
|
+
return;
|
|
539
|
+
}
|
|
540
|
+
if (pending.action === 'reset') {
|
|
541
|
+
resetGame(room, '对手已同意,新一局开始。');
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
const last = room.game.moves.pop();
|
|
545
|
+
if (last) {
|
|
546
|
+
room.game.board[idx(last.x, last.y)] = EMPTY;
|
|
547
|
+
room.game.turn = last.color;
|
|
548
|
+
}
|
|
549
|
+
room.game.winner = EMPTY;
|
|
550
|
+
room.game.winLine = null;
|
|
551
|
+
room.pending = null;
|
|
470
552
|
room.status = room.players[2] ? 'playing' : 'waiting';
|
|
471
553
|
broadcast(room);
|
|
472
554
|
for (const color of [BLACK, WHITE]) {
|
|
473
555
|
const player = room.players[color];
|
|
474
|
-
if (player) player.send({ type: 'notice', protocol: PROTOCOL, text: '
|
|
556
|
+
if (player) player.send({ type: 'notice', protocol: PROTOCOL, text: '对手已同意悔棋。' });
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
function handleLoad(client, message) {
|
|
561
|
+
const room = client.room;
|
|
562
|
+
if (!room) return sendError(client, 'not-in-room', '你还没有加入房间');
|
|
563
|
+
if (room.pending) return sendError(client, 'request-pending', '请先处理待确认的请求');
|
|
564
|
+
if (room.game.moves.length > 0 && room.game.winner === EMPTY) {
|
|
565
|
+
return sendError(client, 'game-running', '只能在开局前或一局结束后导入棋局');
|
|
566
|
+
}
|
|
567
|
+
if (!Array.isArray(message.moves) || message.moves.length === 0 || message.moves.length > SIZE * SIZE) {
|
|
568
|
+
return sendError(client, 'bad-import', '棋谱为空或过长');
|
|
569
|
+
}
|
|
570
|
+
const board = emptyBoard();
|
|
571
|
+
const moves = [];
|
|
572
|
+
let winner = EMPTY;
|
|
573
|
+
let winLine = null;
|
|
574
|
+
for (let i = 0; i < message.moves.length; i += 1) {
|
|
575
|
+
const item = message.moves[i] || {};
|
|
576
|
+
const x = Number(item.x);
|
|
577
|
+
const y = Number(item.y);
|
|
578
|
+
const color = Number(item.color);
|
|
579
|
+
if (!Number.isInteger(x) || !Number.isInteger(y) || x < 0 || y < 0 || x >= SIZE || y >= SIZE) {
|
|
580
|
+
return sendError(client, 'bad-import', '棋谱中有坐标超出棋盘');
|
|
581
|
+
}
|
|
582
|
+
if (color !== BLACK && color !== WHITE) {
|
|
583
|
+
return sendError(client, 'bad-import', '棋谱中的棋子颜色无效');
|
|
584
|
+
}
|
|
585
|
+
if (board[idx(x, y)] !== EMPTY) {
|
|
586
|
+
return sendError(client, 'bad-import', '棋谱中存在重复落子');
|
|
587
|
+
}
|
|
588
|
+
if (winner !== EMPTY) {
|
|
589
|
+
return sendError(client, 'bad-import', '棋谱在五连后仍有落子');
|
|
590
|
+
}
|
|
591
|
+
board[idx(x, y)] = color;
|
|
592
|
+
moves.push({ x, y, color });
|
|
593
|
+
const line = findWin(board, x, y, color);
|
|
594
|
+
if (line) {
|
|
595
|
+
winner = color;
|
|
596
|
+
winLine = line;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
if (winner === EMPTY && moves.length >= SIZE * SIZE) winner = 3;
|
|
600
|
+
room.game.board = board;
|
|
601
|
+
room.game.moves = moves;
|
|
602
|
+
room.game.winner = winner;
|
|
603
|
+
room.game.winLine = winLine;
|
|
604
|
+
const last = moves[moves.length - 1];
|
|
605
|
+
room.game.turn = winner === EMPTY ? 3 - last.color : last.color;
|
|
606
|
+
room.status = winner === EMPTY ? (room.players[2] ? 'playing' : 'waiting') : 'over';
|
|
607
|
+
room.pending = null;
|
|
608
|
+
broadcast(room);
|
|
609
|
+
for (const color of [BLACK, WHITE]) {
|
|
610
|
+
const player = room.players[color];
|
|
611
|
+
if (player) {
|
|
612
|
+
player.send({
|
|
613
|
+
type: 'notice',
|
|
614
|
+
protocol: PROTOCOL,
|
|
615
|
+
text: '已导入棋局(' + moves.length + ' 手),双方棋盘已同步。',
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
function handleHintSetting(client, message) {
|
|
622
|
+
const room = client.room;
|
|
623
|
+
if (!room) return sendError(client, 'not-in-room', '你还没有加入房间');
|
|
624
|
+
if (client.color !== BLACK) return sendError(client, 'host-only', '只有房主可以设置提示功能');
|
|
625
|
+
room.allowHint = message.allow !== false;
|
|
626
|
+
room.updatedAt = nowMs();
|
|
627
|
+
broadcast(room);
|
|
628
|
+
const other = room.players[WHITE];
|
|
629
|
+
if (other) {
|
|
630
|
+
other.send({
|
|
631
|
+
type: 'notice',
|
|
632
|
+
protocol: PROTOCOL,
|
|
633
|
+
text: room.allowHint ? '房主已允许使用提示。' : '房主已关闭提示功能。',
|
|
634
|
+
});
|
|
475
635
|
}
|
|
476
636
|
}
|
|
477
637
|
|
|
@@ -481,8 +641,16 @@ function handleClientMessage(client, message) {
|
|
|
481
641
|
return handleMove(client, message);
|
|
482
642
|
case 'name':
|
|
483
643
|
return handleName(client, message);
|
|
644
|
+
case 'request':
|
|
645
|
+
return handleRequest(client, message);
|
|
646
|
+
case 'respond':
|
|
647
|
+
return handleRespond(client, message);
|
|
648
|
+
case 'load':
|
|
649
|
+
return handleLoad(client, message);
|
|
650
|
+
case 'hint-setting':
|
|
651
|
+
return handleHintSetting(client, message);
|
|
484
652
|
case 'reset':
|
|
485
|
-
return
|
|
653
|
+
return handleRequest(client, { action: 'reset' });
|
|
486
654
|
case 'leave':
|
|
487
655
|
return client.close(1000, '已离开');
|
|
488
656
|
case 'ping':
|
package/package.json
CHANGED
package/preview.html
CHANGED
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
<script src="ui/engine/board.js"></script>
|
|
27
27
|
<script src="ui/engine/evaluate.js"></script>
|
|
28
28
|
<script src="ui/engine/ai.js"></script>
|
|
29
|
-
<script src="ui/gomoku-ui.js?v=0.2.0-
|
|
29
|
+
<script src="ui/gomoku-ui.js?v=0.2.0-32"></script>
|
|
30
30
|
<script>
|
|
31
31
|
(function () {
|
|
32
32
|
var params = new URLSearchParams(window.location.search);
|