From b7dc7d33cafb0547ce12bf2cb6b0d3683b6bc8f8 Mon Sep 17 00:00:00 2001 From: dan63047 Date: Tue, 17 Oct 2023 00:41:45 +0300 Subject: [PATCH] Command to fix #35 consequences --- lib/services/tetrio_crud.dart | 24 +++++++++++++++++++++++- lib/views/tracked_players_view.dart | 15 +++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/services/tetrio_crud.dart b/lib/services/tetrio_crud.dart index e875d49..1ca509c 100644 --- a/lib/services/tetrio_crud.dart +++ b/lib/services/tetrio_crud.dart @@ -407,12 +407,34 @@ class TetrioService extends DB { await ensureDbIsOpen(); final db = getDatabaseOrThrow(); for (TetraLeagueAlphaRecord match in stream.records) { - final results = await db.query(tetraLeagueMatchesTable, where: '$idCol = ?', whereArgs: [match.ownId]); + final results = await db.query(tetraLeagueMatchesTable, where: '$replayID = ?', whereArgs: [match.replayId]); if (results.isNotEmpty) continue; db.insert(tetraLeagueMatchesTable, {idCol: match.ownId, replayID: match.replayId, timestamp: match.timestamp.toString(), player1id: match.endContext.first.userId, player2id: match.endContext.last.userId, endContext1: jsonEncode(match.endContext.first.toJson()), endContext2: jsonEncode(match.endContext.last.toJson())}); } } + Future removeDuplicatesFromTLMatches() async{ + await ensureDbIsOpen(); + final db = getDatabaseOrThrow(); + await db.execute(""" + DELETE FROM $tetraLeagueMatchesTable + WHERE + $idCol IN ( + SELECT + $idCol + FROM ( + SELECT + $idCol, + ROW_NUMBER() OVER ( + PARTITION BY $replayID + ORDER BY $replayID) AS row_num + FROM $tetraLeagueMatchesTable + ) t + WHERE row_num > 1 + ); + """); + } + Future> getTLMatchesbyPlayerID(String playerID) async { await ensureDbIsOpen(); final db = getDatabaseOrThrow(); diff --git a/lib/views/tracked_players_view.dart b/lib/views/tracked_players_view.dart index 7a83b36..0f967c0 100644 --- a/lib/views/tracked_players_view.dart +++ b/lib/views/tracked_players_view.dart @@ -41,6 +41,21 @@ class TrackedPlayersState extends State { return Scaffold( appBar: AppBar( title: Text(t.trackedPlayersViewTitle), + actions: [ + PopupMenuButton( + icon: const Icon(Icons.settings_backup_restore), + itemBuilder: (BuildContext context) => [ + PopupMenuItem( + value: 1, + child: Text("Remove duplicated TL mathces"), + ), + ], + onSelected: (value) { + if (value == 1) {teto.removeDuplicatesFromTLMatches(); + return;} + Navigator.pushNamed(context, value); + }) + ], ), backgroundColor: Colors.black, body: SafeArea(