MVP #141

Merged
flixcoo merged 705 commits from development into main 2026-01-09 12:55:50 +00:00
3 changed files with 143 additions and 37 deletions
Showing only changes of commit 31589855f2 - Show all commits

View File

@@ -10,16 +10,13 @@ class GroupGameDao extends DatabaseAccessor<AppDatabase>
with _$GroupGameDaoMixin { with _$GroupGameDaoMixin {
GroupGameDao(super.db); GroupGameDao(super.db);
/// Checks if there is a group associated with the given [gameId]. /// Associates a group with a game by inserting a record into the
/// Returns `true` if there is a group, otherwise `false`. /// [GroupGameTable].
Future<bool> hasGameGroup({required String gameId}) async { Future<void> addGroupToGame(String gameId, String groupId) async {
final count = await into(groupGameTable).insert(
await (selectOnly(groupGameTable) GroupGameTableCompanion.insert(groupId: groupId, gameId: gameId),
..where(groupGameTable.gameId.equals(gameId)) mode: InsertMode.insertOrReplace,
..addColumns([groupGameTable.groupId.count()])) );
.map((row) => row.read(groupGameTable.groupId.count()))
.getSingle();
return (count ?? 0) > 0;
} }
/// Retrieves the [Group] associated with the given [gameId]. /// Retrieves the [Group] associated with the given [gameId].
@@ -37,11 +34,39 @@ class GroupGameDao extends DatabaseAccessor<AppDatabase>
return group; return group;
} }
/// Associates a group with a game by inserting a record into the /// Checks if there is a group associated with the given [gameId].
/// [GroupGameTable]. /// Returns `true` if there is a group, otherwise `false`.
Future<void> addGroupToGame(String gameId, String groupId) async { Future<bool> gameHasGroup({required String gameId}) async {
await into( final count =
groupGameTable, await (selectOnly(groupGameTable)
).insert(GroupGameTableCompanion.insert(groupId: groupId, gameId: gameId), mode: InsertMode.insertOrReplace); ..where(groupGameTable.gameId.equals(gameId))
..addColumns([groupGameTable.groupId.count()]))
.map((row) => row.read(groupGameTable.groupId.count()))
.getSingle();
return (count ?? 0) > 0;
}
/// Checks if a specific group is associated with a specific game.
/// Returns `true` if the group is in the game, otherwise `false`.
Future<bool> isGroupInGame({
required String gameId,
required String groupId,
}) async {
final count =
await (selectOnly(groupGameTable)
..where(
groupGameTable.gameId.equals(gameId) &
groupGameTable.groupId.equals(groupId),
)
..addColumns([groupGameTable.groupId.count()]))
.map((row) => row.read(groupGameTable.groupId.count()))
.getSingle();
return (count ?? 0) > 0;
}
Future<bool> removeGroupFromGame({required String gameId}) async {
final query = delete(groupGameTable)..where((g) => g.gameId.equals(gameId));
final rowsAffected = await query.go();
return rowsAffected > 0;
} }
} }

View File

@@ -10,16 +10,16 @@ class PlayerGameDao extends DatabaseAccessor<AppDatabase>
with _$PlayerGameDaoMixin { with _$PlayerGameDaoMixin {
PlayerGameDao(super.db); PlayerGameDao(super.db);
/// Checks if there are any players associated with the given [gameId]. /// Associates a player with a game by inserting a record into the
/// Returns `true` if there are players, otherwise `false`. /// [PlayerGameTable].
Future<bool> gameHasPlayers({required String gameId}) async { Future<void> addPlayerToGame({
final count = required String gameId,
await (selectOnly(playerGameTable) required String playerId,
..where(playerGameTable.gameId.equals(gameId)) }) async {
..addColumns([playerGameTable.playerId.count()])) await into(playerGameTable).insert(
.map((row) => row.read(playerGameTable.playerId.count())) PlayerGameTableCompanion.insert(playerId: playerId, gameId: gameId),
.getSingle(); mode: InsertMode.insertOrReplace,
return (count ?? 0) > 0; );
} }
/// Retrieves a list of [Player]s associated with the given [gameId]. /// Retrieves a list of [Player]s associated with the given [gameId].
@@ -38,15 +38,45 @@ class PlayerGameDao extends DatabaseAccessor<AppDatabase>
return players; return players;
} }
/// Associates a player with a game by inserting a record into the /// Checks if there are any players associated with the given [gameId].
/// [PlayerGameTable]. /// Returns `true` if there are players, otherwise `false`.
Future<void> addPlayerToGame({ Future<bool> gameHasPlayers({required String gameId}) async {
final count =
await (selectOnly(playerGameTable)
..where(playerGameTable.gameId.equals(gameId))
..addColumns([playerGameTable.playerId.count()]))
.map((row) => row.read(playerGameTable.playerId.count()))
.getSingle();
return (count ?? 0) > 0;
}
/// Checks if a specific player is associated with a specific game.
/// Returns `true` if the player is in the game, otherwise `false`.
Future<bool> isPlayerInGame({
required String gameId, required String gameId,
required String playerId, required String playerId,
}) async { }) async {
await into(playerGameTable).insert( final count =
PlayerGameTableCompanion.insert(playerId: playerId, gameId: gameId), await (selectOnly(playerGameTable)
mode: InsertMode.insertOrReplace, ..where(playerGameTable.gameId.equals(gameId))
); ..where(playerGameTable.playerId.equals(playerId))
..addColumns([playerGameTable.playerId.count()]))
.map((row) => row.read(playerGameTable.playerId.count()))
.getSingle();
return (count ?? 0) > 0;
}
/// Removes the association of a player with a game by deleting the record
/// from the [PlayerGameTable].
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
Future<bool> removePlayerFromGame({
required String gameId,
required String playerId,
}) async {
final query = delete(playerGameTable)
..where((pg) => pg.gameId.equals(gameId))
..where((pg) => pg.playerId.equals(playerId));
final rowsAffected = await query.go();
return rowsAffected > 0;
} }
} }

