2022-11-07 20:08:24 +00:00
|
|
|
import lodash from 'lodash'
|
2022-11-09 19:48:13 +00:00
|
|
|
import moment from 'moment'
|
2022-11-07 20:08:24 +00:00
|
|
|
|
|
|
|
export default class ProfileRank {
|
|
|
|
constructor (data) {
|
2022-11-09 19:48:13 +00:00
|
|
|
this.groupId = data.groupId || data.groupId
|
2022-11-07 20:08:24 +00:00
|
|
|
this.qq = data.qq
|
|
|
|
this.uid = data.uid + ''
|
|
|
|
}
|
|
|
|
|
|
|
|
static async create (data) {
|
|
|
|
return new ProfileRank(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
key (profile, type) {
|
2022-11-09 19:48:13 +00:00
|
|
|
return `miao:rank:${this.groupId}:${type}:${profile.id}`
|
2022-11-07 20:08:24 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 19:48:13 +00:00
|
|
|
/**
|
|
|
|
* 获取排行信息
|
|
|
|
* @param profile
|
|
|
|
* @param force
|
|
|
|
* @returns {Promise<{}|boolean>}
|
|
|
|
*/
|
2022-11-07 20:08:24 +00:00
|
|
|
async getRank (profile, force = false) {
|
2022-11-07 20:16:30 +00:00
|
|
|
if (!profile.hasData) {
|
|
|
|
return false
|
|
|
|
}
|
2022-11-08 22:04:08 +00:00
|
|
|
let ret = {}
|
|
|
|
const markKey = this.key(profile, 'mark')
|
|
|
|
let markRank = await redis.zRevRank(markKey, this.uid)
|
|
|
|
if (!lodash.isNumber(markRank) || force) {
|
2022-11-07 20:08:24 +00:00
|
|
|
let mark = profile.getArtisMark(false)
|
2022-11-10 20:56:15 +00:00
|
|
|
if (mark && mark._mark) {
|
2022-11-08 22:04:08 +00:00
|
|
|
await redis.zAdd(markKey, { score: mark._mark, value: this.uid })
|
|
|
|
markRank = await redis.zRevRank(markKey, this.uid)
|
2022-11-07 20:08:24 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-08 22:04:08 +00:00
|
|
|
if (lodash.isNumber(markRank)) {
|
|
|
|
let markCount = await redis.zCard(markKey)
|
|
|
|
ret.markRank = markRank + 1
|
|
|
|
ret.markCount = markCount
|
|
|
|
}
|
|
|
|
if (profile.hasDmg) {
|
|
|
|
const dmgKey = this.key(profile, 'dmg')
|
|
|
|
let dmgRank = await redis.zRevRank(dmgKey, this.uid)
|
|
|
|
if (!lodash.isNumber(dmgRank) || force) {
|
|
|
|
let dmg = await profile.calcDmg({ mode: 'single' })
|
2022-11-10 20:56:15 +00:00
|
|
|
if (dmg && dmg.avg) {
|
2022-11-08 22:04:08 +00:00
|
|
|
await redis.zAdd(dmgKey, { score: dmg.avg, value: this.uid })
|
|
|
|
dmgRank = await redis.zRevRank(dmgKey, this.uid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lodash.isNumber(dmgRank)) {
|
|
|
|
let dmgCount = await redis.zCard(dmgKey)
|
|
|
|
ret.dmgRank = dmgRank + 1
|
|
|
|
ret.dmgCount = dmgCount
|
2022-11-07 20:08:24 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-08 22:04:08 +00:00
|
|
|
if (lodash.isEmpty(ret)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (!ret.dmgRank || ret.markRank < ret.dmgRank) {
|
|
|
|
ret.rank = ret.markRank
|
|
|
|
ret.rankType = 'mark'
|
|
|
|
} else {
|
|
|
|
ret.rank = ret.dmgRank
|
|
|
|
ret.rankType = 'dmg'
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2022-11-09 19:48:13 +00:00
|
|
|
/**
|
|
|
|
* 获取群排行UID
|
|
|
|
* @param groupId
|
|
|
|
* @param charId
|
|
|
|
* @param type
|
|
|
|
* @returns {Promise<string|boolean>}
|
|
|
|
*/
|
2022-11-08 22:04:08 +00:00
|
|
|
static async getGroupMaxUid (groupId, charId, type = 'mark') {
|
2022-11-09 19:48:13 +00:00
|
|
|
let uids = await redis.zRange(`miao:rank:${groupId}:${type}:${charId}`, -1, -1)
|
|
|
|
return uids ? uids[0] : false
|
|
|
|
}
|
|
|
|
|
2022-11-10 20:56:15 +00:00
|
|
|
/**
|
|
|
|
* 获取排行榜
|
|
|
|
* @param groupId
|
|
|
|
* @param charId
|
|
|
|
* @param type
|
|
|
|
* @returns {Promise<ConvertArgumentType<ZMember, string>[]|boolean>}
|
|
|
|
*/
|
|
|
|
static async getGroupUidList (groupId, charId, type = 'mark') {
|
2022-11-11 21:57:13 +00:00
|
|
|
let uids = await redis.zRangeWithScores(`miao:rank:${groupId}:${type}:${charId}`, -15, -1)
|
2022-11-10 20:56:15 +00:00
|
|
|
return uids ? uids.reverse() : false
|
|
|
|
}
|
|
|
|
|
2022-11-09 19:48:13 +00:00
|
|
|
/**
|
|
|
|
* 重置群排行
|
|
|
|
* @param groupId
|
|
|
|
* @param charId
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
static async resetRank (groupId, charId = '') {
|
|
|
|
let keys = await redis.keys(`miao:rank:${groupId}:*`)
|
|
|
|
for (let key of keys) {
|
|
|
|
let charRet = /^miao:rank:\d+:(?:mark|dmg):(\d{8})$/.exec(key)
|
|
|
|
if (charRet) {
|
|
|
|
if (charId === '' || charId * 1 === charRet[1] * 1) {
|
|
|
|
await redis.del(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (charId === '') {
|
|
|
|
await redis.del(`miao:rank:${groupId}:cfg`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static async getGroupCfg (groupId) {
|
|
|
|
let ret = {
|
|
|
|
timestamp: (new Date()) * 1,
|
|
|
|
status: 0
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
let cfg = await redis.get(`miao:rank:${groupId}:cfg`)
|
|
|
|
if (!cfg) {
|
|
|
|
await redis.set(`miao:rank:${groupId}:cfg`, JSON.stringify(ret), { EX: 3600 * 24 * 365 })
|
|
|
|
} else {
|
|
|
|
ret = JSON.parse(cfg)
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
ret.time = moment(new Date(ret.timestamp)).format('MM-DD HH:mm')
|
|
|
|
return ret
|
2022-11-07 20:08:24 +00:00
|
|
|
}
|
|
|
|
}
|