Moved dao methods
This commit is contained in:
@@ -29,7 +29,7 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
|
||||
}
|
||||
final players =
|
||||
await db.playerMatchDao.getPlayersOfMatch(matchId: row.id) ?? [];
|
||||
final winner = await getWinner(matchId: row.id);
|
||||
final winner = await db.scoreDao.getWinner(matchId: row.id);
|
||||
return Match(
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
@@ -60,7 +60,7 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
|
||||
final players =
|
||||
await db.playerMatchDao.getPlayersOfMatch(matchId: matchId) ?? [];
|
||||
|
||||
final winner = await getWinner(matchId: matchId);
|
||||
final winner = await db.scoreDao.getWinner(matchId: matchId);
|
||||
|
||||
return Match(
|
||||
id: result.id,
|
||||
@@ -101,7 +101,10 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
|
||||
}
|
||||
|
||||
if (match.winner != null) {
|
||||
await setWinner(matchId: match.id, winnerId: match.winner!.id);
|
||||
await db.scoreDao.setWinner(
|
||||
matchId: match.id,
|
||||
playerId: match.winner!.id,
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -279,7 +282,7 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
|
||||
final group = await db.groupDao.getGroupById(groupId: groupId);
|
||||
final players =
|
||||
await db.playerMatchDao.getPlayersOfMatch(matchId: row.id) ?? [];
|
||||
final winner = await db.matchDao.getWinner(matchId: row.id);
|
||||
final winner = await db.scoreDao.getWinner(matchId: row.id);
|
||||
return Match(
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
@@ -436,93 +439,4 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// Winner methods - handle winner logic via player scores
|
||||
// ============================================================
|
||||
|
||||
/// Checks if a match has a winner.
|
||||
/// Returns true if any player in the match has their score set to 1.
|
||||
Future<bool> hasWinner({required String matchId}) async {
|
||||
final players =
|
||||
await db.playerMatchDao.getPlayersOfMatch(matchId: matchId) ?? [];
|
||||
|
||||
for (final player in players) {
|
||||
final score = await db.playerMatchDao.getPlayerScore(
|
||||
matchId: matchId,
|
||||
playerId: player.id,
|
||||
);
|
||||
if (score == 1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Gets the winner of a match.
|
||||
/// Returns the player with score 1, or null if no winner is set.
|
||||
Future<Player?> getWinner({required String matchId}) async {
|
||||
final players =
|
||||
await db.playerMatchDao.getPlayersOfMatch(matchId: matchId) ?? [];
|
||||
|
||||
for (final player in players) {
|
||||
final score = await db.playerMatchDao.getPlayerScore(
|
||||
matchId: matchId,
|
||||
playerId: player.id,
|
||||
);
|
||||
if (score == 1) {
|
||||
return player;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Sets the winner of a match.
|
||||
/// 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({
|
||||
required String matchId,
|
||||
required String winnerId,
|
||||
}) async {
|
||||
await db.transaction(() async {
|
||||
final players =
|
||||
await db.playerMatchDao.getPlayersOfMatch(matchId: matchId) ?? [];
|
||||
|
||||
// Set all players' scores to 0
|
||||
// TODO: Implement
|
||||
/*for (final player in players) {
|
||||
await db.playerMatchDao.updatePlayerScore(
|
||||
matchId: matchId,
|
||||
playerId: player.id,
|
||||
newScore: 0,
|
||||
);
|
||||
}*/
|
||||
|
||||
// Set the winner's score to 1
|
||||
// TODO: Implement
|
||||
/*await db.playerMatchDao.updatePlayerScore(
|
||||
matchId: matchId,
|
||||
playerId: winnerId,
|
||||
newScore: 1,
|
||||
);*/
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Removes the winner of a match.
|
||||
/// 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 {
|
||||
final winner = await getWinner(matchId: matchId);
|
||||
if (winner == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*final success = await db.playerMatchDao.updatePlayerScore(
|
||||
matchId: matchId,
|
||||
playerId: winner.id,
|
||||
newScore: 0,
|
||||
);*/
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ class PlayerMatchDao extends DatabaseAccessor<AppDatabase>
|
||||
required String matchId,
|
||||
required String playerId,
|
||||
String? teamId,
|
||||
int score = 0,
|
||||
}) async {
|
||||
await into(playerMatchTable).insert(
|
||||
PlayerMatchTableCompanion.insert(
|
||||
@@ -45,21 +44,6 @@ class PlayerMatchDao extends DatabaseAccessor<AppDatabase>
|
||||
return players;
|
||||
}
|
||||
|
||||
/// Retrieves a player's score for a specific match.
|
||||
/// Returns null if the player is not in the match.
|
||||
/// TODO: Implement
|
||||
Future<int?> 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 0;
|
||||
}
|
||||
|
||||
/// Updates the score for a player in a match.
|
||||
/// Returns `true` if the update was successful, otherwise `false`.
|
||||
Future<bool> updatePlayerScore({
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:tallee/data/db/database.dart';
|
||||
import 'package:tallee/data/db/tables/score_table.dart';
|
||||
import 'package:tallee/data/models/player.dart';
|
||||
import 'package:tallee/data/models/score_entry.dart';
|
||||
|
||||
part 'score_dao.g.dart';
|
||||
@@ -169,4 +170,122 @@ class ScoreDao extends DatabaseAccessor<AppDatabase> with _$ScoreDaoMixin {
|
||||
// Return the score from the latest round
|
||||
return scores.last.score;
|
||||
}
|
||||
|
||||
Future<bool> hasWinner({required String matchId}) async {
|
||||
return await getWinner(matchId: matchId) != null;
|
||||
}
|
||||
|
||||
// Setting the winner for a game and clearing previous winner if exists.
|
||||
Future<bool> setWinner({
|
||||
required String matchId,
|
||||
required String playerId,
|
||||
}) async {
|
||||
// Clear previous winner if exists
|
||||
deleteScoresForMatch(matchId: matchId);
|
||||
|
||||
// Set the winner's score to 1
|
||||
final rowsAffected = await into(scoreTable).insert(
|
||||
ScoreTableCompanion.insert(
|
||||
playerId: playerId,
|
||||
matchId: matchId,
|
||||
roundNumber: 0,
|
||||
score: 1,
|
||||
change: 0,
|
||||
),
|
||||
mode: InsertMode.insertOrReplace,
|
||||
);
|
||||
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
// Retrieves the winner of a match based on the highest score.
|
||||
Future<Player?> getWinner({required String matchId}) async {
|
||||
final query = select(scoreTable)
|
||||
..where((s) => s.matchId.equals(matchId))
|
||||
..orderBy([(s) => OrderingTerm.desc(s.score)])
|
||||
..limit(1);
|
||||
final result = await query.getSingleOrNull();
|
||||
|
||||
if (result == null) return null;
|
||||
|
||||
final player = await db.playerDao.getPlayerById(playerId: result.playerId);
|
||||
return Player(
|
||||
id: player.id,
|
||||
name: player.name,
|
||||
createdAt: player.createdAt,
|
||||
description: player.description,
|
||||
);
|
||||
}
|
||||
|
||||
/// Removes the winner of a match.
|
||||
///
|
||||
/// Returns `true` if the winner was removed, `false` if there are multiple
|
||||
/// scores or if the winner cannot be removed.
|
||||
Future<bool> removeWinner({required String matchId}) async {
|
||||
final scores = await getScoresForMatch(matchId: matchId);
|
||||
|
||||
if (scores.length > 1) {
|
||||
return false;
|
||||
} else {
|
||||
return await deleteScoresForMatch(matchId: matchId);
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> hasLooser({required String matchId}) async {
|
||||
return await getLooser(matchId: matchId) != null;
|
||||
}
|
||||
|
||||
// Setting the looser for a game and clearing previous looser if exists.
|
||||
Future<bool> setLooser({
|
||||
required String matchId,
|
||||
required String playerId,
|
||||
}) async {
|
||||
// Clear previous loosers if exists
|
||||
deleteScoresForMatch(matchId: matchId);
|
||||
|
||||
// Set the loosers score to 0
|
||||
final rowsAffected = await into(scoreTable).insert(
|
||||
ScoreTableCompanion.insert(
|
||||
playerId: playerId,
|
||||
matchId: matchId,
|
||||
roundNumber: 0,
|
||||
score: 0,
|
||||
change: 0,
|
||||
),
|
||||
mode: InsertMode.insertOrReplace,
|
||||
);
|
||||
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
/// Retrieves the looser of a match based on the score 0.
|
||||
Future<Player?> getLooser({required String matchId}) async {
|
||||
final query = select(scoreTable)
|
||||
..where((s) => s.matchId.equals(matchId) & s.score.equals(0));
|
||||
final result = await query.getSingleOrNull();
|
||||
|
||||
if (result == null) return null;
|
||||
|
||||
final player = await db.playerDao.getPlayerById(playerId: result.playerId);
|
||||
return Player(
|
||||
id: player.id,
|
||||
name: player.name,
|
||||
createdAt: player.createdAt,
|
||||
description: player.description,
|
||||
);
|
||||
}
|
||||
|
||||
/// Removes the looser of a match.
|
||||
///
|
||||
/// Returns `true` if the looser was removed, `false` if there are multiple
|
||||
/// scores or if the looser cannot be removed.
|
||||
Future<bool> removeLooser({required String matchId}) async {
|
||||
final scores = await getScoresForMatch(matchId: matchId);
|
||||
|
||||
if (scores.length > 1) {
|
||||
return false;
|
||||
} else {
|
||||
return await deleteScoresForMatch(matchId: matchId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user