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"; import { getAchievements } from "./utils/exportData";
const UIAF_Oper = { const UIAF_Oper = {
importOper: { importOper: {
checkUIAFData, checkUIAFData,
readUIAFData, readUIAFData,
importUIAFData, mergeUIAFData,
}, },
exportOper: { exportOper: {
getAchievements, getAchievements,

View File

@@ -21,9 +21,9 @@ export async function checkUIAFData(path: string): Promise<boolean> {
} }
/** /**
* @description 读取本地 UIAF 数据 * @description 读取 UIAF 数据
* @param {string} userPath - 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> { export async function readUIAFData(userPath: string): Promise<string | false> {
if (await fs.exists(userPath)) { if (await fs.exists(userPath)) {
@@ -40,39 +40,35 @@ export async function readUIAFData(userPath: string): Promise<string | false> {
/** /**
* @description 数据合并 * @description 数据合并
* @param {UIAF_Achievement[]|false} localData - 本地数据 * @param {UIAF_Achievement[]} localData - 本地数据
* @param {Achievements} remoteData - 远程数据 * @param {Achievements} remoteData - 远程数据
* @return {Promise<UIAF_Achievement[]|false>} 合并后的数据,如果合并失败则返回 false * @return {Promise<UIAF_Achievement[]|false>} 合并后的数据,如果合并失败则返回 false
*/ */
export async function importUIAFData( export async function mergeUIAFData(
localData: UIAF_Achievement[] | false, localData: UIAF_Achievement[],
remoteData: Achievements remoteData: Achievements
): Promise<UIAF_Achievement[] | false> { ): Promise<UIAF_Achievement[] | false> {
if (localData !== false) { // 遍历 remoteData.list
// 遍历 remoteData.list remoteData.list.map((remoteAchievement: UIAF_Achievement) => {
remoteData.list.map((remoteAchievement: UIAF_Achievement) => { // 查找 id 相同的 localAchievement
// 查找 id 相同的 localAchievement const localAchievement = localData.find(
const localAchievement = localData.find( achievement => achievement.id === remoteAchievement.id
achievement => achievement.id === remoteAchievement.id );
); // 如果没找到,就直接添加
// 如果没找到,就直接添加 if (localAchievement === undefined) {
if (localAchievement === undefined) { localData.push(remoteAchievement);
localData.push(remoteAchievement); } else {
} else { // 检测数据是否需要更新
// 检测数据是否需要更新 if (localAchievement.timestamp < remoteAchievement.timestamp) {
if (localAchievement.timestamp < remoteAchievement.timestamp) { // 更新数据
// 更新数据 localAchievement.timestamp = remoteAchievement.timestamp;
localAchievement.timestamp = remoteAchievement.timestamp; localAchievement.current = remoteAchievement.current;
localAchievement.current = remoteAchievement.current; localAchievement.status = remoteAchievement.status;
localAchievement.status = remoteAchievement.status;
}
} }
}); }
// 按照 id 排序 });
localData.sort((a, b) => a.id - b.id); // 按照 id 排序
// 返回合并后的数据 localData.sort((a, b) => a.id - b.id);
return localData; // 返回合并后的数据
} else { return localData;
return remoteData.list;
}
} }