View File

@@ -14,10 +14,12 @@ void main() {
late Player player3; late Player player3;
late Player player4; late Player player4;
late Player player5; late Player player5;
late Player player6;
late Group testgroup; late Group testgroup;
late Group testgroup2; late Group testgroup2;
late Game testgame; late Game testgame;
late Game testgame2; late Game testgame2;
late Game testgame3;
final fixedDate = DateTime(2025, 19, 11, 00, 11, 23); final fixedDate = DateTime(2025, 19, 11, 00, 11, 23);
final fakeClock = Clock(() => fixedDate); final fakeClock = Clock(() => fixedDate);
@@ -36,6 +38,7 @@ void main() {
player3 = Player(name: 'Charlie'); player3 = Player(name: 'Charlie');
player4 = Player(name: 'Diana'); player4 = Player(name: 'Diana');
player5 = Player(name: 'Eve'); player5 = Player(name: 'Eve');
player6 = Player(name: 'Frank');
testgroup = Group( testgroup = Group(
name: 'Test Group', name: 'Test Group',
members: [player1, player2, player3], members: [player1, player2, player3],
@@ -54,6 +57,7 @@ void main() {
group: testgroup2, group: testgroup2,
players: [player1, player2, player3], players: [player1, player2, player3],
); );
testgame3 = Game(name: 'Third Test Game', players: [player4, player5]);
}); });
}); });
tearDown(() async { tearDown(() async {
@@ -240,10 +244,57 @@ void main() {
expect(gameCount, 0); expect(gameCount, 0);
}); });
// TODO: Implement test(
test('Adding a player to a game works correclty', () async {}); 'Adding and removing player to and from a game works correclty',
() async {
database.gameDao.addGame(game: testgame);
database.playerDao.addPlayer(player: player6);
database.playerGameDao.addPlayerToGame(
gameId: testgame.id,
playerId: player6.id,
);
// TODO: Implement var playerInGame = await database.playerGameDao.isPlayerInGame(
test('Adding a group to a game works correclty', () async {}); gameId: testgame.id,
playerId: player6.id,
);
expect(playerInGame, true);
final playerRemoved = await database.playerGameDao.removePlayerFromGame(
gameId: testgame.id,
playerId: player6.id,
);
expect(playerRemoved, true);
playerInGame = await database.playerGameDao.isPlayerInGame(
gameId: testgame.id,
playerId: player6.id,
);
expect(playerInGame, false);
},
);
test(
'Adding and removing a group to and from a game works correclty',
() async {
database.gameDao.addGame(game: testgame3);
database.groupDao.addGroup(group: testgroup);
database.groupGameDao.addGroupToGame(testgame3.id, testgroup.id);
var gameHasGroup = await database.groupGameDao.gameHasGroup(
gameId: testgame3.id,
);
expect(gameHasGroup, true);
final groupRemoved = await database.groupGameDao.removeGroupFromGame(
gameId: testgame3.id,
);
expect(groupRemoved, true);
gameHasGroup = await database.groupGameDao.gameHasGroup(
gameId: testgame3.id,
);
expect(gameHasGroup, false);
},
);
}); });
} }