Character支持对旅行者、主角、空、荧的处理

This commit is contained in:
yoimiya-kokomi 2022-04-10 13:36:31 +08:00
parent 48bcbcd7ce
commit 15af6b47c2
5 changed files with 107 additions and 81 deletions

View File

@ -236,7 +236,7 @@ async function renderAvatar(e, avatar, render, renderType = "card") {
if (!char) {
return false;
}
let roleId = char.id;
let MysApi = await e.getMysApi({
auth: "all",
targetType: Cfg.get("char.queryOther", true) ? "all" : "self",
@ -250,20 +250,10 @@ async function renderAvatar(e, avatar, render, renderType = "card") {
let avatars = charData.avatars;
let length = avatars.length;
char.checkAvatars(avatars);
avatars = lodash.keyBy(avatars, "id");
if (roleId == 20000000) {
if (avatars["10000005"]) {
roleId = 10000005;
}
if (avatars["10000007"]) {
roleId = 10000007;
}
}
char.roleId = roleId;
if (!avatars[roleId]) {
if (!avatars[char.id]) {
let name = lodash.truncate(e.sender.card, { length: 8 });
if (length > 8) {
e.reply([segment.at(e.user_id, name), `\n没有${e.msg}`]);
@ -272,10 +262,8 @@ async function renderAvatar(e, avatar, render, renderType = "card") {
}
return true;
}
avatar = avatars[roleId];
avatar = avatars[char.id];
}
return await renderCard(e, avatar, render, renderType);
}
@ -288,9 +276,12 @@ async function renderCard(e, avatar, render, renderType = "card") {
let uid = e.targetUser.uid;
let char = Character.get(avatar.name);
let char = Character.get(avatar);
let bg = getCharacterImg(avatar.name);
if (!char) {
return false;
}
let bg = char.getCardImg();
if (renderType === "photo") {
e.reply(segment.image(process.cwd() + "/plugins/miao-plugin/resources/" + bg.img));
@ -302,7 +293,8 @@ async function renderCard(e, avatar, render, renderType = "card") {
talent,
crownNum,
talentMap: { a: "普攻", e: "战技", q: "爆发" },
bg: getCharacterImg(avatar.name),
//bg: getCharacterImg(avatar.name),
bg,
...getCharacterData(avatar),
ds: char.getData("name,id,title,desc"),
cfgScale: Cfg.scale(1.25)
@ -441,44 +433,3 @@ function getCharacterData(avatars) {
set: setArr,
};
}
function getCharacterImg(name) {
if (!fs.existsSync(`./plugins/miao-plugin/resources/character-img/${name}/`)) {
fs.mkdirSync(`./plugins/miao-plugin/resources/character-img/${name}/`);
}
let list = {};
let imgs = fs.readdirSync(`./plugins/miao-plugin/resources/character-img/${name}/`);
imgs = imgs.filter((img) => /\.(png|jpg|webp)/.test(img));
lodash.forEach(imgs, (img) => {
list[img] = `character-img/${name}/${img}`
});
const plusPath = `./plugins/miao-plugin/resources/miao-res-plus/`;
if (fs.existsSync(plusPath)) {
if (!fs.existsSync(`${plusPath}/character-img/${name}/`)) {
fs.mkdirSync(`${plusPath}/character-img/${name}/`);
}
let imgs = fs.readdirSync(`${plusPath}/character-img/${name}/`);
imgs = imgs.filter((img) => /\.(png|jpg|webp)/.test(img));
lodash.forEach(imgs, (img) => {
list[img] = `miao-res-plus/character-img/${name}/${img}`
});
}
let img = lodash.sample(lodash.values(list));
if (!img) {
img = "/character-img/default/01.jpg";
}
let ret = sizeOf(`./plugins/miao-plugin/resources/${img}`);
ret.img = img;
ret.mode = ret.width > ret.height ? "left" : "bottom";
return ret;
}

View File

@ -32,9 +32,11 @@ let Data = {
}
// 检查并创建目录
Data.createDir(root, path, true);
if (fs.existsSync(`${root}/${path}`)) {
let jsonRet = fs.readFileSync(`${root}/${path}`, "utf8");
return JSON.parse(jsonRet);
}
return {}
},
/*

View File

@ -12,24 +12,33 @@ let genshin = await import(`file://${_path}/config/genshin/roleId.js`);
class Character extends Base {
constructor(name) {
console.log('constructor', name)
constructor(name, id) {
super();
if (id * 1 === 10000005) {
name = "空";
} else if (id * 1 === 10000007) {
name = "荧";
}
this.name = name;
let data = Data.readJSON(`${_path}/plugins/miao-plugin/resources/meta/character/${this.name}/`, "data.json");
lodash.extend(this, data);
lodash.extend(this, getMeta(name));
if (name === "主角" || name === "旅行者" || /.主/.test(name)) {
this.id = 20000000;
}
}
getCardImg(def = true) {
let name = this.name;
if (!fs.existsSync(`./plugins/miao-plugin/resources/character-img/${name}/`)) {
fs.mkdirSync(`./plugins/miao-plugin/resources/character-img/${name}/`);
const charImgPath = `./plugins/miao-plugin/resources/character-img/${name}/`;
if (!fs.existsSync(charImgPath)) {
fs.mkdirSync(charImgPath);
}
let list = {};
let imgs = fs.readdirSync(`./plugins/miao-plugin/resources/character-img/${name}/`);
imgs = imgs.filter((img) => /\.(png|jpg|webp)/.test(img));
let imgs = fs.readdirSync(charImgPath);
imgs = imgs.filter((img) => /\.(png|jpg|webp)/i.test(img));
lodash.forEach(imgs, (img) => {
list[img] = `character-img/${name}/${img}`
@ -37,18 +46,19 @@ class Character extends Base {
const plusPath = `./plugins/miao-plugin/resources/miao-res-plus/`;
if (fs.existsSync(plusPath)) {
if (!fs.existsSync(`${plusPath}/character-img/${name}/`)) {
fs.mkdirSync(`${plusPath}/character-img/${name}/`);
const charImgPlusPath = `${plusPath}/character-img/${name}/`;
if (!fs.existsSync(charImgPlusPath)) {
fs.mkdirSync(charImgPlusPath);
}
let imgs = fs.readdirSync(`${plusPath}/character-img/${name}/`);
imgs = imgs.filter((img) => /\.(png|jpg|webp)/.test(img));
imgs = fs.readdirSync(charImgPlusPath);
imgs = imgs.filter((img) => /\.(png|jpg|webp)/i.test(img));
lodash.forEach(imgs, (img) => {
list[img] = `miao-res-plus/character-img/${name}/${img}`
});
}
let img = lodash.sample(lodash.values(list));
@ -65,23 +75,52 @@ class Character extends Base {
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('荧'));
}
}
}
let getMeta = function (name) {
return Data.readJSON(`${_path}/plugins/miao-plugin/resources/meta/character/${name}/`, "data.json") || {};
}
Character.get = function (val) {
let roleid;
let roleid, name;
if (typeof (val) === "number") {
roleid = val;
} else if (val.id) {
roleid = val.id;
name = val.name || YunzaiApps.mysInfo['roleIdToName'](roleid, true);
} else {
roleid = YunzaiApps.mysInfo['roleIdToName'](val);
name = YunzaiApps.mysInfo['roleIdToName'](roleid, true);
}
let name = YunzaiApps.mysInfo['roleIdToName'](roleid, true);
if (!name) {
return false;
}
return new Character(name);
return new Character(name, roleid);
};
Character.getAbbr = function () {
return genshin.abbr;
}

View File

@ -1 +1,18 @@
{}
{
"name": "空",
"abbr": "空",
"id": 10000005,
"title": "",
"star": 5,
"elem": "anemo",
"allegiance": "-",
"weapon": "Sword",
"britydah": " April",
"astro": "旅人座",
"cncv": "",
"jpcv": "",
"desc": "从世界之外漂流而来的旅行者,被神带走血亲,自此踏上寻找七神之路。",
"imgs": {
}
}

View File

@ -1 +1,18 @@
{}
{
"name": "荧",
"abbr": "荧",
"id": 10000007,
"title": "",
"star": 5,
"elem": "anemo",
"allegiance": "-",
"weapon": "Sword",
"britydah": " April",
"astro": "旅人座",
"cncv": "宴宁&多多poi",
"jpcv": "悠木碧&古贺葵",
"desc": "从世界之外漂流而来的旅行者,被神带走血亲,自此踏上寻找七神之路。",
"imgs": {
}
}