mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2025-12-12 09:18:14 +08:00
fix(UIAF): UIAF 单写一个 plugin,先写处理逻辑
This commit is contained in:
15
src/plugins/UIAF/index.ts
Normal file
15
src/plugins/UIAF/index.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { checkUIAFData, readUIAFData, importUIAFData } from "./utils/importData";
|
||||
import { getAchievements } from "./utils/exportData";
|
||||
|
||||
const UIAF_Oper = {
|
||||
importOper: {
|
||||
checkUIAFData,
|
||||
readUIAFData,
|
||||
importUIAFData,
|
||||
},
|
||||
exportOper: {
|
||||
getAchievements,
|
||||
},
|
||||
};
|
||||
|
||||
export default UIAF_Oper;
|
||||
@@ -1,10 +1,12 @@
|
||||
/**
|
||||
* @file UIAF.ts
|
||||
* @file plugins UIAF interface UIAF.ts
|
||||
* @description UIAF interface
|
||||
* @author BTMuli<bt-muli@outlook.com>
|
||||
* @see https://github.com/DGP-Studio/Snap.Genshin.Docs/blob/main/docs/development/UIAF.md
|
||||
* @version v1.1
|
||||
* @since Alpha
|
||||
*/
|
||||
|
||||
/**
|
||||
* @interface Achievements
|
||||
* @description Achievements interface
|
||||
39
src/plugins/UIAF/utils/exportData.ts
Normal file
39
src/plugins/UIAF/utils/exportData.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* @file plugins UIAF utils exportData.ts
|
||||
* @description UIAF export data utils
|
||||
* @author BTMuli<bt-muli@outlook.com>
|
||||
* @since Alpha
|
||||
*/
|
||||
|
||||
import { Achievements, UIAF_Info, UIAF_Achievement } from "../interface/UIAF";
|
||||
import { app, fs } from "@tauri-apps/api";
|
||||
|
||||
/**
|
||||
* @description 获取 UIAF_Info
|
||||
* @return Promise<UIAF_Info>
|
||||
*/
|
||||
async function getUIAFInfo(): Promise<UIAF_Info> {
|
||||
return {
|
||||
export_app: "Tauri.Genshin",
|
||||
export_timestamp: Date.now(),
|
||||
export_app_version: await app.getVersion(),
|
||||
uiaf_version: "v1.1",
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取 Achievements
|
||||
* @param {string} userPath - 本地文件路径
|
||||
* @return Promise<Achievements>
|
||||
*/
|
||||
export async function getAchievements(userPath: string): Promise<Achievements> {
|
||||
// 读取本地文件
|
||||
const achievementsRaw = await fs.readTextFile(userPath);
|
||||
// 解析 JSON
|
||||
const achievements: UIAF_Achievement[] = JSON.parse(achievementsRaw);
|
||||
// 返回
|
||||
return {
|
||||
info: await getUIAFInfo(),
|
||||
list: achievements,
|
||||
};
|
||||
}
|
||||
78
src/plugins/UIAF/utils/importData.ts
Normal file
78
src/plugins/UIAF/utils/importData.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* @file plugins UIAF utils importData.ts
|
||||
* @description UIAF import data utils
|
||||
* @author BTMuli<bt-muli@outlook.com>
|
||||
* @since Alpha
|
||||
*/
|
||||
|
||||
import { Achievements, UIAF_Achievement, UIAF_Info } from "../interface/UIAF";
|
||||
import { fs } from "@tauri-apps/api";
|
||||
|
||||
/**
|
||||
* @description 检测是否存在 UIAF 数据
|
||||
* @description 粗略检测,不保证数据完整性
|
||||
* @param {string} path - UIAF 数据路径
|
||||
* @return {Promise<boolean>} 是否存在 UIAF 数据
|
||||
*/
|
||||
export async function checkUIAFData(path: string): Promise<boolean> {
|
||||
const fileData: string = await fs.readTextFile(path);
|
||||
const UIAFData: UIAF_Info = JSON.parse(fileData)["info"];
|
||||
return UIAFData.uiaf_version !== undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 读取本地 UIAF 数据
|
||||
* @param {string} userPath - UIAF 数据路径
|
||||
* @return {Promise<string>|Promise<false>} UIAF 数据
|
||||
*/
|
||||
export async function readUIAFData(userPath: string): Promise<string | false> {
|
||||
if (await fs.exists(userPath)) {
|
||||
const fileData = await fs.readTextFile(userPath);
|
||||
if (fileData !== undefined && fileData !== null && fileData !== "" && fileData !== "{}") {
|
||||
return fileData;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 数据合并
|
||||
* @param {UIAF_Achievement[]|false} localData - 本地数据
|
||||
* @param {Achievements} remoteData - 远程数据
|
||||
* @return {Promise<UIAF_Achievement[]|false>} 合并后的数据,如果合并失败则返回 false
|
||||
*/
|
||||
export async function importUIAFData(
|
||||
localData: UIAF_Achievement[] | false,
|
||||
remoteData: Achievements
|
||||
): Promise<UIAF_Achievement[] | false> {
|
||||
if (localData !== false) {
|
||||
// 遍历 remoteData.list
|
||||
remoteData.list.map((remoteAchievement: UIAF_Achievement) => {
|
||||
// 查找 id 相同的 localAchievement
|
||||
const localAchievement = localData.find(
|
||||
achievement => achievement.id === remoteAchievement.id
|
||||
);
|
||||
// 如果没找到,就直接添加
|
||||
if (localAchievement === undefined) {
|
||||
localData.push(remoteAchievement);
|
||||
} else {
|
||||
// 检测数据是否需要更新
|
||||
if (localAchievement.timestamp < remoteAchievement.timestamp) {
|
||||
// 更新数据
|
||||
localAchievement.timestamp = remoteAchievement.timestamp;
|
||||
localAchievement.current = remoteAchievement.current;
|
||||
localAchievement.status = remoteAchievement.status;
|
||||
}
|
||||
}
|
||||
});
|
||||
// 按照 id 排序
|
||||
localData.sort((a, b) => a.id - b.id);
|
||||
// 返回合并后的数据
|
||||
return localData;
|
||||
} else {
|
||||
return remoteData.list;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user