From bf52e1f8badeeb9d60ede91a40c54344d5a423a3 Mon Sep 17 00:00:00 2001 From: ohmyga Date: Sun, 14 May 2023 07:48:17 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20fix:=20warp=5Fid=20inco?= =?UTF-8?q?ming=20type=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/application.dart | 7 +- lib/config/db.dart | 3 +- .../sr/services/tools/warp/cache_update.dart | 36 ++++---- lib/libs/sr/services/tools/warp/db.dart | 2 + lib/utils/storage/sqlite.dart | 13 ++- lib/utils/storage/sqlite_fix.dart | 83 +++++++++++++++++++ 6 files changed, 120 insertions(+), 24 deletions(-) create mode 100644 lib/utils/storage/sqlite_fix.dart diff --git a/lib/application.dart b/lib/application.dart index f4ee9a6..ba2bb0c 100644 --- a/lib/application.dart +++ b/lib/application.dart @@ -1,3 +1,8 @@ +/// +/// Author: ohmyga +/// Date: 2023-05-13 17:45:29 +/// LastEditTime: 2023-05-14 07:34:56 +/// /// =========================================================================== /// Copyright (c) 2020-2023, BoxCat. All rights reserved. /// Date: 2023-04-28 18:09:31 @@ -11,7 +16,7 @@ import 'package:go_router/go_router.dart'; class Application { static late GoRouter router; static late GlobalKey rootNavigatorKey; - static int appNumVersion = 100002; + static int appNumVersion = 100006; /* App NavigatorKey */ static GlobalKey appNavigatorKey = GlobalKey(); diff --git a/lib/config/db.dart b/lib/config/db.dart index 65418a5..ac49bc8 100644 --- a/lib/config/db.dart +++ b/lib/config/db.dart @@ -18,7 +18,8 @@ class SCDatabaseConfig { /// Warp Table Name static String warpIndexTable = "warp_index"; - static String warpGachaLogTable = "gacha_log"; + static String warpGachaLogOldTable = "gacha_log"; + static String warpGachaLogTable = "warp_gacha_log"; static String warpItemTable = "item_data"; /// DELETE /// Data Table Name diff --git a/lib/libs/sr/services/tools/warp/cache_update.dart b/lib/libs/sr/services/tools/warp/cache_update.dart index 7c13bc4..d802f37 100644 --- a/lib/libs/sr/services/tools/warp/cache_update.dart +++ b/lib/libs/sr/services/tools/warp/cache_update.dart @@ -1,7 +1,7 @@ /// =========================================================================== /// Copyright (c) 2020-2023, BoxCat. All rights reserved. /// Date: 2023-05-02 01:52:42 -/// LastEditTime: 2023-05-06 00:59:44 +/// LastEditTime: 2023-05-13 18:59:59 /// FilePath: /lib/libs/sr/services/tools/warp/cache_update.dart /// =========================================================================== // ignore_for_file: use_build_context_synchronously @@ -39,15 +39,14 @@ class SrWrapToolCacheUpdateService { } /// 判断数据库中是否包含相同抽卡记录 - static bool _isSameData(String id, List> allData) { - bool isSame = false; - for (var item in allData) { - if (item["raw_id"].toString() == id) { - isSame = true; - break; - } - } - return isSame; + static Future _isSameData(int uid, GachaWarpType warpType, int rawId,) async { + List> gachaItem = await SrWrapToolDatabaseService.userGachaLog( + uid: uid, + rawId: rawId, + gachaType: gachaWarpTypeValue[warpType] + ); + + return gachaItem.isNotEmpty ? true : false; } /// 获取 UP 池数据 @@ -127,12 +126,10 @@ class SrWrapToolCacheUpdateService { if (!hasUser) { await SrWrapToolDatabaseService.insertWarpUser(uid: uid); } - - /// 从数据库中查询当前用户的所有抽卡记录 - List> gachaList = await SrWrapToolDatabaseService.userGachaLog(uid: uid, gachaType: gachaWarpTypeValue[warpType]); + for (var log in firstData) { - if (gachaList.isNotEmpty && _isSameData(log["id"], gachaList)) { + if (await _isSameData(uid, warpType, int.parse(log["id"]))) { firstSame = true; break; } @@ -165,7 +162,8 @@ class SrWrapToolCacheUpdateService { /// 顺带赋值 end_id endId = int.parse(firstData[firstData.length - 1]["id"]); /// 循环请求 - while (!isEnd) { + while (true) { + if (isEnd) break; /// 显示进度弹窗 context.read(globalDialogRiverpod).set(title, child: Text("正在获取第 $page 页数据...")); if (kDebugMode) print("当前页数:$page"); @@ -178,11 +176,9 @@ class SrWrapToolCacheUpdateService { /// 遍历列表 for (var log in list) { - /// 如果当前抽卡记录与数据库中最新一条记录相同,则不再写入 - if (gachaList.isNotEmpty && _isSameData(log["id"], gachaList)) { - isEnd = true; - if (kDebugMode) print("while: 已到末页"); - break; + if (await _isSameData(uid, warpType, int.parse(log["id"]))) { + if (kDebugMode) print("while:存在相同的记录,跳过插入。"); + continue; } String itemType = ""; diff --git a/lib/libs/sr/services/tools/warp/db.dart b/lib/libs/sr/services/tools/warp/db.dart index 0f1c624..7448d8a 100644 --- a/lib/libs/sr/services/tools/warp/db.dart +++ b/lib/libs/sr/services/tools/warp/db.dart @@ -44,6 +44,7 @@ class SrWrapToolDatabaseService { static Future>> userGachaLog({ required int uid, int? gachaId, + int? rawId, int? gachaType, int? itemId, String? itemType, @@ -57,6 +58,7 @@ class SrWrapToolDatabaseService { 'SELECT * FROM ${SCDatabaseConfig.warpGachaLogTable}' ' WHERE uid=$uid' '${gachaId != null ? " AND gacha_id=$gachaId" : ""}' + '${rawId != null ? " AND raw_id=$rawId" : ""}' '${gachaType != null ? " AND gacha_type=$gachaType" : ""}' '${itemId != null ? " AND item_id=$itemId" : ""}' '${itemType != null ? " AND item_type=$itemType" : ""}' diff --git a/lib/utils/storage/sqlite.dart b/lib/utils/storage/sqlite.dart index d178721..2e7e1d5 100644 --- a/lib/utils/storage/sqlite.dart +++ b/lib/utils/storage/sqlite.dart @@ -1,12 +1,15 @@ /// =========================================================================== /// Copyright (c) 2020-2023, BoxCat. All rights reserved. /// Date: 2023-05-01 22:53:02 -/// LastEditTime: 2023-05-04 22:41:42 +/// LastEditTime: 2023-05-14 07:43:24 /// FilePath: /lib/utils/storage/sqlite.dart /// =========================================================================== +import 'dart:io'; + import 'package:sqflite/sqflite.dart'; import 'package:srcat/config/db.dart'; +import 'package:srcat/utils/storage/sqlite_fix.dart'; class SCSQLiteUtils { static Future init() async { @@ -53,11 +56,17 @@ class SCSQLiteUtils { 'DROP TABLE ${SCDatabaseConfig.warpItemTable};' ); } + + /// 备份旧表 + File v2WarpDatabase = File(SCDatabaseConfig.warpMaster); + await v2WarpDatabase.copy("${SCDatabaseConfig.base}/warp.v2.db"); + + await SCSQLiteFixUtils.init(db); } Database database = await openDatabase( SCDatabaseConfig.warpMaster, - version: 2, + version: 3, onCreate: (Database db, int version) => onCreate(db, version), onUpgrade: (Database db, int version, int un) => onUpgrade(db) ); diff --git a/lib/utils/storage/sqlite_fix.dart b/lib/utils/storage/sqlite_fix.dart new file mode 100644 index 0000000..2773937 --- /dev/null +++ b/lib/utils/storage/sqlite_fix.dart @@ -0,0 +1,83 @@ +/// =========================================================================== +/// Copyright (c) 2020-2023, BoxCat. All rights reserved. +/// Date: 2023-05-13 18:09:45 +/// LastEditTime: 2023-05-13 19:59:09 +/// FilePath: /lib/utils/storage/sqlite_fix.dart +/// =========================================================================== + +import 'package:flutter/foundation.dart'; +import 'package:sqflite/sqflite.dart'; +import 'package:srcat/config/db.dart'; + +class SCSQLiteFixUtils { + static Future init(Database db) async { + bool hasTable = await _queryOldTable(db, SCDatabaseConfig.warpGachaLogOldTable); + if (hasTable) { + await _createAndFix(db); + } + return false; + } + + /// 新建新表并剔除重复数据 + static Future _createAndFix(Database db) async { + /// 判断新表是否存在 + bool hasNewTable = await _queryOldTable(db, SCDatabaseConfig.warpGachaLogTable); + + if (!hasNewTable) { + await db.execute( + 'CREATE TABLE ${SCDatabaseConfig.warpGachaLogTable} (' + '"id" INTEGER NOT NULL PRIMARY KEY,' // ID + '"raw_id" int(25) NOT NULL default \'0\',' // 原始 ID + '"uid" int(9) NOT NULL default \'0\',' // UID + '"item_id" int(10) NOT NULL default \'0\',' // 物品 ID + '"item_type" varchar(15) NOT NULL default \'unknown\',' // 物品类型 (lighecone/character) + '"rank_type" int(2) NOT NULL default \'0\',' // 物品星级 + '"gacha_id" int(10) NOT NULL default \'0\',' // 卡池 ID + '"gacha_type" int(3) NOT NULL default \'0\',' // 卡池类型 + '"time" int(10) NOT NULL default \'0\'' // 抽卡时间 + ');' + ); + } + + List> oldData; + /// 查询旧表内所有数据 + try { + oldData = await db.query(SCDatabaseConfig.warpGachaLogOldTable); + } catch (e) { + return; + } + + if (oldData.isNotEmpty) { + List insert = []; + for (var newLog in oldData) { + if (!insert.contains(newLog["raw_id"])) { + try { + await db.insert(SCDatabaseConfig.warpGachaLogTable, { + "raw_id": newLog["raw_id"], + "uid": newLog["uid"], + "item_id": newLog["item_id"], + "item_type": newLog["item_type"], + "rank_type": newLog["rank_type"], + "gacha_id": newLog["gacha_id"], + "gacha_type": newLog["gacha_type"], + "time": newLog["time"], + }); + } catch(e) { + if (kDebugMode) print("数据存在,跳过插入"); + } + insert.add(newLog["raw_id"]); + } + } + /// 将旧表删除 + await db.execute('DROP TABLE ${SCDatabaseConfig.warpGachaLogOldTable}'); + } + } + + /// 检查数据库表是否存在 + static Future _queryOldTable(Database db, String table) async { + List> tables = await db.rawQuery( + "select * from Sqlite_master where type = 'table' and name = '$table'" + ); + return tables.isEmpty ? false : true; + } +}