fix(UIAF): 方法修正

This commit is contained in:
BTMuli
2023-03-08 21:33:35 +08:00
parent abe5867499
commit 8604790f9d
2 changed files with 36 additions and 33 deletions

View File

@@ -1,11 +1,18 @@
import { checkUIAFData, readUIAFData, importUIAFData } from "./utils/importData";
/**
* @file plugins UIAF index.ts
* @description UIAF plugin index
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha
*/
import { checkUIAFData, readUIAFData, mergeUIAFData } from "./utils/importData";
import { getAchievements } from "./utils/exportData";
const UIAF_Oper = {
importOper: {
checkUIAFData,
readUIAFData,
importUIAFData,
mergeUIAFData,
},
exportOper: {
getAchievements,

View File

@@ -21,9 +21,9 @@ export async function checkUIAFData(path: string): Promise<boolean> {
}
/**
* @description 读取本地 UIAF 数据
* @description 读取 UIAF 数据
* @param {string} userPath - UIAF 数据路径
* @return {Promise<string>|Promise<false>} UIAF 数据
* @return {Promise<string|false>} UIAF 数据
*/
export async function readUIAFData(userPath: string): Promise<string | false> {
if (await fs.exists(userPath)) {
@@ -40,39 +40,35 @@ export async function readUIAFData(userPath: string): Promise<string | false> {
/**
* @description 数据合并
* @param {UIAF_Achievement[]|false} localData - 本地数据
* @param {UIAF_Achievement[]} localData - 本地数据
* @param {Achievements} remoteData - 远程数据
* @return {Promise<UIAF_Achievement[]|false>} 合并后的数据,如果合并失败则返回 false
*/
export async function importUIAFData(
localData: UIAF_Achievement[] | false,
export async function mergeUIAFData(
localData: UIAF_Achievement[],
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;
}
// 遍历 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;
}
}
});
// 按照 id 排序
localData.sort((a, b) => a.id - b.id);
// 返回合并后的数据
return localData;
}