First version of inserting into db
This commit is contained in:
88
lib/data/dao/game_session_dao.dart
Normal file
88
lib/data/dao/game_session_dao.dart
Normal file
@@ -0,0 +1,88 @@
|
||||
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: [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,
|
||||
pointLimit: gameSessionResult.pointLimit,
|
||||
caboPenalty: gameSessionResult.caboPenalty,
|
||||
isPointsLimitEnabled: gameSessionResult.isPointsLimitEnabled,
|
||||
isGameFinished: gameSessionResult.isGameFinished,
|
||||
winner: gameSessionResult.winner ?? '',
|
||||
roundNumber: gameSessionResult.roundNumber,
|
||||
roundList: roundList);
|
||||
|
||||
return gameSession;
|
||||
}
|
||||
|
||||
/// Retrieves all game sessions from the database.
|
||||
Future<List<GameSession>> getAllGameSessions() async {
|
||||
final query = select(gameSessionTable);
|
||||
final gameSessionResults = await query.get();
|
||||
|
||||
List<GameSession> gameSessions = await Future.wait(
|
||||
gameSessionResults.map((row) async {
|
||||
List<Player> playerList = await db.playerDao.getPlayersByGameId(row.id);
|
||||
List<Round> roundList = await db.roundsDao.getRoundsByGameId(row.id);
|
||||
|
||||
return GameSession(
|
||||
id: row.id,
|
||||
createdAt: row.createdAt,
|
||||
gameTitle: row.gameTitle,
|
||||
players: playerList,
|
||||
pointLimit: row.pointLimit,
|
||||
caboPenalty: row.caboPenalty,
|
||||
isPointsLimitEnabled: row.isPointsLimitEnabled,
|
||||
isGameFinished: row.isGameFinished,
|
||||
winner: row.winner ?? '',
|
||||
roundNumber: row.roundNumber,
|
||||
roundList: roundList,
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
return gameSessions;
|
||||
}
|
||||
|
||||
Future<void> insertGameSession(GameSession gameSession) async {
|
||||
await into(gameSessionTable).insert(
|
||||
GameSessionTableCompanion.insert(
|
||||
id: gameSession.id,
|
||||
createdAt: gameSession.createdAt,
|
||||
gameTitle: gameSession.gameTitle,
|
||||
pointLimit: gameSession.pointLimit,
|
||||
caboPenalty: gameSession.caboPenalty,
|
||||
isPointsLimitEnabled: gameSession.isPointsLimitEnabled,
|
||||
isGameFinished: gameSession.isGameFinished,
|
||||
winner: Value(gameSession.winner),
|
||||
roundNumber: gameSession.roundNumber,
|
||||
),
|
||||
);
|
||||
|
||||
db.playerDao.insertPlayers(gameSession.id, gameSession.players);
|
||||
|
||||
db.roundsDao.insertMultipleRounds(
|
||||
gameSession.id, gameSession.roundList, gameSession.players);
|
||||
}
|
||||
}
|
||||
9
lib/data/dao/game_session_dao.g.dart
Normal file
9
lib/data/dao/game_session_dao.g.dart
Normal file
@@ -0,0 +1,9 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'game_session_dao.dart';
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
mixin _$GameSessionDaoMixin on DatabaseAccessor<AppDatabase> {
|
||||
$GameSessionTableTable get gameSessionTable =>
|
||||
attachedDatabase.gameSessionTable;
|
||||
}
|
||||
56
lib/data/dao/player_dao.dart
Normal file
56
lib/data/dao/player_dao.dart
Normal file
@@ -0,0 +1,56 @@
|
||||
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;
|
||||
}
|
||||
|
||||
/// Inserts a new player into the database.
|
||||
Future<void> insertPlayers(String gameId, List<Player> players) async {
|
||||
await batch((batch) {
|
||||
for (int i = 0; i < players.length; i++) {
|
||||
batch.insert(
|
||||
playerTable,
|
||||
PlayerTableCompanion.insert(
|
||||
playerId: players[i].playerId,
|
||||
gameId: gameId,
|
||||
name: players[i].name,
|
||||
position: i,
|
||||
totalScore: players[i].totalScore,
|
||||
),
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
10
lib/data/dao/player_dao.g.dart
Normal file
10
lib/data/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;
|
||||
}
|
||||
59
lib/data/dao/round_scores_dao.dart
Normal file
59
lib/data/dao/round_scores_dao.dart
Normal file
@@ -0,0 +1,59 @@
|
||||
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: [RoundScoresTable])
|
||||
class RoundScoresDao extends DatabaseAccessor<AppDatabase>
|
||||
with _$RoundScoresDaoMixin {
|
||||
RoundScoresDao(super.db);
|
||||
|
||||
/// Retrieves all scores for a specific round by its ID.
|
||||
/// This method returns a list of [RoundScore] objects sorted by player
|
||||
/// position in the corresponding gameSession
|
||||
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();
|
||||
}
|
||||
|
||||
/// Retrieves all scores for a specific round by its ID.
|
||||
/// This method returns a list of scores sorted by player position in the
|
||||
/// corresponding gameSession.
|
||||
Future<List<int>> getScoresByRoundId(String roundId) async {
|
||||
List<RoundScore> roundScores = await _getRoundScoresByRoundId(roundId);
|
||||
|
||||
return roundScores.map((score) => score.score).toList();
|
||||
}
|
||||
|
||||
/// Retrieves all score updates for a specific round by its ID.
|
||||
/// This method returns a list of score updates sorted by player position in
|
||||
/// the corresponding gameSession.
|
||||
Future<List<int>> getScoreUpdatesByRoundId(String roundId) async {
|
||||
List<RoundScore> roundScores = await _getRoundScoresByRoundId(roundId);
|
||||
|
||||
return roundScores.map((score) => score.scoreUpdate).toList();
|
||||
}
|
||||
}
|
||||
12
lib/data/dao/round_scores_dao.g.dart
Normal file
12
lib/data/dao/round_scores_dao.g.dart
Normal file
@@ -0,0 +1,12 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'round_scores_dao.dart';
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
mixin _$RoundScoresDaoMixin on DatabaseAccessor<AppDatabase> {
|
||||
$GameSessionTableTable get gameSessionTable =>
|
||||
attachedDatabase.gameSessionTable;
|
||||
$RoundsTableTable get roundsTable => attachedDatabase.roundsTable;
|
||||
$RoundScoresTableTable get roundScoresTable =>
|
||||
attachedDatabase.roundScoresTable;
|
||||
}
|
||||
140
lib/data/dao/rounds_dao.dart
Normal file
140
lib/data/dao/rounds_dao.dart
Normal file
@@ -0,0 +1,140 @@
|
||||
import 'package:cabo_counter/data/db/database.dart';
|
||||
import 'package:cabo_counter/data/db/tables/round_scores_table.dart';
|
||||
import 'package:cabo_counter/data/db/tables/rounds_table.dart';
|
||||
import 'package:cabo_counter/data/dto/player.dart';
|
||||
import 'package:cabo_counter/data/dto/round.dart';
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
part 'rounds_dao.g.dart';
|
||||
|
||||
@DriftAccessor(tables: [RoundsTable, RoundScoresTable])
|
||||
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;
|
||||
}
|
||||
|
||||
/// Retrieves a specific round by its [gameId] and [roundNumber].
|
||||
/// Returns null if the round does not exist.
|
||||
Future<Round?> getRoundByGameIdAndRoundNumber(
|
||||
String gameId, int roundNumber) async {
|
||||
final query = select(roundsTable)
|
||||
..where((tbl) =>
|
||||
tbl.gameId.equals(gameId) & tbl.roundNumber.equals(roundNumber));
|
||||
final roundResult = await query.getSingleOrNull();
|
||||
if (roundResult == null) return null;
|
||||
|
||||
final scoreResult = await Future.wait([
|
||||
db.roundScoresDao.getScoresByRoundId(roundResult.roundId),
|
||||
db.roundScoresDao.getScoreUpdatesByRoundId(roundResult.roundId),
|
||||
]);
|
||||
|
||||
return Round(
|
||||
roundId: roundResult.roundId,
|
||||
gameId: roundResult.gameId,
|
||||
roundNum: roundResult.roundNumber,
|
||||
caboPlayerIndex: roundResult.caboPlayerIndex,
|
||||
kamikazePlayerIndex: roundResult.kamikazePlayerIndex,
|
||||
scores: scoreResult[0],
|
||||
scoreUpdates: scoreResult[1],
|
||||
);
|
||||
}
|
||||
|
||||
/// Inserts a new round into the database.
|
||||
/// This method creates a new round with a unique ID and inserts it
|
||||
/// along with the scores for each player in the round.
|
||||
/// [gameId] is the ID of the game session this round belongs to.
|
||||
/// [round] is the round data to be inserted.
|
||||
/// [players] is the list of players in the game session.
|
||||
Future<void> insertOneRound(
|
||||
String gameId, Round round, List<Player> players) async {
|
||||
var uuid = const Uuid();
|
||||
String roundId = uuid.v1();
|
||||
|
||||
final roundEntry = RoundsTableCompanion.insert(
|
||||
roundId: roundId,
|
||||
gameId: gameId,
|
||||
roundNumber: round.roundNum,
|
||||
caboPlayerIndex: round.caboPlayerIndex,
|
||||
kamikazePlayerIndex: Value(round.kamikazePlayerIndex),
|
||||
);
|
||||
|
||||
await into(roundsTable).insert(roundEntry);
|
||||
|
||||
for (int i = 0; i < players.length; i++) {
|
||||
final player = players[i];
|
||||
final roundScoreEntry = RoundScoresTableCompanion.insert(
|
||||
roundId: roundId,
|
||||
playerId: player.playerId,
|
||||
score: round.scores[i],
|
||||
scoreUpdate: round.scoreUpdates[i],
|
||||
);
|
||||
await into(roundScoresTable).insert(roundScoreEntry);
|
||||
}
|
||||
}
|
||||
|
||||
/// Inserts multiple rounds into the database.
|
||||
/// This method uses a batch operation to insert all rounds and their scores
|
||||
/// in a single transaction.
|
||||
/// [gameId] is the ID of the game session these rounds belong to.
|
||||
/// [rounds] is the list of rounds to be inserted.
|
||||
/// [players] is the list of players in the game session.
|
||||
Future<void> insertMultipleRounds(
|
||||
String gameId, List<Round> rounds, List<Player> players) async {
|
||||
var uuid = const Uuid();
|
||||
|
||||
await batch((batch) {
|
||||
final roundEntries = <RoundsTableCompanion>[];
|
||||
final roundScoreEntries = <RoundScoresTableCompanion>[];
|
||||
|
||||
for (final round in rounds) {
|
||||
final roundId = uuid.v1();
|
||||
roundEntries.add(RoundsTableCompanion.insert(
|
||||
roundId: roundId,
|
||||
gameId: gameId,
|
||||
roundNumber: round.roundNum,
|
||||
caboPlayerIndex: round.caboPlayerIndex,
|
||||
kamikazePlayerIndex: Value(round.kamikazePlayerIndex),
|
||||
));
|
||||
|
||||
for (int i = 0; i < players.length; i++) {
|
||||
roundScoreEntries.add(RoundScoresTableCompanion.insert(
|
||||
roundId: roundId,
|
||||
playerId: players[i].playerId,
|
||||
score: round.scores[i],
|
||||
scoreUpdate: round.scoreUpdates[i],
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
batch.insertAll(roundsTable, roundEntries);
|
||||
batch.insertAll(roundScoresTable, roundScoreEntries);
|
||||
});
|
||||
}
|
||||
}
|
||||
12
lib/data/dao/rounds_dao.g.dart
Normal file
12
lib/data/dao/rounds_dao.g.dart
Normal file
@@ -0,0 +1,12 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'rounds_dao.dart';
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
mixin _$RoundsDaoMixin on DatabaseAccessor<AppDatabase> {
|
||||
$GameSessionTableTable get gameSessionTable =>
|
||||
attachedDatabase.gameSessionTable;
|
||||
$RoundsTableTable get roundsTable => attachedDatabase.roundsTable;
|
||||
$RoundScoresTableTable get roundScoresTable =>
|
||||
attachedDatabase.roundScoresTable;
|
||||
}
|
||||
Reference in New Issue
Block a user