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-13 19:42:24 +00:00
|
|
|
|
import { Common } from '../components/index.js'
|
2022-11-07 20:08:24 +00:00
|
|
|
|
|
|
|
|
|
export default class ProfileRank {
|
|
|
|
|
constructor (data) {
|
2022-11-13 19:42:24 +00:00
|
|
|
|
this.groupId = data.groupId || data.groupId || ''
|
|
|
|
|
if (!this.groupId || this.groupId === 'undefined') {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2022-11-07 20:08:24 +00:00
|
|
|
|
this.qq = data.qq
|
|
|
|
|
this.uid = data.uid + ''
|
2022-11-13 19:42:24 +00:00
|
|
|
|
this.allowRank = false
|
2022-11-07 20:08:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async create (data) {
|
2022-11-13 19:42:24 +00:00
|
|
|
|
let rank = new ProfileRank(data)
|
|
|
|
|
rank.allowRank = await ProfileRank.checkRankLimit(rank.uid)
|
|
|
|
|
return rank
|
2022-11-07 20:08:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-13 19:42:24 +00:00
|
|
|
|
if (!this.groupId || !this.allowRank || !profile.hasData) {
|
2022-11-07 20:16:30 +00:00
|
|
|
|
return false
|
|
|
|
|
}
|
2022-11-08 22:04:08 +00:00
|
|
|
|
let ret = {}
|
2022-11-12 21:35:46 +00:00
|
|
|
|
for (let typeKey of ['mark', 'dmg']) {
|
|
|
|
|
let typeRank = await this.getTypeRank(profile, typeKey, force)
|
|
|
|
|
ret[typeKey] = typeRank
|
2022-11-13 03:52:49 +00:00
|
|
|
|
if (!ret.rank || ret.rank >= typeRank.rank) {
|
2022-11-12 21:35:46 +00:00
|
|
|
|
ret.rank = typeRank.rank
|
|
|
|
|
ret.rankType = typeKey
|
2022-11-07 20:08:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-12 21:35:46 +00:00
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getTypeRank (profile, type, force) {
|
|
|
|
|
if (!profile.hasData || !type) {
|
|
|
|
|
return false
|
2022-11-08 22:04:08 +00:00
|
|
|
|
}
|
2022-11-12 21:35:46 +00:00
|
|
|
|
if (type === 'dmg' && !profile.hasDmg) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
const typeKey = this.key(profile, type)
|
|
|
|
|
let value
|
|
|
|
|
let rank
|
|
|
|
|
if (force) {
|
|
|
|
|
value = await this.getTypeValue(profile, type)
|
|
|
|
|
} else {
|
|
|
|
|
rank = await redis.zRevRank(typeKey, this.uid)
|
|
|
|
|
if (!lodash.isNumber(rank)) {
|
|
|
|
|
value = await this.getTypeValue(profile, type)
|
2022-11-08 22:04:08 +00:00
|
|
|
|
}
|
2022-11-12 21:35:46 +00:00
|
|
|
|
}
|
2022-11-13 19:42:24 +00:00
|
|
|
|
if (value && !lodash.isUndefined(value.score)) {
|
2022-11-12 21:35:46 +00:00
|
|
|
|
await redis.zAdd(typeKey, { score: value.score, value: this.uid })
|
|
|
|
|
}
|
|
|
|
|
if (!lodash.isNumber(rank)) {
|
|
|
|
|
rank = await redis.zRevRank(typeKey, this.uid)
|
|
|
|
|
}
|
|
|
|
|
if (force) {
|
|
|
|
|
return {
|
|
|
|
|
rank: rank + 1,
|
|
|
|
|
value: value.score,
|
|
|
|
|
data: value.data
|
2022-11-07 20:08:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-12 21:35:46 +00:00
|
|
|
|
return {
|
|
|
|
|
rank: rank + 1
|
2022-11-08 22:04:08 +00:00
|
|
|
|
}
|
2022-11-12 21:35:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getTypeValue (profile, type) {
|
|
|
|
|
if (type === 'mark') {
|
|
|
|
|
let mark = profile.getArtisMark(false)
|
|
|
|
|
if (mark && mark._mark) {
|
|
|
|
|
return {
|
|
|
|
|
score: mark._mark * 1,
|
|
|
|
|
data: mark
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-08 22:04:08 +00:00
|
|
|
|
}
|
2022-11-12 21:35:46 +00:00
|
|
|
|
if (type === 'dmg' && profile.hasDmg) {
|
|
|
|
|
let dmg = await profile.calcDmg({ mode: 'single' })
|
|
|
|
|
if (dmg && dmg.avg) {
|
|
|
|
|
return {
|
|
|
|
|
score: dmg.avg,
|
|
|
|
|
data: dmg
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
2022-11-08 22:04:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
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) {
|
2022-11-13 19:42:24 +00:00
|
|
|
|
const rankLimitTxt = {
|
|
|
|
|
1: '无限制',
|
2022-11-13 20:00:56 +00:00
|
|
|
|
2: '面板列表中至少有16个角色的数据',
|
|
|
|
|
3: '面板列表中有 安柏&凯亚&丽莎 的数据',
|
|
|
|
|
4: '面板列表至少有16个角色数据,且包含安柏&凯亚&丽莎'
|
2022-11-13 19:42:24 +00:00
|
|
|
|
}
|
|
|
|
|
let rankLimit = Common.cfg('groupRankLimit') * 1 || 1
|
2022-11-09 19:48:13 +00:00
|
|
|
|
let ret = {
|
|
|
|
|
timestamp: (new Date()) * 1,
|
2022-11-13 19:42:24 +00:00
|
|
|
|
limitTxt: rankLimitTxt[rankLimit],
|
2022-11-09 19:48:13 +00:00
|
|
|
|
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
|
|
|
|
}
|
2022-11-13 19:42:24 +00:00
|
|
|
|
|
|
|
|
|
static async setRankLimit (uid, profiles) {
|
|
|
|
|
if (!uid) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
let basicCount = 0
|
|
|
|
|
let totalCount = 0
|
|
|
|
|
for (let charId in profiles) {
|
|
|
|
|
let profile = profiles[charId]
|
|
|
|
|
if (!profile || !profile.hasData) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if (['安柏', '凯亚', '丽莎'].includes(profile.name)) {
|
|
|
|
|
basicCount++
|
|
|
|
|
}
|
|
|
|
|
totalCount++
|
|
|
|
|
}
|
|
|
|
|
await redis.set(`miao:rank:uid-info:${uid}`, JSON.stringify({
|
|
|
|
|
totalCount,
|
|
|
|
|
basicCount
|
|
|
|
|
}), { EX: 3600 * 24 * 365 })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async checkRankLimit (uid) {
|
|
|
|
|
if (!uid) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
let rankLimit = Common.cfg('groupRankLimit') * 1 || 1
|
|
|
|
|
if (rankLimit === 1) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
let data = await redis.get(`miao:rank:uid-info:${uid}`)
|
|
|
|
|
data = JSON.parse(data)
|
2022-11-13 20:00:56 +00:00
|
|
|
|
if ((data.totalCount || 0) < 16 && [2, 4].includes(rankLimit)) {
|
2022-11-13 19:42:24 +00:00
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if ((data.basicCount || 0) < 3 && [3, 4].includes(rankLimit)) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-07 20:08:24 +00:00
|
|
|
|
}
|