2022-07-27 18:36:49 +00:00
|
|
|
import fs from 'fs'
|
|
|
|
import lodash from 'lodash'
|
2022-08-22 20:53:31 +00:00
|
|
|
import { Character, ProfileReq, ProfileData } from '../models/index.js'
|
2022-07-27 18:36:49 +00:00
|
|
|
import Miao from './profile-data/miao.js'
|
|
|
|
import Enka from './profile-data/enka.js'
|
2022-06-03 09:13:12 +00:00
|
|
|
|
2022-07-27 18:36:49 +00:00
|
|
|
const _path = process.cwd()
|
|
|
|
const userPath = `${_path}/data/UserData/`
|
2022-04-14 20:53:22 +00:00
|
|
|
if (!fs.existsSync(userPath)) {
|
2022-07-27 18:36:49 +00:00
|
|
|
fs.mkdirSync(userPath)
|
2022-04-14 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
2022-08-18 10:13:42 +00:00
|
|
|
ProfileReq.regServ({ Miao, Enka })
|
2022-07-30 20:20:10 +00:00
|
|
|
|
2022-04-14 20:53:22 +00:00
|
|
|
let Profile = {
|
2022-07-27 18:36:49 +00:00
|
|
|
async request (uid, e) {
|
2022-06-06 20:59:47 +00:00
|
|
|
if (uid.toString().length !== 9) {
|
2022-07-27 18:36:49 +00:00
|
|
|
return false
|
2022-06-06 20:59:47 +00:00
|
|
|
}
|
2022-08-18 10:13:42 +00:00
|
|
|
let req = new ProfileReq({ e, uid })
|
2022-07-27 18:36:49 +00:00
|
|
|
let data
|
2022-06-02 10:25:09 +00:00
|
|
|
try {
|
2022-08-18 10:13:42 +00:00
|
|
|
data = await req.request()
|
2022-08-04 20:22:51 +00:00
|
|
|
if (!data) {
|
|
|
|
return false
|
|
|
|
}
|
2022-08-18 10:13:42 +00:00
|
|
|
return Profile.save(uid, data)
|
2022-06-02 10:25:09 +00:00
|
|
|
} catch (err) {
|
2022-07-27 18:36:49 +00:00
|
|
|
console.log(err)
|
|
|
|
e.reply('请求失败')
|
|
|
|
return false
|
2022-04-16 09:40:30 +00:00
|
|
|
}
|
2022-04-14 20:53:22 +00:00
|
|
|
},
|
|
|
|
|
2022-08-18 10:13:42 +00:00
|
|
|
save (uid, data) {
|
2022-07-27 18:36:49 +00:00
|
|
|
let userData = {}
|
|
|
|
const userFile = `${userPath}/${uid}.json`
|
2022-04-14 20:53:22 +00:00
|
|
|
if (fs.existsSync(userFile)) {
|
2022-07-27 18:36:49 +00:00
|
|
|
userData = JSON.parse(fs.readFileSync(userFile, 'utf8')) || {}
|
2022-04-14 20:53:22 +00:00
|
|
|
}
|
2022-07-27 18:36:49 +00:00
|
|
|
lodash.assignIn(userData, lodash.pick(data, 'uid,name,lv,avatar'.split(',')))
|
|
|
|
userData.chars = userData.chars || {}
|
2022-04-14 20:53:22 +00:00
|
|
|
lodash.forEach(data.chars, (char, charId) => {
|
2022-07-27 18:36:49 +00:00
|
|
|
let original = userData.chars[charId] || {}
|
|
|
|
if (char.dataSource === 'miao-pre' && original && original.dataSource) {
|
|
|
|
original.dataSource = char.dataSource
|
2022-06-14 22:07:24 +00:00
|
|
|
} else {
|
2022-07-27 18:36:49 +00:00
|
|
|
userData.chars[charId] = char
|
2022-06-14 22:07:24 +00:00
|
|
|
}
|
2022-07-27 18:36:49 +00:00
|
|
|
})
|
|
|
|
fs.writeFileSync(userFile, JSON.stringify(userData), '', ' ')
|
|
|
|
return data
|
2022-04-14 20:53:22 +00:00
|
|
|
},
|
2022-04-23 21:16:37 +00:00
|
|
|
|
2022-07-27 18:36:49 +00:00
|
|
|
_get (uid, charId) {
|
|
|
|
const userFile = `${userPath}/${uid}.json`
|
|
|
|
let userData = {}
|
2022-04-14 20:53:22 +00:00
|
|
|
if (fs.existsSync(userFile)) {
|
2022-07-27 18:36:49 +00:00
|
|
|
userData = JSON.parse(fs.readFileSync(userFile, 'utf8')) || {}
|
2022-04-14 20:53:22 +00:00
|
|
|
}
|
|
|
|
if (userData && userData.chars && userData.chars[charId]) {
|
2022-07-27 18:36:49 +00:00
|
|
|
return userData.chars[charId]
|
2022-04-14 20:53:22 +00:00
|
|
|
}
|
2022-07-27 18:36:49 +00:00
|
|
|
return false
|
2022-04-15 14:33:51 +00:00
|
|
|
},
|
2022-04-23 21:16:37 +00:00
|
|
|
|
2022-08-18 10:13:42 +00:00
|
|
|
get (uid, charId, onlyHasData = false) {
|
|
|
|
let data = Profile._get(uid, charId)
|
|
|
|
if (data) {
|
|
|
|
let profile = new ProfileData(data)
|
|
|
|
if (onlyHasData && !profile.hasData) {
|
|
|
|
return false
|
2022-06-29 20:40:06 +00:00
|
|
|
}
|
2022-08-18 10:13:42 +00:00
|
|
|
return profile
|
|
|
|
} else {
|
2022-07-27 18:36:49 +00:00
|
|
|
return false
|
2022-06-29 20:40:06 +00:00
|
|
|
}
|
2022-06-14 22:07:24 +00:00
|
|
|
},
|
|
|
|
|
2022-07-27 18:36:49 +00:00
|
|
|
getAll (uid) {
|
|
|
|
const userFile = `${userPath}/${uid}.json`
|
|
|
|
let userData = {}
|
2022-04-23 21:16:37 +00:00
|
|
|
if (fs.existsSync(userFile)) {
|
2022-07-27 18:36:49 +00:00
|
|
|
userData = JSON.parse(fs.readFileSync(userFile, 'utf8')) || {}
|
2022-04-23 21:16:37 +00:00
|
|
|
}
|
|
|
|
if (userData && userData.chars) {
|
2022-08-18 10:13:42 +00:00
|
|
|
let ret = {}
|
|
|
|
lodash.forEach(userData.chars, (ds, id) => {
|
|
|
|
ret[id] = new ProfileData(ds)
|
2022-04-15 14:33:51 +00:00
|
|
|
})
|
2022-07-27 18:36:49 +00:00
|
|
|
return ret
|
2022-04-15 14:33:51 +00:00
|
|
|
}
|
2022-08-18 10:13:42 +00:00
|
|
|
return false
|
2022-04-15 22:05:31 +00:00
|
|
|
},
|
2022-04-23 21:16:37 +00:00
|
|
|
|
2022-07-27 18:36:49 +00:00
|
|
|
inputProfile (uid, e) {
|
|
|
|
let { avatar, inputData } = e
|
|
|
|
let char = Character.get(avatar)
|
|
|
|
let originalData = Profile._get(uid, char.id)
|
2022-06-11 21:54:41 +00:00
|
|
|
if (!originalData || !['enka', 'input2'].includes(originalData.dataSource)) {
|
2022-07-27 18:36:49 +00:00
|
|
|
return `请先获取${char.name}的面板数据后,再进行面板数据更新`
|
2022-06-11 14:10:53 +00:00
|
|
|
}
|
2022-07-27 18:36:49 +00:00
|
|
|
inputData = inputData.replace('#', '')
|
2022-07-30 20:20:10 +00:00
|
|
|
inputData = inputData.replace(/[,;、\n\t]/g, ',')
|
2022-07-27 18:36:49 +00:00
|
|
|
let attr = originalData.attr || {}
|
2022-05-31 21:16:10 +00:00
|
|
|
let attrMap = {
|
|
|
|
hp: /生命/,
|
|
|
|
def: /防御/,
|
|
|
|
atk: /攻击/,
|
|
|
|
mastery: /精通/,
|
2022-06-14 22:07:24 +00:00
|
|
|
cRate: /(暴击率|暴率|暴击$)/,
|
|
|
|
cDmg: /(暴伤|暴击伤害)/,
|
2022-05-31 21:16:10 +00:00
|
|
|
hInc: /治疗/,
|
|
|
|
recharge: /充能/,
|
2022-07-30 20:20:10 +00:00
|
|
|
dmgBonus: /[火水雷草风岩冰素]伤|^伤/,
|
2022-05-31 21:16:10 +00:00
|
|
|
phyBonus: /(物理|物伤)/
|
|
|
|
}
|
2022-07-27 18:36:49 +00:00
|
|
|
lodash.forEach(inputData.split(','), (ds, idx) => {
|
|
|
|
ds = ds.trim()
|
2022-05-31 21:16:10 +00:00
|
|
|
if (!ds) {
|
2022-07-27 18:36:49 +00:00
|
|
|
return
|
2022-05-31 21:16:10 +00:00
|
|
|
}
|
2022-07-30 20:20:10 +00:00
|
|
|
let dRet = /(.*?)([0-9.+\s]+)/.exec(ds)
|
2022-05-31 21:16:10 +00:00
|
|
|
if (!dRet || !dRet[1] || !dRet[2]) {
|
2022-07-27 18:36:49 +00:00
|
|
|
return
|
2022-05-31 21:16:10 +00:00
|
|
|
}
|
2022-07-27 18:36:49 +00:00
|
|
|
let name = dRet[1].trim()
|
|
|
|
let data = dRet[2].trim()
|
|
|
|
name = name.replace('爆', '暴')
|
|
|
|
let range = (src, min = 0, max = 1200) => Math.max(min, Math.min(max, src * 1 || 0))
|
2022-06-04 21:29:24 +00:00
|
|
|
|
2022-05-31 21:16:10 +00:00
|
|
|
lodash.forEach(attrMap, (reg, key) => {
|
|
|
|
if (reg.test(name)) {
|
2022-07-27 18:36:49 +00:00
|
|
|
let tmp = data.split('+')
|
2022-06-04 21:29:24 +00:00
|
|
|
switch (key) {
|
2022-07-27 18:36:49 +00:00
|
|
|
case 'hp':
|
|
|
|
attr[key + 'Base'] = range(tmp[0].trim(), 0, 16000)
|
|
|
|
attr[key] = range(tmp[0].trim() * 1 + tmp[1].trim() * 1, attr[key + 'Base'], 50000)
|
|
|
|
break
|
|
|
|
case 'def':
|
|
|
|
case 'atk':
|
|
|
|
attr[key + 'Base'] = range(tmp[0].trim(), 0, 1100)
|
|
|
|
attr[key] = range(tmp[0].trim() * 1 + tmp[1].trim() * 1, attr[key + 'Base'], 4500)
|
|
|
|
break
|
|
|
|
case 'mastery':
|
|
|
|
attr[key] = range(data, 0, 1200)
|
|
|
|
break
|
|
|
|
case 'cRate':
|
|
|
|
attr[key] = range(data, -95, 120)
|
|
|
|
break
|
|
|
|
case 'cDmg':
|
|
|
|
attr[key] = range(data, 0, 320)
|
|
|
|
break
|
|
|
|
case 'recharge':
|
|
|
|
case 'hInc':
|
|
|
|
attr[key] = range(data, 0, 400)
|
|
|
|
break
|
|
|
|
case 'dmgBonus':
|
|
|
|
case 'phyBonus':
|
|
|
|
attr[key] = range(data, 0, 200)
|
|
|
|
break
|
2022-05-31 21:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
if (lodash.keys(attr) < 3) {
|
2022-07-27 18:36:49 +00:00
|
|
|
return false
|
2022-05-31 21:16:10 +00:00
|
|
|
}
|
|
|
|
|
2022-07-27 18:36:49 +00:00
|
|
|
originalData.dataSource = 'input2'
|
|
|
|
originalData.attr = attr
|
2022-06-11 14:10:53 +00:00
|
|
|
|
2022-07-27 18:36:49 +00:00
|
|
|
let userData = {}
|
|
|
|
const userFile = `${userPath}/${uid}.json`
|
2022-05-31 21:16:10 +00:00
|
|
|
if (fs.existsSync(userFile)) {
|
2022-07-27 18:36:49 +00:00
|
|
|
userData = JSON.parse(fs.readFileSync(userFile, 'utf8')) || {}
|
2022-05-31 21:16:10 +00:00
|
|
|
}
|
2022-07-27 18:36:49 +00:00
|
|
|
userData.chars = userData.chars || {}
|
|
|
|
userData.chars[avatar] = originalData
|
|
|
|
fs.writeFileSync(userFile, JSON.stringify(userData), '', ' ')
|
|
|
|
return true
|
2022-08-08 21:16:37 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getServName (uid) {
|
2022-08-18 10:13:42 +00:00
|
|
|
let Serv = ProfileReq.getServ(uid)
|
|
|
|
return Serv.name
|
2022-04-14 20:53:22 +00:00
|
|
|
}
|
2022-07-27 18:36:49 +00:00
|
|
|
}
|
|
|
|
export default Profile
|