🌱 UIGF 相关处理草创

This commit is contained in:
BTMuli
2023-08-28 10:40:20 +08:00
parent 6e22eddcf5
commit 8073d79856
6 changed files with 212 additions and 31 deletions

View File

@@ -2,7 +2,7 @@
* @file plugins Sqlite index.ts
* @description Sqlite 数据库操作类
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.2.2
* @since Alpha v0.2.3
*/
// tauri
@@ -10,7 +10,7 @@ import Database from "tauri-plugin-sql-api";
// utils
import initDataSql from "./sql/initData";
import initTableSql from "./sql/initTable";
import { importUIAFData } from "./sql/updateData";
import { importUIAFData, importUIGFData } from "./sql/updateData";
import { getUiafStatus } from "../../utils/UIAF";
import {
insertAbyssData,
@@ -30,7 +30,7 @@ class Sqlite {
private readonly dbPath: string = "sqlite:tauri-genshin.db";
/**
* @description 数据库包含的表
* @since Alpha v0.2.0
* @since Alpha v0.2.3
* @private
*/
private readonly tables: string[] = [
@@ -43,6 +43,7 @@ class Sqlite {
"SpiralAbyss",
"UserCharacters",
"UserRecord",
"GachaRecords",
];
/**
@@ -468,6 +469,37 @@ class Sqlite {
if (res.length === 0) return false;
return res;
}
/**
* @description 获取指定 uid 的用户角色数据
* @since Alpha v0.2。3
* @param {string} uid 用户 uid
* @returns {Promise<TGApp.Sqlite.GachaRecords.SingleTable[]>}
*/
public async getGachaRecords(uid: string): Promise<TGApp.Sqlite.GachaRecords.SingleTable[]> {
const db = await Database.load(this.dbPath);
const sql = `SELECT * FROM GachaRecords WHERE uid = '${uid}'`;
const res: TGApp.Sqlite.GachaRecords.SingleTable[] = await db.select(sql);
await db.close();
return res;
}
/**
* @description 合并祈愿数据
* @since Alpha v0.2.3
* @param {string} uid UID
* @param {string} data UIGF 数据
* @returns {Promise<void>}
*/
public async mergeUIGF(uid: string, data: string): Promise<void> {
const db = await Database.load(this.dbPath);
const gachaList: TGApp.Plugins.UIGF.GachaItem[] = JSON.parse(data);
const sql = importUIGFData(uid, gachaList);
for (const item of sql) {
await db.execute(item);
}
await db.close();
}
}
const TGSqlite = new Sqlite();

View File

@@ -1,7 +1,7 @@
-- @file plugins Sqlite sql createTable.sql
-- @brief sqlite数据库创建表语句
-- @author BTMuli <bt-muli@outlook.com>
-- @since Alpha v0.2.0
-- @since Alpha v0.2.3
-- @brief 创建成就数据表
create table if not exists Achievements
@@ -131,7 +131,23 @@ create table if not exists UserCharacters
constellation text,
activeConstellation integer,
costume text,
talent text, -- todo: 数据获取
talent text,
updated text,
primary key (uid, cid)
);
);
-- @brief 创建祈愿数据表
create table if not exists GachaRecords
(
id text primary key not null,
uid text,
gachaType text,
uigfType text,
time text,
itemId text,
name text,
type text,
rank text,
count text,
updated text
);

View File

@@ -5,9 +5,12 @@
* @since Alpha v0.2.0
*/
// utils
import minifySql from "../../../utils/minifySql";
/**
* @description 导入UIAF数据
* @since Alpha v0.1.5
* @since Alpha v0.2.3
* @param {TGApp.Plugins.UIAF.Achievement[]} data
* @returns {string[]} sql
*/
@@ -34,7 +37,43 @@ export function importUIAFData(data: TGApp.Plugins.UIAF.Achievement[]): string[]
WHERE id = ${achievement.id} AND progress != ${achievement.current};
`;
}
return sqlRes.push(sql);
return sqlRes.push(minifySql(sql));
});
return sqlRes;
}
/**
* @description 导入UIGF数据
* @since Alpha v0.2.3
* @param {string} uid - UID
* @param {TGApp.Plugins.UIGF.GachaItem[]} data - UIGF数据
* @returns {string[]} sql
*/
export function importUIGFData(uid: string, data: TGApp.Plugins.UIGF.GachaItem[]): string[] {
const sqlRes: string[] = [];
data.forEach((gacha) => {
const sql = `
INSERT INTO GachaRecords (uid, gachaType, itemId, count, time, name, type, rank, id, uigfType, updated)
VALUES ('${uid}', '${gacha.gacha_type}', '${gacha.item_id ?? null}', '${
gacha.count ?? null
}', '${gacha.time}',
'${gacha.name}', '${gacha.item_type ?? null}', '${gacha.rank_type ?? null}', '${
gacha.id
}',
'${gacha.uigf_gacha_type}', datetime('now', 'localtime'))
ON CONFLICT (id) DO UPDATE SET
uid = '${uid}',
gachaType = '${gacha.gacha_type}',
uigfType = '${gacha.uigf_gacha_type}',
time = '${gacha.time}',
itemId = '${gacha.item_id ?? null}',
count = '${gacha.count ?? null}',
name = '${gacha.name}',
type = '${gacha.item_type ?? null}',
rank = '${gacha.rank_type ?? null}',
updated = datetime('now', 'localtime');
`;
sqlRes.push(minifySql(sql));
});
return sqlRes;
}