Files
rong cb28bbb49e feat: 围棋在线对战(Rust 服务端 + 零构建前端)
- 规则引擎:提子、禁自杀、全局同形(超级劫)、中国规则数子(白贴 7.5 目)、停一手终局
- 服务端:axum + tokio,房间/观战/包干计时/断线重连/语音中继,前端资源内嵌单文件
- 前端:Canvas 木纹棋盘(9/13/19 路、星位、四边坐标、翻转视角)、WebAudio 合成落子声、
  本机双人模式、终局数子面板
- 测试:16 组规则断言 + 5 组 e2e(提子/劫/数子/观战/语音/静态资源)
- 构建缓存遵循 dev-cache-layout:target/ 留项目内并忽略,前端零构建不产生包缓存
2026-10-23 10:12:55 +08:00

209 lines
6.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { BLACK, COLS, WHITE, starPoints } from '../shared/rules.js';
/**
* 棋盘绘制:木板 + 网格 + 星位 + 棋子。
* 坐标换算都收在这里,main.js 只管「点到第几个交叉点」。
*/
/** 让棋盘在给定宽度内铺满,同时留出坐标标签的位置 */
export function computeView(cssWidth, size) {
const cell = Math.max(11, Math.floor(cssWidth / (size + 0.6)));
const margin = Math.round(cell * 0.8);
const width = cell * (size - 1) + margin * 2;
return { cell, margin, size, width, height: width, flipped: false };
}
/** 棋盘交叉点 -> 画布坐标(含黑白翻转) */
export function toScreen(v, x, y) {
const bx = v.flipped ? v.size - 1 - x : x;
const by = v.flipped ? v.size - 1 - y : y;
return [v.margin + bx * v.cell, v.margin + by * v.cell];
}
/** 画布坐标 -> 棋盘上的浮点坐标(取整交给 nearestPoint) */
export function toBoard(v, px, py) {
const bx = (px - v.margin) / v.cell;
const by = (py - v.margin) / v.cell;
const x = v.flipped ? v.size - 1 - bx : bx;
const y = v.flipped ? v.size - 1 - by : by;
return [x, y];
}
export function draw(ctx, v, st) {
const { cell } = v;
ctx.clearRect(0, 0, v.width, v.height);
drawBoardBase(ctx, v);
// 棋子
for (let i = 0; i < st.board.length; i++) {
const stone = st.board[i];
if (!stone) continue;
const [sx, sy] = toScreen(v, i % v.size, Math.floor(i / v.size));
drawStone(ctx, v, sx, sy, stone);
}
// 上一手
if (st.last != null && st.last >= 0) {
const [sx, sy] = toScreen(v, st.last % v.size, Math.floor(st.last / v.size));
ctx.beginPath();
ctx.arc(sx, sy, Math.max(2, cell * 0.13), 0, Math.PI * 2);
ctx.fillStyle = 'rgba(214, 62, 48, 0.95)';
ctx.fill();
ctx.beginPath();
ctx.arc(sx, sy, Math.max(3.5, cell * 0.2), 0, Math.PI * 2);
ctx.strokeStyle = 'rgba(214, 62, 48, 0.55)';
ctx.lineWidth = Math.max(1, cell * 0.045);
ctx.stroke();
}
// 落点预览
if (st.hover != null && st.hover >= 0 && st.board[st.hover] === 0 && st.ghost) {
const [sx, sy] = toScreen(v, st.hover % v.size, Math.floor(st.hover / v.size));
ctx.save();
ctx.globalAlpha = 0.45;
drawStone(ctx, v, sx, sy, st.ghost === 'b' ? BLACK : WHITE);
ctx.restore();
ctx.beginPath();
ctx.arc(sx, sy, cell * 0.47, 0, Math.PI * 2);
ctx.strokeStyle = st.ghost === 'b' ? 'rgba(30,30,30,0.5)' : 'rgba(255,255,255,0.65)';
ctx.lineWidth = Math.max(1, cell * 0.05);
ctx.stroke();
}
}
/* ------------------------------- 木板与网格 ------------------------------- */
function drawBoardBase(ctx, v) {
const { cell, margin, size } = v;
const x0 = margin;
const y0 = margin;
const x1 = margin + (size - 1) * cell;
const y1 = margin + (size - 1) * cell;
// 木色底
const g = ctx.createLinearGradient(0, 0, v.width, v.height);
g.addColorStop(0, '#e9c88f');
g.addColorStop(0.45, '#dfb877');
g.addColorStop(1, '#cfa361');
ctx.fillStyle = g;
ctx.fillRect(0, 0, v.width, v.height);
// 木纹
ctx.save();
ctx.globalAlpha = 0.07;
ctx.strokeStyle = '#7a5628';
for (let i = 0; i < 22; i++) {
const yy = (i / 22) * v.height + Math.sin(i * 1.9) * 5;
ctx.beginPath();
ctx.moveTo(0, yy);
ctx.bezierCurveTo(v.width * 0.3, yy + 8, v.width * 0.7, yy - 8, v.width, yy + 2);
ctx.lineWidth = 1 + (i % 3) * 0.5;
ctx.stroke();
}
ctx.restore();
// 外框(比网格线粗一点)
const pad = cell * 0.34;
ctx.strokeStyle = 'rgba(88,58,22,0.8)';
ctx.lineWidth = Math.max(1.5, cell * 0.05);
ctx.strokeRect(x0 - pad, y0 - pad, x1 - x0 + pad * 2, y1 - y0 + pad * 2);
// 网格
ctx.strokeStyle = 'rgba(74,48,16,0.82)';
ctx.lineWidth = Math.max(1, cell * 0.035);
ctx.beginPath();
for (let i = 0; i < size; i++) {
const p = margin + i * cell;
ctx.moveTo(x0, p);
ctx.lineTo(x1, p);
ctx.moveTo(p, y0);
ctx.lineTo(p, y1);
}
ctx.stroke();
// 星位
const star = starPoints(size);
if (star.length) {
ctx.fillStyle = 'rgba(62,40,14,0.9)';
const r = Math.max(1.6, cell * 0.075);
for (const i of star) {
const [sx, sy] = toScreen(v, i % size, Math.floor(i / size));
ctx.beginPath();
ctx.arc(sx, sy, r, 0, Math.PI * 2);
ctx.fill();
}
}
// 坐标(太小就不画,免得糊成一团)
// 标签按「画布边缘」定位:翻转视角时棋盘坐标会跑到画布外
const fs = Math.round(cell * 0.36);
if (cell >= 13 && fs >= 7) {
const band = Math.round(cell * 0.24); // 画布边缘到标签中心
const right = v.width - band;
const bottom = v.height - band;
ctx.save();
ctx.fillStyle = 'rgba(84,56,22,0.72)';
ctx.font = `${fs}px -apple-system, system-ui, sans-serif`;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
for (let i = 0; i < size; i++) {
const [sx] = toScreen(v, i, 0);
ctx.fillText(COLS[i], sx, band);
ctx.fillText(COLS[i], sx, bottom);
const [, sy] = toScreen(v, 0, i);
const row = String(size - i);
ctx.fillText(row, band, sy);
ctx.fillText(row, right, sy);
}
ctx.restore();
}
}
/* --------------------------------- 棋子 --------------------------------- */
function drawStone(ctx, v, sx, sy, stone) {
const r = v.cell * 0.47;
const isBlack = stone === BLACK;
// 投影
ctx.save();
ctx.shadowColor = 'rgba(50,30,8,0.42)';
ctx.shadowBlur = v.cell * 0.14;
ctx.shadowOffsetY = v.cell * 0.06;
ctx.beginPath();
ctx.arc(sx, sy, r, 0, Math.PI * 2);
ctx.fillStyle = isBlack ? '#141414' : '#f7f3e9';
ctx.fill();
ctx.restore();
// 本体(一点高光,做出蛤碁石 / 那智黑的感觉)
const grd = ctx.createRadialGradient(
sx - r * 0.34,
sy - r * 0.38,
r * 0.12,
sx,
sy,
r * 1.02,
);
if (isBlack) {
grd.addColorStop(0, '#6b6b6b');
grd.addColorStop(0.42, '#2b2b2b');
grd.addColorStop(1, '#08080a');
} else {
grd.addColorStop(0, '#ffffff');
grd.addColorStop(0.58, '#f5f1e6');
grd.addColorStop(1, '#cdc4b2');
}
ctx.beginPath();
ctx.arc(sx, sy, r, 0, Math.PI * 2);
ctx.fillStyle = grd;
ctx.fill();
// 边缘
ctx.beginPath();
ctx.arc(sx, sy, r, 0, Math.PI * 2);
ctx.strokeStyle = isBlack ? 'rgba(0,0,0,0.7)' : 'rgba(120,105,80,0.55)';
ctx.lineWidth = Math.max(0.6, v.cell * 0.025);
ctx.stroke();
}