From ddf24f60047d6fac35f9ba07aefc3421b2eb0fdc Mon Sep 17 00:00:00 2001 From: kissnavel Date: Sun, 30 Nov 2025 23:47:17 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=9B=BD=E9=99=85=E6=9C=8D=E6=8A=BD?= =?UTF-8?q?=E5=8D=A1=E9=93=BE=E6=8E=A5=E4=B8=8E=E6=8C=87=E4=BB=A4=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/gachalog.js | 4 +- lib/gacha.js | 191 ++++++++++++++++++++++++++++++----------------- 2 files changed, 125 insertions(+), 70 deletions(-) diff --git a/apps/gachalog.js b/apps/gachalog.js index 64157b0..edd837d 100644 --- a/apps/gachalog.js +++ b/apps/gachalog.js @@ -3,7 +3,7 @@ import { getAuthKey } from '../lib/authkey.js'; import settings from '../lib/settings.js'; import _ from 'lodash'; import common from '../../../lib/common/common.js'; -import { anaylizeGachaLog, updateGachaLog } from '../lib/gacha.js'; +import { anaylizeGachaLog, updateGachaLog, updateGachaLog_os } from '../lib/gacha.js'; import { getZZZGachaLink, getZZZGachaLogByAuthkey } from '../lib/gacha/core.js'; import { gacha_type_meta_data } from '../lib/gacha/const.js'; import { getQueryVariable } from '../utils/network.js'; @@ -127,7 +127,7 @@ export class GachaLog extends ZZZPlugin { if (/^(1[0-9])[0-9]{8}/i.test(uid)) { const { api, deviceFp } = await this.getAPI(); this.reply('抽卡记录获取中请稍等...可能需要一段时间,请耐心等待'); - const { data, count } = await updateGachaLog(api, uid, deviceFp); + const { data, count } = await updateGachaLog_os(api, uid, deviceFp); let msg = []; msg.push(`抽卡记录更新成功,共${Object.keys(data).length}个卡池`); for (const name in data) { diff --git a/lib/gacha.js b/lib/gacha.js index b23dd7f..8c72344 100644 --- a/lib/gacha.js +++ b/lib/gacha.js @@ -38,8 +38,7 @@ export const updateGachaLog = async (authKey, uid, region, game_biz) => { // 新的抽卡数据 let newCount = {}; // 遍历所有池子 - let gacha_type = /^(1[0-9])[0-9]{8}/i.test(uid) ? gacha_type_meta_data_os : gacha_type_meta_data - for (const name in gacha_type) { + for (const name in gacha_type_meta_data) { if (!previousLog[name]) { // 如果没有数据,初始化为空数组 previousLog[name] = []; @@ -56,75 +55,131 @@ export const updateGachaLog = async (authKey, uid, region, game_biz) => { // 新数据 const newData = []; // 遍历当前池子的所有类型 - for (const type of gacha_type[name]) { + for (const type of gacha_type_meta_data[name]) { // 循环获取数据 queryLabel: while (true) { - if (/^(1[0-9])[0-9]{8}/i.test(uid)) { - // 获取抽卡记录 - const log = await authKey.getFinalData('zzzGacha_Record', { - deviceFp: region, - type, - endId, - }); - if (!log || !log?.gacha_item_list || log?.gacha_item_list?.length === 0) { - break; - } - // 遍历数据 (从最新的开始) - for (let item of log.gacha_item_list) { - item = { - uid: uid, - gacha_id: '0', - gacha_type: '2', - item_id: item.item_id, - count: '1', - time: `${item.date.year}-${item.date.month.toString().padStart(2, '0')}-${item.date.day.toString().padStart(2, '0')} ${item.date.hour.toString().padStart(2, '0')}:${item.date.minute.toString().padStart(2, '0')}:${item.date.second.toString().padStart(2, '0')}`, - name: item.item_name, - lang: 'zh-cn', - item_type: item_type_os[item.item_type], - rank_type: rarity_os[item.rarity], - id: item.id, - square_icon: '', - }; - if (lastSaved && lastSaved.equals(item)) { - // 如果数据相同,说明已经获取完毕 - break queryLabel; - } - // 添加到新数据中 - newData.push(item); - // 当前池子新数据计数加一 - newCount[name]++; - } - // 更新页数和最后一个数据的 id - endId = log.gacha_item_list[log.gacha_item_list.length - 1]?.id || endId; - } else { - // 获取抽卡记录 - const log = await getZZZGachaLogByAuthkey( - authKey, - type, - type[0], - page, - endId, - region, - game_biz - ); - if (!log || !log?.list || log?.list?.length === 0) { - break; - } - // 遍历数据 (从最新的开始) - for (const item of log.list) { - if (lastSaved && lastSaved.equals(item)) { - // 如果数据相同,说明已经获取完毕 - break queryLabel; - } - // 添加到新数据中 - newData.push(item); - // 当前池子新数据计数加一 - newCount[name]++; - } - // 更新页数和最后一个数据的 id - endId = log.list[log.list.length - 1]?.id || endId; - page++; + // 获取抽卡记录 + const log = await getZZZGachaLogByAuthkey( + authKey, + type, + type[0], + page, + endId, + region, + game_biz + ); + if (!log || !log?.list || log?.list?.length === 0) { + break; } + // 遍历数据 (从最新的开始) + for (const item of log.list) { + if (lastSaved && lastSaved.equals(item)) { + // 如果数据相同,说明已经获取完毕 + break queryLabel; + } + // 添加到新数据中 + newData.push(item); + // 当前池子新数据计数加一 + newCount[name]++; + } + // 更新页数和最后一个数据的 id + endId = log.list[log.list.length - 1]?.id || endId; + page++; + // 防止请求过快 + await sleep(1000); + } + } + // 合并新数据和之前的数据 + previousLog[name] = [...newData, ...previousLog[name]]; + } + // 保存数据到文件 + saveGachaLog(uid, previousLog); + // 返回数据 + return { + data: previousLog, + count: newCount, + }; +}; + +/** + * 更新抽卡数据 + * @param {string} api 米游社api + * @param {string} uid ZZZUID + * @param {string} deviceFp 米游社指纹 + * @returns {Promise<{ + * data: { + * [x: string]: SingleGachaLog[]; + * }, + * count: { + * [x: string]: number; + * } + * }>} 更新后的抽卡数据 + */ +export const updateGachaLog_os = async (api, uid, deviceFp) => { + // 获取之前的抽卡数据 + let previousLog = getGachaLog(uid); + if (!previousLog) { + // 如果没有数据,初始化为空对象 + previousLog = {}; + } + // 新的抽卡数据 + let newCount = {}; + // 遍历所有池子 + for (const name in gacha_type_meta_data_os) { + if (!previousLog[name]) { + // 如果没有数据,初始化为空数组 + previousLog[name] = []; + } + // 初始化新数据计数(当前池子) + newCount[name] = 0; + // 转换为 SingleGachaLog 对象 + previousLog[name] = previousLog[name].map(i => new SingleGachaLog(i)); + // 获取上一次保存的数据 + const lastSaved = previousLog[name]?.[0]; + // 初始化最后一个数据的 id + let endId = '0'; + // 新数据 + const newData = []; + // 遍历当前池子的所有类型 + for (const type of gacha_type_meta_data_os[name]) { + // 循环获取数据 + queryLabel: while (true) { + // 获取抽卡记录 + const log = await api.getFinalData('zzzGacha_Record', { + deviceFp, + type, + endId, + }); + if (!log || !log?.gacha_item_list || log?.gacha_item_list?.length === 0) { + break; + } + // 遍历数据 (从最新的开始) + for (let item of log.gacha_item_list) { + item = { + uid: uid, + gacha_id: '0', + gacha_type: '2', + item_id: item.item_id, + count: '1', + time: `${item.date.year}-${item.date.month.toString().padStart(2, '0')}-${item.date.day.toString().padStart(2, '0')} ${item.date.hour.toString().padStart(2, '0')}:${item.date.minute.toString().padStart(2, '0')}:${item.date.second.toString().padStart(2, '0')}`, + name: item.item_name, + lang: 'zh-cn', + item_type: item_type_os[item.item_type], + rank_type: rarity_os[item.rarity], + id: item.id, + square_icon: '', + }; + if (lastSaved && lastSaved.equals(item)) { + // 如果数据相同,说明已经获取完毕 + break queryLabel; + } + // 添加到新数据中 + newData.push(item); + // 当前池子新数据计数加一 + newCount[name]++; + } + // 更新页数和最后一个数据的 id + endId = log.gacha_item_list[log.gacha_item_list.length - 1]?.id || endId; // 防止请求过快 await sleep(1000); }