Miao-Yunzai/src/mys/NoteUser.ts

506 lines
10 KiB
TypeScript
Raw Normal View History

2024-06-09 11:40:24 +08:00
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'
2024-06-11 17:41:11 +08:00
import { Data } from '../miao.js'
2024-06-09 01:00:07 +08:00
/**
2024-06-09 12:42:45 +08:00
* *******************
2024-06-09 01:00:07 +08:00
* Bot实际User用户类
* QQ
*
* User可以注册UID getRegUid / setRegUid
* User可以绑定多个MysUser CKMysUser
2024-06-09 12:42:45 +08:00
* *******************
2024-06-09 01:00:07 +08:00
*/
export default class NoteUser extends BaseModel {
2024-06-11 19:45:04 +08:00
db = null
qq = null
mysUsers = null
_map = null
2024-06-09 11:40:24 +08:00
/**
*
* @param qq
* @returns
*/
2024-06-09 01:00:25 +08:00
constructor(qq) {
2024-06-09 01:00:07 +08:00
super()
// 检查实例缓存
let cacheObj = this._getThis('user', qq)
if (cacheObj) {
return cacheObj
}
this.qq = qq
return this._cacheThis()
}
/**
* OLD Func {{
*/
2024-06-09 12:42:45 +08:00
/**
*
*/
2024-06-09 01:00:25 +08:00
get uid() {
2024-06-09 01:00:07 +08:00
console.warn('NoteUser.uid 默认返回原神UID可更改为 user.getUid(game)')
return this.getUid()
}
2024-06-09 12:42:45 +08:00
/**
* CK的UID列表CK则返回空数组
*/
2024-06-09 01:00:25 +08:00
get ckUids() {
console.warn(
'NoteUser.ckUids 默认返回原神UID可更改为 user.getCkUidList(game)'
)
2024-06-09 01:00:07 +08:00
let uids = this.getCkUidList('gs')
2024-06-09 01:00:25 +08:00
return lodash.map(uids, ds => ds.uid)
2024-06-09 01:00:07 +08:00
}
/**
* ck
* @returns { {ltuid:{ckData, ck, uids}} }
*/
2024-06-09 01:00:25 +08:00
get cks() {
2024-06-09 01:00:07 +08:00
console.warn('NoteUser.cks 即将废弃')
let game = 'gs'
let cks = {}
if (!this.hasCk) {
return cks
}
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)
}
}
}
return cks
}
/**
* End OLD Func }}
*/
// 当前用户是否具备CK
2024-06-09 01:00:25 +08:00
get hasCk() {
2024-06-09 01:00:07 +08:00
return !lodash.isEmpty(this.mysUsers)
}
/**
* NoteUser实例
* @param qq NoterUser对应idqq
* @param db
* * e对象则会识别e.user_iduser对象添加至e.user
* @param data MysCookie数据
* @returns {Promise<NoteUser|*>}
*/
2024-06-09 01:00:25 +08:00
static async create(qq, db = false) {
2024-06-09 01:00:07 +08:00
// 兼容处理传入e
if (qq && qq.user_id) {
let e = qq
let id = e.originalUserId || e.user_id
let mainId = await redis.get(`Yz:NoteUser:mainId:${e.user_id}`)
if (mainId) {
id = mainId
e.mainUserId = mainId
e.originalUserId = e.originalUserId || e.user_id
}
let user = await NoteUser.create(id)
e.user = user
return user
}
let user = new NoteUser(qq)
await user.initDB(db)
// 传入data则使用否则读取
return user
}
2024-06-09 11:40:24 +08:00
/**
*
* @param fn
*/
2024-06-09 01:00:25 +08:00
static async forEach(fn) {
2024-06-09 01:00:07 +08:00
let dbs = await UserDB.findAll()
2024-06-09 01:00:25 +08:00
await Data.forEach(dbs, async db => {
2024-06-09 01:00:07 +08:00
let user = await NoteUser.create(db.id, db)
return await fn(user)
})
}
2024-06-09 11:40:24 +08:00
/**
*
* @param db
* @returns
*/
2024-06-09 01:00:25 +08:00
async initDB(db = false) {
2024-06-09 01:00:07 +08:00
if (this.db && !db) {
return
}
if (db && db !== true) {
this.db = db
} else {
this.db = await UserDB.find(this.qq, 'qq')
}
await this.initMysUser()
this._games = this.db.games
await this.save()
}
2024-06-09 11:40:24 +08:00
/**
* MysUser对象
*/
2024-06-09 01:00:25 +08:00
async initMysUser() {
2024-06-09 01:00:07 +08:00
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
}
}
}
2024-06-09 11:40:24 +08:00
/**
*
*/
2024-06-09 01:00:25 +08:00
async save() {
2024-06-09 01:00:07 +08:00
await this.db.saveDB(this)
}
2024-06-09 11:40:24 +08:00
/**
*
* @param game
* @param type
* @returns
*/
2024-06-09 01:00:25 +08:00
getUidMapList(game = 'gs', type = 'all') {
2024-06-09 01:00:07 +08:00
if (this._map?.[game]?.[type]) {
return this._map[game][type]
}
game = this.gameKey(game)
let uidMap = {}
let uidList = []
2024-06-09 01:00:25 +08:00
lodash.forEach(this.mysUsers, mys => {
2024-06-09 01:00:07 +08:00
if (!mys) {
return
}
2024-06-09 01:00:25 +08:00
lodash.forEach(mys.uids[game] || [], uid => {
2024-06-09 01:00:07 +08:00
uid = uid + ''
if (uid && !uidMap[uid]) {
uidMap[uid] = mys.getUidData(uid, game)
uidList.push(uidMap[uid])
}
})
})
if (type === 'all') {
let gameDs = this.getGameDs(game)
2024-06-09 01:00:25 +08:00
lodash.forEach(gameDs.data, ds => {
2024-06-09 01:00:07 +08:00
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]
}
2024-06-09 11:40:24 +08:00
/**
*
* @param uid
* @param game
* @returns
*/
2024-06-09 01:00:25 +08:00
getUidData(uid = '', game = 'gs') {
2024-06-09 01:00:07 +08:00
if (!uid) {
uid = this.getUid(game)
}
return this.getUidMapList(game, 'all').map[uid]
}
2024-06-09 11:40:24 +08:00
/**
* Uid
* @param uid
* @param game
* @returns
*/
2024-06-09 01:00:25 +08:00
hasUid(uid = '', game = '') {
2024-06-09 01:00:07 +08:00
if (!uid) {
return this.getUidMapList(game, 'all').list?.length > 0
}
return !!this.getUidData(uid, game)
}
2024-06-09 11:40:24 +08:00
/**
* CK-Uid
* @param game
* @returns
*/
2024-06-09 01:00:25 +08:00
getCkUid(game = 'gs') {
2024-06-09 01:00:07 +08:00
let uid = this.getUid(game)
let { map, list } = this.getUidMapList(game, 'ck')
return (map[uid] ? uid : list[0]?.uid) || ''
}
2024-06-09 11:40:24 +08:00
/**
* CK-Uid列表
* @param game
* @returns
*/
2024-06-09 01:00:25 +08:00
getCkUidList(game = 'gs') {
2024-06-09 01:00:07 +08:00
return this.getUidMapList(game, 'ck').list
}
2024-06-09 11:40:24 +08:00
/**
* UID
* @param game
* @returns
*/
2024-06-09 01:00:25 +08:00
getUid(game = 'gs') {
2024-06-09 01:00:07 +08:00
game = this.gameKey(game)
// todo 刷新uid
let ds = this.getGameDs(game)
if (!ds.uid) {
this.setMainUid('', game)
}
return ds.uid || ''
}
2024-06-09 11:40:24 +08:00
/**
* UID列表
* @param game
* @returns
*/
2024-06-09 01:00:25 +08:00
getUidList(game = 'gs') {
2024-06-09 01:00:07 +08:00
return this.getUidMapList(game, 'all').list
}
2024-06-09 11:40:24 +08:00
/**
* MysUser对象
* @param game
* @returns
*/
2024-06-09 01:00:25 +08:00
getMysUser(game = 'gs') {
2024-06-09 01:00:07 +08:00
if (lodash.isEmpty(this.mysUsers)) {
return false
}
let uid = this.getCkUid(game)
if (!uid) {
return false
}
let uidData = this.getUidData(uid, game)
return this.mysUsers[uidData.ltuid]
}
2024-06-09 11:40:24 +08:00
/**
* UID
* @param uid
* @param game
* @param save
*/
2024-06-09 01:00:25 +08:00
addRegUid(uid, game = 'gs', save = true) {
2024-06-09 01:00:07 +08:00
game = this.gameKey(game)
uid = uid + ''
let gameDs = this.getGameDs(game)
gameDs.data[uid] = { uid, type: 'reg' }
this._map = false
this.setMainUid(uid, game, false)
if (save) {
this.save()
}
}
2024-06-09 11:40:24 +08:00
/**
* UID
* @param uid
* @param game
*/
2024-06-09 01:00:25 +08:00
delRegUid(uid, game = 'gs') {
2024-06-09 01:00:07 +08:00
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)
}
this.save()
}
_games = null
2024-06-09 11:40:24 +08:00
/**
*
* @param game
* @returns
*/
2024-06-09 01:00:25 +08:00
getGameDs(game = 'gs') {
2024-06-09 01:00:07 +08:00
game = this.gameKey(game)
if (!this._games[game]) {
this._games[game] = {
uid: '',
data: {}
}
}
return this._games[game]
}
/**
* uid
* @param uid uid
* @param game
*/
2024-06-09 01:00:25 +08:00
autoRegUid(uid = '', game = 'gs') {
2024-06-09 01:00:07 +08:00
if (this.getUid(game)) {
return uid
}
this.addRegUid(uid, game)
return uid
}
2024-06-09 11:40:24 +08:00
/**
* CK生效的UID
* @param uid
* @param game
* @param save
* @returns
*/
2024-06-09 01:00:25 +08:00
setMainUid(uid = '', game = 'gs', save = true) {
2024-06-09 01:00:07 +08:00
this._map = false
game = this.gameKey(game)
if (Number(uid) < 100 || !uid) {
2024-06-09 01:00:07 +08:00
let uids = this.getUidList(game)
uid = (uids?.[uid] || uids?.[0])?.uid || ''
}
if (!uid) {
return false
}
if (this.hasUid(uid, game)) {
let gameDs = this.getGameDs(game)
gameDs.uid = uid
}
if (save) {
this.save()
}
}
2024-06-09 11:40:24 +08:00
/**
* MysUser
* @param mysUser
*/
2024-06-09 01:00:25 +08:00
async addMysUser(mysUser) {
2024-06-09 01:00:07 +08:00
this.mysUsers[mysUser.ltuid] = mysUser
this._map = false
2024-06-09 01:00:25 +08:00
MysUtil.eachGame(game => {
2024-06-09 01:00:07 +08:00
let uid = mysUser.getUid(game)
if (uid && this.getUid(game) == '') {
this.setMainUid(uid, game, false)
}
})
this.save()
}
2024-06-09 11:40:24 +08:00
/**
* CK
* @param ltuid
* @returns
*/
2024-06-09 01:00:25 +08:00
async delCk(ltuid = '') {
2024-06-09 01:00:07 +08:00
console.warn('delCk即将废弃')
return await this.delMysUser(ltuid)
}
2024-06-09 11:40:24 +08:00
/**
*
* @param mysUser
*/
2024-06-09 20:02:18 +08:00
async delMysUser(mysUser: any = '') {
2024-06-09 01:00:07 +08:00
let ltuid = mysUser.ltuid || mysUser
if (ltuid && this.mysUsers[ltuid]) {
let mys = this.mysUsers[ltuid]
this.mysUsers[ltuid] = false
this._map = false
await mys.del()
}
this._map = false
await this.save()
}
2024-06-09 11:40:24 +08:00
/**
*
* @param fn
*/
2024-06-09 01:00:25 +08:00
async eachMysUser(fn) {
2024-06-09 01:00:07 +08:00
await Data.forEach(this.mysUsers, async (mys, ltuid) => {
if (!mys) {
return true
}
return fn(mys, ltuid)
})
}
2024-06-09 11:40:24 +08:00
/**
*
* @param fn
* @returns
*/
2024-06-09 01:00:25 +08:00
async eachAllMysUser(fn) {
2024-06-09 01:00:07 +08:00
return MysUser.forEach(fn)
}
/**
* CK状态
2024-06-09 11:40:24 +08:00
* @returns
2024-06-09 01:00:07 +08:00
*/
2024-06-09 01:00:25 +08:00
async checkCk() {
2024-06-09 01:00:07 +08:00
// 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)
2024-06-09 20:02:18 +08:00
if (mysUser && checkRet) {
2024-06-09 01:00:07 +08:00
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
}
}