From 15af6b47c25aa6d6a1bd6e255e15beebc235b2e8 Mon Sep 17 00:00:00 2001 From: yoimiya-kokomi <592981798@qq.com> Date: Sun, 10 Apr 2022 13:36:31 +0800 Subject: [PATCH] =?UTF-8?q?Character=E6=94=AF=E6=8C=81=E5=AF=B9=E6=97=85?= =?UTF-8?q?=E8=A1=8C=E8=80=85=E3=80=81=E4=B8=BB=E8=A7=92=E3=80=81=E7=A9=BA?= =?UTF-8?q?=E3=80=81=E8=8D=A7=E7=9A=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/character.js | 71 ++++---------------------- components/Data.js | 8 +-- components/models/Character.js | 71 ++++++++++++++++++++------ resources/meta/character/空/data.json | 19 ++++++- resources/meta/character/荧/data.json | 19 ++++++- 5 files changed, 107 insertions(+), 81 deletions(-) diff --git a/apps/character.js b/apps/character.js index da010f5a..7b2c87e4 100644 --- a/apps/character.js +++ b/apps/character.js @@ -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; -} diff --git a/components/Data.js b/components/Data.js index 39417c43..10992e7d 100644 --- a/components/Data.js +++ b/components/Data.js @@ -32,9 +32,11 @@ let Data = { } // 检查并创建目录 Data.createDir(root, path, true); - - let jsonRet = fs.readFileSync(`${root}/${path}`, "utf8"); - return JSON.parse(jsonRet); + if (fs.existsSync(`${root}/${path}`)) { + let jsonRet = fs.readFileSync(`${root}/${path}`, "utf8"); + return JSON.parse(jsonRet); + } + return {} }, /* diff --git a/components/models/Character.js b/components/models/Character.js index 10c64416..e0364a24 100644 --- a/components/models/Character.js +++ b/components/models/Character.js @@ -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; } diff --git a/resources/meta/character/空/data.json b/resources/meta/character/空/data.json index 9e26dfee..79f9e74a 100644 --- a/resources/meta/character/空/data.json +++ b/resources/meta/character/空/data.json @@ -1 +1,18 @@ -{} \ No newline at end of file +{ + "name": "空", + "abbr": "空", + "id": 10000005, + "title": "", + "star": 5, + "elem": "anemo", + "allegiance": "-", + "weapon": "Sword", + "britydah": " April", + "astro": "旅人座", + "cncv": "", + "jpcv": "", + "desc": "从世界之外漂流而来的旅行者,被神带走血亲,自此踏上寻找七神之路。", + "imgs": { + + } +} \ No newline at end of file diff --git a/resources/meta/character/荧/data.json b/resources/meta/character/荧/data.json index 9e26dfee..bc50afbd 100644 --- a/resources/meta/character/荧/data.json +++ b/resources/meta/character/荧/data.json @@ -1 +1,18 @@ -{} \ No newline at end of file +{ + "name": "荧", + "abbr": "荧", + "id": 10000007, + "title": "", + "star": 5, + "elem": "anemo", + "allegiance": "-", + "weapon": "Sword", + "britydah": " April", + "astro": "旅人座", + "cncv": "宴宁&多多poi", + "jpcv": "悠木碧&古贺葵", + "desc": "从世界之外漂流而来的旅行者,被神带走血亲,自此踏上寻找七神之路。", + "imgs": { + + } +} \ No newline at end of file