Updated _getPlacementPrefix method for dense ranks

This commit is contained in:
2025-07-14 11:06:02 +02:00
parent 1c862f896e
commit 16d1c017af
2 changed files with 42 additions and 25 deletions

View File

@@ -58,7 +58,8 @@ class _ActiveGameViewState extends State<ActiveGameView> {
return CupertinoListTile( return CupertinoListTile(
title: Row( title: Row(
children: [ children: [
_getPlacementPrefix(index), _getPlacementPrefix(
index, gameSession.playerScores),
const SizedBox(width: 5), const SizedBox(width: 5),
Text( Text(
gameSession.players[playerIndex], gameSession.players[playerIndex],
@@ -266,36 +267,52 @@ class _ActiveGameViewState extends State<ActiveGameView> {
playerIndices.sort((a, b) { playerIndices.sort((a, b) {
int scoreA = gameSession.playerScores[a]; int scoreA = gameSession.playerScores[a];
int scoreB = gameSession.playerScores[b]; int scoreB = gameSession.playerScores[b];
return scoreA.compareTo(scoreB); if (scoreA != scoreB) {
return scoreA.compareTo(scoreB);
}
return a.compareTo(b);
}); });
return playerIndices; return playerIndices;
} }
/// Returns a widget that displays the placement prefix based on the index. /// Returns a widget representing the placement prefix for a player based on their index.
/// First three places are represented by medals, and the rest are numbered. /// [index] is the index of the player in [players] list,
/// [index] is the index of the player in the descending sorted list. /// [playerScores] is a list of the players scores.
Widget _getPlacementPrefix(int index) { Widget _getPlacementPrefix(int index, playerScores) {
switch (index) { int placement = _calculateDenseRank(index, playerScores);
case 0: return _getPlacementTextWidget(placement);
return const Text( }
'\u{1F947}',
style: TextStyle(fontSize: 22), /// Calculates the dense rank for a player based on their index in the sorted list of players.
); int _calculateDenseRank(int index, List<int> playerScores) {
List<int> sortedIndices = _getSortedPlayerIndices();
List<int> 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: case 1:
return const Text( return const Text('\u{1F947}', style: TextStyle(fontSize: 22)); // 🥇
'\u{1F948}',
style: TextStyle(fontSize: 22),
);
case 2: case 2:
return const Text( return const Text('\u{1F948}', style: TextStyle(fontSize: 22)); // 🥈
'\u{1F949}', case 3:
style: TextStyle(fontSize: 22), return const Text('\u{1F949}', style: TextStyle(fontSize: 22)); // 🥉
);
default: default:
return Text( return Text('$placement.',
' ${index + 1}.', style: const TextStyle(fontWeight: FontWeight.bold));
style: const TextStyle(fontWeight: FontWeight.bold),
);
} }
} }

View File

@@ -2,7 +2,7 @@ name: cabo_counter
description: "Mobile app for the card game Cabo" description: "Mobile app for the card game Cabo"
publish_to: 'none' publish_to: 'none'
version: 0.4.5+494 version: 0.4.6+497
environment: environment:
sdk: ^3.5.4 sdk: ^3.5.4