First version of retrieving a gamesession from the db

This commit is contained in:
2025-08-21 22:30:13 +02:00
parent 4901e27b90
commit 7a420e909a
28 changed files with 868 additions and 1175 deletions

View File

@@ -1,10 +1,39 @@
import 'package:cabo_counter/data/db/database.dart';
import 'package:cabo_counter/data/db/tables/game_session_table.dart';
import 'package:cabo_counter/data/dto/game_session.dart';
import 'package:cabo_counter/data/dto/player.dart';
import 'package:cabo_counter/data/dto/round.dart';
import 'package:drift/drift.dart';
part 'game_session_dao.g.dart';
@DriftAccessor(tables: [])
@DriftAccessor(tables: [GameSessionTable])
class GameSessionDao extends DatabaseAccessor<AppDatabase>
with _$GameSessionDaoMixin {
GameSessionDao(super.db);
/// Retrieves a game session by its ID.
Future<GameSession> getGameSession(String id) async {
final query = select(gameSessionTable)..where((tbl) => tbl.id.equals(id));
final gameSessionResult = await query.getSingle();
List<Player> playerList = await db.playerDao.getPlayersByGameId(id);
List<Round> roundList = await db.roundsDao.getRoundsByGameId(id);
GameSession gameSession = GameSession(
id: gameSessionResult.id,
createdAt: gameSessionResult.createdAt,
gameTitle: gameSessionResult.gameTitle,
players: playerList.map((player) => player.name).toList(),
pointLimit: gameSessionResult.pointLimit,
caboPenalty: gameSessionResult.caboPenalty,
isPointsLimitEnabled: gameSessionResult.isPointsLimitEnabled,
isGameFinished: gameSessionResult.isGameFinished,
winner: gameSessionResult.winner ?? '',
roundNumber: gameSessionResult.roundNumber,
playerScores: playerList.map((player) => player.totalScore).toList(),
roundList: roundList);
return gameSession;
}
}

View File

@@ -3,4 +3,7 @@
part of 'game_session_dao.dart';
// ignore_for_file: type=lint
mixin _$GameSessionDaoMixin on DatabaseAccessor<AppDatabase> {}
mixin _$GameSessionDaoMixin on DatabaseAccessor<AppDatabase> {
$GameSessionTableTable get gameSessionTable =>
attachedDatabase.gameSessionTable;
}

View File

@@ -0,0 +1,38 @@
import 'package:cabo_counter/data/db/database.dart';
import 'package:cabo_counter/data/db/tables/player_table.dart';
import 'package:cabo_counter/data/dto/player.dart';
import 'package:drift/drift.dart';
part 'player_dao.g.dart';
@DriftAccessor(tables: [PlayerTable])
class PlayerDao extends DatabaseAccessor<AppDatabase> with _$PlayerDaoMixin {
PlayerDao(super.db);
/// Retrieves all players from a game by gameId
Future<List<Player>> getPlayersByGameId(String gameId) async {
final query = select(playerTable)
..where((tbl) => tbl.gameId.equals(gameId));
final playerResults = await query.get();
return playerResults.map((row) {
return Player(
playerId: row.playerId,
gameId: row.gameId,
name: row.name,
position: row.position,
totalScore: row.totalScore,
);
}).toList()
..sort((a, b) => a.position.compareTo(b.position));
}
/// Retrieves a players position by its id
Future<int> getPositionByPlayerId(String playerId) async {
final query = select(playerTable)
..where((tbl) => tbl.playerId.equals(playerId));
final result = await query.getSingle();
return result.position;
}
}

View File

@@ -0,0 +1,10 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'player_dao.dart';
// ignore_for_file: type=lint
mixin _$PlayerDaoMixin on DatabaseAccessor<AppDatabase> {
$GameSessionTableTable get gameSessionTable =>
attachedDatabase.gameSessionTable;
$PlayerTableTable get playerTable => attachedDatabase.playerTable;
}

View File

@@ -1,10 +0,0 @@
import 'package:cabo_counter/data/db/database.dart';
import 'package:drift/drift.dart';
part 'player_scores_dao.g.dart';
@DriftAccessor(tables: [])
class PlayerScoresDao extends DatabaseAccessor<AppDatabase>
with _$PlayerScoresDaoMixin {
PlayerScoresDao(super.db);
}

View File

@@ -1,6 +0,0 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'player_scores_dao.dart';
// ignore_for_file: type=lint
mixin _$PlayerScoresDaoMixin on DatabaseAccessor<AppDatabase> {}

View File

