Skip to content

Commit

Permalink
🛠️ fix: warp_id incoming type error
Browse files Browse the repository at this point in the history
  • Loading branch information
bakaomg committed May 13, 2023
1 parent a725ccb commit bf52e1f
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 24 deletions.
7 changes: 6 additions & 1 deletion lib/application.dart
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,7 +16,7 @@ import 'package:go_router/go_router.dart';
class Application {
static late GoRouter router;
static late GlobalKey<NavigatorState> rootNavigatorKey;
static int appNumVersion = 100002;
static int appNumVersion = 100006;

/* App NavigatorKey */
static GlobalKey<NavigatorState> appNavigatorKey = GlobalKey<NavigatorState>();
Expand Down
3 changes: 2 additions & 1 deletion lib/config/db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 16 additions & 20 deletions lib/libs/sr/services/tools/warp/cache_update.dart
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -39,15 +39,14 @@ class SrWrapToolCacheUpdateService {
}

/// 判断数据库中是否包含相同抽卡记录
static bool _isSameData(String id, List<Map<String, dynamic>> allData) {
bool isSame = false;
for (var item in allData) {
if (item["raw_id"].toString() == id) {
isSame = true;
break;
}
}
return isSame;
static Future<bool> _isSameData(int uid, GachaWarpType warpType, int rawId,) async {
List<Map<String, dynamic>> gachaItem = await SrWrapToolDatabaseService.userGachaLog(
uid: uid,
rawId: rawId,
gachaType: gachaWarpTypeValue[warpType]
);

return gachaItem.isNotEmpty ? true : false;
}

/// 获取 UP 池数据
Expand Down Expand Up @@ -127,12 +126,10 @@ class SrWrapToolCacheUpdateService {
if (!hasUser) {
await SrWrapToolDatabaseService.insertWarpUser(uid: uid);
}

/// 从数据库中查询当前用户的所有抽卡记录
List<Map<String, dynamic>> 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;
}
Expand Down Expand Up @@ -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");
Expand All @@ -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 = "";
Expand Down
2 changes: 2 additions & 0 deletions lib/libs/sr/services/tools/warp/db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class SrWrapToolDatabaseService {
static Future<List<Map<String, Object?>>> userGachaLog({
required int uid,
int? gachaId,
int? rawId,
int? gachaType,
int? itemId,
String? itemType,
Expand All @@ -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" : ""}'
Expand Down
13 changes: 11 additions & 2 deletions lib/utils/storage/sqlite.dart
Original file line number Diff line number Diff line change
@@ -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<void> init() async {
Expand Down Expand Up @@ -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)
);
Expand Down
83 changes: 83 additions & 0 deletions lib/utils/storage/sqlite_fix.dart
Original file line number Diff line number Diff line change
@@ -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<bool> init(Database db) async {
bool hasTable = await _queryOldTable(db, SCDatabaseConfig.warpGachaLogOldTable);
if (hasTable) {
await _createAndFix(db);
}
return false;
}

/// 新建新表并剔除重复数据
static Future<void> _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<Map<String, dynamic>> oldData;
/// 查询旧表内所有数据
try {
oldData = await db.query(SCDatabaseConfig.warpGachaLogOldTable);
} catch (e) {
return;
}

if (oldData.isNotEmpty) {
List<int> 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<bool> _queryOldTable(Database db, String table) async {
List<Map<String, dynamic>> tables = await db.rawQuery(
"select * from Sqlite_master where type = 'table' and name = '$table'"
);
return tables.isEmpty ? false : true;
}
}

0 comments on commit bf52e1f

Please sign in to comment.