Fix: Setting & fetching winner
This commit is contained in:
@@ -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);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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(
|
||||||
|
(player) => db.playerMatchDao.addPlayerToMatch(
|
||||||
matchId: matchId,
|
matchId: matchId,
|
||||||
playerId: player.id,
|
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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user