mirror of
https://github.com/ZZZure/ZZZ-Plugin.git
synced 2025-12-16 21:27:47 +00:00
fix: gacha
This commit is contained in:
parent
08c15fd378
commit
f838c0823d
11 changed files with 249 additions and 23 deletions
|
|
@ -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;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue