diff --git a/src/components/userRecord/tur-avatar-grid.vue b/src/components/userRecord/tur-avatar-grid.vue index c20ea03d..0413e06e 100644 --- a/src/components/userRecord/tur-avatar-grid.vue +++ b/src/components/userRecord/tur-avatar-grid.vue @@ -1,21 +1,18 @@ diff --git a/src/components/userRecord/tur-world-grid.vue b/src/components/userRecord/tur-world-grid.vue index 43920b0c..765712f5 100644 --- a/src/components/userRecord/tur-world-grid.vue +++ b/src/components/userRecord/tur-world-grid.vue @@ -1,22 +1,17 @@ diff --git a/src/plugins/Sqlite/index.ts b/src/plugins/Sqlite/index.ts index 8190553d..da582289 100644 --- a/src/plugins/Sqlite/index.ts +++ b/src/plugins/Sqlite/index.ts @@ -8,7 +8,7 @@ import { app } from "@tauri-apps/api"; import Database from "@tauri-apps/plugin-sql"; import initDataSql from "./sql/initData.js"; -import { insertAppData, insertRecordData } from "./sql/insertData.js"; +import { insertAppData } from "./sql/insertData.js"; class Sqlite { /** @@ -131,35 +131,6 @@ class Sqlite { await this.initDB(); } - /** - * @description 保存战绩数据 - * @since Beta v0.3.3 - * @param {TGApp.Game.Record.FullData} data 战绩数据 - * @param {string} uid 用户 uid - * @returns {Promise} - */ - public async saveUserRecord(data: TGApp.Game.Record.FullData, uid: string): Promise { - const db = await this.getDB(); - const sql = insertRecordData(data, uid); - await db.execute(sql); - } - - /** - * @description 获取战绩数据 - * @since Beta v0.3.3 - * @param {string} uid 用户 uid - * @returns {Promise} - */ - public async getUserRecord(uid: string): Promise { - const db = await this.getDB(); - const sql = `SELECT * - FROM UserRecord - WHERE uid = '${uid}'`; - const res: TGApp.Sqlite.Record.SingleTable[] = await db.select(sql); - if (res.length === 0) return false; - return res[0]; - } - /** * @description 检测特定表是否存在 * @since Beta v0.4.5 diff --git a/src/plugins/Sqlite/modules/userAvatar.ts b/src/plugins/Sqlite/modules/userAvatar.ts index 3ff238fc..4b10d3eb 100644 --- a/src/plugins/Sqlite/modules/userAvatar.ts +++ b/src/plugins/Sqlite/modules/userAvatar.ts @@ -59,18 +59,18 @@ function getInsertSql(uid: string, data: TGApp.Game.Avatar.DetailList): string { */ async function getAllUid(): Promise { const db = await TGSqlite.getDB(); - type resType = Array<{ uid: string }>; + type resType = Array<{ uid: number }>; const res = await db.select("SELECT DISTINCT uid FROM UserCharacters;"); - return res.map((i) => i.uid); + return res.map((i) => i.uid.toString()); } /** * @description 获取用户角色数据 * @since Beta v0.5.3 - * @param {string} uid 用户 uid + * @param {number} uid 用户 uid * @returns {Promise} */ -async function getAvatars(uid: string): Promise { +async function getAvatars(uid: number): Promise { const db = await TGSqlite.getDB(); type resType = Array; const res = await db.select("SELECT * FROM UserCharacters WHERE uid = ?;", [uid]); diff --git a/src/plugins/Sqlite/modules/userRecord.ts b/src/plugins/Sqlite/modules/userRecord.ts new file mode 100644 index 00000000..af723e3f --- /dev/null +++ b/src/plugins/Sqlite/modules/userRecord.ts @@ -0,0 +1,133 @@ +/** + * @file plugins/Sqlite/modules/userRecord.ts + * @description Sqlite-用户战绩模块 + * @since Beta v0.6.0 + */ + +import { timestampToDate } from "../../../utils/toolFunc.js"; +import TGSqlite from "../index.js"; +import { transUserRecord } from "../utils/transUserRecord.js"; + +/** + * @description 获取插入Sql + * @since Beta v0.6.0 + * @param {number} uid - 游戏UID + * @param {TGApp.Sqlite.Record.SingleTable} data - 战绩数据 + * @returns {string} + */ +function getInsertSql(uid: number, data: TGApp.Sqlite.Record.SingleTable): string { + return ` + INSERT INTO UserRecord(uid, role, avatars, stats, worldExplore, homes, updated) + VALUES (${uid}, '${data.role}', '${data.avatars}', '${data.stats}', + '${data.worldExplore}', '${data.homes}', '${data.updated}') + ON CONFLICT(uid) DO UPDATE + SET role = '${data.role}', + avatars = '${data.avatars}', + stats = '${data.stats}', + worldExplore = '${data.worldExplore}', + homes = '${data.homes}', + updated = '${data.updated}'; + `; +} + +/** + * @description 解析数据库数据 + * @since Beta v0.6.0 + * @param {TGApp.Sqlite.Record.SingleTable} data - 数据库数据 + * @returns {TGApp.Sqlite.Record.RenderData} 渲染数据 + */ +function parseRecord(data: TGApp.Sqlite.Record.SingleTable): TGApp.Sqlite.Record.RenderData { + return { + uid: data.uid, + role: JSON.parse(data.role), + avatars: JSON.parse(data.avatars), + stats: JSON.parse(data.stats), + worldExplore: JSON.parse(data.worldExplore), + homes: JSON.parse(data.homes), + updated: data.updated, + }; +} + +/** + * @description 转换数据库数据 + * @since Beta v0.6.0 + * @param {number} uid - 游戏UID + * @param {TGApp.Game.Record.FullData} data - 战绩数据 + * @returns {TGApp.Sqlite.Record.SingleTable} + */ +function transRecord( + uid: number, + data: TGApp.Game.Record.FullData, +): TGApp.Sqlite.Record.SingleTable { + const transData = transUserRecord(uid, data); + return { + uid: uid, + role: JSON.stringify(transData.role), + avatars: JSON.stringify(transData.avatars), + stats: JSON.stringify(transData.stats), + worldExplore: JSON.stringify(transData.worldExplore), + homes: JSON.stringify(transData.homes), + updated: timestampToDate(new Date().getTime()), + }; +} + +/** + * @description 获取UID列表 + * @since Beta v0.6.0 + * @returns {Promise} + */ +async function getAllUid(): Promise { + const db = await TGSqlite.getDB(); + type resType = Array<{ uid: number }>; + const res = await db.select("SELECT DISTINCT uid FROM UserRecord;"); + return res.map((e) => e.uid); +} + +/** + * @description 获取指定UID的战绩数据 + * @since Beta v0.6.0 + * @param {number} uid - 游戏UID + * @returns {Promise} + */ +async function getRecord(uid: number): Promise { + const db = await TGSqlite.getDB(); + const res = await db.select( + "SELECT * FROM UserRecord WHERE uid = ?;", + [uid], + ); + if (res.length === 0) return false; + return parseRecord(res[0]); +} + +/** + * @description 保存战绩数据 + * @since Beta v0.6.0 + * @param {number} uid - 游戏UID + * @param {TGApp.Game.Record.FullData} data - 战绩数据 + * @returns {Promise} + */ +async function saveRecord(uid: number, data: TGApp.Game.Record.FullData): Promise { + const db = await TGSqlite.getDB(); + const sql = getInsertSql(uid, transRecord(uid, data)); + await db.execute(sql); +} + +/** + * @description 删除战绩数据 + * @since Beta v0.6.0 + * @param {number} uid + * @returns {Promise} + */ +async function deleteUid(uid: number): Promise { + const db = await TGSqlite.getDB(); + await db.execute("DELETE FROM UserRecord WHERE uid=?;", [uid]); +} + +const TSUserRecord = { + getAllUid, + getRecord, + saveRecord, + deleteUid, +}; + +export default TSUserRecord; diff --git a/src/plugins/Sqlite/sql/insertData.ts b/src/plugins/Sqlite/sql/insertData.ts index 8a29205f..650de6b7 100644 --- a/src/plugins/Sqlite/sql/insertData.ts +++ b/src/plugins/Sqlite/sql/insertData.ts @@ -4,9 +4,6 @@ * @since Beta v0.6.0 */ -import { timestampToDate } from "../../../utils/toolFunc.js"; -import { transUserRecord } from "../utils/transUserRecord.js"; - /** * @description 插入应用数据 * @since Alpha v0.2.0 @@ -22,28 +19,3 @@ export function insertAppData(key: string, value: string): string { updated = datetime('now', 'localtime'); `; } - -/** - * @description 插入原神战绩数据 - * @since Beta v0.6.0 - * @param {TGApp.Game.Record.FullData} data 原神战绩数据 - * @param {string} uid 用户 UID - * @returns {string} sql - */ -export function insertRecordData(data: TGApp.Game.Record.FullData, uid: string): string { - const transData = transUserRecord(data); - const timeNow = timestampToDate(new Date().getTime()); - transData.uid = uid; - return ` - INSERT INTO UserRecord(uid, role, avatars, stats, worldExplore, homes, updated) - VALUES ('${transData.uid}', '${transData.role}', '${transData.avatars}', '${transData.stats}', - '${transData.worldExplore}', '${transData.homes}', '${timeNow}') - ON CONFLICT(uid) DO UPDATE - SET role = '${transData.role}', - avatars = '${transData.avatars}', - stats = '${transData.stats}', - worldExplore = '${transData.worldExplore}', - homes = '${transData.homes}', - updated = '${timeNow}'; - `; -} diff --git a/src/plugins/Sqlite/utils/transUserRecord.ts b/src/plugins/Sqlite/utils/transUserRecord.ts index be7b61d2..aab065f8 100644 --- a/src/plugins/Sqlite/utils/transUserRecord.ts +++ b/src/plugins/Sqlite/utils/transUserRecord.ts @@ -1,49 +1,53 @@ /** * @file plugins/Sqlite/utils/transUserRecord.ts * @description Sqlite 数据转换 用户战绩数据转换模块 - * @since Beta v0.5.5 + * @since Beta v0.6.0 */ /** - * @description 将通过 api 获取到的用户战绩数据转换为数据库中的数据 - * @since Beta v0.4.3 + * @description 将通过 api 获取到的用户战绩数据转换为渲染用的数据 + * @since Beta v0.6.0 + * @param {number} uid - 用户UID * @param {TGApp.Game.Record.FullData} data 用户战绩数据 - * @returns {TGApp.Sqlite.Record.SingleTable} 转换后的用户战绩数据 + * @returns {TGApp.Sqlite.Record.RenderData} 转换后的用户战绩数据 */ -export function transUserRecord(data: TGApp.Game.Record.FullData): TGApp.Sqlite.Record.SingleTable { +export function transUserRecord( + uid: number, + data: TGApp.Game.Record.FullData, +): TGApp.Sqlite.Record.RenderData { return { - uid: "", + uid: uid, role: transRole(data.role), - avatars: transAvatar(data.avatars), + avatars: data.avatars.map(transAvatar).sort((a, b) => b.star - a.star || b.id - a.id), stats: transStat(data.stats), - worldExplore: JSON.stringify(transWorld(data.world_explorations)), - homes: transHome(data.homes), + worldExplore: transWorld(data.world_explorations), + homes: data.homes.map(transHome), updated: "", }; } /** * @description 将角色信息转换为数据库中的数据 - * @since Alpha v0.2.0 + * @since Beta v0.6.0 * @param {TGApp.Game.Record.Role} data 角色信息 - * @returns {string} 转换后的角色信息 + * @returns {TGApp.Sqlite.Record.Role} 转换后的角色信息 */ -function transRole(data: TGApp.Game.Record.Role): string { - const role: TGApp.Sqlite.Record.Role = { +function transRole(data: TGApp.Game.Record.Role): TGApp.Sqlite.Record.Role { + return { nickname: data.nickname, region: data.region, level: data.level, + avatar: data.game_head_icon, }; - return JSON.stringify(role); } /** * @description 将角色列表转换为数据库中的数据 - * @since Alpha v0.2.0 - * @param {TGApp.Game.Record.Avatar[]} data 角色列表 - * @returns {string} 转换后的角色列表 + * @since Beta v0.6.0 + * @param {TGApp.Game.Record.Avatar} data 角色列表 + * @returns {TGApp.Sqlite.Record.Avatar} 转换后的角色列表 */ -function transAvatar(data: TGApp.Game.Record.Avatar[]): string { +function transAvatar(data: TGApp.Game.Record.Avatar): TGApp.Sqlite.Record.Avatar { const elementMap: Record = { Anemo: "风", Geo: "岩", @@ -53,44 +57,26 @@ function transAvatar(data: TGApp.Game.Record.Avatar[]): string { Cryo: "冰", Dendro: "草", }; - const avatars: TGApp.Sqlite.Record.Avatar[] = data - .map((item) => { - return { - id: item.id, - name: item.name, - element: elementMap[item.element], - fetter: item.fetter, - level: item.level, - star: item.rarity === 105 ? 5 : item.rarity, - constellation: item.actived_constellation_num, - isShow: item.is_chosen ? 1 : 0, - }; - }) - .sort((a, b) => { - // 先按星级降序 - if (a.star !== b.star) { - return b.star - a.star; - } - // 再按 id 降序 - return b.id - a.id; - }); - return JSON.stringify(avatars); + return { + id: data.id, + name: data.name, + element: elementMap[data.element], + fetter: data.fetter, + level: data.level, + star: data.rarity === 105 ? 5 : data.rarity, + constellation: data.actived_constellation_num, + isShow: data.is_chosen ? 1 : 0, + }; } /** * @description 将统计信息转换为数据库中的数据 - * @since Beta v0.5.5 + * @since Beta v0.6.0 * @param {TGApp.Game.Record.Stats} data 统计信息 - * @return {string} 转换后的统计信息 + * @return {TGApp.Sqlite.Record.Stats } 转换后的统计信息 */ -function transStat(data: TGApp.Game.Record.Stats): string { - let combatRole: string; - if (!data.role_combat.is_unlock) { - combatRole = "未解锁"; - } else { - combatRole = `第 ${data.role_combat.max_round_id} 幕`; - } - const stats: TGApp.Sqlite.Record.Stats = { +function transStat(data: TGApp.Game.Record.Stats): TGApp.Sqlite.Record.Stats { + return { activeDays: data.active_day_number, achievementNumber: data.achievement_number, avatarNumber: data.avatar_number, @@ -104,14 +90,13 @@ function transStat(data: TGApp.Game.Record.Stats): string { hydroCulus: data.hydroculus_number, pyroCulus: data.pyroculus_number, sprialAbyss: data.spiral_abyss, - combatRole: combatRole, + combatRole: data.role_combat.is_unlock ? `第 ${data.role_combat.max_round_id} 幕` : "未解锁", luxuriousChest: data.luxurious_chest_number, preciousChest: data.precious_chest_number, exquisiteChest: data.exquisite_chest_number, commonChest: data.common_chest_number, magicChest: data.magic_chest_number, }; - return JSON.stringify(stats); } /** @@ -170,22 +155,19 @@ function transWorld(data: TGApp.Game.Record.WorldExplore[]): TGApp.Sqlite.Record /** * @description 将住宅信息转换为数据库中的数据 - * @since Alpha v0.2.0 - * @param {TGApp.Game.Record.Home[]} data 住宅信息 - * @returns {string} 转换后的住宅信息 + * @since Beta v0.6.0 + * @param {TGApp.Game.Record.Home} data 住宅信息 + * @returns {TGApp.Sqlite.Record.Home} 转换后的住宅信息 */ -function transHome(data: TGApp.Game.Record.Home[]): string { - const homes: TGApp.Sqlite.Record.Home[] = data.map((item) => { - return { - comfortIcon: item.comfort_level_icon, - comfortName: item.comfort_level_name, - name: item.name, - level: item.level, - comfort: item.comfort_num, - furniture: item.item_num, - visit: item.visit_num, - bg: item.icon, - }; - }); - return JSON.stringify(homes); +function transHome(data: TGApp.Game.Record.Home): TGApp.Sqlite.Record.Home { + return { + comfortIcon: data.comfort_level_icon, + comfortName: data.comfort_level_name, + name: data.name, + level: data.level, + comfort: data.comfort_num, + furniture: data.item_num, + visit: data.visit_num, + bg: data.icon, + }; } diff --git a/src/types/Sqlite/Record.d.ts b/src/types/Sqlite/Record.d.ts index 8fadfce1..49f76029 100644 --- a/src/types/Sqlite/Record.d.ts +++ b/src/types/Sqlite/Record.d.ts @@ -1,12 +1,12 @@ /** * @file types/Sqlite/Record.d.ts * @description Sqlite 原神战绩相关类型定义文件 - * @since Beta v0.5.5 + * @since Beta v0.6.0 */ /** * @description Sqlite 原神战绩相关类型定义命名空间 - * @since Beta v0.5.5 + * @since Beta v0.6.0 * @namespace Record * @memberof TGApp.Sqlite */ @@ -14,40 +14,64 @@ declare namespace TGApp.Sqlite.Record { /** * @description 原神战绩数据表 * @interface SingleTable - * @since Alpha v0.2.0 - * @property {string} uid - UID - * @description 下面的数据在数据库中以 string 类型存储 - * @property {Role} role - 角色信息 + * @since Beta v0.6.0 + * @property {number} uid - UID + * @property {string} role - 角色信息 + * @property {string} avatars - 角色列表 + * @property {string} stats - 统计信息 + * @property {string} worldExplore - 世界探索信息 + * @property {string} homes - 尘歌壶信息 + * @property {string} updated - 更新时间 + * @return SingleTable + */ + interface SingleTable { + uid: number; + role: string; + avatars: string; + stats: string; + worldExplore: string; + homes: string; + updated: string; + } + + /** + * @description 渲染用数据 + * @interface RenderData + * @since Beta v0.6.0 + * @property {number} uid - UID + * @property {Role} role - 用户信息 * @property {Avatar[]} avatars - 角色列表 * @property {Stats} stats - 统计信息 - * @property {WorldExplore} worldExplore - 世界探索信息 + * @property {WorldExplore[]} worldExplore - 世界探索信息 * @property {Home[]} homes - 尘歌壶信息 * @property {string} updated - 更新时间 - * @return SingleTable + * @returns RenderData */ - interface SingleTable { - uid: string; - role: string; // Role - avatars: string; // Avatar[] - stats: string; // Stats - worldExplore: string; // WorldExplore - homes: string; // Home[] + interface RenderData { + uid: number; + role: Role; + avatars: Avatar[]; + stats: Stats; + worldExplore: WorldExplore[]; + homes: Home[]; updated: string; } /** * @description 角色信息类型 * @interface Role - * @since Alpha v0.2.0 + * @since Beta v0.6.0 * @property {string} nickname - 角色昵称 * @property {string} region - 区域 * @property {number} level - 等级 + * @property {string} avatar - 头像 * @return Role */ interface Role { nickname: string; region: string; level: number; + avatar: string; } /**