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 previousCount = previousPlayer.nameCount;
// Determine the nameCount for the renamed player in the new group.
final newNameCount = await _processNameCount(name: name); final newNameCount = await _processNameCount(name: name);
// Update name and nameCount
final rowsAffected = final rowsAffected =
await (update(playerTable)..where((p) => p.id.equals(playerId))).write( await (update(
playerTable,
)..where((p) => p.id.equals(playerId))).write(
PlayerTableCompanion( PlayerTableCompanion(
name: Value(name), name: Value(name),
nameCount: Value(newNameCount), nameCount: Value(newNameCount),
), ),
); );
// Updating name count for the previous name // Consolidate the previous name group.
final previousNameCount = await getNameCount(name: previousPlayerName); final remainingCount = await getNameCount(name: previousName);
if (previousNameCount > 0) {
// At least one more player with the previous name
// Get the player with the highest count if (remainingCount == 1) {
final player = await getPlayerWithHighestNameCount( // Only one player left
name: previousPlayerName, await (update(playerTable)..where((p) => p.name.equals(previousName)))
.write(const PlayerTableCompanion(nameCount: Value(0)));
} else if (remainingCount > 1 && previousCount > 0) {
// Shift every player above the gap down by one to keep numbering in order.
await (update(playerTable)..where(
(p) =>
p.name.equals(previousName) &
p.nameCount.isBiggerThanValue(previousCount),
))
.write(
PlayerTableCompanion.custom(
nameCount: playerTable.nameCount - const Constant(1),
),
); );
}
if (previousNameCount > 1) {
// Multiple players
final nameCount = await getNameCount(name: previousPlayerName);
await updateNameCount(playerId: player!.id, nameCount: nameCount);
} else {
// Only one player
await updateNameCount(playerId: player!.id, nameCount: 0);
}
}
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