Miao-Yunzai/plugins/genshin/model/mys/NoteUser.js

390 lines
8.7 KiB
JavaScript
Raw Normal View History

2023-03-04 14:30:13 +08:00
/**
* Bot实际User用户类
* 主键QQ
*
* User可以注册UID通过 getRegUid / setRegUid
* 一个User可以绑定多个MysUser CK绑定MysUser
*/
import BaseModel from './BaseModel.js'
import lodash from 'lodash'
import MysUser from './MysUser.js'
import MysUtil from './MysUtil.js'
import { UserDB } from '../db/index.js'
2023-05-09 11:03:38 +08:00
import { Data } from '#miao'
2023-03-04 14:30:13 +08:00
export default class NoteUser extends BaseModel {
2023-05-09 11:03:38 +08:00
constructor (qq) {
2023-03-04 14:30:13 +08:00
super()
// 检查实例缓存
let cacheObj = this._getThis('user', qq)
if (cacheObj) {
return cacheObj
}
this.qq = qq
return this._cacheThis()
}
/**
2023-06-01 03:28:35 +08:00
* OLD Func {{
2023-03-04 14:30:13 +08:00
*/
2023-06-01 03:28:35 +08:00
get uid () {
console.warn('NoteUser.uid 默认返回原神UID可更改为 user.getUid(game)')
return this.getUid()
2023-03-04 14:30:13 +08:00
}
2023-06-01 03:28:35 +08:00
// 获取绑定CK的UID列表如未绑定CK则返回空数组
2023-03-04 14:30:13 +08:00
get ckUids () {
2023-06-01 03:28:35 +08:00
console.warn('NoteUser.ckUids 默认返回原神UID可更改为 user.getCkUidList(game)')
let uids = this.getCkUidList('gs')
return lodash.map(uids, (ds) => ds.uid)
2023-03-04 14:30:13 +08:00
}
/**
* 获取当前用户的所有ck
* @returns { {ltuid:{ckData, ck, uids}} }
*/
get cks () {
2023-06-01 03:28:35 +08:00
console.warn('NoteUser.cks 即将废弃')
2023-05-09 11:03:38 +08:00
let game = 'gs'
2023-03-04 14:30:13 +08:00
let cks = {}
if (!this.hasCk) {
return cks
}
2023-05-09 11:03:38 +08:00
for (let ltuid in this.mysUsers) {
let mys = this.mysUsers[ltuid]
if (mys && mys.ltuid && mys.uid) {
cks[ltuid] = cks[ltuid] || {
ckData: mys.getCkInfo(game),
ck: mys.ck,
uids: mys.getUids(game)
2023-03-04 14:30:13 +08:00
}
}
}
return cks
}
2023-06-01 03:28:35 +08:00
/**
* End OLD Func }}
*/
// 当前用户是否具备CK
get hasCk () {
return !lodash.isEmpty(this.mysUsers)
}
2023-03-04 14:30:13 +08:00
/**
* 创建NoteUser实例
* @param qq NoterUser对应idqq
* @param db
* * 若传入e对象则会识别e.user_id并将user对象添加至e.user
* @param data 用户对应MysCookie数据为空则自动读取
* @returns {Promise<NoteUser|*>}
2023-03-04 14:30:13 +08:00
*/
static async create (qq, db = false) {
// 兼容处理传入e
if (qq && qq.user_id) {
let e = qq
let user = await NoteUser.create(e.user_id)
e.user = user
return user
2023-03-04 14:30:13 +08:00
}
2023-05-09 11:03:38 +08:00
let user = new NoteUser(qq)
await user.initDB(db)
// 传入data则使用否则读取
return user
2023-03-04 14:30:13 +08:00
}
static async forEach (fn) {
2023-05-09 11:03:38 +08:00
let dbs = await UserDB.findAll()
2023-05-31 03:19:27 +08:00
await Data.forEach(dbs, async (db) => {
2023-05-09 11:03:38 +08:00
let user = await NoteUser.create(db.id, db)
return await fn(user)
})
2023-03-04 14:30:13 +08:00
}
// 初始化数据
2023-05-09 11:03:38 +08:00
async initDB (db = false) {
if (this.db && !db) {
return
}
2023-06-01 03:28:35 +08:00
if (db && db !== true) {
this.db = db
} else {
this.db = await UserDB.find(this.qq, 'qq')
}
2023-05-09 11:03:38 +08:00
await this.initMysUser()
this._games = this.db.games
2023-05-11 06:08:03 +08:00
await this.save()
2023-03-04 14:30:13 +08:00
}
// 初始化MysUser对象
async initMysUser () {
let ltuids = this.db?.ltuids || ''
this.mysUsers = {}
for (let ltuid of ltuids.split(',')) {
let mys = await MysUser.create(ltuid)
if (mys) {
this.mysUsers[ltuid] = mys
2023-03-04 14:30:13 +08:00
}
}
}
2023-05-09 11:03:38 +08:00
async save () {
await this.db.saveDB(this)
}
2023-06-01 03:28:35 +08:00
getUidMapList (game = 'gs', type = 'all') {
if (this._map?.[game]?.[type]) {
return this._map[game][type]
}
game = this.gameKey(game)
let uidMap = {}
let uidList = []
lodash.forEach(this.mysUsers, (mys) => {
if (!mys) {
return
}
lodash.forEach(mys.uids[game] || [], (uid) => {
uid = uid + ''
if (uid && !uidMap[uid]) {
uidMap[uid] = mys.getUidData(uid, game)
uidList.push(uidMap[uid])
}
})
})
if (type === 'all') {
let gameDs = this.getGameDs(game)
lodash.forEach(gameDs.data, (ds) => {
if (ds.uid && !uidMap[ds.uid]) {
uidMap[ds.uid] = ds
uidList.push(ds)
}
})
}
this._map = this._map || {}
this._map[game] = this._map[game] || {}
this._map[game][type] = {
map: uidMap,
list: uidList
}
return this._map[game][type]
}
2023-06-01 03:28:35 +08:00
getUidData (uid = '', game = 'gs') {
if (!uid) {
uid = this.getUid(game)
2023-05-09 11:03:38 +08:00
}
2023-06-01 03:28:35 +08:00
return this.getUidMapList(game, 'all').map[uid]
2023-05-09 11:03:38 +08:00
}
2023-06-01 03:28:35 +08:00
/** 有Uid */
hasUid (uid = '', game = '') {
if (!uid) {
return this.getUidMapList(game, 'all').list?.length > 0
}
return !!this.getUidData(uid, game)
}
2023-06-01 03:28:35 +08:00
/** 获取CK-Uid */
getCkUid (game = 'gs') {
let uid = this.getUid(game)
2023-06-01 03:28:35 +08:00
let { map, list } = this.getUidMapList(game, 'ck')
return (map[uid] ? uid : list[0]?.uid) || ''
}
/** 获取CK-Uid列表 */
getCkUidList (game = 'gs') {
return this.getUidMapList(game, 'ck').list
}
/** 获取当前UID */
getUid (game = 'gs') {
game = this.gameKey(game)
2023-06-01 03:28:35 +08:00
// todo 刷新uid
let ds = this.getGameDs(game)
if (!ds.uid) {
this.setMainUid('', game)
}
return ds.uid || ''
}
2023-06-01 03:28:35 +08:00
/** 获取UID列表 */
getUidList (game = 'gs') {
return this.getUidMapList(game, 'all').list
}
/** 获取当前的MysUser对象 */
getMysUser (game = 'gs') {
if (lodash.isEmpty(this.mysUsers)) {
return false
}
2023-06-01 03:28:35 +08:00
let uid = this.getCkUid(game)
if (!uid) {
return false
}
2023-06-01 03:28:35 +08:00
let uidData = this.getUidData(uid, game)
return this.mysUsers[uidData.ltuid]
}
// 添加UID
addRegUid (uid, game = 'gs', save = true) {
2023-06-01 03:28:35 +08:00
game = this.gameKey(game)
2023-05-11 06:08:03 +08:00
uid = uid + ''
2023-06-01 03:28:35 +08:00
let gameDs = this.getGameDs(game)
gameDs.data[uid] = { uid, type: 'reg' }
this._map = false
this.setMainUid(uid, game, false)
if (save) {
this.save()
}
}
// 删除UID
2023-06-01 03:28:35 +08:00
delRegUid (uid, game = 'gs') {
game = this.gameKey(game)
let gameDs = this.getGameDs(game)
let dsData = gameDs.data
delete dsData[uid]
gameDs.data = dsData
this._map = false
if (gameDs.uid === uid) {
this.setMainUid('', game, false)
2023-03-04 14:30:13 +08:00
}
2023-06-01 03:28:35 +08:00
this.save()
2023-03-04 14:30:13 +08:00
}
2023-06-01 03:28:35 +08:00
getGameDs (game = 'gs') {
game = this.gameKey(game)
if (!this._games[game]) {
this._games[game] = {
uid: '',
data: {}
}
}
return this._games[game]
2023-03-04 14:30:13 +08:00
}
2023-03-04 14:30:13 +08:00
/**
* 设置当前用户的绑定uid
* @param uid 要绑定的uid
2023-05-09 11:03:38 +08:00
* @param game
2023-03-04 14:30:13 +08:00
*/
2023-06-01 03:28:35 +08:00
autoRegUid (uid = '', game = 'gs') {
if (this.getUid(game)) {
2023-05-09 11:03:38 +08:00
return uid
}
2023-06-01 03:28:35 +08:00
this.addRegUid(uid, game)
2023-05-09 11:03:38 +08:00
return uid
}
// 切换绑定CK生效的UID
setMainUid (uid = '', game = 'gs', save = true) {
2023-06-01 03:28:35 +08:00
this._map = false
game = this.gameKey(game)
if (uid < 100 || !uid) {
let uids = this.getUidList(game)
uid = (uids?.[uid] || uids?.[0])?.uid || ''
}
if (!uid) {
return false
2023-03-04 14:30:13 +08:00
}
2023-06-01 03:28:35 +08:00
if (this.hasUid(uid, game)) {
let gameDs = this.getGameDs(game)
gameDs.uid = uid
}
if (save) {
this.save()
2023-03-04 14:30:13 +08:00
}
}
2023-03-04 14:30:13 +08:00
// 添加MysUser
2023-06-01 03:28:35 +08:00
async addMysUser (mysUser) {
this.mysUsers[mysUser.ltuid] = mysUser
2023-06-01 03:28:35 +08:00
this._map = false
MysUtil.eachGame((game) => {
let uid = mysUser.getUid(game)
2023-09-29 04:05:44 +08:00
if (uid && this.getUid(game) == '') {
this.setMainUid(uid, game, false)
2023-06-01 03:28:35 +08:00
}
})
this.save()
}
// 删除当前用户绑定CK
2023-05-11 06:08:03 +08:00
async delCk (ltuid = '') {
2023-06-01 03:28:35 +08:00
console.warn('delCk即将废弃')
2023-05-11 06:08:03 +08:00
return await this.delMysUser(ltuid)
}
2023-06-01 03:28:35 +08:00
async delMysUser (mysUser = '') {
let ltuid = mysUser.ltuid || mysUser
2023-05-11 06:08:03 +08:00
if (ltuid && this.mysUsers[ltuid]) {
let mys = this.mysUsers[ltuid]
2023-06-01 03:28:35 +08:00
this.mysUsers[ltuid] = false
this._map = false
2023-05-11 06:08:03 +08:00
await mys.del()
2023-03-04 14:30:13 +08:00
}
2023-06-01 03:28:35 +08:00
this._map = false
2023-05-11 06:08:03 +08:00
await this.save()
2023-03-04 14:30:13 +08:00
}
async eachMysUser (fn) {
await Data.forEach(this.mysUsers, async (mys, ltuid) => {
if (!mys) {
return true
}
return fn(mys, ltuid)
})
}
async eachAllMysUser (fn) {
return MysUser.forEach(fn)
}
2023-03-04 14:30:13 +08:00
/**
* 检查当前用户绑定的CK状态
*/
async checkCk () {
// TODO:待完善文案
let cks = this.cks
let ret = []
for (let ltuid in cks) {
let ck = cks[ltuid].ck
if (!ltuid || !ck) {
continue
}
let checkRet = await MysUser.checkCkStatus(ck)
// TODO: 若checkRet中返回了不同的uid进行CK保存更新
// 失效
let mysUser = await MysUser.create(ck)
if (mysUser) {
let status = checkRet.status
if (status === 0 || status === 1) {
// status为1时无法查询天赋但仍可查询角色保留CK
await mysUser.initCache()
} else if (status === 2) {
// status为2时无法查询角色删除ck cache
// 因仍能查询体力故保留ck记录不直接删除
await mysUser.del()
} else if (status === 3) {
// status为3时CK完全失效用户删除此CK
await this.delCk(ltuid)
}
}
ret.push({
ltuid,
...checkRet
})
}
return ret
}
}