feat: updatePlayerName keeps created order in nameCount
All checks were successful
Pull Request Pipeline / test (pull_request) Successful in 46s
Pull Request Pipeline / lint (pull_request) Successful in 54s

This commit is contained in:
2026-05-22 20:06:27 +02:00
parent 4dcd4f0f71
commit 5a652a5f2c

View File

@@ -159,49 +159,63 @@ class PlayerDao extends DatabaseAccessor<AppDatabase> with _$PlayerDaoMixin {
/* Update */ /* Update */
/// Updates the name of the player with the given [playerId] to [name]. /// Updates the name of the player with the given [playerId] to [name].
///
/// Keeps the `nameCount` values of the affected name groups consistent:
/// - The renamed player gets a fresh `nameCount` for the new name group.
/// - All players in the previous name group whose `nameCount` was greater
/// than the removed one get decremented by 1, so the numbering stays
/// contiguous (1..N) in `createdAt` order.
/// - If only one player remains in the previous name group, their
/// `nameCount` is reset to 0.
Future<bool> updatePlayerName({ Future<bool> updatePlayerName({
required String playerId, required String playerId,
required String name, required String name,
}) async { }) async {
// Get previous name and name count for the player before updating return transaction(() async {
final previousPlayerName = final previousPlayer = await (select(
await (select(playerTable)..where((p) => p.id.equals(playerId))) playerTable,
.map((row) => row.name) )..where((p) => p.id.equals(playerId))).getSingleOrNull();
.getSingleOrNull() ?? if (previousPlayer == null) return false;
'';
// Update name count for the new name final previousName = previousPlayer.name;
final newNameCount = await _processNameCount(name: name); final previousCount = previousPlayer.nameCount;
// Update name and nameCount // Determine the nameCount for the renamed player in the new group.
final rowsAffected = final newNameCount = await _processNameCount(name: name);
await (update(playerTable)..where((p) => p.id.equals(playerId))).write(
PlayerTableCompanion(
name: Value(name),
nameCount: Value(newNameCount),
),
);
// Updating name count for the previous name final rowsAffected =
final previousNameCount = await getNameCount(name: previousPlayerName); await (update(
if (previousNameCount > 0) { playerTable,
// At least one more player with the previous name )..where((p) => p.id.equals(playerId))).write(
PlayerTableCompanion(
name: Value(name),
nameCount: Value(newNameCount),
),
);
// Get the player with the highest count // Consolidate the previous name group.
final player = await getPlayerWithHighestNameCount( final remainingCount = await getNameCount(name: previousName);
name: previousPlayerName,
);
if (previousNameCount > 1) { if (remainingCount == 1) {
// Multiple players // Only one player left
final nameCount = await getNameCount(name: previousPlayerName); await (update(playerTable)..where((p) => p.name.equals(previousName)))
await updateNameCount(playerId: player!.id, nameCount: nameCount); .write(const PlayerTableCompanion(nameCount: Value(0)));
} else { } else if (remainingCount > 1 && previousCount > 0) {
// Only one player // Shift every player above the gap down by one to keep numbering in order.
await updateNameCount(playerId: player!.id, nameCount: 0); await (update(playerTable)..where(
(p) =>
p.name.equals(previousName) &
p.nameCount.isBiggerThanValue(previousCount),
))
.write(
PlayerTableCompanion.custom(
nameCount: playerTable.nameCount - const Constant(1),
),
);
} }
}
return rowsAffected > 0; return rowsAffected > 0;
});
} }
/// Updates the description of the player with the given [playerId] to /// Updates the description of the player with the given [playerId] to