2022-07-23 20:32:10 +00:00
|
|
|
import Base from './Base.js'
|
|
|
|
import lodash from 'lodash'
|
|
|
|
import fs from 'fs'
|
|
|
|
import Data from '../Data.js'
|
|
|
|
import sizeOf from 'image-size'
|
|
|
|
|
2022-07-30 20:20:10 +00:00
|
|
|
let aliasMap = {}
|
|
|
|
let idMap = {}
|
|
|
|
let abbrMap = {}
|
2022-07-23 20:32:10 +00:00
|
|
|
let wifeMap = {}
|
|
|
|
const _path = process.cwd()
|
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-07-23 20:32:10 +00:00
|
|
|
async function init () {
|
|
|
|
let sysCfg = await Data.importModule('plugins/miao-plugin/config/system', 'character.js')
|
|
|
|
let custom = await Data.importModule('plugins/miao-plugin/config', 'character.js')
|
2022-06-29 23:05:31 +00:00
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
lodash.forEach([custom.customCharacters, sysCfg.characters], (roleIds) => {
|
2022-06-29 23:05:31 +00:00
|
|
|
lodash.forEach(roleIds || {}, (aliases, id) => {
|
2022-07-23 20:32:10 +00:00
|
|
|
aliases = aliases || []
|
2022-06-29 23:05:31 +00:00
|
|
|
if (aliases.length === 0) {
|
2022-07-23 20:32:10 +00:00
|
|
|
return
|
2022-06-29 23:05:31 +00:00
|
|
|
}
|
|
|
|
// 建立别名映射
|
|
|
|
lodash.forEach(aliases || [], (alias) => {
|
2022-07-23 20:32:10 +00:00
|
|
|
alias = alias.toLowerCase()
|
|
|
|
aliasMap[alias] = id
|
2022-06-29 23:05:31 +00:00
|
|
|
})
|
2022-07-23 20:32:10 +00:00
|
|
|
aliasMap[id] = id
|
|
|
|
idMap[id] = aliases[0]
|
2022-06-29 23:05:31 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
lodash.forEach([sysCfg.wifeData, custom.wifeData], (wifeData) => {
|
2022-06-29 23:05:31 +00:00
|
|
|
lodash.forEach(wifeData || {}, (ids, type) => {
|
2022-07-23 20:32:10 +00:00
|
|
|
type = Data.def({ girlfriend: 0, boyfriend: 1, daughter: 2, son: 3 }[type], type)
|
2022-06-29 23:05:31 +00:00
|
|
|
if (!wifeMap[type]) {
|
2022-07-23 20:32:10 +00:00
|
|
|
wifeMap[type] = {}
|
2022-06-29 23:05:31 +00:00
|
|
|
}
|
2022-06-30 18:53:05 +00:00
|
|
|
Data.eachStr(ids, (id) => {
|
2022-07-23 20:32:10 +00:00
|
|
|
id = aliasMap[id]
|
2022-06-29 23:05:31 +00:00
|
|
|
if (id) {
|
2022-07-23 20:32:10 +00:00
|
|
|
wifeMap[type][id] = true
|
2022-06-29 23:05:31 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2022-07-23 20:32:10 +00:00
|
|
|
abbrMap = sysCfg.abbr
|
2022-06-29 23:05:31 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
await init()
|
2022-06-29 23:05:31 +00:00
|
|
|
|
2022-03-26 08:21:44 +00:00
|
|
|
class Character extends Base {
|
2022-07-23 20:32:10 +00:00
|
|
|
constructor (name, id) {
|
|
|
|
super()
|
2022-04-10 05:36:31 +00:00
|
|
|
|
|
|
|
if (id * 1 === 10000005) {
|
2022-07-23 20:32:10 +00:00
|
|
|
name = '空'
|
2022-04-10 05:36:31 +00:00
|
|
|
} else if (id * 1 === 10000007) {
|
2022-07-23 20:32:10 +00:00
|
|
|
name = '荧'
|
2022-04-10 05:36:31 +00:00
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
this.name = name
|
|
|
|
lodash.extend(this, getMeta(name))
|
|
|
|
if (name === '主角' || name === '旅行者' || /.主/.test(name)) {
|
|
|
|
this.id = 20000000
|
2022-04-10 05:36:31 +00:00
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
this.id = id
|
2022-03-27 20:58:02 +00:00
|
|
|
}
|
2022-04-08 21:52:05 +00:00
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
getCardImg (se = false, def = true) {
|
|
|
|
let name = this.name
|
|
|
|
let list = []
|
2022-06-16 20:19:27 +00:00
|
|
|
let addImg = function (charImgPath, disable = false) {
|
2022-07-23 20:32:10 +00:00
|
|
|
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)) {
|
2022-07-23 20:32:10 +00:00
|
|
|
fs.mkdirSync(dirPath)
|
2022-06-16 20:19:27 +00:00
|
|
|
}
|
|
|
|
if (disable) {
|
|
|
|
return
|
2022-03-27 20:58:02 +00:00
|
|
|
}
|
2022-04-08 21:52:05 +00:00
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
let imgs = fs.readdirSync(dirPath)
|
|
|
|
imgs = imgs.filter((img) => /\.(png|jpg|webp|jpeg)/i.test(img))
|
2022-04-08 21:52:05 +00:00
|
|
|
lodash.forEach(imgs, (img) => {
|
2022-07-23 20:32:10 +00:00
|
|
|
list.push(`${charImgPath}/${img}`)
|
|
|
|
})
|
2022-03-27 20:58:02 +00:00
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
addImg(`character-img/${name}`)
|
|
|
|
addImg(`character-img/${name}/upload`)
|
2022-06-16 20:19:27 +00:00
|
|
|
addImg(`character-img/${name}/se`, !se)
|
2022-03-27 20:58:02 +00:00
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
const plusPath = './plugins/miao-plugin/resources/miao-res-plus/'
|
2022-06-16 20:19:27 +00:00
|
|
|
if (fs.existsSync(plusPath)) {
|
2022-07-23 20:32:10 +00:00
|
|
|
addImg(`miao-res-plus/character-img/${name}`)
|
|
|
|
addImg(`miao-res-plus/character-img/${name}/se`, !se)
|
2022-06-16 20:19:27 +00:00
|
|
|
}
|
2022-04-08 21:52:05 +00:00
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
let img = lodash.sample(list)
|
2022-04-08 21:52:05 +00:00
|
|
|
|
|
|
|
if (!img) {
|
|
|
|
if (def) {
|
2022-07-23 20:32:10 +00:00
|
|
|
img = '/character-img/default/01.jpg'
|
2022-04-08 21:52:05 +00:00
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
let ret = sizeOf(`./plugins/miao-plugin/resources/${img}`)
|
|
|
|
ret.img = img
|
|
|
|
ret.mode = ret.width > ret.height ? 'left' : 'bottom'
|
|
|
|
return ret
|
2022-04-08 21:52:05 +00:00
|
|
|
}
|
2022-04-10 05:36:31 +00:00
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
checkAvatars (avatars) {
|
2022-04-10 05:36:31 +00:00
|
|
|
if (!lodash.includes([20000000, 10000005, 10000007], this.id * 1)) {
|
2022-07-23 20:32:10 +00:00
|
|
|
return
|
2022-04-10 05:36:31 +00:00
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
let avatarIds = []
|
2022-04-10 05:36:31 +00:00
|
|
|
if (lodash.isArray(avatars)) {
|
2022-07-23 20:32:10 +00:00
|
|
|
avatarIds = lodash.map(avatars, (a) => a.id * 1)
|
2022-04-10 05:36:31 +00:00
|
|
|
} else {
|
2022-07-23 20:32:10 +00:00
|
|
|
avatarIds = [avatars.id]
|
2022-04-10 05:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lodash.includes(avatarIds, 10000005)) {
|
|
|
|
// 空
|
2022-07-23 20:32:10 +00:00
|
|
|
lodash.extend(this, getMeta('空'))
|
2022-04-10 05:36:31 +00:00
|
|
|
} else if (lodash.includes(avatarIds, 10000007)) {
|
|
|
|
// 荧
|
2022-07-23 20:32:10 +00:00
|
|
|
lodash.extend(this, getMeta('荧'))
|
2022-04-10 05:36:31 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-05 07:53:13 +00:00
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
getAvatarTalent (talent = {}, cons = 0, mode = 'level') {
|
|
|
|
let ret = {}
|
|
|
|
let consTalent = this.getConsTalent()
|
2022-06-29 20:40:06 +00:00
|
|
|
lodash.forEach(['a', 'e', 'q'], (key) => {
|
2022-07-23 20:32:10 +00:00
|
|
|
let ds = talent[key]
|
2022-06-29 20:40:06 +00:00
|
|
|
if (ds) {
|
2022-07-23 20:32:10 +00:00
|
|
|
let level
|
2022-06-29 20:40:06 +00:00
|
|
|
if (lodash.isNumber(ds)) {
|
2022-07-23 20:32:10 +00:00
|
|
|
level = ds
|
2022-06-29 20:40:06 +00:00
|
|
|
} else {
|
2022-07-23 20:32:10 +00:00
|
|
|
level = mode === 'level' ? ds.level || ds.level_current || ds.original || ds.level_original : ds.original || ds.level_original || ds.level || ds.level_current
|
2022-06-29 20:40:06 +00:00
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
if (mode === 'level') {
|
2022-06-29 20:40:06 +00:00
|
|
|
// 基于level计算original
|
|
|
|
ret[key] = {
|
|
|
|
level,
|
2022-07-23 20:32:10 +00:00
|
|
|
original: (key !== 'a' && cons >= consTalent[key]) ? (level - 3) : level
|
2022-06-29 20:40:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// 基于original计算level
|
|
|
|
ret[key] = {
|
|
|
|
original: level,
|
2022-07-23 20:32:10 +00:00
|
|
|
level: (key !== 'a' && cons >= consTalent[key]) ? (level + 3) : level
|
2022-06-29 20:40:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2022-08-03 18:32:10 +00:00
|
|
|
if (this.id * 1 !== 10000033) {
|
|
|
|
let a = ret.a || {}
|
|
|
|
if (a.level > 10) {
|
|
|
|
a.level = 10
|
|
|
|
a.original = 10
|
|
|
|
}
|
|
|
|
}
|
2022-08-06 22:36:05 +00:00
|
|
|
if (this.id * 1 === 10000033) {
|
|
|
|
let a = ret.a || {}
|
|
|
|
a.original = a.level - 1
|
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
return ret
|
2022-06-29 20:40:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
getConsTalent () {
|
|
|
|
let talent = this.talent || false
|
2022-06-30 19:04:20 +00:00
|
|
|
if (!talent) {
|
|
|
|
return { e: 3, q: 5 }
|
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
let e = talent.e.name
|
|
|
|
let q = talent.q.name
|
|
|
|
let c3 = this.cons['3'].desc
|
|
|
|
let c5 = this.cons['5'].desc
|
2022-06-29 20:40:06 +00:00
|
|
|
return {
|
|
|
|
e: c3.includes(e) ? 3 : 5,
|
2022-07-23 20:32:10 +00:00
|
|
|
q: c5.includes(q) ? 5 : 3
|
2022-06-29 20:40:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
get weaponType () {
|
2022-06-05 07:53:13 +00:00
|
|
|
const map = {
|
2022-07-23 20:32:10 +00:00
|
|
|
sword: '单手剑',
|
|
|
|
catalyst: '法器',
|
|
|
|
bow: '弓',
|
|
|
|
claymore: '双手剑',
|
|
|
|
polearm: '长柄武器'
|
2022-06-05 07:53:13 +00:00
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
let weaponType = this.weapon || ''
|
|
|
|
return map[weaponType.toLowerCase()] || ''
|
2022-06-05 07:53:13 +00:00
|
|
|
}
|
2022-06-29 23:05:31 +00:00
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
get isCustom () {
|
|
|
|
return !/10\d{6}/.test(this.id)
|
2022-06-29 23:05:31 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 22:12:57 +00:00
|
|
|
get abbr () {
|
|
|
|
return abbrMap[this.name] || this.name
|
|
|
|
}
|
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
checkWifeType (type) {
|
|
|
|
return !!wifeMap[type][this.id]
|
2022-06-29 23:05:31 +00:00
|
|
|
}
|
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) {
|
2022-07-23 20:32:10 +00:00
|
|
|
return Data.readJSON(`${_path}/plugins/miao-plugin/resources/meta/character/${name}/`, 'data.json') || {}
|
2022-04-10 05:36:31 +00:00
|
|
|
}
|
2022-03-24 14:40:59 +00:00
|
|
|
|
|
|
|
Character.get = function (val) {
|
2022-07-23 20:32:10 +00:00
|
|
|
let id, name
|
2022-06-26 00:55:14 +00:00
|
|
|
if (!val) {
|
2022-07-23 20:32:10 +00:00
|
|
|
return false
|
2022-06-26 00:55:14 +00:00
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
if (typeof (val) === 'number' || /^\d*$/.test(val)) {
|
|
|
|
id = val
|
2022-04-10 05:36:31 +00:00
|
|
|
} else if (val.id) {
|
2022-07-23 20:32:10 +00:00
|
|
|
id = val.id
|
|
|
|
name = val.name || idMap[id]
|
2022-04-04 21:36:44 +00:00
|
|
|
} else {
|
2022-07-23 20:32:10 +00:00
|
|
|
id = aliasMap[val]
|
2022-04-10 07:08:03 +00:00
|
|
|
}
|
|
|
|
if (!name) {
|
2022-07-23 20:32:10 +00:00
|
|
|
name = idMap[id]
|
2022-04-04 21:36:44 +00:00
|
|
|
}
|
2022-03-26 08:21:44 +00:00
|
|
|
if (!name) {
|
2022-07-23 20:32:10 +00:00
|
|
|
return false
|
2022-03-24 14:40:59 +00:00
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
return new Character(name, id)
|
|
|
|
}
|
2022-04-10 05:36:31 +00:00
|
|
|
|
2022-04-04 21:36:44 +00:00
|
|
|
Character.getAbbr = function () {
|
2022-07-23 20:32:10 +00:00
|
|
|
return abbrMap
|
|
|
|
}
|
|
|
|
|
|
|
|
Character.checkWifeType = function (charid, type) {
|
2022-07-30 20:20:10 +00:00
|
|
|
return !!wifeMap[type][charid]
|
2022-04-04 21:36:44 +00:00
|
|
|
}
|
|
|
|
|
2022-04-10 20:48:53 +00:00
|
|
|
Character.getRandomImg = function (type) {
|
2022-07-23 20:32:10 +00:00
|
|
|
let chars = fs.readdirSync(metaPath)
|
|
|
|
let ret = []
|
|
|
|
type = type === 'party' ? 'party' : 'profile'
|
2022-04-10 20:48:53 +00:00
|
|
|
lodash.forEach(chars, (char) => {
|
|
|
|
if (fs.existsSync(`${metaPath}/${char}/${type}.png`)) {
|
2022-07-23 20:32:10 +00:00
|
|
|
ret.push(`/meta/character/${char}/${type}.png`)
|
2022-04-10 20:48:53 +00:00
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
})
|
|
|
|
return lodash.sample(ret)
|
2022-04-10 20:48:53 +00:00
|
|
|
}
|
|
|
|
|
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: '班尼特,心海,琴,芭芭拉,七七,迪奥娜,托马,空,荧,阿贝多,钟离'
|
|
|
|
}
|
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
let idSort = {}
|
2022-05-04 20:53:20 +00:00
|
|
|
lodash.forEach(charPosIdx, (chars, pos) => {
|
2022-07-23 20:32:10 +00:00
|
|
|
chars = chars.split(',')
|
2022-05-04 20:53:20 +00:00
|
|
|
lodash.forEach(chars, (name, idx) => {
|
2022-07-23 20:32:10 +00:00
|
|
|
let id = aliasMap[name]
|
2022-06-29 23:05:31 +00:00
|
|
|
if (id) {
|
2022-07-23 20:32:10 +00:00
|
|
|
idSort[id] = pos * 100 + idx
|
2022-05-04 20:53:20 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Character.sortIds = function (arr) {
|
2022-07-23 20:32:10 +00:00
|
|
|
return arr.sort((a, b) => (idSort[a] || 300) - (idSort[b] || 300))
|
2022-05-04 20:53:20 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
export default Character
|