From 07b9b7825296bfa3d903f69c2caa0f2ff6aa7f9b Mon Sep 17 00:00:00 2001 From: gelbeinhalb Date: Sat, 7 Feb 2026 17:31:19 +0100 Subject: [PATCH] add replace players in a match method --- lib/data/dao/match_dao.dart | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lib/data/dao/match_dao.dart b/lib/data/dao/match_dao.dart index 063ac32..8f7a2c0 100644 --- a/lib/data/dao/match_dao.dart +++ b/lib/data/dao/match_dao.dart @@ -355,6 +355,38 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { return rowsAffected > 0; } + /// Replaces all players in a match with the provided list of players. + /// Removes all existing players from the match and adds the new players. + /// Also adds any new players to the player table if they don't exist. + Future replaceMatchPlayers({ + required String matchId, + required List newPlayers, + }) async { + await db.transaction(() async { + // Remove all existing players from the match + final deleteQuery = delete(db.playerMatchTable) + ..where((p) => p.matchId.equals(matchId)); + await deleteQuery.go(); + + // Add new players to the player table if they don't exist + await Future.wait( + newPlayers.map((player) async { + if (!await db.playerDao.playerExists(playerId: player.id)) { + await db.playerDao.addPlayer(player: player); + } + }), + ); + + // Add the new players to the match + await Future.wait( + newPlayers.map((player) => db.playerMatchDao.addPlayerToMatch( + matchId: matchId, + playerId: player.id, + )), + ); + }); + } + // ============================================================ // TEMPORARY: Winner methods - these are stubs and do not persist data // TODO: Implement proper winner handling