2022-03-24 13:14:22 +00:00
|
|
|
import Base from "./Base.js";
|
2022-03-24 14:40:59 +00:00
|
|
|
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-04-05 19:49:37 +00:00
|
|
|
import path from "path";
|
2022-04-08 21:52:05 +00:00
|
|
|
import sizeOf from "image-size";
|
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-04-04 21:36:44 +00:00
|
|
|
let genshin = await import(`file://${_path}/config/genshin/roleId.js`);
|
|
|
|
|
2022-04-10 20:48:53 +00:00
|
|
|
const metaPath = `${_path}/plugins/miao-plugin/resources/meta/character/`
|
2022-03-24 14:40:59 +00:00
|
|
|
|
2022-03-26 08:21:44 +00:00
|
|
|
class Character extends Base {
|
2022-04-10 05:36:31 +00:00
|
|
|
constructor(name, id) {
|
2022-03-26 08:21:44 +00:00
|
|
|
super();
|
2022-04-10 05:36:31 +00:00
|
|
|
|
|
|
|
if (id * 1 === 10000005) {
|
|
|
|
name = "空";
|
|
|
|
} else if (id * 1 === 10000007) {
|
|
|
|
name = "荧";
|
|
|
|
}
|
2022-04-05 19:49:37 +00:00
|
|
|
this.name = name;
|
2022-04-10 05:36:31 +00:00
|
|
|
lodash.extend(this, getMeta(name));
|
|
|
|
if (name === "主角" || name === "旅行者" || /.主/.test(name)) {
|
|
|
|
this.id = 20000000;
|
|
|
|
}
|
2022-03-27 20:58:02 +00:00
|
|
|
}
|
2022-04-08 21:52:05 +00:00
|
|
|
|
2022-06-16 20:19:27 +00:00
|
|
|
|
|
|
|
getCardImg(se = false, def = true) {
|
2022-04-08 21:52:05 +00:00
|
|
|
let name = this.name;
|
|
|
|
|
2022-04-10 05:36:31 +00:00
|
|
|
const charImgPath = `./plugins/miao-plugin/resources/character-img/${name}/`;
|
|
|
|
|
2022-04-08 21:52:05 +00:00
|
|
|
|
2022-06-16 20:19:27 +00:00
|
|
|
let list = [];
|
2022-04-08 21:52:05 +00:00
|
|
|
|
2022-06-16 20:19:27 +00:00
|
|
|
let addImg = function (charImgPath, disable = false) {
|
|
|
|
let dirPath = `./plugins/miao-plugin/resources/${charImgPath}`;
|
2022-04-08 21:52:05 +00:00
|
|
|
|
2022-06-16 20:19:27 +00:00
|
|
|
if (!fs.existsSync(dirPath)) {
|
|
|
|
fs.mkdirSync(dirPath);
|
|
|
|
}
|
|
|
|
if (disable) {
|
|
|
|
return
|
2022-03-27 20:58:02 +00:00
|
|
|
}
|
2022-04-08 21:52:05 +00:00
|
|
|
|
2022-06-16 20:19:27 +00:00
|
|
|
let imgs = fs.readdirSync(dirPath);
|
2022-05-01 22:13:59 +00:00
|
|
|
imgs = imgs.filter((img) => /\.(png|jpg|webp|jpeg)/i.test(img));
|
2022-04-08 21:52:05 +00:00
|
|
|
lodash.forEach(imgs, (img) => {
|
2022-06-16 20:19:27 +00:00
|
|
|
list.push(`${charImgPath}/${img}`);
|
2022-04-08 21:52:05 +00:00
|
|
|
});
|
2022-03-27 20:58:02 +00:00
|
|
|
}
|
2022-06-16 20:19:27 +00:00
|
|
|
addImg(`character-img/${name}`);
|
|
|
|
addImg(`character-img/${name}/se`, !se)
|
2022-03-27 20:58:02 +00:00
|
|
|
|
2022-06-16 20:19:27 +00:00
|
|
|
const plusPath = `./plugins/miao-plugin/resources/miao-res-plus/`;
|
|
|
|
if (fs.existsSync(plusPath)) {
|
|
|
|
addImg(`miao-res-plus/character-img/${name}`);
|
|
|
|
addImg(`miao-res-plus/character-img/${name}/se`, !se);
|
|
|
|
}
|
2022-04-08 21:52:05 +00:00
|
|
|
|
2022-06-16 20:19:27 +00:00
|
|
|
let img = lodash.sample(list);
|
2022-04-08 21:52:05 +00:00
|
|
|
|
|
|
|
if (!img) {
|
|
|
|
if (def) {
|
|
|
|
img = "/character-img/default/01.jpg";
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let ret = sizeOf(`./plugins/miao-plugin/resources/${img}`);
|
|
|
|
ret.img = img;
|
|
|
|
ret.mode = ret.width > ret.height ? "left" : "bottom";
|
|
|
|
return ret;
|
|
|
|
}
|
2022-04-10 05:36:31 +00:00
|
|
|
|
|
|
|
checkAvatars(avatars) {
|
|
|
|
if (!lodash.includes([20000000, 10000005, 10000007], this.id * 1)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let avatarIds = [];
|
|
|
|
if (lodash.isArray(avatars)) {
|
|
|
|
avatarIds = lodash.map(avatars, (a) => a.id * 1);
|
|
|
|
} else {
|
|
|
|
avatarIds = [avatars.id];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lodash.includes(avatarIds, 10000005)) {
|
|
|
|
// 空
|
|
|
|
lodash.extend(this, getMeta('空'));
|
|
|
|
} else if (lodash.includes(avatarIds, 10000007)) {
|
|
|
|
// 荧
|
|
|
|
lodash.extend(this, getMeta('荧'));
|
|
|
|
}
|
|
|
|
}
|
2022-06-05 07:53:13 +00:00
|
|
|
|
|
|
|
get weaponType() {
|
|
|
|
const map = {
|
|
|
|
sword: "单手剑",
|
|
|
|
catalyst: "法器",
|
|
|
|
bow: "弓",
|
|
|
|
claymore: "双手剑",
|
|
|
|
polearm: "长柄武器"
|
|
|
|
}
|
|
|
|
let weaponType = this.weapon || "";
|
|
|
|
return map[weaponType.toLowerCase()] || "";
|
|
|
|
}
|
2022-03-26 08:21:44 +00:00
|
|
|
}
|
2022-03-24 13:14:22 +00:00
|
|
|
|
2022-04-10 05:36:31 +00:00
|
|
|
let getMeta = function (name) {
|
|
|
|
return Data.readJSON(`${_path}/plugins/miao-plugin/resources/meta/character/${name}/`, "data.json") || {};
|
|
|
|
}
|
2022-03-24 14:40:59 +00:00
|
|
|
|
|
|
|
Character.get = function (val) {
|
2022-04-10 05:36:31 +00:00
|
|
|
let roleid, name;
|
2022-04-10 07:08:03 +00:00
|
|
|
if (typeof (val) === "number" || /^\d*$/.test(val)) {
|
2022-04-04 21:36:44 +00:00
|
|
|
roleid = val;
|
2022-04-10 05:36:31 +00:00
|
|
|
} else if (val.id) {
|
|
|
|
roleid = val.id;
|
|
|
|
name = val.name || YunzaiApps.mysInfo['roleIdToName'](roleid, true);
|
2022-04-04 21:36:44 +00:00
|
|
|
} else {
|
|
|
|
roleid = YunzaiApps.mysInfo['roleIdToName'](val);
|
2022-04-10 07:08:03 +00:00
|
|
|
}
|
|
|
|
if (!name) {
|
2022-04-10 05:36:31 +00:00
|
|
|
name = YunzaiApps.mysInfo['roleIdToName'](roleid, true);
|
2022-04-04 21:36:44 +00:00
|
|
|
}
|
2022-03-26 08:21:44 +00:00
|
|
|
if (!name) {
|
2022-03-24 14:40:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-04-10 05:36:31 +00:00
|
|
|
|
2022-04-10 07:08:03 +00:00
|
|
|
|
2022-04-10 05:36:31 +00:00
|
|
|
return new Character(name, roleid);
|
2022-03-24 13:14:22 +00:00
|
|
|
};
|
|
|
|
|
2022-04-10 05:36:31 +00:00
|
|
|
|
2022-04-04 21:36:44 +00:00
|
|
|
Character.getAbbr = function () {
|
|
|
|
return genshin.abbr;
|
|
|
|
}
|
|
|
|
|
2022-04-10 20:48:53 +00:00
|
|
|
Character.getRandomImg = function (type) {
|
|
|
|
let chars = fs.readdirSync(metaPath);
|
|
|
|
let ret = [];
|
|
|
|
type = type === "party" ? "party" : "profile";
|
|
|
|
lodash.forEach(chars, (char) => {
|
|
|
|
if (fs.existsSync(`${metaPath}/${char}/${type}.png`)) {
|
|
|
|
ret.push(`/meta/character/${char}/${type}.png`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return lodash.sample(ret);
|
|
|
|
}
|
|
|
|
|
2022-05-04 20:53:20 +00:00
|
|
|
|
|
|
|
let charPosIdx = {
|
2022-05-05 10:10:29 +00:00
|
|
|
1: '宵宫,雷神,胡桃,甘雨,优菈,一斗,公子,绫人,魈,可莉,迪卢克,凝光,刻晴,辛焱,烟绯,雷泽',
|
2022-05-04 20:53:20 +00:00
|
|
|
2: '夜兰,八重,九条,行秋,香菱,安柏,凯亚,丽莎,北斗,菲谢尔,重云,罗莎莉亚,埃洛伊',
|
|
|
|
3: '申鹤,莫娜,早柚,云堇,久岐忍,五郎,砂糖,万叶,温迪',
|
|
|
|
4: '班尼特,心海,琴,芭芭拉,七七,迪奥娜,托马,空,荧,阿贝多,钟离'
|
|
|
|
}
|
|
|
|
|
|
|
|
let idSort = {};
|
|
|
|
lodash.forEach(charPosIdx, (chars, pos) => {
|
|
|
|
chars = chars.split(",");
|
|
|
|
lodash.forEach(chars, (name, idx) => {
|
2022-05-05 22:56:04 +00:00
|
|
|
if (global.YunzaiApps) {
|
|
|
|
let id = YunzaiApps.mysInfo['roleIdToName'](name);
|
|
|
|
if (id) {
|
|
|
|
idSort[id] = pos * 100 + idx;
|
|
|
|
}
|
2022-05-04 20:53:20 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
Character.sortIds = function (arr) {
|
|
|
|
return arr.sort((a, b) => (idSort[a] || 300) - (idSort[b] || 300));
|
|
|
|
}
|
|
|
|
|
2022-03-24 13:14:22 +00:00
|
|
|
export default Character;
|