miao-plugin/components/Profile.js

130 lines
3.0 KiB
JavaScript
Raw Normal View History

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-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-04-14 20:53:22 +00:00
let Profile = {
2022-07-27 18:36:49 +00:00
async request (uid, e) {
if (uid.toString().length !== 9) {
2022-07-27 18:36:49 +00:00
return false
}
2022-08-18 10:13:42 +00:00
let req = new ProfileReq({ e, uid })
2022-07-27 18:36:49 +00:00
let data
try {
2022-08-18 10:13:42 +00:00
data = await req.request()
if (!data) {
return false
}
2022-08-18 10:13:42 +00:00
return Profile.save(uid, data)
} catch (err) {
2022-07-27 18:36:49 +00:00
console.log(err)
e.reply('请求失败')
return false
}
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-11-13 20:43:59 +00:00
try {
userData = JSON.parse(fs.readFileSync(userFile, 'utf8')) || {}
} catch (e) {
userData = {}
}
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) => {
userData.chars[charId] = char
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-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-10-25 20:11:01 +00:00
try {
userData = JSON.parse(fs.readFileSync(userFile, 'utf8')) || {}
} catch (e) {
}
2022-04-14 20:53:22 +00:00
}
if (userData && userData.chars) {
let char = Character.get(charId)
if (char.isTraveler) {
let charData = userData.chars['10000005'] || userData.chars['10000007'] || false
if (charData) {
char.checkAvatars(charData, uid)
}
return charData
} else {
return userData.chars[charId]
}
2022-04-14 20:53:22 +00:00
}
2022-07-27 18:36:49 +00:00
return false
},
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, uid)
2022-08-18 10:13:42 +00:00
if (onlyHasData && !profile.hasData) {
return false
}
2022-08-18 10:13:42 +00:00
return profile
} else {
2022-07-27 18:36:49 +00:00
return false
}
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 = {}
if (fs.existsSync(userFile)) {
2022-11-13 20:43:59 +00:00
try {
userData = JSON.parse(fs.readFileSync(userFile, 'utf8')) || {}
} catch (e) {
userData = {}
}
}
if (userData && userData.chars) {
2022-08-18 10:13:42 +00:00
let ret = {}
lodash.forEach(userData.chars, (ds, id) => {
let profile = new ProfileData(ds, uid)
if (profile.hasData) {
ret[id] = profile
}
})
2022-07-27 18:36:49 +00:00
return ret
}
2022-08-18 10:13:42 +00:00
return false
2022-04-15 22:05:31 +00:00
},
2022-11-07 20:08:24 +00:00
async forEach (uid, fn) {
let profiles = Profile.getAll(uid)
for (let id in profiles) {
let ret = await fn(profiles[id], id)
if (ret === false) {
return false
}
}
},
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