From b56223e8eaf70d524e34e03fb0364ada25640611 Mon Sep 17 00:00:00 2001 From: Kokomi <102026640+yoimiya-kokomi@users.noreply.github.com> Date: Sat, 3 Jun 2023 20:31:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=87=8D=E7=BD=AE=E6=8E=92?= =?UTF-8?q?=E5=90=8D=E7=9A=84=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/profile/ProfileRank.js | 37 ++++++++--------- components/Common.js | 82 +------------------------------------ models/ProfileRank.js | 77 +++++++++++++++++++++++++++++++++- 3 files changed, 94 insertions(+), 102 deletions(-) diff --git a/apps/profile/ProfileRank.js b/apps/profile/ProfileRank.js index 6956f94f..352f426e 100644 --- a/apps/profile/ProfileRank.js +++ b/apps/profile/ProfileRank.js @@ -1,6 +1,6 @@ -import { Character, ProfileRank, ProfileDmg, Player } from '#miao.models' import ProfileDetail from './ProfileDetail.js' import { Data, Common, Format } from '#miao' +import { Character, ProfileRank, ProfileDmg, Player } from '#miao.models' import lodash from 'lodash' export async function groupRank (e) { @@ -113,7 +113,7 @@ export async function resetRank (e) { * @returns {Promise} */ export async function refreshRank (e) { - let groupId = e.group_id + let groupId = e.group_id || '' if (!groupId) { return true } @@ -124,25 +124,24 @@ export async function refreshRank (e) { e.reply('面板数据刷新中,等待时间可能较长,请耐心等待...') let game = e.isSr ? 'sr' : 'gs' await ProfileRank.resetRank(groupId) - let groupUids = await Common.getGroupUids(e, game) + let uidMap = await ProfileRank.getUserUidMap(e, game) let count = 0 - for (let qq in groupUids) { - for (let { uid, type } of groupUids[qq]) { - let player = new Player(uid, game) - let profiles = player.getProfiles() - // 刷新rankLimit - await ProfileRank.setUidInfo({ uid, profiles, qq, uidType: type }) - let rank = await ProfileRank.create({ groupId, uid, qq }) - for (let id in profiles) { - let profile = profiles[id] - if (!profile.hasData) { - continue - } - await rank.getRank(profile, true) - } - if (rank.allowRank) { - count++ + for (let uid in uidMap) { + let { qq, type } = uidMap[uid] + let player = new Player(uid, game) + let profiles = player.getProfiles() + // 刷新rankLimit + await ProfileRank.setUidInfo({ uid, profiles, qq, uidType: type }) + let rank = await ProfileRank.create({ groupId, uid, qq }) + for (let id in profiles) { + let profile = profiles[id] + if (!profile.hasData) { + continue } + await rank.getRank(profile, true) + } + if (rank.allowRank) { + count++ } } e.reply(`本群排名已刷新,共刷新${count}个UID数据...`) diff --git a/components/Common.js b/components/Common.js index f11fa4f6..23efc80c 100644 --- a/components/Common.js +++ b/components/Common.js @@ -1,7 +1,5 @@ import Cfg from './Cfg.js' import Render from './common/Render.js' -import { Version } from './index.js' -import lodash from 'lodash' const Common = { render: Render.render, @@ -12,86 +10,8 @@ const Common = { async downFile () { console.log('down file') - }, - - async getNoteQQUids (e, game='gs') { - let ret = {} - if (Version.isV3) { - if (e.runtime) { - let noteCks = await e.runtime?.gsCfg?.getBingCk(game) || {} - lodash.forEach(noteCks.ck, (ck, _qq) => { - let qq = ck.qq || _qq - let uid = ck.uid - if (qq && uid) { - ret[qq] = ret[qq] || [] - if (!ret[qq].includes(uid)) { - ret[qq].push(uid) - } - } - }) - } - } else { - lodash.forEach(global.NoteCookie || {}, (ck) => { - const { qq, uid } = ck - if (qq && uid) { - ret[qq] = ret[qq] || [] - ret[qq].push(uid) - } - }) - } - return ret - }, - - async getBindUid (qq, runtime, game = 'gs') { - if (Version.isMiao && runtime.NoteUser) { - let user = await runtime.NoteUser.create(qq) - return user ? user.getUid(game) : false - } - if (Version.isV3) { - return await redis.get(`Yz:genshin:mys:qq-uid:${qq}`) - } else { - return await redis.get(`genshin:id-uid:${qq}`) - } - }, - - async getGroupUids (e, game = 'gs') { - // 获取ck用户列表 - let noteUids = await Common.getNoteQQUids(e, game) - let ret = {} - let uidMap = {} - - let groupMemMap = await e.group?.getMemberMap() - // 优先匹配ck uid - for (let [qq] of groupMemMap) { - if (noteUids[qq]) { - for (let uid of noteUids[qq]) { - ret[qq] = ret[qq] || [] - if (!uidMap[uid]) { - ret[qq].push({ - uid, - type: 'ck' - }) - uidMap[uid] = qq - } - } - } - } - // 获取绑定uid - for (let [qq] of groupMemMap) { - if (ret[qq]) { - continue - } - let uid = await Common.getBindUid(qq, e.runtime, game) - if (uid && !uidMap[uid]) { - ret[qq] = [{ - uid, - type: 'bind' - }] - uidMap[uid] = qq - } - } - return ret } + } export default Common diff --git a/models/ProfileRank.js b/models/ProfileRank.js index a6795353..6a49d9b7 100644 --- a/models/ProfileRank.js +++ b/models/ProfileRank.js @@ -1,6 +1,6 @@ import lodash from 'lodash' import moment from 'moment' -import { Cfg, Common, Data } from '#miao' +import { Cfg, Common, Data, Version } from '#miao' export default class ProfileRank { constructor (data) { @@ -69,7 +69,7 @@ export default class ProfileRank { * @param charId * @returns {Promise} */ - static async resetRank (groupId, groupMemList, charId = '') { + 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|crit|valid):(\d{8})$/.exec(key) @@ -188,6 +188,77 @@ export default class ProfileRank { return false } + static async getUserUidMap (e, game = 'gs') { + let rn = e.runtime + let groupMemMap = await e.group?.getMemberMap() || [] + let users = {} + for (let [qq] of groupMemMap) { + users[qq] = true + } + + let uidMap = {} + let qqMap = {} + let add = (qq, uid, type) => { + if (!uidMap || type === 'ck') { + uidMap[uid] = { uid, qq, type: type === 'ck' ? 'ck' : 'bind' } + } + qqMap[qq] = true + } + + let keys = await redis.keys('miao:rank:uid-info:*') + for (let key of keys) { + let data = await Data.redisGet(key) + let { qq, uidType } = data + if (!users[qq]) continue + let uidRet = /miao:rank:uid-info:(\d{9})/.exec(key) + if (qq && uidType && uidRet?.[1]) { + add(qq, uidRet[1], uidType === 'ck' ? 'ck' : 'bind') + } + } + + if (rn.NoteUser) { + // Miao-Yunzai + await rn.NoteUser.forEach(async (user) => { + if (!users[user.qq]) return true + let uids = user.getUidList(game) + lodash.forEach(uids, (ds) => { + let { uid, type } = ds + add(user.qq, uid, type) + }) + }) + } else if (Version.isV3) { + if (rn?.gsCfg?.getBingCk) { + // Yunzai-V3 + let noteCks = await rn.gsCfg.getBingCk(game) || {} + lodash.forEach(noteCks.ck, (ck, _qq) => { + let qq = ck.qq || _qq + let uid = ck.uid + if (!users[qq]) return true + add(qq, uid, 'ck') + }) + } + } else { + // V2 + lodash.forEach(global.NoteCookie || {}, (ck) => { + const { qq, uid } = ck + if (!users[qq]) return true + if (qq && uid) { + add(qq, uid, 'ck') + } + }) + } + + for (let qq in users) { + if (qqMap[qq]) continue + let uid = await redis.get(Version.isV3 ? `Yz:genshin:mys:qq-uid:${qq}` : `genshin:id-uid:${qq}`) + if (uid) { + add(qq, uid, 'bind') + } + } + + return uidMap + } + /** * 1: '无限制', * 2: '绑定有CK的用户', @@ -347,4 +418,6 @@ export default class ProfileRank { } return false } + + }