From 9adcc29cda75b65b55255ec82f0993f522ac6672 Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Thu, 21 May 2026 23:57:59 +0200 Subject: [PATCH] fix: updatePlayerName corrects the name count after renaming to different name --- lib/data/dao/player_dao.dart | 20 +++++++++----- test/db_tests/entities/player_test.dart | 36 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/lib/data/dao/player_dao.dart b/lib/data/dao/player_dao.dart index 2f8ef6e..8b60aa4 100644 --- a/lib/data/dao/player_dao.dart +++ b/lib/data/dao/player_dao.dart @@ -169,7 +169,6 @@ class PlayerDao extends DatabaseAccessor with _$PlayerDaoMixin { .map((row) => row.name) .getSingleOrNull() ?? ''; - final previousNameCount = await getNameCount(name: previousPlayerName); // Update name count for the new name final newNameCount = await _processNameCount(name: name); @@ -183,16 +182,23 @@ class PlayerDao extends DatabaseAccessor with _$PlayerDaoMixin { ), ); + // Updating name count for the previous name + final previousNameCount = await getNameCount(name: previousPlayerName); if (previousNameCount > 0) { - // Get the player with that name and the hightest nameCount, and update their nameCount to previousNameCount + // At least one more player with the previous name + + // Get the player with the highest count final player = await getPlayerWithHighestNameCount( name: previousPlayerName, ); - if (player != null) { - await updateNameCount( - playerId: player.id, - nameCount: previousNameCount, - ); + + if (previousNameCount > 1) { + // Multiple players + final nameCount = await getNameCount(name: previousPlayerName); + await updateNameCount(playerId: player!.id, nameCount: nameCount - 1); + } else { + // Only one player + await updateNameCount(playerId: player!.id, nameCount: 0); } } return rowsAffected > 0; diff --git a/test/db_tests/entities/player_test.dart b/test/db_tests/entities/player_test.dart index d25f320..1061eba 100644 --- a/test/db_tests/entities/player_test.dart +++ b/test/db_tests/entities/player_test.dart @@ -233,6 +233,42 @@ void main() { expect(allPlayers, isEmpty); }); + test('updatePlayerName() updates the nameCount correctly', () async { + await database.playerDao.addPlayer(player: testPlayer1); + await database.playerDao.addPlayer(player: testPlayer2); + + final newName = testPlayer1.name; + await database.playerDao.updatePlayerName( + playerId: testPlayer2.id, + name: newName, + ); + + var player = await database.playerDao.getPlayerById( + playerId: testPlayer1.id, + ); + expect(player.nameCount, 1); + + player = await database.playerDao.getPlayerById( + playerId: testPlayer2.id, + ); + expect(player.nameCount, 2); + + await database.playerDao.updatePlayerName( + playerId: testPlayer1.id, + name: 'different name', + ); + + player = await database.playerDao.getPlayerById( + playerId: testPlayer1.id, + ); + expect(player.nameCount, 0); + + player = await database.playerDao.getPlayerById( + playerId: testPlayer2.id, + ); + expect(player.nameCount, 0); + }); + test('updatePlayerDescription() works correctly', () async { await database.playerDao.addPlayer(player: testPlayer1);