Compare commits
6 Commits
7aba8554c0
...
a12f4eb1c1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a12f4eb1c1 | ||
|
|
0eb8e2683c | ||
|
|
07b9b78252 | ||
|
|
fa9ad9dbae | ||
|
|
25699dffc0 | ||
|
|
70d6178829 |
@@ -231,4 +231,44 @@ class GroupDao extends DatabaseAccessor<AppDatabase> with _$GroupDaoMixin {
|
|||||||
final rowsAffected = await query.go();
|
final rowsAffected = await query.go();
|
||||||
return rowsAffected > 0;
|
return rowsAffected > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Replaces all players in a group with the provided list of players.
|
||||||
|
/// Removes all existing players from the group and adds the new players.
|
||||||
|
/// Also adds any new players to the player table if they don't exist.
|
||||||
|
Future<void> replaceGroupPlayers({
|
||||||
|
required String groupId,
|
||||||
|
required List<Player> newPlayers,
|
||||||
|
}) async {
|
||||||
|
await db.transaction(() async {
|
||||||
|
// Remove all existing players from the group
|
||||||
|
final deleteQuery = delete(db.playerGroupTable)
|
||||||
|
..where((p) => p.groupId.equals(groupId));
|
||||||
|
await deleteQuery.go();
|
||||||
|
|
||||||
|
// Add new players to the player table if they don't exist
|
||||||
|
await Future.wait(
|
||||||
|
newPlayers.map((player) async {
|
||||||
|
if (!await db.playerDao.playerExists(playerId: player.id)) {
|
||||||
|
await db.playerDao.addPlayer(player: player);
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Add the new players to the group
|
||||||
|
await db.batch(
|
||||||
|
(b) => b.insertAll(
|
||||||
|
db.playerGroupTable,
|
||||||
|
newPlayers
|
||||||
|
.map(
|
||||||
|
(player) => PlayerGroupTableCompanion.insert(
|
||||||
|
playerId: player.id,
|
||||||
|
groupId: groupId,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.toList(),
|
||||||
|
mode: InsertMode.insertOrReplace,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -355,39 +355,119 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
|
|||||||
return rowsAffected > 0;
|
return rowsAffected > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Replaces all players in a match with the provided list of players.
|
||||||
|
/// Removes all existing players from the match and adds the new players.
|
||||||
|
/// Also adds any new players to the player table if they don't exist.
|
||||||
|
Future<void> replaceMatchPlayers({
|
||||||
|
required String matchId,
|
||||||
|
required List<Player> newPlayers,
|
||||||
|
}) async {
|
||||||
|
await db.transaction(() async {
|
||||||
|
// Remove all existing players from the match
|
||||||
|
final deleteQuery = delete(db.playerMatchTable)
|
||||||
|
..where((p) => p.matchId.equals(matchId));
|
||||||
|
await deleteQuery.go();
|
||||||
|
|
||||||
|
// Add new players to the player table if they don't exist
|
||||||
|
await Future.wait(
|
||||||
|
newPlayers.map((player) async {
|
||||||
|
if (!await db.playerDao.playerExists(playerId: player.id)) {
|
||||||
|
await db.playerDao.addPlayer(player: player);
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Add the new players to the match
|
||||||
|
await Future.wait(
|
||||||
|
newPlayers.map((player) => db.playerMatchDao.addPlayerToMatch(
|
||||||
|
matchId: matchId,
|
||||||
|
playerId: player.id,
|
||||||
|
)),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -308,5 +308,35 @@ void main() {
|
|||||||
);
|
);
|
||||||
expect(secondRemoval, false);
|
expect(secondRemoval, false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Verifies that replaceGroupPlayers removes all existing players and replaces with new list.
|
||||||
|
test('replaceGroupPlayers replaces all group members correctly', () async {
|
||||||
|
// Create initial group with 3 players
|
||||||
|
await database.groupDao.addGroup(group: testGroup);
|
||||||
|
|
||||||
|
// Verify initial members
|
||||||
|
var groupMembers = await database.groupDao.getGroupById(
|
||||||
|
groupId: testGroup.id,
|
||||||
|
);
|
||||||
|
expect(groupMembers.members.length, 3);
|
||||||
|
|
||||||
|
// Replace with new list containing 2 different players
|
||||||
|
final newPlayersList = [testPlayer3, testPlayer4];
|
||||||
|
await database.groupDao.replaceGroupPlayers(
|
||||||
|
groupId: testGroup.id,
|
||||||
|
newPlayers: newPlayersList,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Get updated group and verify members
|
||||||
|
groupMembers = await database.groupDao.getGroupById(
|
||||||
|
groupId: testGroup.id,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(groupMembers.members.length, 2);
|
||||||
|
expect(groupMembers.members.any((p) => p.id == testPlayer3.id), true);
|
||||||
|
expect(groupMembers.members.any((p) => p.id == testPlayer4.id), true);
|
||||||
|
expect(groupMembers.members.any((p) => p.id == testPlayer1.id), false);
|
||||||
|
expect(groupMembers.members.any((p) => p.id == testPlayer2.id), false);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -884,5 +884,36 @@ void main() {
|
|||||||
expect(playersInTeam.length, 1);
|
expect(playersInTeam.length, 1);
|
||||||
expect(playersInTeam[0].id, testPlayer2.id);
|
expect(playersInTeam[0].id, testPlayer2.id);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Verifies that replaceMatchPlayers removes all existing players and replaces with new list.
|
||||||
|
test('replaceMatchPlayers replaces all match players correctly', () async {
|
||||||
|
// Create initial match with 3 players
|
||||||
|
await database.matchDao.addMatch(match: testMatchOnlyPlayers);
|
||||||
|
|
||||||
|
// Verify initial players
|
||||||
|
var matchPlayers = await database.matchDao.getMatchById(
|
||||||
|
matchId: testMatchOnlyPlayers.id,
|
||||||
|
);
|
||||||
|
expect(matchPlayers.players.length, 3);
|
||||||
|
|
||||||
|
// Replace with new list containing 2 different players
|
||||||
|
final newPlayersList = [testPlayer1, testPlayer2];
|
||||||
|
await database.matchDao.replaceMatchPlayers(
|
||||||
|
matchId: testMatchOnlyPlayers.id,
|
||||||
|
newPlayers: newPlayersList,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Get updated match and verify players
|
||||||
|
matchPlayers = await database.matchDao.getMatchById(
|
||||||
|
matchId: testMatchOnlyPlayers.id,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(matchPlayers.players.length, 2);
|
||||||
|
expect(matchPlayers.players.any((p) => p.id == testPlayer1.id), true);
|
||||||
|
expect(matchPlayers.players.any((p) => p.id == testPlayer2.id), true);
|
||||||
|
expect(matchPlayers.players.any((p) => p.id == testPlayer4.id), false);
|
||||||
|
expect(matchPlayers.players.any((p) => p.id == testPlayer5.id), false);
|
||||||
|
expect(matchPlayers.players.any((p) => p.id == testPlayer6.id), false);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user