From 9ed6ddb33d7b5acb5a378dba91061461723925cc Mon Sep 17 00:00:00 2001 From: dan63047 Date: Tue, 3 Sep 2024 00:17:09 +0300 Subject: [PATCH] ok that works faster but i still need some tests --- lib/data_objects/tetrio.dart | 9 ++++++--- lib/services/tetrio_crud.dart | 19 +++++++++++++++---- lib/views/main_view.dart | 10 +++++----- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/lib/data_objects/tetrio.dart b/lib/data_objects/tetrio.dart index 25092c2..c2b02c2 100644 --- a/lib/data_objects/tetrio.dart +++ b/lib/data_objects/tetrio.dart @@ -1375,7 +1375,11 @@ class TetraLeague { apm = json['apm']?.toDouble(); pps = json['pps']?.toDouble(); vs = json['vs']?.toDouble(); - decaying = json['decaying'] ?? false; + decaying = switch(json['decaying'].runtimeType){ + int => json['decaying'] == 1, + bool => json['decaying'], + _ => false + }; standing = json['standing'] ?? -1; percentile = json['percentile'] != null ? json['percentile'].toDouble() : rankCutoffs[rank]; standingLocal = json['standing_local'] ?? -1; @@ -1402,8 +1406,7 @@ class TetraLeague { Map toJson() { final Map data = {}; - data['id'] = id; - data['timestamp'] = timestamp.millisecondsSinceEpoch; + data['id'] = id+timestamp.millisecondsSinceEpoch.toRadixString(16); if (gamesPlayed > 0) data['gamesplayed'] = gamesPlayed; if (gamesWon > 0) data['gameswon'] = gamesWon; if (tr >= 0) data['tr'] = tr; diff --git a/lib/services/tetrio_crud.dart b/lib/services/tetrio_crud.dart index e97d16c..1d93cab 100644 --- a/lib/services/tetrio_crud.dart +++ b/lib/services/tetrio_crud.dart @@ -6,6 +6,7 @@ import 'dart:developer' as developer; import 'dart:io'; import 'package:path_provider/path_provider.dart'; import 'package:sqflite/sql.dart'; +import 'package:sqflite_common_ffi/sqflite_ffi.dart'; import 'package:tetra_stats/data_objects/tetra_stats.dart'; import 'package:tetra_stats/data_objects/tetrio_multiplayer_replay.dart'; import 'package:tetra_stats/main.dart' show packageInfo; @@ -72,7 +73,6 @@ const String createTetrioTLReplayStats = ''' const String createTetrioLeagueTable = ''' CREATE TABLE IF NOT EXISTS "tetrioLeague" ( "id" TEXT NOT NULL, - "timestamp" INTEGER NOT NULL, "gamesplayed" INTEGER NOT NULL DEFAULT 0, "gameswon" INTEGER NOT NULL DEFAULT 0, "tr" REAL, @@ -93,7 +93,8 @@ const String createTetrioLeagueTable = ''' "next_rank" TEXT, "next_at" INTEGER NOT NULL DEFAULT -1, "percentile_rank" TEXT NOT NULL DEFAULT 'z', - "season" INTEGER NOT NULL DEFAULT 1 + "season" INTEGER NOT NULL DEFAULT 1, + PRIMARY KEY("id") ) '''; @@ -592,6 +593,7 @@ class TetrioService extends DB { // that one api returns csv instead of json List> csv = const CsvToListConverter().convert(response.body)..removeAt(0); List history = []; + Batch batch = db.batch(); for (List entry in csv){ // each entry is one state TetraLeague state = TetraLeague( id: id, @@ -617,8 +619,9 @@ class TetrioService extends DB { season: 1 ); history.add(state); - await db.insert(tetrioLeagueTable, state.toJson(), conflictAlgorithm: ConflictAlgorithm.replace); + batch.insert(tetrioLeagueTable, state.toJson(), conflictAlgorithm: ConflictAlgorithm.replace); } + batch.commit(); return history; case 404: developer.log("fetchTLHistory: Probably, history doesn't exist", name: "services/tetrio_crud", error: response.statusCode); @@ -1112,12 +1115,20 @@ class TetrioService extends DB { Future storeState(TetraLeague league) async { await ensureDbIsOpen(); final db = getDatabaseOrThrow(); - List test = await db.query(tetrioLeagueTable, where: '"id" = ? AND "gamesplayed" = ? AND "rd" = ?', whereArgs: [league.id, league.gamesPlayed, league.rd]); + List test = await db.query(tetrioLeagueTable, where: '"id" LIKE ? AND "gamesplayed" = ? AND "rd" = ?', whereArgs: ["${league.id}%", league.gamesPlayed, league.rd]); if (test.isEmpty) { await db.insert(tetrioLeagueTable, league.toJson()); } } + Future> getHistory(String id, {int season = currentSeason}) async { + await ensureDbIsOpen(); + final db = getDatabaseOrThrow(); + List raw = await db.query(tetrioLeagueTable, where: '"id" = ? AND "season" = ?', whereArgs: [id, season]); + List result = [for (var entry in raw) TetraLeague.fromJson(entry as Map, DateTime.fromMillisecondsSinceEpoch(int.parse(entry["id"].substring(24), radix: 16)), entry["season"], entry["id"].substring(0, 24))]; + return result; + } + /// Remove state (which is [tetrioPlayer]) from the local database // Future deleteState(TetrioPlayer tetrioPlayer) async { // await ensureDbIsOpen(); diff --git a/lib/views/main_view.dart b/lib/views/main_view.dart index 2d4fc1d..078a2fb 100644 --- a/lib/views/main_view.dart +++ b/lib/views/main_view.dart @@ -217,7 +217,7 @@ class _MainState extends State with TickerProviderStateMixin { // Making list of Tetra League matches bool isTracking = await teto.isPlayerTracking(me.userId); - List states = []; + List states = await teto.getHistory(me.userId); TetraLeague? compareWith; Set uniqueTL = {}; List storedRecords = await teto.getTLMatchesbyPlayerID(me.userId); // get old matches @@ -277,8 +277,8 @@ class _MainState extends State with TickerProviderStateMixin { // if (uniqueTL.isEmpty) uniqueTL.add(summaries.league); // } // Also i need previous Tetra League State for comparison if avaliable - if (uniqueTL.length >= 2){ - compareWith = uniqueTL.toList().elementAtOrNull(uniqueTL.length - 2); + if (states.length >= 2){ + compareWith = states.elementAtOrNull(states.length - 2); chartsData = >>[ // Dumping charts data into dropdown menu items, while cheking if every entry is valid DropdownMenuItem(value: [for (var tl in uniqueTL) if (tl.gamesPlayed > 9) _HistoryChartSpot(tl.timestamp, tl.gamesPlayed, tl.rank, tl.tr)], child: Text(t.statCellNum.tr)), DropdownMenuItem(value: [for (var tl in uniqueTL) if (tl.gamesPlayed > 9) _HistoryChartSpot(tl.timestamp, tl.gamesPlayed, tl.rank, tl.glicko!)], child: const Text("Glicko")), @@ -312,7 +312,7 @@ class _MainState extends State with TickerProviderStateMixin { changePlayer(me.userId); }); } - return [me, summaries, news, tlStream, recentZenith, recentZenithEX]; + return [me, summaries, news, tlStream, recentZenith, recentZenithEX, states]; //return [me, records, states, tlMatches, compareWith, isTracking, news, topTR, recent, sprint, blitz, tlMatches.elementAtOrNull(0)?.timestamp]; } @@ -471,7 +471,7 @@ class _MainState extends State with TickerProviderStateMixin { child: TLThingy( tl: snapshot.data![1].league, userID: snapshot.data![0].userId, - states: const [], //snapshot.data![2], + states: snapshot.data![6], //topTR: snapshot.data![7]?.tr, //lastMatchPlayed: snapshot.data![11], bot: snapshot.data![0].role == "bot",