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