fix(achievements): 精简代码,完成 IndexedDB 的接轨,删去 dev 相关页面

This commit is contained in:
BTMuli
2023-03-16 00:55:36 +08:00
parent e2e82a2fd9
commit e2211b513f
14 changed files with 124 additions and 383 deletions

View File

@@ -5,18 +5,13 @@
* @since Alpha
*/
import { checkUIAFData, readUIAFData, mergeUIAFData } from "./utils/importData";
import { getAchievements } from "./utils/exportData";
import { checkUIAFData, readUIAFData } from "./utils/importData";
import { getUIAFInfo } from "./utils/exportData";
const UIAF_Oper = {
importOper: {
checkUIAFData,
readUIAFData,
mergeUIAFData,
},
exportOper: {
getAchievements,
},
checkUIAFData,
readUIAFData,
getUIAFInfo,
};
export default UIAF_Oper;

View File

@@ -5,14 +5,14 @@
* @since Alpha
*/
import { Achievements, UIAF_Info, UIAF_Achievement } from "../interface/UIAF";
import { app, fs } from "@tauri-apps/api";
import { UIAF_Info } from "../interface/UIAF";
import { app } from "@tauri-apps/api";
/**
* @description 获取 UIAF_Info
* @return Promise<UIAF_Info>
*/
async function getUIAFInfo(): Promise<UIAF_Info> {
export async function getUIAFInfo(): Promise<UIAF_Info> {
return {
export_app: "Tauri.Genshin",
export_timestamp: Math.floor(Date.now() / 1000),
@@ -20,20 +20,3 @@ async function getUIAFInfo(): Promise<UIAF_Info> {
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,
};
}

View File

@@ -5,7 +5,7 @@
* @since Alpha
*/
import { Achievements, UIAF_Achievement, UIAF_Info } from "../interface/UIAF";
import { UIAF_Info } from "../interface/UIAF";
import { fs } from "@tauri-apps/api";
/**
@@ -39,37 +39,3 @@ export async function readUIAFData(userPath: string): Promise<string | false> {
return false;
}
}
/**
* @description 数据合并
* @since Alpha
* @param {UIAF_Achievement[]} localData - 本地数据
* @param {Achievements} remoteData - 远程数据
* @return {Promise<UIAF_Achievement[]>} 合并后的数据
*/
export async function mergeUIAFData(
localData: UIAF_Achievement[],
remoteData: Achievements
): Promise<UIAF_Achievement[]> {
// 遍历 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;
}