implement winner methods
This commit is contained in:
@@ -388,38 +388,86 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// TEMPORARY: Winner methods - these are stubs and do not persist data
|
// Winner methods - handle winner logic via player scores
|
||||||
// TODO: Implement proper winner handling
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
/// TEMPORARY: Checks if a match has a winner.
|
/// Checks if a match has a winner.
|
||||||
/// Currently returns true if the match has any players.
|
/// 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) ?? [];
|
||||||
return players.isNotEmpty;
|
|
||||||
|
for (final player in players) {
|
||||||
|
final score = await db.playerMatchDao.getPlayerScore(
|
||||||
|
matchId: matchId,
|
||||||
|
playerId: player.id,
|
||||||
|
);
|
||||||
|
if (score == 1) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// TEMPORARY: Gets the winner of a match.
|
/// Gets the winner of a match.
|
||||||
/// Currently returns the first player in the match's player list.
|
/// 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) ?? [];
|
||||||
return players.isNotEmpty ? players.first : null;
|
|
||||||
|
for (final player in players) {
|
||||||
|
final score = await db.playerMatchDao.getPlayerScore(
|
||||||
|
matchId: matchId,
|
||||||
|
playerId: player.id,
|
||||||
|
);
|
||||||
|
if (score == 1) {
|
||||||
|
return player;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// TEMPORARY: Sets the winner of a match.
|
/// Sets the winner of a match.
|
||||||
/// Currently does nothing - winner is not persisted.
|
/// Sets all players' scores to 0, then sets the specified player's score to 1.
|
||||||
|
/// Returns `true` if the operation was successful, otherwise `false`.
|
||||||
Future<bool> setWinner({
|
Future<bool> setWinner({
|
||||||
required String matchId,
|
required String matchId,
|
||||||
required String winnerId,
|
required String winnerId,
|
||||||
}) async {
|
}) async {
|
||||||
// TODO: Implement winner persistence
|
await db.transaction(() async {
|
||||||
|
final players = await db.playerMatchDao.getPlayersOfMatch(matchId: matchId) ?? [];
|
||||||
|
|
||||||
|
// Set all players' scores to 0
|
||||||
|
for (final player in players) {
|
||||||
|
await db.playerMatchDao.updatePlayerScore(
|
||||||
|
matchId: matchId,
|
||||||
|
playerId: player.id,
|
||||||
|
newScore: 0,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the winner's score to 1
|
||||||
|
await db.playerMatchDao.updatePlayerScore(
|
||||||
|
matchId: matchId,
|
||||||
|
playerId: winnerId,
|
||||||
|
newScore: 1,
|
||||||
|
);
|
||||||
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// TEMPORARY: Removes the winner of a match.
|
/// Removes the winner of a match.
|
||||||
/// Currently does nothing - winner is not persisted.
|
/// Sets the current winner's score to 0 (no winner).
|
||||||
|
/// Returns `true` if a winner was removed, otherwise `false`.
|
||||||
Future<bool> removeWinner({required String matchId}) async {
|
Future<bool> removeWinner({required String matchId}) async {
|
||||||
// TODO: Implement winner persistence
|
final winner = await getWinner(matchId: matchId);
|
||||||
return true;
|
if (winner == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
final success = await db.playerMatchDao.updatePlayerScore(
|
||||||
|
matchId: matchId,
|
||||||
|
playerId: winner.id,
|
||||||
|
newScore: 0,
|
||||||
|
);
|
||||||
|
return success;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user