mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2025-12-13 09:28:14 +08:00
✨ feat(db): 数据源更改
This commit is contained in:
@@ -7,16 +7,14 @@
|
||||
|
||||
// tauri
|
||||
import { app, fs, path } from "@tauri-apps/api";
|
||||
// data
|
||||
import { TGAppData } from "../data";
|
||||
// utils
|
||||
import { UpdateTGDataByKey } from "./TGIndex";
|
||||
import TGSqlite from "../core/database/TGSqlite";
|
||||
|
||||
/**
|
||||
* @description 时间戳转换为日期
|
||||
* @since Alpha v0.1.4
|
||||
* @param {number} timestamp - 时间戳
|
||||
* @returns {string} 日期
|
||||
* @returns {string} 日期 2021-01-01 00:00:00
|
||||
*/
|
||||
export function timestampToDate (timestamp: number): string {
|
||||
return new Date(timestamp * 1000).toLocaleString("zh", {
|
||||
@@ -101,66 +99,27 @@ export async function readUiafData (userPath: string): Promise<string | false> {
|
||||
/**
|
||||
* @description 根据成就数据导出 UIAF 数据
|
||||
* @since Alpha v0.1.4
|
||||
* @param {BTMuli.Genshin.Achievement[]} achievementData - 成就数据
|
||||
* @param {TGPlugin.UIAF.Achievement[]} achievementData - 成就数据
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function backupUiafData (achievementData: BTMuli.Genshin.Achievement[]): Promise<void> {
|
||||
const res = [] as TGPlugin.UIAF.Achievement[];
|
||||
export async function backupUiafData (achievementData: TGPlugin.UIAF.Achievement[]): Promise<void> {
|
||||
const savePath = `${await path.appLocalDataDir()}\\userData\\UIAF.json`;
|
||||
achievementData.forEach((achievement) => {
|
||||
if (achievement.completed || achievement.progress !== 0) {
|
||||
return res.push({
|
||||
id: achievement.id,
|
||||
timestamp: achievement.completed && achievement.completed_time ? Math.round(new Date(achievement.completed_time).getTime() / 1000) : 0,
|
||||
current: achievement.progress,
|
||||
status: getUiafStatus(achievement.completed, achievement.progress),
|
||||
});
|
||||
}
|
||||
});
|
||||
await fs.writeTextFile(savePath, JSON.stringify(res, null, 2));
|
||||
await fs.writeTextFile(savePath, JSON.stringify(achievementData, null, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 根据 UIAF 数据恢复成就数据
|
||||
* @since Alpha v0.1.4
|
||||
* @returns {Promise<{total: number, completed: number}> | false} 恢复的成就数量
|
||||
* @returns {Promise<{total: number, fin: number}> | false} 恢复的成就数量
|
||||
*/
|
||||
export async function restoreUiafData (): Promise<{ total: number, completed: number } | false> {
|
||||
export async function restoreUiafData (): Promise<{ total: number, fin: number } | false> {
|
||||
const uiafPath = `${await path.appLocalDataDir()}\\userData\\UIAF.json`;
|
||||
// 检测是否存在 UIAF 数据
|
||||
if (!await fs.exists(uiafPath)) {
|
||||
return false;
|
||||
}
|
||||
const uiafData = JSON.parse(await fs.readTextFile(uiafPath)) as TGPlugin.UIAF.Achievement[];
|
||||
const achievementData = TGAppData.achievements;
|
||||
const seriesData = TGAppData.achievementSeries;
|
||||
uiafData.forEach((uiafAchievement) => {
|
||||
// 更新成就数据
|
||||
const localAchievement = achievementData[uiafAchievement.id];
|
||||
localAchievement.completed = uiafAchievement.status === 2 || uiafAchievement.status === 3;
|
||||
localAchievement.progress = uiafAchievement.current;
|
||||
// eslint-disable-next-line camelcase
|
||||
localAchievement.completed_time = uiafAchievement.timestamp !== 0 ? timestampToDate(uiafAchievement.timestamp) : "";
|
||||
UpdateTGDataByKey("Achievements", localAchievement);
|
||||
// 更新成就系列数据
|
||||
if (localAchievement.completed) {
|
||||
const localSeries = seriesData[localAchievement.series];
|
||||
// eslint-disable-next-line camelcase
|
||||
localSeries.completed_count += 1;
|
||||
seriesData[localAchievement.series] = localSeries;
|
||||
UpdateTGDataByKey("AchievementSeries", localSeries);
|
||||
}
|
||||
});
|
||||
// 获取 total 和 completed
|
||||
let total = 0;
|
||||
let completed = 0;
|
||||
Object.values(seriesData).forEach((series) => {
|
||||
total += series.total_count;
|
||||
completed += series.completed_count;
|
||||
});
|
||||
await TGSqlite.UIAF.import(uiafData);
|
||||
// 返回
|
||||
return {
|
||||
total,
|
||||
completed,
|
||||
};
|
||||
return await TGSqlite.search.overview();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user