Updated score and winner handling

This commit is contained in:
2026-04-21 18:38:00 +02:00
parent 522441b0ca
commit 9364f0d9d6
19 changed files with 286 additions and 179 deletions

View File

@@ -36,6 +36,7 @@ class GroupDetailView extends StatefulWidget {
}
class _GroupDetailViewState extends State<GroupDetailView> {
late final AppLocalizations loc;
late final AppDatabase db;
bool isLoading = true;
late Group _group;
@@ -51,13 +52,12 @@ class _GroupDetailViewState extends State<GroupDetailView> {
super.initState();
_group = widget.group;
db = Provider.of<AppDatabase>(context, listen: false);
loc = AppLocalizations.of(context);
_loadStatistics();
}
@override
Widget build(BuildContext context) {
final loc = AppLocalizations.of(context);
return Scaffold(
backgroundColor: CustomTheme.backgroundColor,
appBar: AppBar(
@@ -259,28 +259,36 @@ class _GroupDetailViewState extends State<GroupDetailView> {
/// Determines the best player in the group based on match wins
String _getBestPlayer(List<Match> matches) {
final bestPlayerCounts = <Player, int>{};
final mvpCounts = <Player, int>{};
// Count wins for each player
for (var match in matches) {
if (match.winner != null &&
_group.members.any((m) => m.id == match.winner?.id)) {
print(match.winner);
bestPlayerCounts.update(
match.winner!,
(value) => value + 1,
ifAbsent: () => 1,
);
final mvps = match.mvp;
for (final mvpPlayer in mvps) {
if (_group.members.any((m) => m.id == mvpPlayer.id)) {
mvpCounts.update(mvpPlayer, (value) => value + 1, ifAbsent: () => 1);
}
}
}
// Sort players by win count
final sortedPlayers = bestPlayerCounts.entries.toList()
final sortedMvps = mvpCounts.entries.toList()
..sort((a, b) => b.value.compareTo(a.value));
// Get the best player
bestPlayer = sortedPlayers.isNotEmpty ? sortedPlayers.first.key.name : '-';
if (sortedMvps.isEmpty) {
return '-';
}
return bestPlayer;
// Check if there are multiple players with the same value
final highestMvpCount = sortedMvps.first.value;
final topPlayers = sortedMvps
.where((entry) => entry.value == highestMvpCount)
.toList();
switch (topPlayers.length) {
case 0:
return '-';
case 1:
return topPlayers.first.key.name;
default:
return loc.tie;
}
}
}