@@ -1,9 +0,0 @@
import 'package:cabo_counter/data/db/database.dart';
import 'package:drift/drift.dart';
part 'players_dao.g.dart';
@DriftAccessor(tables: [])
class PlayersDao extends DatabaseAccessor<AppDatabase> with _$PlayersDaoMixin {
PlayersDao(super.db);
}

View File

@@ -1,6 +0,0 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'players_dao.dart';
// ignore_for_file: type=lint
mixin _$PlayersDaoMixin on DatabaseAccessor<AppDatabase> {}

View File

@@ -1,10 +1,51 @@
import 'package:cabo_counter/data/db/database.dart';
import 'package:cabo_counter/data/db/tables/round_scores_table.dart';
import 'package:cabo_counter/data/dto/round_score.dart';
import 'package:drift/drift.dart';
part 'round_scores_dao.g.dart';
@DriftAccessor(tables: [])
@DriftAccessor(tables: [RoundScoresTable])
class RoundScoresDao extends DatabaseAccessor<AppDatabase>
with _$RoundScoresDaoMixin {
RoundScoresDao(super.db);
/// Retrieves all scores for a specific round by its ID.
Future<List<RoundScore>> getRoundScoresByRoundId(String roundId) async {
final query = select(roundScoresTable)
..where((tbl) => tbl.roundId.equals(roundId));
final result = await query.get();
// Get positions for each player
final scoresWithPosition = await Future.wait(result.map((row) async {
final position = await db.playerDao.getPositionByPlayerId(row.playerId);
return MapEntry(row, position);
}));
// Sort rows by position
scoresWithPosition.sort((a, b) => a.value.compareTo(b.value));
return scoresWithPosition.map((entry) {
final row = entry.key;
return RoundScore(
roundId: roundId,
playerId: row.playerId,
score: row.score,
scoreUpdate: row.scoreUpdate,
);
}).toList();
}
Future<List<int>> getScoresByRoundId(String roundId) async {
List<RoundScore> roundScores = await getRoundScoresByRoundId(roundId);
return roundScores.map((score) => score.score).toList();
}
Future<List<int>> getScoreUpdatesByRoundId(String roundId) async {
List<RoundScore> roundScores = await getRoundScoresByRoundId(roundId);
return roundScores.map((score) => score.scoreUpdate).toList();
}
}

View File

@@ -3,4 +3,10 @@
part of 'round_scores_dao.dart';
// ignore_for_file: type=lint
mixin _$RoundScoresDaoMixin on DatabaseAccessor<AppDatabase> {}
mixin _$RoundScoresDaoMixin on DatabaseAccessor<AppDatabase> {
$GameSessionTableTable get gameSessionTable =>
attachedDatabase.gameSessionTable;
$RoundsTableTable get roundsTable => attachedDatabase.roundsTable;
$RoundScoresTableTable get roundScoresTable =>
attachedDatabase.roundScoresTable;
}

View File

@@ -1,9 +1,39 @@
import 'package:cabo_counter/data/db/database.dart';
import 'package:cabo_counter/data/db/tables/rounds_table.dart';
import 'package:cabo_counter/data/dto/round.dart';
import 'package:drift/drift.dart';
part 'rounds_dao.g.dart';
@DriftAccessor(tables: [])
@DriftAccessor(tables: [RoundsTable])
class RoundsDao extends DatabaseAccessor<AppDatabase> with _$RoundsDaoMixin {
RoundsDao(super.db);
/// Retrieves all rounds for a specific game session by its ID.
Future<List<Round>> getRoundsByGameId(String gameId) async {
final query = select(roundsTable)
..where((tbl) => tbl.gameId.equals(gameId));
final roundResult = await query.get();
final roundList = await Future.wait(
roundResult.map((row) async {
final scores = await db.roundScoresDao.getScoresByRoundId(row.roundId);
final roundScores =
await db.roundScoresDao.getScoreUpdatesByRoundId(row.roundId);
return Round(
roundId: row.roundId,
gameId: row.gameId,
roundNum: row.roundNumber,
caboPlayerIndex: row.caboPlayerIndex,
kamikazePlayerIndex: row.kamikazePlayerIndex,
scores: scores,
scoreUpdates: roundScores,
);
}),
);
return roundList;
}
}

View File

@@ -3,4 +3,8 @@
part of 'rounds_dao.dart';
// ignore_for_file: type=lint
mixin _$RoundsDaoMixin on DatabaseAccessor<AppDatabase> {}
mixin _$RoundsDaoMixin on DatabaseAccessor<AppDatabase> {
$GameSessionTableTable get gameSessionTable =>
attachedDatabase.gameSessionTable;
$RoundsTableTable get roundsTable => attachedDatabase.roundsTable;
}