miao-plugin/components/models/Character.js

211 lines
5.4 KiB
JavaScript
Raw Normal View History

2022-03-24 13:14:22 +00:00
import Base from "./Base.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-04-03 22:06:03 +00:00
const _path = process.cwd();
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: "岩"
};
let genshin = await import(`file://${_path}/config/genshin/roleId.js`);
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
2022-04-03 22:06:03 +00:00
async cacheImg() {
2022-04-04 21:42:05 +00:00
let cacheDir = `${_path}/resources/cache`;
2022-04-03 22:06:03 +00:00
Data.createDir(`${_path}/resources/`, `cache`)
let fileList = [
...lodash.values(this.img),
...lodash.map(lodash.values(this.talents), (d) => d.icon),
...lodash.map(this.passiveTalents, (d) => d.icon),
...lodash.map(lodash.values(this.cons), (d) => d.icon)
];
fileList = lodash.uniq(fileList);
2022-04-04 21:42:05 +00:00
let fileMap = await Data.cacheFile(fileList, `${_path}/resources/cache/`);
2022-04-03 22:47:06 +00:00
let path = Data.getUrlPath()
lodash.forEach(this.img, (url, key) => {
if (path[url]) {
this.img[key] = path[url]
}
});
console.log(this);
2022-04-03 22:06:03 +00:00
}
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
// 处理基础信息
2022-04-05 14:24:02 +00:00
let ret = Data.getData(meta, "Name,Key,Title,desc:Description,astro:AstrolabeName", metaCfg);
ret.star = /4star/.test(meta.Star) ? 4 : 5;
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
// 处理属性
2022-04-05 14:24:02 +00:00
ret.stat = Data.getData(meta, "BaseHP,BaseATK,BaseDEF,aStat:AscensionStat,aStatValue:AscensionStatValue", metaCfg);
ret.lvStat = lodash.map(meta.CharStat, (d) => Data.getData(d, "Name,Values", metaCfg));
2022-03-26 08:21:44 +00:00
// 处理材料
let itemKey = lodash.map("talent,boss,gemStone,Local,monster,weekly".split(","), (a) => `${a}:${lodash.upperFirst(a)}.Name`);
ret.item = Data.getData(meta, itemKey, metaCfg)
// 处理天赋
2022-04-05 14:24:02 +00:00
ret.talent = {
2022-03-26 08:21:44 +00:00
a: getTalentData(meta.NormalAttack),
e: getTalentData(meta.TalentE),
q: getTalentData(meta.TalentQ),
};
// 处理其他天赋
2022-04-05 14:24:02 +00:00
ret.passive = lodash.map(meta.PassiveTalents, (d) => Data.getData(d, "Name,desc:Description,icon:Source", metaCfg))
2022-03-26 08:21:44 +00:00
// 处理命座信息
let cons = {};
lodash.forEach(meta.Constellation, (data, key) => {
2022-04-03 22:06:03 +00:00
cons[key.replace("Constellation", "")] = Data.getData(data, "Name,icon:Source,desc:Description", metaCfg);
2022-03-26 08:21:44 +00:00
})
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) {
let roleid;
if (typeof (val) === "number") {
roleid = val;
} else {
roleid = YunzaiApps.mysInfo['roleIdToName'](val);
}
2022-03-26 08:21:44 +00:00
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
};
Character.getAbbr = function () {
return genshin.abbr;
}
2022-04-05 14:24:02 +00:00
Character.getMetaData = getMetaData;
2022-03-24 13:14:22 +00:00
export default Character;