Fix: Setting & fetching winner

This commit is contained in:
2026-03-05 12:25:01 +01:00
parent 8e75f6af56
commit 5ed35362ac

View File

@@ -27,9 +27,9 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
if (row.groupId != null) { if (row.groupId != null) {
group = await db.groupDao.getGroupById(groupId: row.groupId!); group = await db.groupDao.getGroupById(groupId: row.groupId!);
} }
final players = await db.playerMatchDao.getPlayersOfMatch( final players =
matchId: row.id, await db.playerMatchDao.getPlayersOfMatch(matchId: row.id) ?? [];
) ?? []; final winner = await getWinner(matchId: row.id);
return Match( return Match(
id: row.id, id: row.id,
name: row.name ?? '', name: row.name ?? '',
@@ -39,6 +39,7 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
notes: row.notes ?? '', notes: row.notes ?? '',
createdAt: row.createdAt, createdAt: row.createdAt,
endedAt: row.endedAt, endedAt: row.endedAt,
winner: winner,
); );
}), }),
); );
@@ -56,7 +57,10 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
group = await db.groupDao.getGroupById(groupId: result.groupId!); group = await db.groupDao.getGroupById(groupId: result.groupId!);
} }
final players = await db.playerMatchDao.getPlayersOfMatch(matchId: matchId) ?? []; final players =
await db.playerMatchDao.getPlayersOfMatch(matchId: matchId) ?? [];
final winner = await getWinner(matchId: matchId);
return Match( return Match(
id: result.id, id: result.id,
@@ -67,6 +71,7 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
notes: result.notes ?? '', notes: result.notes ?? '',
createdAt: result.createdAt, createdAt: result.createdAt,
endedAt: result.endedAt, endedAt: result.endedAt,
winner: winner,
); );
} }
@@ -94,6 +99,10 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
playerId: p.id, playerId: p.id,
); );
} }
if (match.winner != null) {
await setWinner(matchId: match.id, winnerId: match.winner!.id);
}
}); });
} }
@@ -112,20 +121,20 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
if (uniqueGames.isNotEmpty) { if (uniqueGames.isNotEmpty) {
await db.batch( await db.batch(
(b) => b.insertAll( (b) => b.insertAll(
db.gameTable, db.gameTable,
uniqueGames.values uniqueGames.values
.map( .map(
(game) => GameTableCompanion.insert( (game) => GameTableCompanion.insert(
id: game.id, id: game.id,
name: game.name, name: game.name,
ruleset: game.ruleset.name, ruleset: game.ruleset.name,
description: game.description, description: game.description,
color: game.color.name, color: game.color.name,
icon: game.icon, icon: game.icon,
createdAt: game.createdAt, createdAt: game.createdAt,
), ),
) )
.toList(), .toList(),
mode: InsertMode.insertOrIgnore, mode: InsertMode.insertOrIgnore,
), ),
@@ -134,18 +143,18 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
// Add all groups of the matches in batch // Add all groups of the matches in batch
await db.batch( await db.batch(
(b) => b.insertAll( (b) => b.insertAll(
db.groupTable, db.groupTable,
matches matches
.where((match) => match.group != null) .where((match) => match.group != null)
.map( .map(
(match) => GroupTableCompanion.insert( (match) => GroupTableCompanion.insert(
id: match.group!.id, id: match.group!.id,
name: match.group!.name, name: match.group!.name,
description: match.group!.description, description: match.group!.description,
createdAt: match.group!.createdAt, createdAt: match.group!.createdAt,
), ),
) )
.toList(), .toList(),
mode: InsertMode.insertOrIgnore, mode: InsertMode.insertOrIgnore,
), ),
@@ -153,20 +162,20 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
// Add all matches in batch // Add all matches in batch
await db.batch( await db.batch(
(b) => b.insertAll( (b) => b.insertAll(
matchTable, matchTable,
matches matches
.map( .map(
(match) => MatchTableCompanion.insert( (match) => MatchTableCompanion.insert(
id: match.id, id: match.id,
gameId: match.game.id, gameId: match.game.id,
groupId: Value(match.group?.id), groupId: Value(match.group?.id),
name: Value(match.name), name: Value(match.name),
notes: Value(match.notes), notes: Value(match.notes),
createdAt: match.createdAt, createdAt: match.createdAt,
endedAt: Value(match.endedAt), endedAt: Value(match.endedAt),
), ),
) )
.toList(), .toList(),
mode: InsertMode.insertOrReplace, mode: InsertMode.insertOrReplace,
), ),
@@ -188,17 +197,17 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
if (uniquePlayers.isNotEmpty) { if (uniquePlayers.isNotEmpty) {
await db.batch( await db.batch(
(b) => b.insertAll( (b) => b.insertAll(
db.playerTable, db.playerTable,
uniquePlayers.values uniquePlayers.values
.map( .map(
(p) => PlayerTableCompanion.insert( (p) => PlayerTableCompanion.insert(
id: p.id, id: p.id,
name: p.name, name: p.name,
description: p.description, description: p.description,
createdAt: p.createdAt, createdAt: p.createdAt,
), ),
) )
.toList(), .toList(),
mode: InsertMode.insertOrIgnore, mode: InsertMode.insertOrIgnore,
), ),
@@ -253,9 +262,9 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
/// Retrieves the number of matches in the database. /// Retrieves the number of matches in the database.
Future<int> getMatchCount() async { Future<int> getMatchCount() async {
final count = final count =
await (selectOnly(matchTable)..addColumns([matchTable.id.count()])) await (selectOnly(matchTable)..addColumns([matchTable.id.count()]))
.map((row) => row.read(matchTable.id.count())) .map((row) => row.read(matchTable.id.count()))
.getSingle(); .getSingle();
return count ?? 0; return count ?? 0;
} }
@@ -315,15 +324,16 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
} }
/// Updates the group of the match with the given [matchId]. /// Updates the group of the match with the given [matchId].
/// Replaces the existing group association with the new group specified by [newGroupId].
/// Pass null to remove the group association. /// Pass null to remove the group association.
/// Returns `true` if more than 0 rows were affected, otherwise `false`. /// Returns `true` if more than 0 rows were affected, otherwise `false`.
Future<bool> updateMatchGroup({ Future<bool> updateMatchGroup({
required String matchId, required String matchId,
required String? groupId, required String? newGroupId,
}) async { }) async {
final query = update(matchTable)..where((g) => g.id.equals(matchId)); final query = update(matchTable)..where((g) => g.id.equals(matchId));
final rowsAffected = await query.write( final rowsAffected = await query.write(
MatchTableCompanion(groupId: Value(groupId)), MatchTableCompanion(groupId: Value(newGroupId)),
); );
return rowsAffected > 0; return rowsAffected > 0;
} }
@@ -379,10 +389,12 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
// Add the new players to the match // Add the new players to the match
await Future.wait( await Future.wait(
newPlayers.map((player) => db.playerMatchDao.addPlayerToMatch( newPlayers.map(
matchId: matchId, (player) => db.playerMatchDao.addPlayerToMatch(
playerId: player.id, matchId: matchId,
)), playerId: player.id,
),
),
); );
}); });
} }
@@ -394,7 +406,8 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
/// Checks if a match has a winner. /// Checks if a match has a winner.
/// Returns true if any player in the match has their score set to 1. /// Returns true if any player in the match has their score set to 1.
Future<bool> hasWinner({required String matchId}) async { Future<bool> hasWinner({required String matchId}) async {
final players = await db.playerMatchDao.getPlayersOfMatch(matchId: matchId) ?? []; final players =
await db.playerMatchDao.getPlayersOfMatch(matchId: matchId) ?? [];
for (final player in players) { for (final player in players) {
final score = await db.playerMatchDao.getPlayerScore( final score = await db.playerMatchDao.getPlayerScore(
@@ -411,7 +424,8 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
/// Gets the winner of a match. /// Gets the winner of a match.
/// Returns the player with score 1, or null if no winner is set. /// Returns the player with score 1, or null if no winner is set.
Future<Player?> getWinner({required String matchId}) async { Future<Player?> getWinner({required String matchId}) async {
final players = await db.playerMatchDao.getPlayersOfMatch(matchId: matchId) ?? []; final players =
await db.playerMatchDao.getPlayersOfMatch(matchId: matchId) ?? [];
for (final player in players) { for (final player in players) {
final score = await db.playerMatchDao.getPlayerScore( final score = await db.playerMatchDao.getPlayerScore(
@@ -433,7 +447,8 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
required String winnerId, required String winnerId,
}) async { }) async {
await db.transaction(() async { await db.transaction(() async {
final players = await db.playerMatchDao.getPlayersOfMatch(matchId: matchId) ?? []; final players =
await db.playerMatchDao.getPlayersOfMatch(matchId: matchId) ?? [];
// Set all players' scores to 0 // Set all players' scores to 0
for (final player in players) { for (final player in players) {