Implemented methods and test for winner
This commit is contained in:
@@ -253,4 +253,49 @@ class GameDao extends DatabaseAccessor<AppDatabase> with _$GameDaoMixin {
|
||||
final rowsAffected = await query.go();
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
/// Sets the winner of the game with the given [gameId] to the player with
|
||||
/// the given [winnerId].
|
||||
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
||||
Future<bool> setWinner({
|
||||
required String gameId,
|
||||
required String winnerId,
|
||||
}) async {
|
||||
final query = update(gameTable)..where((g) => g.id.equals(gameId));
|
||||
final rowsAffected = await query.write(
|
||||
GameTableCompanion(winnerId: Value(winnerId)),
|
||||
);
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
/// Retrieves the winner of the game with the given [gameId].
|
||||
/// Returns the [Player] who won the game, or `null` if no winner is set.
|
||||
Future<Player?> getWinner({required String gameId}) async {
|
||||
final query = select(gameTable)..where((g) => g.id.equals(gameId));
|
||||
final result = await query.getSingleOrNull();
|
||||
if (result == null || result.winnerId == null) {
|
||||
return null;
|
||||
}
|
||||
final winner = await db.playerDao.getPlayerById(playerId: result.winnerId!);
|
||||
return winner;
|
||||
}
|
||||
|
||||
/// Removes the winner of the game with the given [gameId].
|
||||
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
||||
Future<bool> removeWinner({required String gameId}) async {
|
||||
final query = update(gameTable)..where((g) => g.id.equals(gameId));
|
||||
final rowsAffected = await query.write(
|
||||
const GameTableCompanion(winnerId: Value(null)),
|
||||
);
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
/// Checks if the game with the given [gameId] has a winner set.
|
||||
/// Returns `true` if a winner is set, otherwise `false`.
|
||||
Future<bool> hasWinner({required String gameId}) async {
|
||||
final query = select(gameTable)
|
||||
..where((g) => g.id.equals(gameId) & g.winnerId.isNotNull());
|
||||
final result = await query.getSingleOrNull();
|
||||
return result != null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,5 +234,79 @@ void main() {
|
||||
gameCount = await database.gameDao.getGameCount();
|
||||
expect(gameCount, 0);
|
||||
});
|
||||
|
||||
test('Checking if game has winner works correclty', () async {
|
||||
await database.gameDao.addGame(game: testGame1);
|
||||
await database.gameDao.addGame(game: testGameOnlyGroup);
|
||||
|
||||
var hasWinner = await database.gameDao.hasWinner(gameId: testGame1.id);
|
||||
expect(hasWinner, true);
|
||||
|
||||
hasWinner = await database.gameDao.hasWinner(
|
||||
gameId: testGameOnlyGroup.id,
|
||||
);
|
||||
expect(hasWinner, false);
|
||||
});
|
||||
|
||||
test('Fetching the winner of a game works correctly', () async {
|
||||
await database.gameDao.addGame(game: testGame1);
|
||||
|
||||
final winner = await database.gameDao.getWinner(gameId: testGame1.id);
|
||||
if (winner == null) {
|
||||
fail('Winner is null');
|
||||
} else {
|
||||
expect(winner.id, testGame1.winner!.id);
|
||||
expect(winner.name, testGame1.winner!.name);
|
||||
expect(winner.createdAt, testGame1.winner!.createdAt);
|
||||
}
|
||||
});
|
||||
|
||||
test('Updating the winner of a game works correctly', () async {
|
||||
await database.gameDao.addGame(game: testGame1);
|
||||
|
||||
final winner = await database.gameDao.getWinner(gameId: testGame1.id);
|
||||
if (winner == null) {
|
||||
fail('Winner is null');
|
||||
} else {
|
||||
expect(winner.id, testGame1.winner!.id);
|
||||
expect(winner.name, testGame1.winner!.name);
|
||||
expect(winner.createdAt, testGame1.winner!.createdAt);
|
||||
expect(winner.id, testPlayer4.id);
|
||||
expect(winner.id != testPlayer5.id, true);
|
||||
}
|
||||
|
||||
await database.gameDao.setWinner(
|
||||
gameId: testGame1.id,
|
||||
winnerId: testPlayer5.id,
|
||||
);
|
||||
|
||||
final newWinner = await database.gameDao.getWinner(gameId: testGame1.id);
|
||||
|
||||
if (newWinner == null) {
|
||||
fail('New winner is null');
|
||||
} else {
|
||||
expect(newWinner.id, testPlayer5.id);
|
||||
expect(newWinner.name, testPlayer5.name);
|
||||
expect(newWinner.createdAt, testPlayer5.createdAt);
|
||||
}
|
||||
});
|
||||
|
||||
test('Removing a winner works correctly', () async {
|
||||
await database.gameDao.addGame(game: testGame2);
|
||||
|
||||
var hasWinner = await database.gameDao.hasWinner(gameId: testGame2.id);
|
||||
expect(hasWinner, true);
|
||||
|
||||
await database.gameDao.removeWinner(gameId: testGame2.id);
|
||||
|
||||
hasWinner = await database.gameDao.hasWinner(gameId: testGame2.id);
|
||||
expect(hasWinner, false);
|
||||
|
||||
final removedWinner = await database.gameDao.getWinner(
|
||||
gameId: testGame2.id,
|
||||
);
|
||||
|
||||
expect(removedWinner, null);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user