mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2025-12-16 09:58:13 +08:00
♻️ 重新建表以存储新格式数据
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* @file plugins/Sqlite/index.ts
|
||||
* @description Sqlite 数据库操作类
|
||||
* @since Beta v0.5.0
|
||||
* @since Beta v0.5.3
|
||||
*/
|
||||
|
||||
import { app } from "@tauri-apps/api";
|
||||
@@ -14,7 +14,6 @@ import {
|
||||
insertAppData,
|
||||
insertGameAccountData,
|
||||
insertRecordData,
|
||||
insertRoleData,
|
||||
} from "./sql/insertData.js";
|
||||
|
||||
class Sqlite {
|
||||
@@ -289,60 +288,6 @@ class Sqlite {
|
||||
return res[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 保存用户角色数据
|
||||
* @since Beta v0.3.3
|
||||
* @param {string} uid 用户 uid
|
||||
* @param {TGApp.Game.Character.ListItem[]} data 角色数据
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public async saveUserCharacter(
|
||||
uid: string,
|
||||
data: TGApp.Game.Character.ListItem[],
|
||||
): Promise<void> {
|
||||
const db = await this.getDB();
|
||||
const sql = insertRoleData(uid, data);
|
||||
await db.execute(sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 保存用户角色天赋数据
|
||||
* @since Beta v0.3.3
|
||||
* @param {string} uid 用户 uid
|
||||
* @param {number} cid 角色 ID
|
||||
* @param {TGApp.Sqlite.Character.RoleTalent[]} data 角色天赋数据
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public async saveUserCharacterTalent(
|
||||
uid: string,
|
||||
cid: number,
|
||||
data: TGApp.Sqlite.Character.RoleTalent[],
|
||||
): Promise<void> {
|
||||
const db = await this.getDB();
|
||||
const sql = `UPDATE UserCharacters
|
||||
SET talent = '${JSON.stringify(data)}',
|
||||
updated = datetime('now', 'localtime')
|
||||
WHERE uid = '${uid}'
|
||||
AND cid = ${cid}`;
|
||||
await db.execute(sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取用户角色数据
|
||||
* @since Beta v0.3.3
|
||||
* @param {string} uid 用户 uid
|
||||
* @returns {Promise<TGApp.Sqlite.Character.UserRole[]|false>}
|
||||
*/
|
||||
public async getUserCharacter(uid: string): Promise<TGApp.Sqlite.Character.UserRole[] | false> {
|
||||
const db = await this.getDB();
|
||||
const sql = `SELECT *
|
||||
FROM UserCharacters
|
||||
WHERE uid = '${uid}'`;
|
||||
const res: TGApp.Sqlite.Character.UserRole[] = await db.select(sql);
|
||||
if (res.length === 0) return false;
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 检测特定表是否存在
|
||||
* @since Beta v0.4.5
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
|
||||
import TGSqlite from "../index.js";
|
||||
import { insertRoleData } from "../sql/insertData.js";
|
||||
|
||||
/**
|
||||
* @description 获取用户角色id列表
|
||||
@@ -18,8 +19,52 @@ async function getAllAvatarId(): Promise<string[]> {
|
||||
return res.map((i) => i.cid);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取用户角色数据
|
||||
* @since Beta v0.5.3
|
||||
* @param {string} uid 用户 uid
|
||||
* @returns {Promise<TGApp.Sqlite.Character.UserRole[]>}
|
||||
*/
|
||||
async function getAvatars(uid: string): Promise<TGApp.Sqlite.Character.UserRole[]> {
|
||||
const db = await TGSqlite.getDB();
|
||||
type resType = Array<TGApp.Sqlite.Character.UserRoleDB>;
|
||||
const res = await db.select<resType>("SELECT * FROM UserCharacters WHERE uid = ?;", [uid]);
|
||||
return res.map((i) => {
|
||||
return {
|
||||
uid: i.uid,
|
||||
cid: i.cid,
|
||||
avatar: JSON.parse(i.avatar),
|
||||
weapon: JSON.parse(i.weapon),
|
||||
relics: JSON.parse(i.relics),
|
||||
constellations: JSON.parse(i.constellations),
|
||||
costumes: JSON.parse(i.costumes),
|
||||
skills: JSON.parse(i.skills),
|
||||
propSelected: JSON.parse(i.propSelected),
|
||||
propBase: JSON.parse(i.propBase),
|
||||
propExtra: JSON.parse(i.propExtra),
|
||||
propRecommend: JSON.parse(i.propRecommend),
|
||||
updated: i.updated,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 保存用户角色数据
|
||||
* @since Beta v0.5.3
|
||||
* @param {string} uid 用户 uid
|
||||
* @param {TGApp.Game.Avatar.DetailList[]} data 角色数据
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async function saveAvatars(uid: string, data: TGApp.Game.Avatar.DetailList[]): Promise<void> {
|
||||
const db = await TGSqlite.getDB();
|
||||
const sql = insertRoleData(uid, data);
|
||||
await db.execute(sql);
|
||||
}
|
||||
|
||||
const TSUserAvatar = {
|
||||
getAllAvatarId,
|
||||
getAvatars,
|
||||
saveAvatars,
|
||||
};
|
||||
|
||||
export default TSUserAvatar;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
-- @file plugins Sqlite sql createTable.sql
|
||||
-- @file plugins/Sqlite/sql/createTable.sql
|
||||
-- @brief sqlite数据库创建表语句
|
||||
-- @since Beta v0.4.5
|
||||
-- @since Beta v0.5.3
|
||||
|
||||
-- @brief 创建成就数据表
|
||||
create table if not exists Achievements
|
||||
@@ -114,23 +114,24 @@ create table if not exists UserRecord
|
||||
updated text
|
||||
);
|
||||
|
||||
-- @brief 删除旧的角色数据表
|
||||
drop table if exists UserCharacters;
|
||||
|
||||
-- @brief 创建角色数据表
|
||||
create table if not exists UserCharacters
|
||||
(
|
||||
uid integer,
|
||||
cid integer,
|
||||
img text,
|
||||
name text,
|
||||
fetter integer,
|
||||
level integer,
|
||||
element text,
|
||||
star integer,
|
||||
avatar text,
|
||||
weapon text,
|
||||
reliquary text,
|
||||
constellation text,
|
||||
activeConstellation integer,
|
||||
costume text,
|
||||
talent text,
|
||||
relics text,
|
||||
constellations text,
|
||||
costumes text,
|
||||
skills text,
|
||||
propSelected text,
|
||||
propBase text,
|
||||
propExtra text,
|
||||
propRecommend text,
|
||||
updated text,
|
||||
primary key (uid, cid)
|
||||
);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* @file plugins Sqlite sql insertData.ts
|
||||
* @file plugins/Sqlite/sql/insertData.ts
|
||||
* @description Sqlite 插入数据 sql 语句
|
||||
* @since Beta v0.3.3
|
||||
* @since Beta v0.5.3
|
||||
*/
|
||||
|
||||
import { transCharacterData, transFloorData } from "../utils/transAbyssData.js";
|
||||
@@ -237,34 +237,32 @@ export function insertRecordData(data: TGApp.Game.Record.FullData, uid: string):
|
||||
|
||||
/**
|
||||
* @description 插入用户角色数据
|
||||
* @since Alpha v0.2.1
|
||||
* @since Beta v0.5.3
|
||||
* @param {string} uid 用户 UID
|
||||
* @param {TGApp.User.Character.Item[]} data 角色数据
|
||||
* @param {TGApp.Game.Avatar.DetailList[]} data 角色数据
|
||||
* @returns {string} sql
|
||||
*/
|
||||
export function insertRoleData(uid: string, data: TGApp.Game.Character.ListItem[]): string {
|
||||
export function insertRoleData(uid: string, data: TGApp.Game.Avatar.DetailList[]): string {
|
||||
const sql = data.map((item) => {
|
||||
const role = transUserRoles(item);
|
||||
return `
|
||||
INSERT INTO UserCharacters (uid, cid, name, img, name, fetter, level, element, star, weapon, reliquary,
|
||||
constellation, activeConstellation, costume, updated)
|
||||
VALUES (${uid}, ${role.cid}, '${role.name}', '${role.img}', '${role.name}', ${role.fetter}, ${role.level},
|
||||
'${role.element}', ${role.star}, '${role.weapon}', '${role.reliquary}', '${role.constellation}',
|
||||
${role.activeConstellation}, '${role.costume}', datetime('now', 'localtime'))
|
||||
INSERT INTO UserCharacters (uid, cid, avatar, weapon, relics, constellations, costumes, skills,
|
||||
propSelected, propBase, propExtra, propRecommend, updated)
|
||||
VALUES (${uid}, ${role.cid}, '${role.avatar}', '${role.weapon}', '${role.relics}', '${role.constellations}',
|
||||
'${role.costumes}', '${role.skills}', '${role.propSelected}', '${role.propBase}', '${role.propExtra}',
|
||||
'${role.propRecommend}', datetime('now', 'localtime'))
|
||||
ON CONFLICT(uid, cid) DO UPDATE
|
||||
SET name = '${role.name}',
|
||||
img = '${role.img}',
|
||||
name = '${role.name}',
|
||||
fetter = ${role.fetter},
|
||||
level = ${role.level},
|
||||
element = '${role.element}',
|
||||
star = ${role.star},
|
||||
weapon = '${role.weapon}',
|
||||
reliquary = '${role.reliquary}',
|
||||
constellation = '${role.constellation}',
|
||||
activeConstellation = ${role.activeConstellation},
|
||||
costume = '${role.costume}',
|
||||
updated = datetime('now', 'localtime');
|
||||
SET avatar = '${role.avatar}',
|
||||
weapon = '${role.weapon}',
|
||||
relics = '${role.relics}',
|
||||
constellations = '${role.constellations}',
|
||||
costumes = '${role.costumes}',
|
||||
skills = '${role.skills}',
|
||||
propSelected = '${role.propSelected}',
|
||||
propBase = '${role.propBase}',
|
||||
propExtra = '${role.propExtra}',
|
||||
propRecommend = '${role.propRecommend}',
|
||||
updated = datetime('now', 'localtime');
|
||||
`;
|
||||
});
|
||||
return sql.join("");
|
||||
|
||||
@@ -1,157 +1,31 @@
|
||||
/**
|
||||
* @file plugins Sqlite utils transUserRoles.ts
|
||||
* @file plugins/Sqlite/utils/transUserRoles.ts
|
||||
* @description 转换用户角色数据格式,用于数据库存储
|
||||
* @author BTMuli <bt-muli@outlook.com>
|
||||
* @since Alpha v0.2.0
|
||||
* @since Beta v0.5.3
|
||||
*/
|
||||
|
||||
enum EnumElementEn {
|
||||
pyro = "Pyro",
|
||||
hydro = "Hydro",
|
||||
anemo = "Anemo",
|
||||
electro = "Electro",
|
||||
cryo = "Cryo",
|
||||
geo = "Geo",
|
||||
dendro = "Dendro",
|
||||
}
|
||||
|
||||
enum EnumElement {
|
||||
pyro = "火元素",
|
||||
hydro = "水元素",
|
||||
anemo = "风元素",
|
||||
electro = "雷元素",
|
||||
cryo = "冰元素",
|
||||
geo = "岩元素",
|
||||
dendro = "草元素",
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 将通过 api 获取到的用户角色数据转换为数据库中的数据
|
||||
* @since Alpha v0.2.0
|
||||
* @since Beta v0.5.3
|
||||
* @param {TGApp.Game.Character.ListItem} data 用户角色数据
|
||||
* @returns {TGApp.Sqlite.Character.UserRole} 转换后的用户角色数据
|
||||
*/
|
||||
export function transUserRoles(
|
||||
data: TGApp.Game.Character.ListItem,
|
||||
): TGApp.Sqlite.Character.UserRole {
|
||||
data: TGApp.Game.Avatar.DetailList,
|
||||
): TGApp.Sqlite.Character.UserRoleDB {
|
||||
return {
|
||||
uid: -1,
|
||||
cid: data.id,
|
||||
img: data.image,
|
||||
name: data.name,
|
||||
fetter: data.fetter,
|
||||
level: data.level,
|
||||
element: transElement(data.element),
|
||||
star: data.rarity === 105 ? 5 : data.rarity,
|
||||
weapon: transWeapon(data.weapon),
|
||||
reliquary: transReliquary(data.reliquaries),
|
||||
constellation: transConstellation(data.constellations),
|
||||
activeConstellation: data.actived_constellation_num,
|
||||
costume: JSON.stringify(data.costumes),
|
||||
talent: "",
|
||||
cid: data.base.id,
|
||||
avatar: JSON.stringify(data.base),
|
||||
weapon: JSON.stringify(data.weapon),
|
||||
relics: JSON.stringify(data.relics),
|
||||
constellations: JSON.stringify(data.constellations),
|
||||
costumes: JSON.stringify(data.costumes),
|
||||
skills: JSON.stringify(data.skills),
|
||||
propSelected: JSON.stringify(data.selected_properties),
|
||||
propBase: JSON.stringify(data.base_properties),
|
||||
propExtra: JSON.stringify(data.extra_properties),
|
||||
propRecommend: JSON.stringify(data.recommend_relic_property),
|
||||
updated: "",
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 将角色元素转换为数据库中的数据
|
||||
* @since Alpha v0.2.0
|
||||
* @param {EnumElementEn} data 角色元素
|
||||
* @returns {EnumElement} 转换后的角色元素
|
||||
*/
|
||||
function transElement(data: EnumElementEn): EnumElement {
|
||||
switch (data) {
|
||||
case EnumElementEn.pyro:
|
||||
return EnumElement.pyro;
|
||||
case EnumElementEn.hydro:
|
||||
return EnumElement.hydro;
|
||||
case EnumElementEn.anemo:
|
||||
return EnumElement.anemo;
|
||||
case EnumElementEn.electro:
|
||||
return EnumElement.electro;
|
||||
case EnumElementEn.cryo:
|
||||
return EnumElement.cryo;
|
||||
case EnumElementEn.geo:
|
||||
return EnumElement.geo;
|
||||
case EnumElementEn.dendro:
|
||||
return EnumElement.dendro;
|
||||
default:
|
||||
throw new Error("未知的角色元素类型");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 将角色武器转换为数据库中的数据
|
||||
* @since Alpha v0.2.0
|
||||
* @param {TGApp.Game.Character.LIWeapon} data 角色武器
|
||||
* @returns {string} 转换后的角色武器
|
||||
*/
|
||||
function transWeapon(data: TGApp.Game.Character.LIWeapon): string {
|
||||
const weapon: TGApp.Sqlite.Character.RoleWeapon = {
|
||||
id: data.id,
|
||||
name: data.name,
|
||||
type: data.type_name,
|
||||
star: data.rarity,
|
||||
level: data.level,
|
||||
promote: data.promote_level,
|
||||
description: data.desc,
|
||||
affix: data.affix_level,
|
||||
};
|
||||
return JSON.stringify(weapon);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 将角色命座转换为数据库中的数据
|
||||
* @since Alpha v0.2.0
|
||||
* @param {TGApp.Game.Character.LIRelic[]} data 角色命座
|
||||
* @returns {string} 转换后的角色命座
|
||||
*/
|
||||
function transReliquary(data: TGApp.Game.Character.LIRelic[]): string {
|
||||
if (data.length === 0) {
|
||||
return "";
|
||||
}
|
||||
const reliquary: TGApp.Sqlite.Character.RoleReliquary[] = [];
|
||||
for (const item of data) {
|
||||
reliquary.push({
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
pos: item.pos,
|
||||
posName: item.pos_name,
|
||||
star: item.rarity,
|
||||
level: item.level,
|
||||
icon: item.icon,
|
||||
set: {
|
||||
id: item.set.id,
|
||||
name: item.set.name,
|
||||
effect: item.set.affixes.map((setItem) => {
|
||||
return {
|
||||
active: setItem.activation_number,
|
||||
description: setItem.effect,
|
||||
};
|
||||
}),
|
||||
},
|
||||
});
|
||||
}
|
||||
return JSON.stringify(reliquary);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 将角色命座转换为数据库中的数据
|
||||
* @since Alpha v0.2.0
|
||||
* @param {TGApp.Game.Character.LIConstellation[]} data 角色命座
|
||||
* @returns {string} 转换后的角色命座
|
||||
*/
|
||||
function transConstellation(data: TGApp.Game.Character.LIConstellation[]): string {
|
||||
const constellation: TGApp.Sqlite.Character.RoleConstellation[] = [];
|
||||
for (const item of data) {
|
||||
constellation.push({
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
icon: item.icon,
|
||||
description: item.effect,
|
||||
active: item.is_actived,
|
||||
pos: item.pos,
|
||||
});
|
||||
}
|
||||
return JSON.stringify(constellation);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user