fix: gacha

This commit is contained in:
bietiaop 2024-07-10 14:04:01 +08:00
parent 08c15fd378
commit f838c0823d
11 changed files with 249 additions and 23 deletions

65
lib/assets.js Normal file
View file

@ -0,0 +1,65 @@
import { findLowestLatencyUrl } from '../utils/network.js';
let lastFindFastestUrl = {
url: null,
time: 0,
};
const URL_LIB = {
'[JPFRP]': 'http://jp-3.lcf.1l1.icu:17217',
'[HKFRP]': 'http://hk-1.lcf.1l1.icu:10200',
'[USFRP]': 'http://us-6.lcf.1l1.icu:28596',
'[XiaoWu]': 'http://frp.xiaowuap.com:63481',
'[Chuncheon]': 'https://kr.qxqx.cf',
'[Seoul]': 'https://kr-s.qxqx.cf',
'[Singapore]': 'https://sg.qxqx.cf',
};
const TYPE_PATH = {
wiki: 'wiki',
resource: 'resource',
guide: 'guide',
};
const RESOURCE_PATH = {
role: 'role',
role_circle: 'role_circle',
weapon: 'weapon',
};
const GUIDE_PATH = {
flower: 'flower',
};
export const getFatestUrl = async () => {
if (
lastFindFastestUrl.url &&
Date.now() - lastFindFastestUrl.time < 1000 * 60 * 5
) {
return lastFindFastestUrl.url;
}
const urls = Object.values(URL_LIB);
const url = findLowestLatencyUrl(urls);
lastFindFastestUrl = {
url,
time: Date.now(),
};
return url;
};
/**
* Get resource remote path
* @param {keyof TYPE_PATH} type
* @param {keyof RESOURCE_PATH | keyof GUIDE_PATH} label
* @param {string} name
* @returns
*/
export const getRemotePath = async (type, label, name) => {
const url = await getFatestUrl();
return `${url}/ZZZeroUID/${type}/${label}/${name}`;
};
// 获取资源远程路径
export const getResourceRemotePath = async (label, name) => {
return getRemotePath(TYPE_PATH.resource, label, name);
};

View file

@ -1 +1,5 @@
export * as element from './convert/element.js';
export * as char from './convert/char.js';
export * as weapon from './convert/weapon.js';

View file

