From af7e1f2dea0d054abf3879ed1225fe5303cecdba Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Tue, 22 Apr 2025 23:34:25 +0200 Subject: [PATCH] Corrected routing and reloading of widgets --- lib/data/game_session.dart | 13 ++--- lib/views/active_game_view.dart | 12 ++-- lib/views/round_view.dart | 100 ++++++++++++++++---------------- 3 files changed, 62 insertions(+), 63 deletions(-) diff --git a/lib/data/game_session.dart b/lib/data/game_session.dart index 5895cd1..18e9ca9 100644 --- a/lib/data/game_session.dart +++ b/lib/data/game_session.dart @@ -14,13 +14,7 @@ class GameSession { required this.players, required this.gameMode, }); - List> playerScores = [ - [7, 1, 4, 2], - [7, 0, 3, 4], - [5, 5, 0, 0], - [10, 3, 3, 2], - [30, 10, 15, 5] - ]; + List> playerScores = List.generate(5, (_) => [0]); @override String toString() { @@ -30,12 +24,15 @@ class GameSession { 'playerScores: $playerScores]'); } + void increaseRound() { + round++; + } + int getLengthOfPlayerNames() { int length = 0; for (String player in players) { length += player.length; } - print('Namenslänge: $length'); return length; } diff --git a/lib/views/active_game_view.dart b/lib/views/active_game_view.dart index 1ca8eb2..6b6d8b1 100644 --- a/lib/views/active_game_view.dart +++ b/lib/views/active_game_view.dart @@ -13,11 +13,9 @@ class ActiveGameView extends StatefulWidget { } class _ActiveGameViewState extends State { - List sortedPlayerIndices = []; - @override Widget build(BuildContext context) { - sortedPlayerIndices = _getSortedPlayerIndices(); + List sortedPlayerIndices = _getSortedPlayerIndices(); return CupertinoPageScaffold( navigationBar: CupertinoNavigationBar( middle: Text(widget.gameSession.gameTitle), @@ -68,7 +66,7 @@ class _ActiveGameViewState extends State { ), ListView.builder( shrinkWrap: true, - itemCount: widget.gameSession.playerScores[0].length, + itemCount: widget.gameSession.round, itemBuilder: (BuildContext context, int index) { return Padding( padding: EdgeInsets.all(1), @@ -80,8 +78,8 @@ class _ActiveGameViewState extends State { index + 1 == widget.gameSession.playerScores[0].length ? Text('⏳', style: TextStyle(fontSize: 22)) : Text('✅', style: TextStyle(fontSize: 22)), - onTap: () { - Navigator.push( + onTap: () async { + final val = await Navigator.push( context, CupertinoPageRoute( fullscreenDialog: true, @@ -90,6 +88,7 @@ class _ActiveGameViewState extends State { roundNumber: index + 1), ), ); + setState(() {}); }, )); }, @@ -105,6 +104,7 @@ class _ActiveGameViewState extends State { List _getSortedPlayerIndices() { List playerIndices = List.generate(widget.gameSession.players.length, (index) => index); + print('Player Indices: $playerIndices'); // Sort the indices based on the summed points playerIndices.sort((a, b) { int scoreA = widget.gameSession.playerScores[a][0]; diff --git a/lib/views/round_view.dart b/lib/views/round_view.dart index cee71f1..04703ab 100644 --- a/lib/views/round_view.dart +++ b/lib/views/round_view.dart @@ -40,10 +40,10 @@ class _RoundViewState extends State { @override void initState() { print('Runde ${widget.roundNumber} geöffnet'); - print('Neuste Runde: ${widget.gameSession.playerScores[0].length}'); - if (widget.gameSession.round < widget.gameSession.playerScores[0].length) { - print('Die Runde ${widget.gameSession.round} ist kleiner als die neuste ' - 'Runde ${widget.gameSession.playerScores[0].length}, somit werden ' + print('Neuste Runde: ${widget.gameSession.round}'); + if (widget.roundNumber < widget.gameSession.round) { + print('Die Runde ${widget.roundNumber} ist kleiner als die neuste ' + 'Runde ${widget.gameSession.round}, somit werden ' 'die bereits eingetragenen Punkte angezeigt.'); // If the current round has already been played, the text fields @@ -66,9 +66,7 @@ class _RoundViewState extends State { previousPageTitle: 'Übersicht', leading: CupertinoButton( padding: EdgeInsets.zero, - onPressed: () { - Navigator.pop(context); - }, + onPressed: () => Navigator.pop(context, widget.gameSession), child: Text('Abbrechen'), ), ), @@ -259,7 +257,10 @@ class _RoundViewState extends State { children: [ CupertinoButton( onPressed: _areRoundInputsValid() - ? () => {_finishRound(), Navigator.pop(context)} + ? () => { + _finishRound(), + Navigator.pop(context, widget.gameSession) + } : null, child: const Text('Fertig'), ), @@ -337,6 +338,7 @@ class _RoundViewState extends State { } _calculateScoredPoints(); widget.gameSession.sumPoints(); + widget.gameSession.increaseRound(); print('Die Punktesummen wurden aktualisiert'); } @@ -357,42 +359,51 @@ class _RoundViewState extends State { /// Every player with the lowest score gets 0 points. /// Every other player gets their round score. void _calculateScoredPoints() { - // List of the scores of the current round - List roundScores = []; - for (TextEditingController c in _scoreControllerList) { - roundScores.add(int.parse(c.text)); - } print('Spieler: ${gameSession.players}'); - print('Punkte: $roundScores'); - print('${gameSession.players[_caboPlayerIndex]} hat CABO gesagt'); - print('${gameSession.players[_caboPlayerIndex]} hat ' - '${roundScores[_caboPlayerIndex]} Punkte'); - /// List of the index of the player(s) with the lowest score - List lowestScoreIndex = _getLowestScoreIndex(roundScores); // A player has Kamikaze if (_kamikazePlayerIndex != null) { print('${widget.gameSession.players[_kamikazePlayerIndex!]} hat Kamikaze ' 'und bekommt 0 Punkte'); print('Alle anderen Spieler bekommen 50 Punkte'); - _applyKamikaze(_kamikazePlayerIndex!, roundScores); - } - // The player who said CABO is one of the players which have the - // fewest points. - else if (lowestScoreIndex.contains(_caboPlayerIndex)) { - print('${widget.gameSession.players[_caboPlayerIndex]} hat CABO gesagt ' - 'und bekommt 0 Punkte'); - print('Alle anderen Spieler bekommen ihre Punkte'); - _assignPoints([_caboPlayerIndex], -1, roundScores); + _applyKamikaze(_kamikazePlayerIndex!, + List.generate(widget.gameSession.players.length, (index) => 0)); } else { - // A player other than the one who said CABO has the fewest points. - print('${widget.gameSession.players[_caboPlayerIndex]} hat CABO gesagt, ' - 'jedoch nicht die wenigsten Punkte.'); - print('Folgende:r Spieler haben die wenigsten Punkte:'); - for (int i in lowestScoreIndex) { - print('${widget.gameSession.players[i]}: ${roundScores[i]} Punkte'); + // List of the scores of the current round + List roundScores = []; + for (TextEditingController c in _scoreControllerList) { + if (c.text.isNotEmpty) roundScores.add(int.parse(c.text)); + } + + print('Punkte: $roundScores'); + print('${gameSession.players[_caboPlayerIndex]} hat CABO gesagt'); + print('${gameSession.players[_caboPlayerIndex]} hat ' + '${roundScores[_caboPlayerIndex]} Punkte'); + + /// List of the index of the player(s) with the lowest score + List lowestScoreIndex = _getLowestScoreIndex(roundScores); + print('Folgende Spieler haben die niedrigsten Punte:'); + for (int i in lowestScoreIndex) { + print('${widget.gameSession.players[i]} (${roundScores[i]} Punkte)'); + } + // The player who said CABO is one of the players which have the + // fewest points. + if (lowestScoreIndex.contains(_caboPlayerIndex)) { + print('${widget.gameSession.players[_caboPlayerIndex]} hat CABO gesagt ' + 'und bekommt 0 Punkte'); + print('Alle anderen Spieler bekommen ihre Punkte'); + _assignPoints([_caboPlayerIndex], -1, roundScores); + } else { + // A player other than the one who said CABO has the fewest points. + print( + '${widget.gameSession.players[_caboPlayerIndex]} hat CABO gesagt, ' + 'jedoch nicht die wenigsten Punkte.'); + print('Folgende:r Spieler haben die wenigsten Punkte:'); + for (int i in lowestScoreIndex) { + print('${widget.gameSession.players[i]}: ${roundScores[i]} Punkte'); + } + _assignPoints(lowestScoreIndex, _caboPlayerIndex, roundScores); } - _assignPoints(lowestScoreIndex, _caboPlayerIndex, roundScores); } } @@ -400,27 +411,17 @@ class _RoundViewState extends State { /// multiple players with the same lowest score, all of them are returned. /// [roundScores] is a list of the scores of all players in the current round. List _getLowestScoreIndex(List roundScores) { - print('_getLowestScoreIndex() aufgerufen'); int lowestScore = roundScores[0]; List lowestScoreIndex = [0]; - print('Niedrigster Score: ${gameSession.players[lowestScoreIndex[0]]} ' - '($lowestScore Punkte)'); + for (int i = 1; i < roundScores.length; i++) { if (roundScores[i] < lowestScore) { - print('Neuer niedrigster Score: ${gameSession.players[i]} ' - '(${roundScores[i]} Punkte)'); lowestScore = roundScores[i]; lowestScoreIndex = [i]; } else if (roundScores[i] == lowestScore) { - print('${gameSession.players[i]} hat ebenfalls am wenigsten Punkte ' - '(${roundScores[i]} Punkte)'); lowestScoreIndex.add(i); } } - print('Folgende Spieler haben die niedrigsten Punte:'); - for (int i in lowestScoreIndex) { - print('${widget.gameSession.players[i]} (${roundScores[i]} Punkte)'); - } return lowestScoreIndex; } @@ -444,19 +445,20 @@ class _RoundViewState extends State { /// current round. void _assignPoints( List winnnerIndex, int loserIndex, List roundScores) { - print('Punkte der Spieler'); + print('Folgende Punkte wurden aus der Runde übernommen:'); for (int i = 0; i < roundScores.length; i++) { print('${widget.gameSession.players[i]}: ${roundScores[i]}'); } for (int i in winnnerIndex) { - print('${widget.gameSession.players[i]} bekommt 0 Punkte'); + print( + '${widget.gameSession.players[i]} hat gewonnen und bekommt 0 Punkte'); roundScores[i] = 0; } if (loserIndex != -1) { print('${widget.gameSession.players[loserIndex]} bekommt 5 Fehlerpunkte'); roundScores[loserIndex] += 5; } - print('Aktualisierte Punkte'); + print('Aktualisierte Punkte:'); for (int i = 0; i < roundScores.length; i++) { print('${widget.gameSession.players[i]}: ${roundScores[i]}'); }