mirror of
https://github.com/yoimiya-kokomi/miao-plugin.git
synced 2024-11-25 17:35:34 +00:00
43 lines
1.2 KiB
JavaScript
43 lines
1.2 KiB
JavaScript
import fs from 'node:fs'
|
|
import { Data } from '../../components/index.js'
|
|
import lodash from 'lodash'
|
|
|
|
const _path = process.cwd()
|
|
const userPath = `${_path}/data/UserData/`
|
|
if (!fs.existsSync(userPath)) {
|
|
fs.mkdirSync(userPath)
|
|
}
|
|
|
|
let ProfileFile = {
|
|
getData (uid) {
|
|
let data = Data.readJSON('/data/UserData', 'root')
|
|
if (data && data.chars) {
|
|
return data
|
|
} else {
|
|
return {
|
|
uid,
|
|
chars: {}
|
|
}
|
|
}
|
|
},
|
|
saveData (profile) {
|
|
let userData = {}
|
|
const userFile = `${userPath}/${uid}.json`
|
|
if (fs.existsSync(userFile)) {
|
|
userData = JSON.parse(fs.readFileSync(userFile, 'utf8')) || {}
|
|
}
|
|
lodash.assignIn(userData, lodash.pick(data, 'uid,name,lv,avatar'.split(',')))
|
|
userData.chars = userData.chars || {}
|
|
lodash.forEach(data.chars, (char, charId) => {
|
|
let original = userData.chars[charId] || {}
|
|
if (char.dataSource === 'miao-pre' && original && original.dataSource) {
|
|
original.dataSource = char.dataSource
|
|
} else {
|
|
userData.chars[charId] = char
|
|
}
|
|
})
|
|
fs.writeFileSync(userFile, JSON.stringify(userData), '', ' ')
|
|
return data
|
|
}
|
|
}
|
|
export default ProfileFile |