fix: 修复攻略

This commit is contained in:
bietiaop 2024-07-27 12:49:16 +08:00
parent 2f83ea81a4
commit 075a6d4fd0
4 changed files with 101 additions and 90 deletions

View file

@ -1,3 +1,7 @@
# 1.2
* 添加伤害计算,支持目前为止所有 `驱动盘` 以及 `艾莲``朱鸢` 的伤害计算,`强攻武器` 支持 `B` 级以上。
* 修复攻略问题,若有对不上的问题,请“刷新攻略”。
# 1.1 # 1.1
* 支持自定义面板图,详情请查看 `README` * 支持自定义面板图,详情请查看 `README`

View file

@ -6,12 +6,12 @@ import common from '../../../lib/common/common.js';
import { ZZZPlugin } from '../lib/plugin.js'; import { ZZZPlugin } from '../lib/plugin.js';
import { rulePrefix } from '../lib/common.js'; import { rulePrefix } from '../lib/common.js';
import { atlasToName } from '../lib/convert/char.js'; import { atlasToName } from '../lib/convert/char.js';
import { imageResourcesPath, configPath } from '../lib/path.js'; import { imageResourcesPath } from '../lib/path.js';
import _ from 'lodash'; import _ from 'lodash';
import settings from '../lib/settings.js'; import settings from '../lib/settings.js';
import { downloadFile } from '../lib/download.js';
const ZZZ_GUIDES_PATH = path.join(imageResourcesPath, 'guides'); const ZZZ_GUIDES_PATH = path.join(imageResourcesPath, 'guides');
const ZZZ_GUIDES_CONFIG_PATH = path.join(configPath, 'guide.yaml');
export class Guide extends ZZZPlugin { export class Guide extends ZZZPlugin {
constructor() { constructor() {
@ -23,20 +23,20 @@ export class Guide extends ZZZPlugin {
rule: [ rule: [
{ {
reg: `^${rulePrefix}攻略(说明|帮助)$`, reg: `^${rulePrefix}攻略(说明|帮助)$`,
fnc: 'GuideHelp' fnc: 'GuideHelp',
}, },
{ {
reg: `^${rulePrefix}设置默认攻略(\\d+|all)$`, reg: `^${rulePrefix}设置默认攻略(\\d+|all)$`,
fnc: 'SetDefaultGuide' fnc: 'SetDefaultGuide',
}, },
{ {
reg: `^${rulePrefix}设置所有攻略显示个数(\\d+)$`, reg: `^${rulePrefix}设置所有攻略显示个数(\\d+)$`,
fnc: 'SetMaxForwardGuide' fnc: 'SetMaxForwardGuide',
}, },
{ {
reg: `${rulePrefix}(更新)?\\S+攻略(\\d+|all)?$`, reg: `${rulePrefix}(更新)?\\S+攻略(\\d+|all)?$`,
fnc: 'Guide', fnc: 'Guide',
} },
], ],
}); });
@ -51,7 +51,7 @@ export class Guide extends ZZZPlugin {
[2724610], [2724610],
[2722266], [2722266],
[2723586], [2723586],
[2716049] [2716049],
]; ];
this.source = [ this.source = [
'新艾利都快讯', '新艾利都快讯',
@ -60,41 +60,42 @@ export class Guide extends ZZZPlugin {
'猫冬', '猫冬',
'月光中心', '月光中心',
'苦雪的清心花凉糕Suki', '苦雪的清心花凉糕Suki',
'HoYo青枫' 'HoYo青枫',
]; ];
// 最大攻略数量 // 最大攻略数量
this.maxNum = this.source.length; this.maxNum = this.source.length;
// 最大显示攻略数量 // 最大显示攻略数量
this.maxForwardGuides = _.get(settings.getConfig('guide'), 'max_forward_guides', 4); this.maxForwardGuides = _.get(
settings.getConfig('guide'),
'max_forward_guides',
4
);
} }
async init() { // async init() {
for (let group = 1; group <= this.maxNum; group++) { // for (let group = 1; group <= this.maxNum; group++) {
let guideFolder = this.getGuideFolder(group); // let guideFolder = this.getGuideFolder(group);
if (!fs.existsSync(guideFolder)) { // if (!fs.existsSync(guideFolder)) {
fs.mkdirSync(guideFolder, { recursive: true }); // fs.mkdirSync(guideFolder, { recursive: true });
} // }
} // }
} // }
getGuideFolder(groupIndex) { getGuideFolder(groupIndex) {
let guideFolder = path.join(ZZZ_GUIDES_PATH, this.source[groupIndex - 1]); let guideFolder = path.join(ZZZ_GUIDES_PATH, this.source[groupIndex - 1]);
return guideFolder; return guideFolder;
} }
getGuidePath(groupIndex, characterName) { async getGuidePath(groupIndex, characterName, isUpdate = false) {
let filename = `role_guide_${characterName}.png`; const filename = `role_guide_${characterName}.png`;
let guidePath = path.join(this.getGuideFolder(groupIndex), filename); const guidePath = path.join(this.getGuideFolder(groupIndex), filename);
return guidePath; if (fs.existsSync(guidePath) && !isUpdate) return guidePath;
} return await this.getImg(characterName, groupIndex);
canGetImageFromFile(guidePath, isUpdate) {
return fs.existsSync(guidePath) && !isUpdate;
} }
async Guide() { async Guide() {
let reg = new RegExp(`${rulePrefix}(更新)?(\\S+)攻略(\\d+|all)?$`); let reg = new RegExp(`${rulePrefix}(更新|刷新)?(\\S+)攻略(\\d+|all)?$`);
let [ let [
, ,
, ,
@ -113,25 +114,19 @@ export class Guide extends ZZZPlugin {
await this.reply(`超过攻略数量(${this.maxNum}`); await this.reply(`超过攻略数量(${this.maxNum}`);
return; return;
} }
let name = atlasToName(atlas); const name = atlasToName(atlas);
if (!name) { if (!name) {
await this.reply('该角色不存在'); await this.reply('该角色不存在');
return; return;
} }
if (group === 0) { if (group === 0) {
// eslint-disable-next-line no-unused-vars const msg = [];
let msg = [];
// eslint-disable-next-line no-unused-vars
for (let i = 1; i <= this.maxNum; i++) { for (let i = 1; i <= this.maxNum; i++) {
let guidePath = this.getGuidePath(i, name); const guidePath = await this.getGuidePath(i, name, !!isUpdate);
if (this.canGetImageFromFile(guidePath, isUpdate)) { // msg.push(segment.image(`file://${guidePath}`));
msg.push(segment.image(`file://${guidePath}`)); msg.push(segment.image(guidePath));
continue;
}
if (i <= this.maxForwardGuides && (await this.getImg(name, i))) {
msg.push(segment.image(`file://${guidePath}`));
}
} }
if (msg.length) { if (msg.length) {
await this.reply(await common.makeForwardMsg(this.e, [msg])); await this.reply(await common.makeForwardMsg(this.e, [msg]));
@ -139,15 +134,9 @@ export class Guide extends ZZZPlugin {
return false; return false;
} }
let guidePath = this.getGuidePath(group, name); const guidePath = await this.getGuidePath(group, name, !!isUpdate);
if (this.canGetImageFromFile(guidePath, isUpdate)) { await this.e.reply(segment.image(guidePath));
await this.e.reply(segment.image(`file://${guidePath}`)); return false;
return;
}
if (await this.getImg(name, group)) {
await this.e.reply(segment.image(`file://${guidePath}`));
}
} }
/** 下载攻略图 */ /** 下载攻略图 */
@ -166,12 +155,15 @@ export class Guide extends ZZZPlugin {
} }
// 搜索时过滤特殊符号譬如「11号」 // 搜索时过滤特殊符号譬如「11号」
const filtered_name = name.replace(/[^\w\s]|_/g, '') const filtered_name = name.replace(/[^a-zA-Z0-9\u4e00-\u9fa5]/g, '');
let posts = lodash.flatten(lodash.map(mysRes, item => item.data.posts)); let posts = lodash.flatten(lodash.map(mysRes, item => item.data.posts));
let url, created_at, updated_at; let url, created_at, updated_at;
for (let val of posts) { for (const val of posts) {
if (val.post.subject.replace(/【[^】]*本[^】]*】/g, '').includes(filtered_name)) { if (
val.post.subject
.replace(/【[^】]*本[^】]*】/g, '')
.includes(filtered_name)
) {
let max = 0; let max = 0;
val.image_list.forEach((v, i) => { val.image_list.forEach((v, i) => {
if ( if (
@ -193,17 +185,19 @@ export class Guide extends ZZZPlugin {
); );
return false; return false;
} }
console.log(`${this.e.logFnc} 下载${name}攻略图 - ${this.source[group - 1]}`); logger.debug(
`${this.e.logFnc} 下载${name}攻略图 - ${this.source[group - 1]}`
);
const download = await fetch(url); const filename = `role_guide_${name}.png`;
const arrayBuffer = await download.arrayBuffer(); const guidePath = path.join(this.getGuideFolder(group), filename);
const buffer = Buffer.from(arrayBuffer); const download = await downloadFile(url, guidePath);
let guidePath = this.getGuidePath(group, name);
fs.writeFileSync(guidePath, buffer);
console.log(`${this.e.logFnc} 下载${name}攻略成功 - ${this.source[group - 1]}`); logger.debug(
`${this.e.logFnc} 下载${name}攻略成功 - ${this.source[group - 1]}`
);
return true; return download;
} }
/** 获取数据 */ /** 获取数据 */
@ -216,7 +210,7 @@ export class Guide extends ZZZPlugin {
} }
/** %攻略帮助 */ /** %攻略帮助 */
async GuideHelp () { async GuideHelp() {
let reply_msg = [ let reply_msg = [
'绝区零角色攻略帮助:', '绝区零角色攻略帮助:',
'%艾莲攻略+攻略id', '%艾莲攻略+攻略id',
@ -225,46 +219,40 @@ export class Guide extends ZZZPlugin {
'%设置所有攻略显示个数+攻略id', '%设置所有攻略显示个数+攻略id',
'示例: %艾莲攻略2', '示例: %艾莲攻略2',
'', '',
'攻略来源:' '攻略来源:',
].concat(this.source.map((element, index) => `${index + 1}: ${element}`)) ].concat(this.source.map((element, index) => `${index + 1}: ${element}`));
await this.e.reply(reply_msg.join('\n')) await this.e.reply(reply_msg.join('\n'));
}
setConfig(key, value) {
let config = fs.readFileSync(ZZZ_GUIDES_CONFIG_PATH, 'utf8')
let newREG = new RegExp(`^${key}: (.*)`, 'gm')
config = config.replace(newREG, `${key}: ${value}`)
fs.writeFileSync(ZZZ_GUIDES_CONFIG_PATH, config, 'utf8')
} }
/** %设置默认攻略1 */ /** %设置默认攻略1 */
async SetDefaultGuide () { async SetDefaultGuide() {
let match = /设置默认攻略(\d+|all)$/g.exec(this.e.msg) let match = /设置默认攻略(\d+|all)$/g.exec(this.e.msg);
let guide_id = match[1] let guide_id = match[1];
if (guide_id == 'all') { if (guide_id == 'all') {
guide_id = 0 guide_id = 0;
} }
guide_id = Number(guide_id);
if (guide_id > this.maxNum) { if (guide_id > this.maxNum) {
let reply_msg = [ let reply_msg = [
'绝区零默认攻略设置方式为:', '绝区零默认攻略设置方式为:',
'%设置默认攻略[0123...]', '%设置默认攻略[0123...]',
`请增加数字0-${this.maxNum}其中一个,或者增加 all 以显示所有攻略`, `请增加数字0-${this.maxNum}其中一个,或者增加 all 以显示所有攻略`,
'攻略来源请输入 %攻略帮助 查看' '攻略来源请输入 %攻略帮助 查看',
] ];
await this.e.reply(reply_msg.join('\n')) await this.e.reply(reply_msg.join('\n'));
return return;
} }
this.setConfig('default_guide', guide_id) settings.setSingleConfig('guide', 'default_guide', guide_id);
let source_name = guide_id == 0 ? 'all' : this.source[guide_id - 1] let source_name = guide_id == 0 ? 'all' : this.source[guide_id - 1];
await this.e.reply(`绝区零默认攻略已设置为: ${guide_id} (${source_name})`) await this.e.reply(`绝区零默认攻略已设置为: ${guide_id} (${source_name})`);
} }
/** %设置所有攻略显示个数3 */ /** %设置所有攻略显示个数3 */
async SetMaxForwardGuide () { async SetMaxForwardGuide() {
let match = /设置所有攻略显示个数(\d+)$/g.exec(this.e.msg) let match = /设置所有攻略显示个数(\d+)$/g.exec(this.e.msg);
let max_forward_guide = Number(match[1]) let max_forward_guide = Number(match[1]);
this.setConfig('max_forward_guides', max_forward_guide) this.setSingleConfig('max_forward_guides', max_forward_guide);
await this.e.reply(`绝区零所有攻略显示个数已设置为: ${max_forward_guide}`) await this.e.reply(`绝区零所有攻略显示个数已设置为: ${max_forward_guide}`);
} }
} }

View file

@ -24,7 +24,7 @@ const ZZZ_SUIT_PATH = path.join(imageResourcesPath, 'suit');
* @param {string} savePath 保存路径 * @param {string} savePath 保存路径
* @returns * @returns
*/ */
const downloadFile = async (url, savePath) => { export const downloadFile = async (url, savePath) => {
const _download = async (url, savePath, retry = 0) => { const _download = async (url, savePath, retry = 0) => {
// 重试次数超过 5 次则返回 null // 重试次数超过 5 次则返回 null
if (retry > 5) { if (retry > 5) {

View file

@ -153,12 +153,31 @@ class Setting {
/** /**
* 设置对应模块用户配置 * 设置对应模块用户配置
* @param {'atlas'|'config'|'gacha'|'panel'} app * @param {'atlas'|'config'|'gacha'|'panel'|'guide'} app
* @param {object} obj * @param {object} obj
* @returns * @returns
*/ */
setConfig(app, obj) { setConfig(app, obj) {
return this.setYaml(app, 'config', { ...this.getdefSet(app), ...obj }); // 先获取默认配置
const defSet = this.getdefSet(app);
// 再获取用户配置
const config = this.getConfig(app);
return this.setYaml(app, 'config', { ...defSet, ...config, ...obj });
}
/**
* 设置对应模块用户配置
* @param {'atlas'|'config'|'gacha'|'panel'|'guide'} app
* @param {string} key
* @param {string} value
* @returns {boolean}
*/
setSingleConfig(app, key, value) {
// 先获取默认配置
const defSet = this.getdefSet(app);
const config = this.getConfig(app);
config[key] = value;
return this.setYaml(app, 'config', { ...defSet, ...config });
} }
// 将对象写入YAML文件 // 将对象写入YAML文件