@ -1,5 +1,5 @@
import settings from '../settings.js';
import PartnerId2SpriteId from '../../resources/map/PartnerId2SpriteId.json' assert { type: "json" };
import PartnerId2SpriteId from '../../resources/map/PartnerId2Data.json' assert { type: 'json' };
/**
*

View file

@ -1,10 +1,10 @@
import WeaponId2Sprite from '../../resources/map/WeaponId2Sprite.json' assert { type: "json" };
import WeaponId2Sprite from '../../resources/map/WeaponId2Sprite.json' assert { type: 'json' };
/**
* @param {string} id
* @returns string
*/
export const IDToWeaponName = id => {
export const IDToWeaponFileName = id => {
const data = WeaponId2Sprite?.[id];
return data;
};
@ -13,7 +13,7 @@ export const IDToWeaponName = id => {
* @param {string} name
* @returns string
*/
export const weaponNameToID = name => {
export const weaponFileNameToID = name => {
for (const [id, data] of Object.entries(WeaponId2Sprite)) {
if (data === name) return id;
}

View file

@ -2,14 +2,39 @@ import path from 'path';
import fs from 'fs';
import { ZZZ_SQUARE_AVATAR, ZZZ_SQUARE_BANGBOO } from './mysapi/api.js';
import { imageResourcesPath } from './path.js';
import { weapon } from './convert.js';
import { getResourceRemotePath } from './assets.js';
const ZZZ_SQUARE_AVATAR_PATH = path.join(imageResourcesPath, 'square_avatar');
const ZZZ_SQUARE_BANGBOO_PATH = path.join(
imageResourcesPath,
'bangboo_square_avatar'
);
const ZZZ_WEAPON_PATH = path.join(imageResourcesPath, 'weapon');
const ZZZ_GUIDES_PATH = path.join(imageResourcesPath, 'guides');
// 将下面的下载封装起来支持错误重试5次
const downloadFile = async (url, savePath) => {
const _download = async (url, savePath, retry = 0) => {
if (retry > 5) {
return null;
}
try {
const download = await fetch(url);
const arrayBuffer = await download.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
if (!fs.existsSync(path.dirname(savePath))) {
fs.mkdirSync(path.dirname(savePath), { recursive: true });
}
fs.writeFileSync(savePath, buffer);
return savePath;
} catch (error) {
return await _download(url, savePath, retry + 1);
}
};
return await _download(url, savePath);
};
/**
*
* @param {string | number} charID
@ -21,14 +46,8 @@ export const getSquareAvatar = async charID => {
if (fs.existsSync(avatarPath)) return avatarPath;
const url = `${ZZZ_SQUARE_AVATAR}/${filename}`;
const savePath = avatarPath;
const download = await fetch(url);
const arrayBuffer = await download.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
if (!fs.existsSync(ZZZ_SQUARE_AVATAR_PATH)) {
fs.mkdirSync(ZZZ_SQUARE_AVATAR_PATH, { recursive: true });
}
fs.writeFileSync(savePath, buffer);
return avatarPath;
const download = await downloadFile(url, savePath);
return download;
};
/**
@ -42,12 +61,25 @@ export const getSquareBangboo = async bangbooId => {
if (fs.existsSync(bangbooPath)) return bangbooPath;
const url = `${ZZZ_SQUARE_BANGBOO}/${filename}`;
const savePath = bangbooPath;
const download = await fetch(url);
const arrayBuffer = await download.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
if (!fs.existsSync(ZZZ_SQUARE_BANGBOO_PATH)) {
fs.mkdirSync(ZZZ_SQUARE_BANGBOO_PATH, { recursive: true });
}
fs.writeFileSync(savePath, buffer);
return bangbooPath;
const download = await downloadFile(url, savePath);
return download;
};
/**
* Get weapon image path
* @param {string} id
* @returns Promise<string>
*/
export const getWeaponImage = async id => {
logger.mark('getWeaponImage', id);
const name = weapon.IDToWeaponFileName(id);
logger.mark('getWeaponImage', name);
const filename = `${name}.png`;
const weaponPath = path.join(ZZZ_WEAPON_PATH, filename);
if (fs.existsSync(weaponPath)) return weaponPath;
const url = await getResourceRemotePath('weapon', filename);
const savePath = weaponPath;
const download = await downloadFile(url, savePath);
logger.mark('getWeaponImage', download);
return download;
};

View file

@ -100,6 +100,7 @@ export async function updateGachaLog(authKey, uid) {
const lastSaved = previousLog[name]?.[0];
let page = 1;
let endId = '0';
const newData = [];
for (const type of gacha_type_meta_data[name]) {
queryLabel: while (true) {
const log = await getZZZGachaLogByAuthkey(
@ -116,13 +117,14 @@ export async function updateGachaLog(authKey, uid) {
if (lastSaved && lastSaved.equals(item)) {
break queryLabel;
}
previousLog[name].push(item);
newData.push(item);
}
endId = log.list[log.list.length - 1]?.id || endId;
page++;
await sleep(400);
}
}
previousLog[name] = [...newData, ...previousLog[name]];
}
saveGachaLog(uid, previousLog);
return previousLog;
@ -180,9 +182,9 @@ export async function anaylizeGachaLog(uid) {
let luck = 0;
let i = 0;
for (const item of data) {
await item.get_assets();
let isUp = true;
if (item.rank_type === '4') {
await item.get_assets();
if (NORMAL_LIST.includes(item.name)) {
isUp = false;
}