From 16d1c017afbf2df8b3cc0a2f0e64fc8ee4bacc81 Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Mon, 14 Jul 2025 11:06:02 +0200 Subject: [PATCH 1/9] Updated _getPlacementPrefix method for dense ranks --- lib/presentation/views/active_game_view.dart | 65 ++++++++++++-------- pubspec.yaml | 2 +- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/lib/presentation/views/active_game_view.dart b/lib/presentation/views/active_game_view.dart index 704952a..c280e59 100644 --- a/lib/presentation/views/active_game_view.dart +++ b/lib/presentation/views/active_game_view.dart @@ -58,7 +58,8 @@ class _ActiveGameViewState extends State { return CupertinoListTile( title: Row( children: [ - _getPlacementPrefix(index), + _getPlacementPrefix( + index, gameSession.playerScores), const SizedBox(width: 5), Text( gameSession.players[playerIndex], @@ -266,36 +267,52 @@ class _ActiveGameViewState extends State { playerIndices.sort((a, b) { int scoreA = gameSession.playerScores[a]; int scoreB = gameSession.playerScores[b]; - return scoreA.compareTo(scoreB); + if (scoreA != scoreB) { + return scoreA.compareTo(scoreB); + } + return a.compareTo(b); }); return playerIndices; } - /// Returns a widget that displays the placement prefix based on the index. - /// First three places are represented by medals, and the rest are numbered. - /// [index] is the index of the player in the descending sorted list. - Widget _getPlacementPrefix(int index) { - switch (index) { - case 0: - return const Text( - '\u{1F947}', - style: TextStyle(fontSize: 22), - ); + /// Returns a widget representing the placement prefix for a player based on their index. + /// [index] is the index of the player in [players] list, + /// [playerScores] is a list of the players scores. + Widget _getPlacementPrefix(int index, playerScores) { + int placement = _calculateDenseRank(index, playerScores); + return _getPlacementTextWidget(placement); + } + + /// Calculates the dense rank for a player based on their index in the sorted list of players. + int _calculateDenseRank(int index, List playerScores) { + List sortedIndices = _getSortedPlayerIndices(); + List denseRanks = []; + int rank = 1; + for (int i = 0; i < sortedIndices.length; i++) { + if (i > 0) { + int prevScore = playerScores[sortedIndices[i - 1]]; + int currScore = playerScores[sortedIndices[i]]; + if (currScore != prevScore) { + rank++; + } + } + denseRanks.add(rank); + } + return denseRanks[index]; + } + + /// Returns a text widget representing the placement text based on the given placement number. + Text _getPlacementTextWidget(int placement) { + switch (placement) { case 1: - return const Text( - '\u{1F948}', - style: TextStyle(fontSize: 22), - ); + return const Text('\u{1F947}', style: TextStyle(fontSize: 22)); // 🥇 case 2: - return const Text( - '\u{1F949}', - style: TextStyle(fontSize: 22), - ); + return const Text('\u{1F948}', style: TextStyle(fontSize: 22)); // 🥈 + case 3: + return const Text('\u{1F949}', style: TextStyle(fontSize: 22)); // 🥉 default: - return Text( - ' ${index + 1}.', - style: const TextStyle(fontWeight: FontWeight.bold), - ); + return Text('$placement.', + style: const TextStyle(fontWeight: FontWeight.bold)); } } diff --git a/pubspec.yaml b/pubspec.yaml index 9db5d4b..d50b43a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,7 +2,7 @@ name: cabo_counter description: "Mobile app for the card game Cabo" publish_to: 'none' -version: 0.4.5+494 +version: 0.4.6+497 environment: sdk: ^3.5.4 From 48784fd29048c4b8bd246c95aa1b56600e1673fe Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Mon, 14 Jul 2025 11:07:14 +0200 Subject: [PATCH 2/9] Updated comments --- lib/presentation/views/active_game_view.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/presentation/views/active_game_view.dart b/lib/presentation/views/active_game_view.dart index c280e59..427fdc8 100644 --- a/lib/presentation/views/active_game_view.dart +++ b/lib/presentation/views/active_game_view.dart @@ -316,6 +316,7 @@ class _ActiveGameViewState extends State { } } + /// Shows a dialog to confirm deleting the game session. Future _showDeleteGameDialog() async { return await showCupertinoDialog( context: context, @@ -348,6 +349,8 @@ class _ActiveGameViewState extends State { false; } + /// Removes the game session in the game manager and navigates back to the previous screen. + /// If the game session does not exist in the game list, it shows an error dialog. Future _removeGameSession(GameSession gameSession) async { if (gameManager.gameExistsInGameList(gameSession.id)) { Navigator.pop(context); From f8bbbb04f3af4f08e571612bd1e8ca2061464e3a Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Mon, 14 Jul 2025 11:12:32 +0200 Subject: [PATCH 3/9] Added type annotation --- lib/presentation/views/active_game_view.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/presentation/views/active_game_view.dart b/lib/presentation/views/active_game_view.dart index 427fdc8..8698c62 100644 --- a/lib/presentation/views/active_game_view.dart +++ b/lib/presentation/views/active_game_view.dart @@ -278,7 +278,7 @@ class _ActiveGameViewState extends State { /// Returns a widget representing the placement prefix for a player based on their index. /// [index] is the index of the player in [players] list, /// [playerScores] is a list of the players scores. - Widget _getPlacementPrefix(int index, playerScores) { + Widget _getPlacementPrefix(int index, List playerScores) { int placement = _calculateDenseRank(index, playerScores); return _getPlacementTextWidget(placement); } From 4a0ce067b5f3a91056baf9cf61fe0ffe2a554b67 Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Mon, 14 Jul 2025 11:14:22 +0200 Subject: [PATCH 4/9] Updated function _getSortedPlayerIndices() --- lib/presentation/views/active_game_view.dart | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/presentation/views/active_game_view.dart b/lib/presentation/views/active_game_view.dart index 8698c62..6eaee45 100644 --- a/lib/presentation/views/active_game_view.dart +++ b/lib/presentation/views/active_game_view.dart @@ -32,7 +32,10 @@ class _ActiveGameViewState extends State { return ListenableBuilder( listenable: gameSession, builder: (context, _) { - List sortedPlayerIndices = _getSortedPlayerIndices(); + List playerIndices = + List.generate(gameSession.players.length, (index) => index); + List sortedPlayerIndices = + _getSortedPlayerIndices(playerIndices); return CupertinoPageScaffold( navigationBar: CupertinoNavigationBar( middle: Text(gameSession.gameTitle), @@ -260,9 +263,7 @@ class _ActiveGameViewState extends State { /// Returns a list of player indices sorted by their scores in /// ascending order. - List _getSortedPlayerIndices() { - List playerIndices = - List.generate(gameSession.players.length, (index) => index); + List _getSortedPlayerIndices(List playerIndices) { // Sort the indices based on the summed points playerIndices.sort((a, b) { int scoreA = gameSession.playerScores[a]; @@ -285,7 +286,7 @@ class _ActiveGameViewState extends State { /// Calculates the dense rank for a player based on their index in the sorted list of players. int _calculateDenseRank(int index, List playerScores) { - List sortedIndices = _getSortedPlayerIndices(); + List sortedIndices = _getSortedPlayerIndices(playerScores); List denseRanks = []; int rank = 1; for (int i = 0; i < sortedIndices.length; i++) { From 84e6f3f28653e95bbd7486f27c89d0f867b4454e Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Mon, 14 Jul 2025 11:14:44 +0200 Subject: [PATCH 5/9] Updated build method --- lib/presentation/views/active_game_view.dart | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/presentation/views/active_game_view.dart b/lib/presentation/views/active_game_view.dart index 6eaee45..2153d1d 100644 --- a/lib/presentation/views/active_game_view.dart +++ b/lib/presentation/views/active_game_view.dart @@ -29,13 +29,13 @@ class _ActiveGameViewState extends State { @override Widget build(BuildContext context) { + List playerIndices = + List.generate(gameSession.players.length, (index) => index); + List sortedPlayerIndices = _getSortedPlayerIndices(playerIndices); + return ListenableBuilder( listenable: gameSession, builder: (context, _) { - List playerIndices = - List.generate(gameSession.players.length, (index) => index); - List sortedPlayerIndices = - _getSortedPlayerIndices(playerIndices); return CupertinoPageScaffold( navigationBar: CupertinoNavigationBar( middle: Text(gameSession.gameTitle), From 8b4a8bb86be4523807cff3ce428fbf30258e0926 Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Mon, 14 Jul 2025 11:30:55 +0200 Subject: [PATCH 6/9] Refactored and simplified --- lib/presentation/views/active_game_view.dart | 33 +++++++++----------- pubspec.yaml | 2 +- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/lib/presentation/views/active_game_view.dart b/lib/presentation/views/active_game_view.dart index 2153d1d..a4c52ee 100644 --- a/lib/presentation/views/active_game_view.dart +++ b/lib/presentation/views/active_game_view.dart @@ -20,6 +20,8 @@ class ActiveGameView extends StatefulWidget { class _ActiveGameViewState extends State { late final GameSession gameSession; + late final List denseRanks; + late List sortedPlayerIndices; @override void initState() { @@ -29,13 +31,11 @@ class _ActiveGameViewState extends State { @override Widget build(BuildContext context) { - List playerIndices = - List.generate(gameSession.players.length, (index) => index); - List sortedPlayerIndices = _getSortedPlayerIndices(playerIndices); - return ListenableBuilder( listenable: gameSession, builder: (context, _) { + sortedPlayerIndices = _getSortedPlayerIndices(); + denseRanks = _calculateDenseRank(gameSession.playerScores); return CupertinoPageScaffold( navigationBar: CupertinoNavigationBar( middle: Text(gameSession.gameTitle), @@ -61,8 +61,7 @@ class _ActiveGameViewState extends State { return CupertinoListTile( title: Row( children: [ - _getPlacementPrefix( - index, gameSession.playerScores), + _getPlacementTextWidget(index), const SizedBox(width: 5), Text( gameSession.players[playerIndex], @@ -263,7 +262,9 @@ class _ActiveGameViewState extends State { /// Returns a list of player indices sorted by their scores in /// ascending order. - List _getSortedPlayerIndices(List playerIndices) { + List _getSortedPlayerIndices() { + List playerIndices = + List.generate(gameSession.players.length, (index) => index); // Sort the indices based on the summed points playerIndices.sort((a, b) { int scoreA = gameSession.playerScores[a]; @@ -276,17 +277,9 @@ class _ActiveGameViewState extends State { return playerIndices; } - /// Returns a widget representing the placement prefix for a player based on their index. - /// [index] is the index of the player in [players] list, - /// [playerScores] is a list of the players scores. - Widget _getPlacementPrefix(int index, List playerScores) { - int placement = _calculateDenseRank(index, playerScores); - return _getPlacementTextWidget(placement); - } - /// Calculates the dense rank for a player based on their index in the sorted list of players. - int _calculateDenseRank(int index, List playerScores) { - List sortedIndices = _getSortedPlayerIndices(playerScores); + List _calculateDenseRank(List playerScores) { + List sortedIndices = _getSortedPlayerIndices(); List denseRanks = []; int rank = 1; for (int i = 0; i < sortedIndices.length; i++) { @@ -299,11 +292,13 @@ class _ActiveGameViewState extends State { } denseRanks.add(rank); } - return denseRanks[index]; + return denseRanks; } /// Returns a text widget representing the placement text based on the given placement number. - Text _getPlacementTextWidget(int placement) { + /// [index] is the index of the player in [players] list, + Text _getPlacementTextWidget(int index) { + int placement = denseRanks[index]; switch (placement) { case 1: return const Text('\u{1F947}', style: TextStyle(fontSize: 22)); // 🥇 diff --git a/pubspec.yaml b/pubspec.yaml index d50b43a..95fea1d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,7 +2,7 @@ name: cabo_counter description: "Mobile app for the card game Cabo" publish_to: 'none' -version: 0.4.6+497 +version: 0.4.6+504 environment: sdk: ^3.5.4 From f4120d28a9948c20214d167fc367ef763bd5d5b1 Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Mon, 14 Jul 2025 11:38:28 +0200 Subject: [PATCH 7/9] Removed late --- lib/presentation/views/active_game_view.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/presentation/views/active_game_view.dart b/lib/presentation/views/active_game_view.dart index a4c52ee..1ea0a0d 100644 --- a/lib/presentation/views/active_game_view.dart +++ b/lib/presentation/views/active_game_view.dart @@ -20,7 +20,7 @@ class ActiveGameView extends StatefulWidget { class _ActiveGameViewState extends State { late final GameSession gameSession; - late final List denseRanks; + late List denseRanks; late List sortedPlayerIndices; @override From 5a939f4447e7bfb3d8ef651a5c04b372a7c8bf4e Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Mon, 14 Jul 2025 11:39:32 +0200 Subject: [PATCH 8/9] Unnessecary code --- lib/presentation/views/active_game_view.dart | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/presentation/views/active_game_view.dart b/lib/presentation/views/active_game_view.dart index 1ea0a0d..9715f77 100644 --- a/lib/presentation/views/active_game_view.dart +++ b/lib/presentation/views/active_game_view.dart @@ -35,7 +35,8 @@ class _ActiveGameViewState extends State { listenable: gameSession, builder: (context, _) { sortedPlayerIndices = _getSortedPlayerIndices(); - denseRanks = _calculateDenseRank(gameSession.playerScores); + denseRanks = _calculateDenseRank( + gameSession.playerScores, sortedPlayerIndices); return CupertinoPageScaffold( navigationBar: CupertinoNavigationBar( middle: Text(gameSession.gameTitle), @@ -278,8 +279,8 @@ class _ActiveGameViewState extends State { } /// Calculates the dense rank for a player based on their index in the sorted list of players. - List _calculateDenseRank(List playerScores) { - List sortedIndices = _getSortedPlayerIndices(); + List _calculateDenseRank( + List playerScores, List sortedIndices) { List denseRanks = []; int rank = 1; for (int i = 0; i < sortedIndices.length; i++) { From f7676da88d15f0d49d559645c488aff7e3deab0c Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Mon, 14 Jul 2025 11:40:03 +0200 Subject: [PATCH 9/9] Added white space --- lib/presentation/views/active_game_view.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/presentation/views/active_game_view.dart b/lib/presentation/views/active_game_view.dart index 9715f77..ab07804 100644 --- a/lib/presentation/views/active_game_view.dart +++ b/lib/presentation/views/active_game_view.dart @@ -308,7 +308,7 @@ class _ActiveGameViewState extends State { case 3: return const Text('\u{1F949}', style: TextStyle(fontSize: 22)); // 🥉 default: - return Text('$placement.', + return Text(' $placement.', style: const TextStyle(fontWeight: FontWeight.bold)); } }