♻️ 移除留影叙佳期,重构卡池组件

This commit is contained in:
目棃
2024-03-07 15:51:05 +08:00
parent e168a341ef
commit 44699308b7
6 changed files with 148 additions and 149 deletions

View File

@@ -1,7 +1,7 @@
/**
* @file plugins/Mys/index.ts
* @description Mys plugin index
* @since Beta v0.3.9
* @since Beta v0.4.4
*/
import MysApi from "./api";
@@ -15,7 +15,7 @@ import getNewsList from "./request/getNewsList";
import getPositionData from "./request/getPositionData";
import getPostData from "./request/getPostData";
import { getVoteInfo, getVoteResult } from "./request/getVoteData";
import getGachaCard from "./utils/getGachaCard";
import { getGachaCard } from "./utils/getGachaCard";
import getLotteryCard from "./utils/getLotteryCard";
import { getActivityCard, getNewsCard, getNoticeCard } from "./utils/getNewsCard";
import getPositionCard from "./utils/getPositionCard";

View File

@@ -1,12 +1,12 @@
/**
* @file plugins/Mys/types/Gacha.d.ts
* @description Mys 插件卡池类型定义文件
* @since Beta v0.3.3
* @since Beta v0.4.4
*/
/**
* @description Mys 卡池类型定义
* @since Beta v0.3.3
* @since Beta v0.4.4
* @namespace TGApp.Plugins.Mys.Gacha
* @memberof TGApp.Plugins.Mys
*/
@@ -27,13 +27,13 @@ declare namespace TGApp.Plugins.Mys.Gacha {
/**
* @description 卡池信息
* @since Alpha v0.2.1
* @since Beta v0.4.4
* @interface Data
* @property {string} id 卡池ID
* @property {string} title 卡池标题
* @property {string} activity_url 卡池对应帖子
* @property {string} content_before_act 卡池内容
* @property {MiniItem[]} pool 卡池包含的角色
* @property {Array<{icon: string; url: string}>} pool 卡池角色头像
* @property {string} voice_icon 卡池角色语音头像
* @property {string} voice_url 卡池角色语音URL
* @property {string} voice_status 卡池角色语音状态
@@ -43,11 +43,14 @@ declare namespace TGApp.Plugins.Mys.Gacha {
* @return Data
*/
interface Data {
id: string;
id: number;
title: string;
activity_url: string;
content_before_act: string;
pool: MiniItem[];
pool: Array<{
icon: string;
url: string;
}>;
voice_icon: string;
voice_url: string;
voice_status: string;
@@ -55,45 +58,47 @@ declare namespace TGApp.Plugins.Mys.Gacha {
end_time: string;
}
/**
* @description 基本信息
* @since Alpha v0.2.1
* @interface MiniItem
* @property {string} icon 图标
* @property {string} url 链接
* @return MiniItem
*/
interface MiniItem {
icon: string;
url: string;
}
/**
* @description 用于渲染的卡池数据
* @since Beta v0.3.3
* @since Beta v0.4.4
* @interface RenderCard
* @property {string} id 卡池ID
* @property {string} title 卡池标题
* @property {string} subtitle 卡池副标题
* @property {string} cover 卡池封面
* @property {number} postId 卡池对应帖子ID
* @property {MiniItem[]} characters 卡池包含的角色
* @property {MiniItem} voice 卡池角色语音
* @property {Array<RenderItem>} characters 卡池角色头像
* @property {string} time.str 卡池时间字符串
* @property {string} time.startStamp 卡池开始时间戳
* @property {string} time.endStamp 卡池结束时间戳
* @return RenderCard
*/
interface RenderCard {
id: number;
title: string;
subtitle: string;
cover: string;
postId: number;
characters: MiniItem[];
voice: MiniItem;
characters: RenderItem[];
time: {
str: string;
startStamp: number;
endStamp: number;
};
}
/**
* @description 用于渲染的卡池角色头像
* @since Beta v0.4.4
* @interface RenderItem
* @property {string} icon 角色头像
* @property {string} url 角色详情页URL
* @property {TGApp.App.Character.WikiBriefInfo} info 角色简略信息
* @return RenderItem
*/
interface RenderItem {
icon: string;
url: string;
info?: TGApp.App.Character.WikiBriefInfo;
}
}

View File

@@ -1,64 +1,87 @@
/**
* @file plugins/Mys/utils/getGachaCard.ts
* @description Mys 插件抽卡工具
* @since Beta v0.3.9
* @since Beta v0.4.4
*/
import { AppCharacterData } from "../../../data";
import getPostData from "../request/getPostData";
/**
* @description 根据单个卡池信息转为渲染用的卡池信息
* @since Beta v0.4.4
* @param {TGApp.Plugins.Mys.Gacha.Data} data 卡池信息
* @param {string} poolCover 卡池封面
* @returns {Promise<TGApp.Plugins.Mys.Gacha.RenderCard>}
*/
async function getGachaItemCard(
data: TGApp.Plugins.Mys.Gacha.Data,
poolCover?: string,
): Promise<TGApp.Plugins.Mys.Gacha.RenderCard> {
let cover = "/source/UI/empty.webp";
const postId: number | undefined = Number(data.activity_url.split("/").pop()) || undefined;
if (postId === undefined || isNaN(postId)) {
throw new Error("无法获取帖子 ID");
}
if (poolCover !== undefined) {
cover = poolCover;
} else {
try {
console.log("调用 getPostData");
const post = await getPostData(postId);
cover = post.cover?.url ?? post.post.images[0];
} catch (error) {
console.error(error);
}
}
const timeStr = `${data.start_time} ~ ${data.end_time}`;
const characters: TGApp.Plugins.Mys.Gacha.RenderItem[] = [];
for (const character of data.pool) {
const item: TGApp.Plugins.Mys.Gacha.RenderItem = {
icon: character.icon,
url: character.url,
};
const contentId = character.url.match(/(?<=content\/)\d+/)?.[0];
if (contentId) {
const itemF = AppCharacterData.find((item) => item.contentId.toString() === contentId);
if (itemF) {
item.info = itemF;
}
}
characters.push(item);
}
return {
id: data.id,
title: data.title,
subtitle: data.content_before_act,
cover,
postId,
characters,
time: {
str: timeStr,
startStamp: new Date(data.start_time).getTime(),
endStamp: new Date(data.end_time).getTime(),
},
};
}
/**
* @description 根据卡池信息转为渲染用的卡池信息
* @since Beta v0.3.9
* @since Beta v0.4.4
* @param {TGApp.Plugins.Mys.Gacha.Data[]} gachaData 卡池信息
* @param {Record<number, string>} poolCover 卡池封面
* @returns {Promise<TGApp.Plugins.Mys.Gacha.RenderCard[]>}
*/
async function getGachaCard(
export async function getGachaCard(
gachaData: TGApp.Plugins.Mys.Gacha.Data[],
poolCover?: Record<number, string>,
): Promise<TGApp.Plugins.Mys.Gacha.RenderCard[]> {
const gachaCard: TGApp.Plugins.Mys.Gacha.RenderCard[] = [];
await Promise.allSettled(
gachaData.map(async (data: TGApp.Plugins.Mys.Gacha.Data) => {
let cover = "/source/UI/empty.webp";
const postId: number | undefined = Number(data.activity_url.split("/").pop()) || undefined;
if (postId === undefined || isNaN(postId)) {
throw new Error("无法获取帖子 ID");
}
if (poolCover !== undefined) {
cover = poolCover[postId];
} else {
try {
console.log("调用 getPostData");
const post = await getPostData(postId);
cover = post.cover?.url ?? post.post.images[0];
} catch (error) {
console.error(error);
}
}
const timeStr = `${data.start_time} ~ ${data.end_time}`;
return gachaCard.push({
title: data.title,
subtitle: data.content_before_act,
cover,
postId,
characters: data.pool.map((character) => ({
icon: character.icon,
url: character.url,
})),
voice: {
icon: data.voice_icon || "/source/UI/lumine.webp",
url: data.voice_url,
},
time: {
str: timeStr,
startStamp: new Date(data.start_time).getTime(),
endStamp: new Date(data.end_time).getTime(),
},
});
const item = await getGachaItemCard(data, poolCover?.[Number(data.id)]);
gachaCard.push(item);
}),
);
return gachaCard;
}
export default getGachaCard;