From 36aad89952e4a3fb737cda915d6bb717c77cb03f Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Sun, 20 Jul 2025 23:29:25 +0200 Subject: [PATCH] Updated gameSession logic so more than one player can be winner --- lib/data/game_session.dart | 16 ++++++++++------ lib/presentation/views/active_game_view.dart | 8 ++++---- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/data/game_session.dart b/lib/data/game_session.dart index a9164be..3dcf4c2 100644 --- a/lib/data/game_session.dart +++ b/lib/data/game_session.dart @@ -299,15 +299,19 @@ class GameSession extends ChangeNotifier { /// It iterates through the player scores and finds the player /// with the lowest score. void _setWinner() { - int score = playerScores[0]; - String lowestPlayer = players[0]; + int minScore = playerScores.reduce((a, b) => a < b ? a : b); + List lowestPlayers = []; for (int i = 0; i < players.length; i++) { - if (playerScores[i] < score) { - score = playerScores[i]; - lowestPlayer = players[i]; + if (playerScores[i] == minScore) { + lowestPlayers.add(players[i]); } } - winner = lowestPlayer; + if (lowestPlayers.length > 1) { + winner = + '${lowestPlayers.sublist(0, lowestPlayers.length - 1).join(', ')} & ${lowestPlayers.last}'; + } else { + winner = lowestPlayers.first; + } notifyListeners(); } diff --git a/lib/presentation/views/active_game_view.dart b/lib/presentation/views/active_game_view.dart index 58d0885..e1d1638 100644 --- a/lib/presentation/views/active_game_view.dart +++ b/lib/presentation/views/active_game_view.dart @@ -9,6 +9,7 @@ import 'package:cabo_counter/presentation/views/mode_selection_view.dart'; import 'package:cabo_counter/presentation/views/points_view.dart'; import 'package:cabo_counter/presentation/views/round_view.dart'; import 'package:cabo_counter/services/local_storage_service.dart'; +import 'package:collection/collection.dart'; import 'package:confetti/confetti.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; @@ -467,8 +468,7 @@ class _ActiveGameViewState extends State { /// Plays the confetti animation and shows a dialog with the winner's information. Future _playFinishAnimation(BuildContext context) async { String winner = widget.gameSession.winner; - int winnerIndex = widget.gameSession.players.indexOf(winner); - int points = widget.gameSession.playerScores[winnerIndex]; + int winnerPoints = widget.gameSession.playerScores.min; confettiController.play(); @@ -480,8 +480,8 @@ class _ActiveGameViewState extends State { builder: (BuildContext context) { return CupertinoAlertDialog( title: Text(AppLocalizations.of(context).end_of_game_title), - content: Text(AppLocalizations.of(context) - .end_of_game_message(1, winner, points)), + content: Text(AppLocalizations.of(context).end_of_game_message( + winner.contains('&') ? 2 : 1, winner, winnerPoints)), actions: [ CupertinoDialogAction( child: Text(AppLocalizations.of(context).ok),