Updated gameSession logic so more than one player can be winner

This commit is contained in:
2025-07-20 23:29:25 +02:00
parent b38a3be288
commit 36aad89952
2 changed files with 14 additions and 10 deletions

View File

@@ -299,15 +299,19 @@ class GameSession extends ChangeNotifier {
/// It iterates through the player scores and finds the player /// It iterates through the player scores and finds the player
/// with the lowest score. /// with the lowest score.
void _setWinner() { void _setWinner() {
int score = playerScores[0]; int minScore = playerScores.reduce((a, b) => a < b ? a : b);
String lowestPlayer = players[0]; List<String> lowestPlayers = [];
for (int i = 0; i < players.length; i++) { for (int i = 0; i < players.length; i++) {
if (playerScores[i] < score) { if (playerScores[i] == minScore) {
score = playerScores[i]; lowestPlayers.add(players[i]);
lowestPlayer = players[i];
} }
} }
winner = lowestPlayer; if (lowestPlayers.length > 1) {
winner =
'${lowestPlayers.sublist(0, lowestPlayers.length - 1).join(', ')} & ${lowestPlayers.last}';
} else {
winner = lowestPlayers.first;
}
notifyListeners(); notifyListeners();
} }

View File

@@ -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/points_view.dart';
import 'package:cabo_counter/presentation/views/round_view.dart'; import 'package:cabo_counter/presentation/views/round_view.dart';
import 'package:cabo_counter/services/local_storage_service.dart'; import 'package:cabo_counter/services/local_storage_service.dart';
import 'package:collection/collection.dart';
import 'package:confetti/confetti.dart'; import 'package:confetti/confetti.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@@ -467,8 +468,7 @@ class _ActiveGameViewState extends State<ActiveGameView> {
/// Plays the confetti animation and shows a dialog with the winner's information. /// Plays the confetti animation and shows a dialog with the winner's information.
Future<void> _playFinishAnimation(BuildContext context) async { Future<void> _playFinishAnimation(BuildContext context) async {
String winner = widget.gameSession.winner; String winner = widget.gameSession.winner;
int winnerIndex = widget.gameSession.players.indexOf(winner); int winnerPoints = widget.gameSession.playerScores.min;
int points = widget.gameSession.playerScores[winnerIndex];
confettiController.play(); confettiController.play();
@@ -480,8 +480,8 @@ class _ActiveGameViewState extends State<ActiveGameView> {
builder: (BuildContext context) { builder: (BuildContext context) {
return CupertinoAlertDialog( return CupertinoAlertDialog(
title: Text(AppLocalizations.of(context).end_of_game_title), title: Text(AppLocalizations.of(context).end_of_game_title),
content: Text(AppLocalizations.of(context) content: Text(AppLocalizations.of(context).end_of_game_message(
.end_of_game_message(1, winner, points)), winner.contains('&') ? 2 : 1, winner, winnerPoints)),
actions: [ actions: [
CupertinoDialogAction( CupertinoDialogAction(
child: Text(AppLocalizations.of(context).ok), child: Text(AppLocalizations.of(context).ok),