miao-plugin/components/models/Character.js

146 lines
3.5 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";
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();
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-26 08:21:44 +00:00
class Character extends Base {
constructor(name, id) {
2022-03-26 08:21:44 +00:00
super();
if (id * 1 === 10000005) {
name = "空";
} else if (id * 1 === 10000007) {
name = "荧";
}
this.name = name;
lodash.extend(this, getMeta(name));
if (name === "主角" || name === "旅行者" || /.主/.test(name)) {
this.id = 20000000;
}
}
2022-04-08 21:52:05 +00:00
getCardImg(def = true) {
let name = this.name;
const charImgPath = `./plugins/miao-plugin/resources/character-img/${name}/`;
if (!fs.existsSync(charImgPath)) {
fs.mkdirSync(charImgPath);
2022-04-08 21:52:05 +00:00
}
let list = {};
let imgs = fs.readdirSync(charImgPath);
imgs = imgs.filter((img) => /\.(png|jpg|webp)/i.test(img));
2022-04-08 21:52:05 +00:00
lodash.forEach(imgs, (img) => {
list[img] = `character-img/${name}/${img}`
});
const plusPath = `./plugins/miao-plugin/resources/miao-res-plus/`;
if (fs.existsSync(plusPath)) {
const charImgPlusPath = `${plusPath}/character-img/${name}/`;
if (!fs.existsSync(charImgPlusPath)) {
fs.mkdirSync(charImgPlusPath);
}
2022-04-08 21:52:05 +00:00
imgs = fs.readdirSync(charImgPlusPath);
imgs = imgs.filter((img) => /\.(png|jpg|webp)/i.test(img));
2022-04-08 21:52:05 +00:00
lodash.forEach(imgs, (img) => {
list[img] = `miao-res-plus/character-img/${name}/${img}`
});
}
2022-04-08 21:52:05 +00:00
let img = lodash.sample(lodash.values(list));
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;
}
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-03-26 08:21:44 +00:00
}
2022-03-24 13:14:22 +00:00
let getMeta = function (name) {
return Data.readJSON(`${_path}/plugins/miao-plugin/resources/meta/character/${name}/`, "data.json") || {};
}
Character.get = function (val) {
let roleid, name;
2022-04-10 07:08:03 +00:00
if (typeof (val) === "number" || /^\d*$/.test(val)) {
roleid = val;
} else if (val.id) {
roleid = val.id;
name = val.name || YunzaiApps.mysInfo['roleIdToName'](roleid, true);
} else {
roleid = YunzaiApps.mysInfo['roleIdToName'](val);
2022-04-10 07:08:03 +00:00
}
if (!name) {
name = YunzaiApps.mysInfo['roleIdToName'](roleid, true);
}
2022-03-26 08:21:44 +00:00
if (!name) {
return false;
}
2022-04-10 07:08:03 +00:00
return new Character(name, roleid);
2022-03-24 13:14:22 +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-03-24 13:14:22 +00:00
export default Character;