多机器人(src/bot/botPool.js、src/config/botsConfig.js) - .env 用 MC_BOTS 一次配置多个账号(JSON 数组或 用户名:密码 逗号分隔),未配置时回退单机器人 - 每个机器人独立连接 / 登录 / 扩地状态机,共享一份领土数据服务与运行日志 - 密码只从 .env 读取;data/bots.json 只存启用状态、备注名、自动开始与各自的配置覆盖 - 每个机器人可覆盖全局配置(策略 / 目标邦国 / 目标数量等),未覆盖字段回退全局默认 开拓日志(src/core/expansionLog.js) - 逐条记录开拓玩家、邦国、区块、方块坐标、时间与判定来源,追加写入 data/expansion-log.jsonl - 支持按机器人 / 邦国 / 时间筛选、统计、导出带 BOM 的 CSV,超过 16MB 自动轮转 游戏公屏完整日志与聊天框(src/core/chatLog.js) - 每个机器人的全部公屏 / 系统 / 动作栏消息按机器人分文件留档,自身发出的聊天与指令同样留档 - 控制台新增公屏聊天页签与消息发送框 登录验证门禁 - 游戏侧:未完成登录验证时拒绝发送指令 / 聊天,isReady() 也不放行扩地任务; 新增 server.loginConfirmTimeoutMs 兜底,避免服务器不回提示导致永远卡在未登录 - 控制台侧:新增 WEB_PASSWORD 与 /api/login、/api/session,页面登录框验证通过才允许操作 大地图(src/core/bigMapService.js) - 爬取服务器地图站点(squaremap)的瓦片图片与领土标记,一张图上看全部机器人坐标 - 坐标系对源站实测标定并用 27 个王城交叉验证;瓦片磁盘缓存,层级 0 启动预爬取、更高层级按需穿透 - 领土按 markers.json 原始多边形绘制(预编译 Path2D,even-odd 保留孔洞),叠加王城与机器人标签 配套改造 - TerritoryModel 新增 flyingChunksOf / ownChunksOf,邦国归属改为按名称显式传入, 不再依赖全局 selfStateName(多机器人会互相覆盖) - Web 接口以 /api/bots/:id/... 为主,原单机器人接口保留为默认机器人的等价别名 - 新增 scripts/check-web-ids.js 校验前端元素引用一致性 测试从 154 增至 242,全部通过。
238 行
8.8 KiB
JavaScript
238 行
8.8 KiB
JavaScript
'use strict';
|
|
|
|
const test = require('node:test');
|
|
const assert = require('node:assert');
|
|
const fs = require('fs');
|
|
const os = require('os');
|
|
const path = require('path');
|
|
|
|
const { BotPool } = require('../src/bot/botPool');
|
|
const { BotStore } = require('../src/config/botStore');
|
|
const { defaultConfig, normalizeConfig } = require('../src/config/expansionConfig');
|
|
const { Logger } = require('../src/util/logger');
|
|
const { TerritoryModel } = require('../src/core/territoryModel');
|
|
const chunkPos = require('../src/core/chunkPos');
|
|
|
|
/** 造一个只提供 getModel 的数据服务桩 */
|
|
function fakeDataService() {
|
|
const map = new Map([[
|
|
'A',
|
|
{ name: 'A', capital: { x: 0, z: 0 }, chunks: new Set([chunkPos.key(0, 0)]), color: 0x75bd20 }
|
|
]]);
|
|
const model = new TerritoryModel(map);
|
|
model.setSelfStateName('A');
|
|
return {
|
|
model,
|
|
getModel: () => model,
|
|
setSelectedStateName: (name) => model.setSelfStateName(name),
|
|
addSessionClaim: () => {},
|
|
refreshAsync: () => {},
|
|
detectStateByPlayer: async () => null,
|
|
snapshot: () => ({ stateCount: 1 })
|
|
};
|
|
}
|
|
|
|
function connection(username) {
|
|
return {
|
|
id: username,
|
|
username,
|
|
password: 'pw',
|
|
auth: 'offline',
|
|
host: 'mc.example.com',
|
|
port: 25565,
|
|
version: '1.21.4',
|
|
label: username
|
|
};
|
|
}
|
|
|
|
/** 建一个含 2 个机器人、使用临时 bots.json 的池 */
|
|
function setup(options) {
|
|
const opts = options || {};
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'autostore-pool-'));
|
|
const storePath = path.join(dir, 'bots.json');
|
|
if (opts.settings) fs.writeFileSync(storePath, JSON.stringify({ bots: opts.settings }), 'utf8');
|
|
const config = normalizeConfig(Object.assign(defaultConfig(), opts.config || {}));
|
|
const pool = new BotPool({
|
|
connections: opts.connections || [connection('BotA'), connection('BotB')],
|
|
config,
|
|
logger: new Logger({ echo: false }),
|
|
dataService: fakeDataService(),
|
|
botStore: new BotStore(storePath)
|
|
});
|
|
return { pool, storePath, config, dir };
|
|
}
|
|
|
|
test('BotPool 为每个凭据建立一个机器人条目', () => {
|
|
const { pool } = setup();
|
|
assert.strictEqual(pool.size, 2);
|
|
assert.deepStrictEqual(pool.ids(), ['BotA', 'BotB']);
|
|
assert.strictEqual(pool.defaultId, 'BotA');
|
|
assert.strictEqual(pool.get('BotA').manager.connection.username, 'BotA');
|
|
assert.strictEqual(pool.get().id, 'BotA', '不传 id 时返回默认机器人');
|
|
});
|
|
|
|
test('require 找不到机器人时抛出 404', () => {
|
|
const { pool } = setup();
|
|
assert.throws(() => pool.require('不存在'), (err) => err.statusCode === 404);
|
|
});
|
|
|
|
test('getConfigFor 未设置覆盖时等于全局配置', () => {
|
|
const { pool } = setup({ config: { strategy: 4, targetCount: 20 } });
|
|
const config = pool.getConfigFor('BotA');
|
|
assert.strictEqual(config.strategy, 4);
|
|
assert.strictEqual(config.targetCount, 20);
|
|
});
|
|
|
|
test('每个机器人可以有独立的配置覆盖,互不影响', () => {
|
|
const { pool } = setup({ config: { strategy: 1, targetCount: 10 } });
|
|
pool.setBotSetting('BotB', { config: { strategy: 2, targetCount: 3, stateName: 'B国' } });
|
|
|
|
const a = pool.getConfigFor('BotA');
|
|
const b = pool.getConfigFor('BotB');
|
|
assert.strictEqual(a.strategy, 1);
|
|
assert.strictEqual(a.targetCount, 10);
|
|
assert.strictEqual(b.strategy, 2);
|
|
assert.strictEqual(b.targetCount, 3);
|
|
assert.strictEqual(b.stateName, 'B国');
|
|
// 未覆盖的字段回退到全局
|
|
assert.strictEqual(b.waitTimeoutSeconds, a.waitTimeoutSeconds);
|
|
});
|
|
|
|
test('setBotSetting 持久化到 bots.json,重建池后仍生效', () => {
|
|
const { pool, storePath } = setup();
|
|
pool.setBotSetting('BotB', { enabled: false, autoStart: true, label: '二号机', config: { targetCount: 7 } });
|
|
|
|
const saved = JSON.parse(fs.readFileSync(storePath, 'utf8'));
|
|
assert.strictEqual(saved.bots.BotB.enabled, false);
|
|
assert.strictEqual(saved.bots.BotB.autoStart, true);
|
|
assert.strictEqual(saved.bots.BotB.label, '二号机');
|
|
|
|
const rebuilt = new BotPool({
|
|
connections: [connection('BotA'), connection('BotB')],
|
|
config: defaultConfig(),
|
|
dataService: fakeDataService(),
|
|
botStore: new BotStore(storePath)
|
|
});
|
|
assert.strictEqual(rebuilt.get('BotB').manager.label, '二号机');
|
|
assert.strictEqual(rebuilt.getConfigFor('BotB').targetCount, 7);
|
|
assert.strictEqual(rebuilt.settingSnapshot('BotB').enabled, false);
|
|
});
|
|
|
|
test('设置里绝不写入密码(密码只来自环境变量)', () => {
|
|
const { pool, storePath } = setup();
|
|
pool.setBotSetting('BotA', { label: '主号' });
|
|
const text = fs.readFileSync(storePath, 'utf8');
|
|
assert.ok(!text.includes('pw'), 'bots.json 中不得出现密码');
|
|
assert.ok(!/password/i.test(text), 'bots.json 中不得出现 password 字段');
|
|
});
|
|
|
|
test('snapshot 汇总各机器人的连接状态与任务状态', () => {
|
|
const { pool } = setup();
|
|
const snap = pool.snapshot();
|
|
assert.strictEqual(snap.length, 2);
|
|
assert.strictEqual(snap[0].id, 'BotA');
|
|
assert.strictEqual(snap[0].state, 'idle');
|
|
assert.strictEqual(snap[0].enabled, true);
|
|
assert.strictEqual(snap[0].task.state, 'IDLE');
|
|
assert.strictEqual(snap[0].task.botId, 'BotA');
|
|
});
|
|
|
|
test('positions 输出各机器人坐标供大地图打点', () => {
|
|
const { pool } = setup();
|
|
const positions = pool.positions();
|
|
assert.strictEqual(positions.length, 2);
|
|
assert.strictEqual(positions[0].id, 'BotA');
|
|
assert.strictEqual(positions[0].position, null, '未进入世界时没有坐标');
|
|
assert.strictEqual(positions[0].stateName, '');
|
|
});
|
|
|
|
test('未完成登录验证时拒绝发送指令与聊天', () => {
|
|
const { pool } = setup();
|
|
const command = pool.sendCommand('BotA', 'u claimmode');
|
|
assert.strictEqual(command.ok, false);
|
|
assert.ok(command.message.includes('未完成登录验证'));
|
|
|
|
const chat = pool.sendChat('BotA', '大家好');
|
|
assert.strictEqual(chat.ok, false);
|
|
assert.ok(chat.message.includes('未完成登录验证'));
|
|
});
|
|
|
|
test('完成登录验证后允许发送指令与聊天', () => {
|
|
const { pool } = setup();
|
|
const entry = pool.get('BotA');
|
|
const sent = [];
|
|
entry.manager.bot = { chat: (text) => sent.push(text) };
|
|
entry.manager.state = 'online';
|
|
entry.manager.loggedIn = true;
|
|
|
|
assert.strictEqual(entry.manager.canOperate(), true);
|
|
assert.strictEqual(pool.sendChat('BotA', '大家好').ok, true);
|
|
assert.strictEqual(pool.sendCommand('BotA', '/spawn').ok, true);
|
|
|
|
// 指令队列有节流,这里直接驱动写入验证内容
|
|
entry.manager._commandQueue.length = 0;
|
|
entry.manager._chatQueue.length = 0;
|
|
entry.manager.sendChat('直接发送', { internal: true });
|
|
entry.manager._chatQueue.length = 0;
|
|
entry.manager._writeChat('直接发送');
|
|
assert.deepStrictEqual(sent, ['直接发送']);
|
|
});
|
|
|
|
test('startTask 在机器人未就绪时返回失败且不抛异常', async () => {
|
|
const { pool } = setup();
|
|
const result = await pool.startTask('BotA');
|
|
assert.strictEqual(result.ok, false);
|
|
assert.ok(result.message.includes('尚未就绪'));
|
|
});
|
|
|
|
test('startTask 带配置覆盖时写入该机器人的专属配置', async () => {
|
|
const { pool } = setup({ config: { strategy: 1 } });
|
|
await pool.startTask('BotB', { strategy: 3, targetCount: 5 });
|
|
const config = pool.getConfigFor('BotB');
|
|
assert.strictEqual(config.strategy, 3);
|
|
assert.strictEqual(config.targetCount, 5);
|
|
// 另一个机器人不受影响
|
|
assert.strictEqual(pool.getConfigFor('BotA').strategy, 1);
|
|
});
|
|
|
|
test('applyGlobalConfig 下发到全部机器人并保留各自的覆盖', () => {
|
|
const { pool } = setup({ config: { strategy: 1, targetCount: 10 } });
|
|
pool.setBotSetting('BotB', { config: { targetCount: 3 } });
|
|
|
|
const next = normalizeConfig({ strategy: 4, targetCount: 99 }, pool.config);
|
|
pool.applyGlobalConfig(next);
|
|
|
|
assert.strictEqual(pool.get('BotA').manager.config.strategy, 4);
|
|
assert.strictEqual(pool.getConfigFor('BotA').targetCount, 99);
|
|
assert.strictEqual(pool.getConfigFor('BotB').targetCount, 3, '机器人专属覆盖优先于全局');
|
|
});
|
|
|
|
test('stop 会停止全部机器人的任务并断开连接', () => {
|
|
const { pool } = setup();
|
|
let disconnected = 0;
|
|
for (const entry of pool.entries.values()) {
|
|
entry.manager.disconnect = () => { disconnected++; };
|
|
}
|
|
pool.stop();
|
|
assert.strictEqual(disconnected, 2);
|
|
});
|
|
|
|
test('tick 驱动全部机器人且单个异常不影响其它', () => {
|
|
const { pool } = setup();
|
|
const ticks = [];
|
|
pool.get('BotA').controller.tick = () => { throw new Error('boom'); };
|
|
pool.get('BotB').controller.tick = () => { ticks.push('B'); };
|
|
assert.doesNotThrow(() => pool.tick());
|
|
assert.deepStrictEqual(ticks, ['B']);
|
|
});
|
|
|
|
test('未启用(enabled=false)的机器人不参与连接', () => {
|
|
const { pool } = setup({ settings: { BotB: { enabled: false } } });
|
|
const connected = [];
|
|
for (const entry of pool.entries.values()) {
|
|
entry.manager.connect = () => { connected.push(entry.id); };
|
|
}
|
|
pool.start();
|
|
assert.deepStrictEqual(connected, ['BotA']);
|
|
});
|