From 341fe15972d32bf4832f73e400e249f7f2a91d1d Mon Sep 17 00:00:00 2001 From: Kokomi <102026640+yoimiya-kokomi@users.noreply.github.com> Date: Wed, 16 Nov 2022 04:07:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=91=BD=E4=BB=A4`#=E5=88=B7?= =?UTF-8?q?=E6=96=B0=E6=8E=92=E5=90=8D`=EF=BC=8C=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E7=BE=A4=E6=88=90=E5=91=98=E7=9A=84=E9=9D=A2?= =?UTF-8?q?=E6=9D=BF=E4=BF=A1=E6=81=AF=E8=BF=9B=E8=A1=8C=E9=87=8D=E6=8E=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/character/ProfileList.js | 2 +- apps/character/ProfileRank.js | 53 +++++++++++++++--- apps/profile.js | 7 ++- components/Common.js | 90 ++++++++++++++++++++++++++++--- config/system/character_system.js | 2 +- models/ProfileRank.js | 55 ++++++------------- 6 files changed, 155 insertions(+), 54 deletions(-) diff --git a/apps/character/ProfileList.js b/apps/character/ProfileList.js index 8c74b9a0..398e4ba5 100644 --- a/apps/character/ProfileList.js +++ b/apps/character/ProfileList.js @@ -29,7 +29,7 @@ export async function profileList (e) { // 获取面板数据 let profiles = Profile.getAll(uid) // 检测标志位 - await ProfileRank.setRankLimit(uid, profiles, isSelfUid) + await ProfileRank.setRankUidInfo({ uid, profiles, qq: e.user_id, uidType: isSelfUid ? 'ck' : 'bind' }) let groupId = e.group_id if (groupId) { diff --git a/apps/character/ProfileRank.js b/apps/character/ProfileRank.js index f219b89e..5a1c3c54 100644 --- a/apps/character/ProfileRank.js +++ b/apps/character/ProfileRank.js @@ -68,12 +68,6 @@ export async function resetRank (e) { let name = msg.replace(/(#|重置|重设|排名|排行|群|群内|面板|详情|面版)/g, '').trim() let charId = '' let charName = '全部角色' - let groupMemList = [] - let groupMemMap = await e.group.getMemberMap() - groupMemMap.forEach((v, k) => { - groupMemList.push(k) - }); - if (name) { let char = Character.get(name) if (!char) { @@ -83,10 +77,55 @@ export async function resetRank (e) { charId = char.id charName = char.name } - await ProfileRank.resetRank(groupId, groupMemList, charId) + await ProfileRank.resetRank(groupId, charId) e.reply(`本群${charName}排名已重置...`) } +const getUid = async function (qq) { + let uidReg = /[1-9][0-9]{8}/ + let nCookie = global.NoteCookie || false + if (nCookie && nCookie[qq]) { + let nc = nCookie[qq] + if (nc.uid && uidReg.test(nc.uid)) { + return nc.uid + } + } + let uid = await redis.get(`Yz:genshin:mys:qq-uid:${qq}`) + if (uid && uidReg.test(uid)) { + return uid + } +} + +export async function refreshRank (e) { + let groupId = e.group_id + if (!groupId) { + return true + } + if (!e.isMaster) { + e.reply('只有管理员可刷新排名...') + return true + } + let groupUids = await Common.getGroupUids(e) + let count = 0 + for (let qq in groupUids) { + for (let { uid, type } of groupUids[qq]) { + let profiles = Profile.getAll(uid) + // 刷新rankLimit + await ProfileRank.setRankUidInfo({ 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) + } + count++ + } + } + e.reply(`本群排名已刷新,共刷新${count}个UID数据...`) +} + async function renderCharRankList ({ e, uids, char, mode, groupId }) { let list = [] diff --git a/apps/profile.js b/apps/profile.js index 2ade574a..650184eb 100644 --- a/apps/profile.js +++ b/apps/profile.js @@ -6,7 +6,7 @@ import { renderProfile } from './character/ProfileDetail.js' import { profileStat } from './character/ProfileStat.js' import { profileList } from './character/ProfileList.js' import { enemyLv } from './character/ProfileUtils.js' -import { groupRank, resetRank } from './character/ProfileRank.js' +import { groupRank, resetRank, refreshRank } from './character/ProfileRank.js' let app = App.init({ id: 'profile', @@ -27,6 +27,11 @@ app.reg('reset-rank', resetRank, { name: '重置排名' }) +app.reg('refresh-rank', refreshRank, { + rule: /^#(刷新)(全部)?(排名|排行)$/, + name: '重置排名' +}) + app.reg('rank-list', groupRank, { rule: /^#(群|群内)?.+(排名|排行|列表)(列表|榜)?$/, name: '面板排名榜' diff --git a/components/Common.js b/components/Common.js index aa3a6c14..6a879ea9 100644 --- a/components/Common.js +++ b/components/Common.js @@ -1,12 +1,90 @@ import Cfg from './Cfg.js' import render from './common-lib/render.js' +import { Version } from './index.js' +import lodash from 'lodash' -function sleep (ms) { - return new Promise((resolve) => setTimeout(resolve, ms)) -} - -export default { +const Common = { render, cfg: Cfg.get, - sleep + sleep (ms) { + return new Promise((resolve) => setTimeout(resolve, ms)) + }, + + async getNoteQQUids (e) { + let ret = {} + if (Version.isV3) { + if (e.runtime) { + let noteCks = await e.runtime?.gsCfg?.getBingCk() || {} + lodash.forEach(noteCks, (cks, qq) => { + lodash.forEach(cks, (ck) => { + let { qq, uid } = ck + 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) { + 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) { + // 获取ck用户列表 + let noteUids = await Common.getNoteQQUids(e) + 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) + if (uid && !uidMap[uid]) { + ret[qq] = { + uid, + type: 'bind' + } + uidMap[uid] = qq + } + } + return ret + } } + +export default Common diff --git a/config/system/character_system.js b/config/system/character_system.js index 4ed6c7e5..e1a64055 100644 --- a/config/system/character_system.js +++ b/config/system/character_system.js @@ -68,7 +68,7 @@ export const characters = { 10000072: ['坎蒂丝', 'Candace', '坎迪斯'], 10000073: ['纳西妲', 'Nahida', '草神', '小吉祥', '大吉祥', '小草神', '大慈树王', '小吉祥草王', '草萝莉', '羽毛球', '摩诃善法大吉祥智慧主', '智慧主', '智慧之神', '布耶尔'], 10000074: ['莱依拉', 'Layla', '莱依菈', '来依菈', '来依拉'], - 10000075: ['流浪者', 'Wanderer', '散兵', '国崩', '雷电国崩', '大炮', '雷电大炮', '雷大炮', '伞兵', '斯卡拉姆齐', '七叶寂照秘密主', '正机之神'], + 10000075: ['流浪者', 'Wanderer', '散兵', '国崩', '雷电国崩', '大炮', '雷电大炮', '雷大炮', '伞兵', '斯卡拉姆齐', '七叶寂照秘密主'], 10000076: ['珐露珊', 'Faruzan', '法露珊', '法璐珊', '法露姗', '法璐姗', '珐露姗', '珐璐姗'], // 自定义角色 diff --git a/models/ProfileRank.js b/models/ProfileRank.js index 5182a7ac..e0b753af 100644 --- a/models/ProfileRank.js +++ b/models/ProfileRank.js @@ -1,6 +1,6 @@ import lodash from 'lodash' import moment from 'moment' -import { Common, Profile } from '../components/index.js' +import { Common } from '../components/index.js' export default class ProfileRank { constructor (data) { @@ -149,36 +149,6 @@ export default class ProfileRank { if (charId === '') { await redis.del(`miao:rank:${groupId}:cfg`) } - - let getUid = async function (qq) { - let uidReg = /[1-9][0-9]{8}/ - let nCookie = global.NoteCookie || false - if (nCookie && nCookie[qq]) { - let nc = nCookie[qq] - if (nc.uid && uidReg.test(nc.uid)) { - return nc.uid - } - } - let uid = await redis.get(`Yz:genshin:mys:qq-uid:${qq}`) - if (uid && uidReg.test(uid)) { - return uid - } - } - - for (let qq of groupMemList) { - let uid = await getUid(qq) - if (!uid) { continue } - let rankObj = await ProfileRank.create({ groupId, uid, qq}) - if (charId === ''){ - await Profile.forEach(uid, async function (profile) { - await rankObj.getRank(profile, true) - }); - } - else { - let profile = Profile.get(uid, charId) - await rankObj.getRank(profile, true) - } - } } static async getGroupCfg (groupId) { @@ -208,7 +178,7 @@ export default class ProfileRank { return ret } - static async setRankLimit (uid, profiles, isSelfUid = false) { + static async setRankUidInfo ({ uid, qq, profiles, uidType = 'bind' }) { if (!uid) { return false } @@ -233,11 +203,20 @@ export default class ProfileRank { } catch (e) { data = {} } - await redis.set(`miao:rank:uid-info:${uid}`, JSON.stringify({ - totalCount, - basicCount, - isSelfUid: !!(isSelfUid || data?.isSelfUid) - }), { EX: 3600 * 24 * 365 }) + data.totalCount = totalCount + data.basicCount = basicCount + if (data.isSelfUid) { + delete data.isSelfUid + data.uidType = 'ck' + } + if (uidType === 'ck') { + data.qq = qq || data.qq || '' + data.uidType = 'ck' + } else { + data.qq = data.qq || qq || '' + data.uidType = data.uidType || 'bind' + } + await redis.set(`miao:rank:uid-info:${uid}`, JSON.stringify(data), { EX: 3600 * 24 * 365 }) } /** @@ -260,7 +239,7 @@ export default class ProfileRank { } let data = await redis.get(`miao:rank:uid-info:${uid}`) data = JSON.parse(data) - if (data.isSelfUid) { + if (data.isSelfUid || data.uidType === 'ck') { return true } if (rankLimit === 2) {