From 3857665444d297f21aac034f10596bebad8a80ab Mon Sep 17 00:00:00 2001 From: gelbeinhalb Date: Fri, 16 Jan 2026 12:23:24 +0100 Subject: [PATCH] update player_match_dao.dart --- lib/data/dao/player_match_dao.dart | 78 ++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 3 deletions(-) diff --git a/lib/data/dao/player_match_dao.dart b/lib/data/dao/player_match_dao.dart index 7ebaee6..a2185e6 100644 --- a/lib/data/dao/player_match_dao.dart +++ b/lib/data/dao/player_match_dao.dart @@ -1,23 +1,31 @@ import 'package:drift/drift.dart'; import 'package:game_tracker/data/db/database.dart'; import 'package:game_tracker/data/db/tables/player_match_table.dart'; +import 'package:game_tracker/data/db/tables/team_table.dart'; import 'package:game_tracker/data/dto/player.dart'; part 'player_match_dao.g.dart'; -@DriftAccessor(tables: [PlayerMatchTable]) +@DriftAccessor(tables: [PlayerMatchTable, TeamTable]) class PlayerMatchDao extends DatabaseAccessor with _$PlayerMatchDaoMixin { PlayerMatchDao(super.db); /// Associates a player with a match by inserting a record into the - /// [PlayerMatchTable]. + /// [PlayerMatchTable]. Optionally associates with a team and sets initial score. Future addPlayerToMatch({ required String matchId, required String playerId, + String? teamId, + int score = 0, }) async { await into(playerMatchTable).insert( - PlayerMatchTableCompanion.insert(playerId: playerId, matchId: matchId), + PlayerMatchTableCompanion.insert( + playerId: playerId, + matchId: matchId, + teamId: Value(teamId), + score: score, + ), mode: InsertMode.insertOrIgnore, ); } @@ -38,6 +46,50 @@ class PlayerMatchDao extends DatabaseAccessor return players; } + /// Retrieves a player's score for a specific match. + /// Returns null if the player is not in the match. + Future getPlayerScore({ + required String matchId, + required String playerId, + }) async { + final result = await (select(playerMatchTable) + ..where( + (p) => p.matchId.equals(matchId) & p.playerId.equals(playerId), + )) + .getSingleOrNull(); + return result?.score; + } + + /// Updates the score for a player in a match. + /// Returns `true` if the update was successful, otherwise `false`. + Future updatePlayerScore({ + required String matchId, + required String playerId, + required int newScore, + }) async { + final rowsAffected = await (update(playerMatchTable) + ..where( + (p) => p.matchId.equals(matchId) & p.playerId.equals(playerId), + )) + .write(PlayerMatchTableCompanion(score: Value(newScore))); + return rowsAffected > 0; + } + + /// Updates the team for a player in a match. + /// Returns `true` if the update was successful, otherwise `false`. + Future updatePlayerTeam({ + required String matchId, + required String playerId, + required String? teamId, + }) async { + final rowsAffected = await (update(playerMatchTable) + ..where( + (p) => p.matchId.equals(matchId) & p.playerId.equals(playerId), + )) + .write(PlayerMatchTableCompanion(teamId: Value(teamId))); + return rowsAffected > 0; + } + /// Checks if there are any players associated with the given [matchId]. /// Returns `true` if there are players, otherwise `false`. Future matchHasPlayers({required String matchId}) async { @@ -114,6 +166,7 @@ class PlayerMatchDao extends DatabaseAccessor (id) => PlayerMatchTableCompanion.insert( playerId: id, matchId: matchId, + score: 0, ), ) .toList(); @@ -127,4 +180,23 @@ class PlayerMatchDao extends DatabaseAccessor } }); } + + /// Retrieves all players in a specific team for a match. + Future> getPlayersInTeam({ + required String matchId, + required String teamId, + }) async { + final result = await (select(playerMatchTable) + ..where( + (p) => p.matchId.equals(matchId) & p.teamId.equals(teamId), + )) + .get(); + + if (result.isEmpty) return []; + + final futures = result.map( + (row) => db.playerDao.getPlayerById(playerId: row.playerId), + ); + return Future.wait(futures); + } }