From afa0ba190b1247029fa9802e7b472c8420990182 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9B=AE=E6=A3=83?= Date: Sun, 5 May 2024 02:46:07 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=A5=88=E6=84=BF=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/User/Gacha.vue | 60 +++++----- src/plugins/Sqlite/index.ts | 100 +--------------- src/plugins/Sqlite/modules/userGacha.ts | 147 ++++++++++++++++++++++++ src/plugins/Sqlite/sql/updateData.ts | 75 ++++++------ src/types/Sqlite/GachaRecords.d.ts | 2 +- 5 files changed, 217 insertions(+), 167 deletions(-) create mode 100644 src/plugins/Sqlite/modules/userGacha.ts diff --git a/src/pages/User/Gacha.vue b/src/pages/User/Gacha.vue index 6aa4154e..f420b5f7 100644 --- a/src/pages/User/Gacha.vue +++ b/src/pages/User/Gacha.vue @@ -5,11 +5,11 @@
增量刷新 + >增量刷新 + 全量刷新 + >全量刷新 + 导入 导出 @@ -52,7 +52,7 @@ import GroHistory from "../../components/gachaRecord/gro-history.vue"; import GroOverview from "../../components/gachaRecord/gro-overview.vue"; import ToLoading from "../../components/overlay/to-loading.vue"; import { AppCharacterData, AppWeaponData } from "../../data"; -import TGSqlite from "../../plugins/Sqlite"; +import TSUserGacha from "../../plugins/Sqlite/modules/userGacha"; import { useAppStore } from "../../store/modules/app"; import { useUserStore } from "../../store/modules/user"; import TGLogger from "../../utils/TGLogger"; @@ -79,7 +79,7 @@ const tab = ref(""); onMounted(async () => { await TGLogger.Info("[UserGacha][onMounted] 进入角色祈愿页面"); loadingTitle.value = "正在获取祈愿 UID 列表"; - selectItem.value = await TGSqlite.getUidList(); + selectItem.value = await TSUserGacha.getUidList(); if (selectItem.value.length === 0) { showSnackbar({ color: "error", @@ -91,7 +91,7 @@ onMounted(async () => { } uidCur.value = selectItem.value[0]; loadingTitle.value = `正在获取祈愿数据,默认 UID:${uidCur.value}`; - gachaListCur.value = await TGSqlite.getGachaRecords(uidCur.value); + gachaListCur.value = await TSUserGacha.getGachaRecords(uidCur.value); await TGLogger.Info( `[UserGacha][onMounted] 获取到 ${uidCur.value} 的 ${gachaListCur.value.length} 条祈愿数据`, ); @@ -159,11 +159,11 @@ async function confirmRefresh(force: boolean = false): Promise { ]; if (force) { loadingTitle.value = "正在获取数据库祈愿最新 ID"; - checkList[0] = await TGSqlite.getGachaCheck(account.gameUid, "200"); - checkList[1] = await TGSqlite.getGachaCheck(account.gameUid, "301"); - checkList[2] = await TGSqlite.getGachaCheck(account.gameUid, "400"); - checkList[3] = await TGSqlite.getGachaCheck(account.gameUid, "302"); - checkList[4] = await TGSqlite.getGachaCheck(account.gameUid, "500"); + checkList[0] = await TSUserGacha.getGachaCheck(account.gameUid, "200"); + checkList[1] = await TSUserGacha.getGachaCheck(account.gameUid, "301"); + checkList[2] = await TSUserGacha.getGachaCheck(account.gameUid, "400"); + checkList[3] = await TSUserGacha.getGachaCheck(account.gameUid, "302"); + checkList[4] = await TSUserGacha.getGachaCheck(account.gameUid, "500"); } console.log(checkList); loadingTitle.value = "正在刷新新手祈愿数据"; @@ -190,7 +190,7 @@ async function confirmRefresh(force: boolean = false): Promise { window.location.reload(); } -// 获取祈愿数据并写入数据库 +// 获取祈愿数据并写入数据库,不用考虑多语言,因为从api获取的数据是中文 async function getGachaLogs( pool: string, endId: string = "0", @@ -236,7 +236,7 @@ async function getGachaLogs( } uigfList.push(tempItem); }); - await TGSqlite.mergeUIGF(account.gameUid, uigfList); + await TSUserGacha.mergeUIGF(account.gameUid, uigfList); if (check !== undefined && gachaRes.some((i) => i.id === check)) { await new Promise((resolve) => { setTimeout(() => { @@ -334,7 +334,7 @@ async function handleImportBtn(savePath?: string): Promise { }); return; } - await TGSqlite.mergeUIGF(remoteData.info.uid, remoteData.list); + await TSUserGacha.mergeUIGF(remoteData.info.uid, remoteData.list); loading.value = false; showSnackbar({ text: `成功导入 ${remoteData.list.length} 条祈愿数据`, @@ -342,14 +342,14 @@ async function handleImportBtn(savePath?: string): Promise { await TGLogger.Info( `[UserGacha][handleImportBtn] 成功导入 ${remoteData.info.uid} 的 ${remoteData.list.length} 条祈愿数据`, ); - setTimeout(() => { - window.location.reload(); - }, 1000); + // setTimeout(() => { + // window.location.reload(); + // }, 1000); } // 导出按钮点击事件 async function handleExportBtn(): Promise { - const gachaList = await TGSqlite.getGachaRecords(uidCur.value); + const gachaList = await TSUserGacha.getGachaRecords(uidCur.value); if (gachaList.length === 0) { showSnackbar({ color: "error", @@ -460,25 +460,25 @@ async function deleteGacha(): Promise { await TGLogger.Info(`[UserGacha][${uidCur.value}][deleteGacha] 已取消祈愿数据删除`); return; } - const uidList = await TGSqlite.getUidList(); + const uidList = await TSUserGacha.getUidList(); let secondConfirm: string | boolean | undefined; if (uidList.length <= 1) { secondConfirm = await showConfirm({ title: "删除后数据库将为空,确定删除?", text: `UID:${uidCur.value},共 ${gachaListCur.value.length} 条数据`, }); - } - if (!secondConfirm) { - showSnackbar({ - color: "cancel", - text: "已取消祈愿数据删除", - }); - await TGLogger.Info(`[UserGacha][${uidCur.value}][deleteGacha] 已取消祈愿数据删除`); - return; + if (!secondConfirm) { + showSnackbar({ + color: "cancel", + text: "已取消祈愿数据删除", + }); + await TGLogger.Info(`[UserGacha][${uidCur.value}][deleteGacha] 已取消祈愿数据删除`); + return; + } } loadingTitle.value = `正在删除${uidCur.value}的祈愿数据`; loading.value = true; - await TGSqlite.deleteGachaRecords(uidCur.value); + await TSUserGacha.deleteGachaRecords(uidCur.value); loading.value = false; showSnackbar({ text: `已成功删除 ${uidCur.value} 的祈愿数据`, @@ -493,7 +493,7 @@ async function deleteGacha(): Promise { // 监听 UID 变化 watch(uidCur, async (newUid) => { - gachaListCur.value = await TGSqlite.getGachaRecords(newUid); + gachaListCur.value = await TSUserGacha.getGachaRecords(newUid); showSnackbar({ text: `成功获取 ${gachaListCur.value.length} 条祈愿数据`, }); diff --git a/src/plugins/Sqlite/index.ts b/src/plugins/Sqlite/index.ts index 8cf96510..7971a9bf 100644 --- a/src/plugins/Sqlite/index.ts +++ b/src/plugins/Sqlite/index.ts @@ -18,7 +18,7 @@ import { insertRecordData, insertRoleData, } from "./sql/insertData"; -import { importUIAFData, importUIGFData } from "./sql/updateData"; +import { importUIAFData } from "./sql/updateData"; class Sqlite { /** @@ -115,15 +115,13 @@ class Sqlite { /** * @description 插入 Account 数据 - * @since Beta v0.4.1 + * @since Beta v0.4.7 * @param {TGApp.User.Account.Game[]} accounts * @returns {Promise} */ public async saveAccount(accounts: TGApp.User.Account.Game[]): Promise { const db = await this.getDB(); - // 为了防止多账号的情况,先清空数据表 - const clear = "DELETE FROM GameAccount WHERE 1=1;"; - await db.execute(clear); + await db.execute("DELETE FROM GameAccount WHERE true;"); for (const a of accounts) { const sql = insertGameAccountData(a); await db.execute(sql); @@ -399,98 +397,6 @@ class Sqlite { return res; } - /** - * @description 获取已有 uid 列表 - * @since Beta v0.3.3 - * @returns {Promise} - */ - public async getUidList(): Promise { - const db = await this.getDB(); - const sql = "SELECT DISTINCT uid FROM GachaRecords"; - const res: Array<{ uid: string }> = await db.select(sql); - return res.map((item) => item.uid); - } - - /** - * @description 获取指定 uid 的用户角色数据 - * @since Beta v0.3.3 - * @param {string} uid 用户 uid - * @returns {Promise} - */ - public async getGachaRecords(uid: string): Promise { - const db = await this.getDB(); - const sql = `SELECT * - FROM GachaRecords - WHERE uid = '${uid}'`; - return await db.select(sql); - } - - /** - * @description 删除指定 uid 的祈愿数据 - * @since Beta v0.3.3 - * @param {string} uid 用户 uid - * @returns {Promise} - */ - public async deleteGachaRecords(uid: string): Promise { - const db = await this.getDB(); - const sql = `DELETE - FROM GachaRecords - WHERE uid = '${uid}'`; - await db.execute(sql); - } - - /** - * @description 合并祈愿数据 - * @since Beta v0.3.3 - * @param {string} uid UID - * @param {TGApp.Plugins.UIGF.GachaItem[]} data UIGF 数据 - * @returns {Promise} - */ - public async mergeUIGF(uid: string, data: TGApp.Plugins.UIGF.GachaItem[]): Promise { - const db = await this.getDB(); - const sql = importUIGFData(uid, data); - for (const item of sql) { - await db.execute(item); - } - } - - /** - * @description 判断今天是否是某个角色的生日 - * @since Beta v0.3.6 - * @returns {Promise} - */ - public async isBirthday(): Promise { - const db = await this.getDB(); - const dateNow = new Date(); - const date = `${dateNow.getMonth() + 1},${dateNow.getDate()}`; - const sql = `SELECT name - FROM AppCharacters - WHERE birthday = '${date}';`; - const res: Array<{ name: string }> = await db.select(sql); - if (res.length === 0) return false; - return res.map((item) => item.name).join("、"); - } - - /** - * @description 用于检测祈愿增量更新的 gacha id - * @since Beta v0.4.4 - * @param {string} uid 用户 uid - * @param {string} type 卡池类型 - * @returns {Promise} - */ - async getGachaCheck(uid: string, type: string): Promise { - const db = await this.getDB(); - const sql = `SELECT id - FROM GachaRecords - WHERE uid = '${uid}' - AND gachaType = '${type}' - ORDER BY id DESC - LIMIT 1;`; - const res: Array<{ id: string }> = await db.select(sql); - if (res.length === 0) return undefined; - return res[0].id; - } - /** * @description 检测特定表是否存在 * @since Beta v0.4.5 diff --git a/src/plugins/Sqlite/modules/userGacha.ts b/src/plugins/Sqlite/modules/userGacha.ts new file mode 100644 index 00000000..5fa2724e --- /dev/null +++ b/src/plugins/Sqlite/modules/userGacha.ts @@ -0,0 +1,147 @@ +/** + * @file plugins/Sqlite/modules/userGacha.ts + * @description 用户祈愿模块 + * @since Beta v0.4.7 + */ + +import { AppCharacterData, AppWeaponData } from "../../../data/index"; +import TGSqlite from "../index"; +import { importUIGFData } from "../sql/updateData"; + +type gachaItemTypeRes = + | ["角色", TGApp.App.Character.WikiBriefInfo] + | ["武器", TGApp.App.Weapon.WikiBriefInfo] + | ["未知", "未知"]; + +/** + * @description 根据 item_id 获取角色/武器类型 + * @since Beta v0.4.7 + * @param {string} item_id - item_id + * @return {gachaItemTypeRes} + */ +function getGachaItemType(item_id: string): gachaItemTypeRes { + const findAvatar = AppCharacterData.find((i) => i.id === item_id); + if (findAvatar !== undefined) return ["角色", findAvatar]; + const findWeapon = AppWeaponData.find((i) => i.id === item_id); + if (findWeapon !== undefined) return ["武器", findWeapon]; + return ["未知", "未知"]; +} + +/** + * @description 转换祈愿数据,防止多语言 + * @since Beta v0.4.7 + * @param {TGApp.Plugins.UIGF.GachaItem} gacha - UIGF数据 + * @return {TGApp.Plugins.UIGF.GachaItem} 转换后的数据 + */ +function transGacha(gacha: TGApp.Plugins.UIGF.GachaItem): TGApp.Plugins.UIGF.GachaItem { + const type = getGachaItemType(gacha.item_id); + let res = gacha; + res.item_type = type[0]; + if (type[0] === "角色") { + const data: TGApp.App.Character.WikiBriefInfo = type[1]; + res = { + gacha_type: gacha.gacha_type, + item_id: gacha.item_id, + count: gacha.count ?? "1", + time: gacha.time, + name: data.name, + item_type: "角色", + rank_type: data.star.toString(), + id: gacha.id, + uigf_gacha_type: gacha.uigf_gacha_type, + }; + } else if (type[0] === "武器") { + const data: TGApp.App.Weapon.WikiBriefInfo = type[1]; + res = { + gacha_type: gacha.gacha_type, + item_id: gacha.item_id, + count: gacha.count ?? "1", + time: gacha.time, + name: data.name, + item_type: "武器", + rank_type: data.star.toString(), + id: gacha.id, + uigf_gacha_type: gacha.uigf_gacha_type, + }; + } + return res; +} + +/** + * @description 获取数据库的uid列表 + * @since Beta v0.4.7 + * @return {Promise} + */ +async function getUidList(): Promise { + const db = await TGSqlite.getDB(); + type resType = Array<{ uid: string }>; + const res = await db.select("SELECT DISTINCT uid FROM GachaRecords;"); + return res.map((i) => i.uid); +} + +/** + * @description 获取检测增量更新的记录 ID + * @since Beta v0.4.7 + * @param {string} uid - UID + * @param {string} type - 类型 + * @returns {Promise} + */ +async function getGachaCheck(uid: string, type: string): Promise { + const db = await TGSqlite.getDB(); + type resType = Array<{ id: string }>; + const res = await db.select( + "SELECT id FROM GachaRecords WHERE uid = ? AND gachaType = ? ORDER BY id DESC LIMIT 1;", + [uid, type], + ); + if (res.length === 0) return undefined; + return res[0].id; +} + +/** + * @description 获取用户祈愿记录 + * @since Beta v0.4.7 + * @param {string} uid - UID + * @return {Promise} + */ +async function getGachaRecords(uid: string): Promise { + const db = await TGSqlite.getDB(); + return await db.select("SELECT * FROM GachaRecords WHERE uid = ?;", [uid]); +} + +/** + * @description 删除指定UID的祈愿记录 + * @since Beta v0.4.7 + * @param {string} uid - UID + * @return {Promise} + */ +async function deleteGachaRecords(uid: string): Promise { + const db = await TGSqlite.getDB(); + await db.execute("DELETE FROM GachaRecords WHERE uid = ?;", [uid]); +} + +/** + * @description 合并祈愿数据 + * @since Beta v0.4.7 + * @param {string} uid - UID + * @param {TGApp.Plugins.UIGF.GachaItem[]} data - UIGF数据 + * @return {Promise} + */ +async function mergeUIGF(uid: string, data: TGApp.Plugins.UIGF.GachaItem[]): Promise { + const db = await TGSqlite.getDB(); + for (const gacha of data) { + const trans = transGacha(gacha); + console.log(trans); + const sql = importUIGFData(uid, trans); + await db.execute(sql); + } +} + +const TSUserGacha = { + getUidList, + getGachaCheck, + getGachaRecords, + deleteGachaRecords, + mergeUIGF, +}; + +export default TSUserGacha; diff --git a/src/plugins/Sqlite/sql/updateData.ts b/src/plugins/Sqlite/sql/updateData.ts index 7cf8b20d..8bedb46b 100644 --- a/src/plugins/Sqlite/sql/updateData.ts +++ b/src/plugins/Sqlite/sql/updateData.ts @@ -1,11 +1,9 @@ /** - * @file plugins Sqlite sql updateData.ts + * @file plugins/Sqlite/sql/updateData.ts * @description 更新数据 - * @author BTMuli - * @since Alpha v0.2.0 + * @since Beta v0.4.7 */ -// utils import minifySql from "../../../utils/minifySql"; /** @@ -26,15 +24,21 @@ export function importUIAFData(data: TGApp.Plugins.UIAF.Achievement[]): string[] .replace("T", " ") .slice(0, 19); sql = ` - UPDATE Achievements - SET isCompleted = 1, completedTime = '${completedTime}', progress = ${achievement.current}, updated = datetime('now', 'localtime') - WHERE id = ${achievement.id} AND (isCompleted = 0 OR completedTime != '${completedTime}' OR progress != ${achievement.current}); + UPDATE Achievements + SET isCompleted = 1, + completedTime = '${completedTime}', + progress = ${achievement.current}, + updated = datetime('now', 'localtime') + WHERE id = ${achievement.id} + AND (isCompleted = 0 OR completedTime != '${completedTime}' OR progress != ${achievement.current}); `; } else { sql = ` - UPDATE Achievements - SET progress = ${achievement.current}, updated = datetime('now', 'localtime') - WHERE id = ${achievement.id} AND progress != ${achievement.current}; + UPDATE Achievements + SET progress = ${achievement.current}, + updated = datetime('now', 'localtime') + WHERE id = ${achievement.id} + AND progress != ${achievement.current}; `; } return sqlRes.push(minifySql(sql)); @@ -43,37 +47,30 @@ export function importUIAFData(data: TGApp.Plugins.UIAF.Achievement[]): string[] } /** - * @description 导入UIGF数据 - * @since Alpha v0.2.3 + * @description 导入UIGF数据-单项 + * @since Beta v0.4.7 * @param {string} uid - UID - * @param {TGApp.Plugins.UIGF.GachaItem[]} data - UIGF数据 - * @returns {string[]} sql + * @param {TGApp.Plugins.UIGF.GachaItem} gacha - UIGF数据 + * @returns {string} sql */ -export function importUIGFData(uid: string, data: TGApp.Plugins.UIGF.GachaItem[]): string[] { - const sqlRes: string[] = []; - data.forEach((gacha) => { - const sql = ` +export function importUIGFData(uid: string, gacha: TGApp.Plugins.UIGF.GachaItem): string { + 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 - }', + 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; + 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'); + `; + return minifySql(sql); } diff --git a/src/types/Sqlite/GachaRecords.d.ts b/src/types/Sqlite/GachaRecords.d.ts index e1738698..ba1b725b 100644 --- a/src/types/Sqlite/GachaRecords.d.ts +++ b/src/types/Sqlite/GachaRecords.d.ts @@ -23,7 +23,7 @@ declare namespace TGApp.Sqlite.GachaRecords { * @property {string} updated - 数据库更新时间 * @return SingleTable */ - export interface SingleTable { + interface SingleTable { id: string; uid: string; gachaType: string;