mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2026-04-27 22:49:39 +08:00
198
src/plugins/Sqlite/modules/userAbyss.ts
Normal file
198
src/plugins/Sqlite/modules/userAbyss.ts
Normal file
@@ -0,0 +1,198 @@
|
||||
/**
|
||||
* @file plugins/Sqlite/modules/userAbyss.ts
|
||||
* @description Sqlite-用户深渊模块
|
||||
* @since Beta v0.6.0
|
||||
*/
|
||||
|
||||
import { path } from "@tauri-apps/api";
|
||||
import { exists, mkdir, readTextFile, writeTextFile } from "@tauri-apps/plugin-fs";
|
||||
|
||||
import TGLogger from "../../../utils/TGLogger.js";
|
||||
import { timestampToDate } from "../../../utils/toolFunc.js";
|
||||
import TGSqlite from "../index.js";
|
||||
import { transCharacterData, transFloorData } from "../utils/transAbyssData.js";
|
||||
|
||||
/**
|
||||
* @description 直接插入数据
|
||||
* @since Beta v0.6.0
|
||||
* @param {TGApp.Sqlite.Abyss.SingleTable} data - 数据
|
||||
* @returns {string}
|
||||
*/
|
||||
function getRestoreSql(data: TGApp.Sqlite.Abyss.SingleTable): string {
|
||||
const timeNow = timestampToDate(new Date().getTime());
|
||||
return `
|
||||
INSERT INTO SpiralAbyss (uid, id, startTime, endTime, totalBattleTimes, totalWinTimes,
|
||||
maxFloor, totalStar, isUnlock, revealRank, defeatRank, damageRank,
|
||||
takeDamageRank, normalSkillRank, energySkillRank, floors, updated)
|
||||
VALUES ('${data.uid}', ${data.id}, '${data.startTime}', '${data.endTime}', ${data.totalBattleTimes},
|
||||
${data.totalWinTimes}, '${data.maxFloor}', ${data.totalStar},
|
||||
${data.isUnlock}, '${data.revealRank}', '${data.defeatRank}', '${data.damageRank}',
|
||||
'${data.takeDamageRank}', '${data.normalSkillRank}', '${data.energySkillRank}', '${data.floors}',
|
||||
'${timeNow}')
|
||||
ON CONFLICT(uid, id) DO UPDATE
|
||||
SET startTime = '${data.startTime}',
|
||||
endTime = '${data.endTime}',
|
||||
totalBattleTimes = ${data.totalBattleTimes},
|
||||
totalWinTimes = ${data.totalWinTimes},
|
||||
maxFloor = '${data.maxFloor}',
|
||||
totalStar = ${data.totalStar},
|
||||
isUnlock = ${data.isUnlock},
|
||||
revealRank = '${data.revealRank}',
|
||||
defeatRank = '${data.defeatRank}',
|
||||
damageRank = '${data.damageRank}',
|
||||
takeDamageRank = '${data.takeDamageRank}',
|
||||
normalSkillRank = '${data.normalSkillRank}',
|
||||
energySkillRank = '${data.energySkillRank}',
|
||||
floors = '${data.floors}',
|
||||
updated = '${timeNow}';
|
||||
`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取深渊数据的插入更新Sql
|
||||
* @since Beta v0.6.0
|
||||
* @param {string} uid - 用户UID
|
||||
* @param {TGApp.Game.Abyss.FullData} data -深渊数据
|
||||
* @returns {string}
|
||||
*/
|
||||
function getInsertSql(uid: string, data: TGApp.Game.Abyss.FullData): string {
|
||||
const startTime = timestampToDate(Number(data.start_time) * 1000);
|
||||
const endTime = timestampToDate(Number(data.end_time) * 1000);
|
||||
const isUnlock = data.is_unlock ? 1 : 0;
|
||||
const revealRank = transCharacterData(data.reveal_rank);
|
||||
const defeatRank = transCharacterData(data.defeat_rank);
|
||||
const damageRank = transCharacterData(data.damage_rank);
|
||||
const takeDamageRank = transCharacterData(data.take_damage_rank);
|
||||
const normalSkillRank = transCharacterData(data.normal_skill_rank);
|
||||
const energySkillRank = transCharacterData(data.energy_skill_rank);
|
||||
const floors = transFloorData(data.floors);
|
||||
const timeNow = timestampToDate(new Date().getTime());
|
||||
return `
|
||||
INSERT INTO SpiralAbyss (uid, id, startTime, endTime, totalBattleTimes, totalWinTimes,
|
||||
maxFloor, totalStar, isUnlock, revealRank, defeatRank, damageRank,
|
||||
takeDamageRank, normalSkillRank, energySkillRank, floors, updated)
|
||||
VALUES ('${uid}', ${data.schedule_id}, '${startTime}', '${endTime}', ${data.total_battle_times},
|
||||
${data.total_win_times}, '${data.max_floor}', ${data.total_star},
|
||||
${isUnlock}, '${revealRank}', '${defeatRank}', '${damageRank}', '${takeDamageRank}',
|
||||
'${normalSkillRank}', '${energySkillRank}', '${floors}', '${timeNow}')
|
||||
ON CONFLICT(uid, id) DO UPDATE
|
||||
SET startTime = '${startTime}',
|
||||
endTime = '${endTime}',
|
||||
totalBattleTimes = ${data.total_battle_times},
|
||||
totalWinTimes = ${data.total_win_times},
|
||||
maxFloor = '${data.max_floor}',
|
||||
totalStar = ${data.total_star},
|
||||
isUnlock = ${isUnlock},
|
||||
revealRank = '${revealRank}',
|
||||
defeatRank = '${defeatRank}',
|
||||
damageRank = '${damageRank}',
|
||||
takeDamageRank = '${takeDamageRank}',
|
||||
normalSkillRank = '${normalSkillRank}',
|
||||
energySkillRank = '${energySkillRank}',
|
||||
floors = '${floors}',
|
||||
updated = '${timeNow}';
|
||||
`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取所有有数据的UID
|
||||
* @since Beta v0.6.0
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async function getAllUid(): Promise<Array<string>> {
|
||||
const db = await TGSqlite.getDB();
|
||||
type resType = Array<{ uid: string }>;
|
||||
const res = await db.select<resType>("SELECT DISTINCT uid FROM SpiralAbyss;");
|
||||
return res.map((i) => i.uid);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取深渊数据
|
||||
* @since Beta v0.6.0
|
||||
* @param {string} uid - 游戏UID
|
||||
* @returns {Promise<TGApp.Sqlite.Abyss.SingleTable[]>}
|
||||
*/
|
||||
async function getAbyss(uid?: string): Promise<TGApp.Sqlite.Abyss.SingleTable[]> {
|
||||
const db = await TGSqlite.getDB();
|
||||
if (uid === undefined) {
|
||||
return await db.select<TGApp.Sqlite.Abyss.SingleTable[]>(
|
||||
"SELECT * FROM SpiralAbyss order by id DESC;",
|
||||
);
|
||||
}
|
||||
return await db.select<TGApp.Sqlite.Abyss.SingleTable[]>(
|
||||
"SELECT * FROM SpiralAbyss WHERE uid = ? order by id DESC;",
|
||||
[uid],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 保存深渊数据
|
||||
* @since Beta v0.6.0
|
||||
* @param {string} uid - 游戏UID
|
||||
* @param {TGApp.Game.Abyss.FullData} data - 深渊数据
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async function saveAbyss(uid: string, data: TGApp.Game.Abyss.FullData): Promise<void> {
|
||||
const db = await TGSqlite.getDB();
|
||||
await db.execute(getInsertSql(uid, data));
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 删除指定UID存档的数据
|
||||
* @since Beta v0.6.0
|
||||
* @param {string} uid - 游戏UID
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async function delAbyss(uid: string): Promise<void> {
|
||||
const db = await TGSqlite.getDB();
|
||||
await db.execute("DELETE FROM SpiralAbyss WHERE uid = ?;", [uid]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 备份深渊数据
|
||||
* @since Beta v0.6.0
|
||||
* @param {string} dir - 备份目录
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async function backupAbyss(dir: string): Promise<void> {
|
||||
if (!(await exists(dir))) {
|
||||
await mkdir(dir, { recursive: true });
|
||||
await TGLogger.Warn(`未检测到备份目录,已创建`);
|
||||
}
|
||||
const data = await getAbyss();
|
||||
await writeTextFile(`${dir}${path.sep()}abyss.json`, JSON.stringify(data));
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 恢复深渊数据
|
||||
* @since Beta v0.6.0
|
||||
* @param {string} dir - 备份文件目录
|
||||
* @returns {Promise<boolean>}
|
||||
*/
|
||||
async function restoreAbyss(dir: string): Promise<boolean> {
|
||||
const filePath = `${dir}${path.sep()}abyss.json`;
|
||||
if (!(await exists(filePath))) return false;
|
||||
try {
|
||||
const data: TGApp.Sqlite.Abyss.SingleTable[] = JSON.parse(await readTextFile(filePath));
|
||||
const db = await TGSqlite.getDB();
|
||||
for (const abyss of data) {
|
||||
await db.execute(getRestoreSql(abyss));
|
||||
}
|
||||
return true;
|
||||
} catch (e) {
|
||||
await TGLogger.Error(`恢复深渊数据失败${filePath}`);
|
||||
await TGLogger.Error(`${e}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const TSUserAbyss = {
|
||||
getAllUid,
|
||||
getAbyss,
|
||||
saveAbyss,
|
||||
delAbyss,
|
||||
backupAbyss,
|
||||
restoreAbyss,
|
||||
};
|
||||
|
||||
export default TSUserAbyss;
|
||||
@@ -307,14 +307,14 @@ async function restoreUiaf(dir: string): Promise<boolean> {
|
||||
if (!(await exists(dir))) return false;
|
||||
const filesRead = await readDir(dir);
|
||||
const files = filesRead.filter((item) => item.name.includes("UIAF_") && item.isFile);
|
||||
// 正则匹配 UIAF_xx.json
|
||||
for (const file of files) {
|
||||
try {
|
||||
// todo 完善正则判断
|
||||
const reg = new RegExp(/(.*)UIAF_d{9}.json/);
|
||||
const reg = /UIAF_(\d+).json/;
|
||||
if (!file.name.match(reg)) return false;
|
||||
const uid: number = Number(file.name.match(reg)![0]);
|
||||
const data: TGApp.Plugins.UIAF.Achievement[] = JSON.parse(await readTextFile(file.name));
|
||||
const filePath = `${dir}${path.sep()}${file.name}`;
|
||||
const data: TGApp.Plugins.UIAF.Achievement[] = JSON.parse(await readTextFile(filePath));
|
||||
await TSUserAchi.mergeUiaf(data, uid);
|
||||
} catch (e) {
|
||||
await TGLogger.Error(`[UIAF][RESTORE] 恢复成就数据${file.name} `);
|
||||
|
||||
Reference in New Issue
Block a user