miao-plugin/components/models/Character.js

172 lines
4.5 KiB
JavaScript
Raw Normal View History

2022-03-24 13:14:22 +00:00
import Base from "./Base.js";
2022-03-26 08:21:44 +00:00
import { roleId, abbr } from "../../../../config/genshin/roleId.js";
import lodash from "lodash";
import fs from "fs";
2022-03-26 08:21:44 +00:00
import Data from "../Data.js";
import request from "request";
2022-03-24 13:14:22 +00:00
2022-03-26 08:21:44 +00:00
let characterMap = {};
2022-03-26 08:21:44 +00:00
// 读取配置
let characterMeta = Data.readJSON("./plugins/miao-plugin/components/meta", "characters.json")// JSON.parse(fs.readFileSync(__dirname + "../meta/characters.json", "utf8"));
characterMeta = lodash.keyBy(characterMeta, (d) => d.Name);
const elemName = {
pyro: "火",
hydro: "水",
dendro: "草",
electro: "雷",
anemo: "风",
cryo: "冰",
geo: "岩"
};
2022-03-26 08:21:44 +00:00
class Character extends Base {
constructor(name) {
super();
this._name = name;
2022-03-26 08:21:44 +00:00
this.sName = this.name;
this.id = YunzaiApps.mysInfo['roleIdToName'](name);
lodash.extend(this, getMetaData(name));
}
get name() {
if (this.roleId) {
if (this.roleId * 1 === 10000005) {
this._name = "空";
} else if (this.roleId * 1 === 10000007) {
this._name = "荧";
}
}
return this._name;
}
set name(name) {
this._name = name;
}
2022-03-26 08:21:44 +00:00
async checkImgCache(resDir) {
// 处理img信息
let chcheDir = resDir + "/cache/";
}
2022-03-26 08:21:44 +00:00
}
let cacheImgFile = async function (url, cacheDir) {
let ret = /^https:\/\/(.*)$/.exec(url);
if (ret && ret[1] && /\.(png|jpg|gif|jpeg|webp)$/.test(ret[1])) {
let fileName = ret[1];
Data.createDir(cacheDir, fileName);
request(imgUrl).pipe(fs.createWriteStream(cacheDir + fileName));
}
2022-03-26 08:21:44 +00:00
}
2022-03-24 13:14:22 +00:00
2022-03-26 08:21:44 +00:00
// 获取指定角色的Meta信息
let getMetaData = function (name) {
if (!characterMeta[name]) {
return {};
}
2022-03-26 08:21:44 +00:00
const metaCfg = { lowerFirstKey: true },
meta = characterMeta[name];
2022-03-26 08:21:44 +00:00
// 处理基础信息
let ret = Data.getData(meta, "Name,Title,desc:Description,astro:AstrolabeName", metaCfg);
2022-03-26 08:21:44 +00:00
// 处理图像信息
ret.img = Data.getData(meta, "Weapon,Element,City,Profile,GachaCard,GachaSplash,Source", metaCfg);
// 处理元素
let elemRet = /([a-z]*).png$/.exec(meta.Element);
if (elemRet && elemRet[1]) {
ret.elementType = ret[1];
ret.element = elemName[ret.elementType];
}
2022-03-26 08:21:44 +00:00
// 处理属性
ret.stat = Data.getData(meta, "BaseHP,BaseATK,BaseDEF,aStat:AscensionStat,aStatValue:AscensionStatValue");
ret.statPerLv = lodash.map(meta.CharStat, (d) => Data.getData(d, "Name,Values", metaCfg));
// 处理材料
let itemKey = lodash.map("talent,boss,gemStone,Local,monster,weekly".split(","), (a) => `${a}:${lodash.upperFirst(a)}.Name`);
ret.item = Data.getData(meta, itemKey, metaCfg)
// 处理天赋
ret.talents = {
a: getTalentData(meta.NormalAttack),
e: getTalentData(meta.TalentE),
q: getTalentData(meta.TalentQ),
};
// 处理其他天赋
ret.passiveTalents = lodash.map(meta.PassiveTalents, (d) => Data.getData(d, "Name,desc:Description,icon:Source", metaCfg))
// 处理命座信息
let cons = {};
lodash.forEach(meta.Constellation, (data, key) => {
cons[key.replace("Constellation")] = Data.getData(data, "Name,icon:Source,desc:Description", metaCfg);
})
ret.cons = cons;
return ret;
2022-03-24 13:14:22 +00:00
}
2022-03-26 08:21:44 +00:00
// 获取Meta中的天赋信息
const getTalentData = function (data) {
let ret = Data.getData(data, "Name,icon:Source,desc:Description", { lowerFirstKey: true });
let attr = [], table = [], tableKeys;
lodash.forEach(data.Table, (tr) => {
let tmp = { name: tr.Name }, isTable = true, isDef = false, lastVal;
// 检查当前行是否是表格数据
lodash.forEach(tr.Values, (v) => {
// 如果为空则退出循环
if (v === "") {
isTable = false;
return false;
}
if (typeof (lastVal) === "undefined") {
// 设定初始值
lastVal = v;
} else if (lastVal != v) {
// 如果与初始值不一样,则标记退出循环
isDef = true;
return false;
}
});
if (isTable && isDef) {
if (!tableKeys) {
tableKeys = lodash.keys(tr.Values);
}
tmp.value = lodash.map(tableKeys, (k) => tr.Values[k])
table.push(tmp);
} else {
tmp.value = lastVal;
attr.push(tmp)
}
})
ret.attr = attr;
ret.table = table;
ret.tableKeys = tableKeys;
return ret;
}
2022-03-24 13:14:22 +00:00
Character.get = function (val) {
2022-03-26 08:21:44 +00:00
let roleid = YunzaiApps.mysInfo['roleIdToName'](val);
let name = YunzaiApps.mysInfo['roleIdToName'](roleid, true);
if (!name) {
return false;
}
2022-03-26 08:21:44 +00:00
if (!characterMap[name]) {
let character = new Character(name);
characterMap[name] = character;
}
2022-03-26 08:21:44 +00:00
return characterMap[name];
2022-03-24 13:14:22 +00:00
};
export default Character;