First version of retrieving a gamesession from the db
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
38
lib/data/db/dao/player_dao.dart
Normal file
38
lib/data/db/dao/player_dao.dart
Normal 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;
|
||||
}
|
||||
}
|
||||
10
lib/data/db/dao/player_dao.g.dart
Normal file
10
lib/data/db/dao/player_dao.g.dart
Normal 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;
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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> {}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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> {}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import 'package:cabo_counter/data/db/dao/game_session_dao.dart';
|
||||
import 'package:cabo_counter/data/db/dao/player_dao.dart';
|
||||
import 'package:cabo_counter/data/db/dao/round_scores_dao.dart';
|
||||
import 'package:cabo_counter/data/db/dao/rounds_dao.dart';
|
||||
import 'package:cabo_counter/data/db/tables/game_session_table.dart';
|
||||
import 'package:cabo_counter/data/db/tables/player_scores_table.dart';
|
||||
import 'package:cabo_counter/data/db/tables/players_table.dart';
|
||||
import 'package:cabo_counter/data/db/tables/player_table.dart';
|
||||
import 'package:cabo_counter/data/db/tables/round_scores_table.dart';
|
||||
import 'package:cabo_counter/data/db/tables/rounds_table.dart';
|
||||
import 'package:drift/drift.dart';
|
||||
@@ -9,13 +12,9 @@ import 'package:path_provider/path_provider.dart';
|
||||
|
||||
part 'database.g.dart';
|
||||
|
||||
@DriftDatabase(tables: [
|
||||
GameSessionTable,
|
||||
PlayerScoresTable,
|
||||
PlayersTable,
|
||||
RoundScoresTable,
|
||||
RoundsTable
|
||||
])
|
||||
@DriftDatabase(
|
||||
tables: [GameSessionTable, PlayerTable, RoundScoresTable, RoundsTable],
|
||||
daos: [GameSessionDao, PlayerDao, RoundsDao, RoundScoresDao])
|
||||
class AppDatabase extends _$AppDatabase {
|
||||
AppDatabase([QueryExecutor? executor]) : super(executor ?? _openConnection());
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,13 +0,0 @@
|
||||
import 'package:cabo_counter/data/db/tables/game_session_table.dart';
|
||||
import 'package:cabo_counter/data/db/tables/players_table.dart';
|
||||
import 'package:drift/drift.dart';
|
||||
|
||||
class PlayerScoresTable extends Table {
|
||||
TextColumn get roundId =>
|
||||
text().references(GameSessionTable, #id, onDelete: KeyAction.cascade)();
|
||||
TextColumn get playerName => text().references(PlayersTable, #name)();
|
||||
IntColumn get totalScore => integer()();
|
||||
|
||||
@override
|
||||
Set<Column<Object>> get primaryKey => {roundId, playerName};
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
import 'package:cabo_counter/data/db/tables/game_session_table.dart';
|
||||
import 'package:drift/drift.dart';
|
||||
|
||||
class PlayersTable extends Table {
|
||||
class PlayerTable extends Table {
|
||||
TextColumn get playerId =>
|
||||
text().references(GameSessionTable, #id, onDelete: KeyAction.cascade)();
|
||||
TextColumn get gameId =>
|
||||
text().references(GameSessionTable, #id, onDelete: KeyAction.cascade)();
|
||||
IntColumn get totalScore => integer()();
|
||||
IntColumn get position => integer()();
|
||||
TextColumn get name => text()();
|
||||
}
|
||||
@@ -3,11 +3,11 @@ import 'package:drift/drift.dart';
|
||||
|
||||
class RoundScoresTable extends Table {
|
||||
TextColumn get roundId =>
|
||||
text().references(RoundsTable, #id, onDelete: KeyAction.cascade)();
|
||||
TextColumn get playerName => text()();
|
||||
text().references(RoundsTable, #roundId, onDelete: KeyAction.cascade)();
|
||||
TextColumn get playerId => text()();
|
||||
IntColumn get score => integer()();
|
||||
IntColumn get scoreUpdate => integer()();
|
||||
|
||||
@override
|
||||
Set<Column<Object>> get primaryKey => {roundId, playerName};
|
||||
Set<Column<Object>> get primaryKey => {roundId, playerId};
|
||||
}
|
||||
|
||||
@@ -6,7 +6,8 @@ class RoundsTable extends Table {
|
||||
TextColumn get gameId =>
|
||||
text().references(GameSessionTable, #id, onDelete: KeyAction.cascade)();
|
||||
IntColumn get roundNumber => integer()();
|
||||
TextColumn get kamikazePlayer => text().nullable()();
|
||||
IntColumn get caboPlayerIndex => integer()();
|
||||
IntColumn get kamikazePlayerIndex => integer().nullable()();
|
||||
|
||||
@override
|
||||
Set<Column<Object>> get primaryKey => {roundId};
|
||||
|
||||
Reference in New Issue
Block a user