miao-plugin/models/character-lib/CharImg.js
yoimiya-kokomi b5c1daf472 框架底层角色相关逻辑重构,角色图像资源迁移为webp格式
角色面板支持旅行者,暂未支持伤害计算
`#雷主天赋`、`#草主命座`功能升级
2022-09-04 05:08:57 +08:00

78 lines
2.0 KiB
JavaScript

import fs from 'fs'
import lodash from 'lodash'
import sizeOf from 'image-size'
const CharImg = {
getCardImg (name, se = false, def = true) {
let list = []
let addImg = function (charImgPath, disable = false) {
let dirPath = `./plugins/miao-plugin/resources/${charImgPath}`
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath)
}
if (disable) {
return
}
let imgs = fs.readdirSync(dirPath)
imgs = imgs.filter((img) => /\.(png|jpg|webp|jpeg)/i.test(img))
lodash.forEach(imgs, (img) => {
list.push(`${charImgPath}/${img}`)
})
}
addImg(`character-img/${name}`)
addImg(`character-img/${name}/upload`)
addImg(`character-img/${name}/se`, !se)
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)
}
let img = lodash.sample(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
},
getImgs (name, cId = '', elem = '') {
let imgs = {}
const path = `/meta/character/${name}/`
const icon = elem ? `${elem}/icons` : 'icons'
const img = elem ? `${elem}/imgs` : 'imgs'
let add = (key, path1, type = 'webp') => {
imgs[key] = `${path}${path1}.${type}`
}
add('face', `imgs/face${cId}`)
add('side', 'imgs/side')
add('gacha', 'imgs/gacha')
add('splash', `imgs/splash${cId}`)
add('card', `${img}/card`)
add('banner', `${img}/banner`)
for (let i = 1; i <= 6; i++) {
add(`cons${i}`, `${icon}/cons-${i}`)
}
for (let i = 0; i <= 3; i++) {
add(`passive${i}`, `${icon}/passive-${i}`)
}
for (let k of ['a', 'e', 'q']) {
add(k, `${icon}/talent-${k}`)
}
return imgs
}
}
export default CharImg