Corrected routing and reloading of widgets

This commit is contained in:
Felix Kirchner
2025-04-22 23:34:25 +02:00
parent 6f57a952ed
commit af7e1f2dea
3 changed files with 62 additions and 63 deletions

View File

@@ -14,13 +14,7 @@ class GameSession {
required this.players, required this.players,
required this.gameMode, required this.gameMode,
}); });
List<List<int>> playerScores = [ List<List<int>> playerScores = List.generate(5, (_) => [0]);
[7, 1, 4, 2],
[7, 0, 3, 4],
[5, 5, 0, 0],
[10, 3, 3, 2],
[30, 10, 15, 5]
];
@override @override
String toString() { String toString() {
@@ -30,12 +24,15 @@ class GameSession {
'playerScores: $playerScores]'); 'playerScores: $playerScores]');
} }
void increaseRound() {
round++;
}
int getLengthOfPlayerNames() { int getLengthOfPlayerNames() {
int length = 0; int length = 0;
for (String player in players) { for (String player in players) {
length += player.length; length += player.length;
} }
print('Namenslänge: $length');
return length; return length;
} }

View File

@@ -13,11 +13,9 @@ class ActiveGameView extends StatefulWidget {
} }
class _ActiveGameViewState extends State<ActiveGameView> { class _ActiveGameViewState extends State<ActiveGameView> {
List<int> sortedPlayerIndices = [];
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
sortedPlayerIndices = _getSortedPlayerIndices(); List<int> sortedPlayerIndices = _getSortedPlayerIndices();
return CupertinoPageScaffold( return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar( navigationBar: CupertinoNavigationBar(
middle: Text(widget.gameSession.gameTitle), middle: Text(widget.gameSession.gameTitle),
@@ -68,7 +66,7 @@ class _ActiveGameViewState extends State<ActiveGameView> {
), ),
ListView.builder( ListView.builder(
shrinkWrap: true, shrinkWrap: true,
itemCount: widget.gameSession.playerScores[0].length, itemCount: widget.gameSession.round,
itemBuilder: (BuildContext context, int index) { itemBuilder: (BuildContext context, int index) {
return Padding( return Padding(
padding: EdgeInsets.all(1), padding: EdgeInsets.all(1),
@@ -80,8 +78,8 @@ class _ActiveGameViewState extends State<ActiveGameView> {
index + 1 == widget.gameSession.playerScores[0].length index + 1 == widget.gameSession.playerScores[0].length
? Text('', style: TextStyle(fontSize: 22)) ? Text('', style: TextStyle(fontSize: 22))
: Text('', style: TextStyle(fontSize: 22)), : Text('', style: TextStyle(fontSize: 22)),
onTap: () { onTap: () async {
Navigator.push( final val = await Navigator.push(
context, context,
CupertinoPageRoute( CupertinoPageRoute(
fullscreenDialog: true, fullscreenDialog: true,
@@ -90,6 +88,7 @@ class _ActiveGameViewState extends State<ActiveGameView> {
roundNumber: index + 1), roundNumber: index + 1),
), ),
); );
setState(() {});
}, },
)); ));
}, },
@@ -105,6 +104,7 @@ class _ActiveGameViewState extends State<ActiveGameView> {
List<int> _getSortedPlayerIndices() { List<int> _getSortedPlayerIndices() {
List<int> playerIndices = List<int> playerIndices =
List<int>.generate(widget.gameSession.players.length, (index) => index); List<int>.generate(widget.gameSession.players.length, (index) => index);
print('Player Indices: $playerIndices');
// Sort the indices based on the summed points // Sort the indices based on the summed points
playerIndices.sort((a, b) { playerIndices.sort((a, b) {
int scoreA = widget.gameSession.playerScores[a][0]; int scoreA = widget.gameSession.playerScores[a][0];

View File

@@ -40,10 +40,10 @@ class _RoundViewState extends State<RoundView> {
@override @override
void initState() { void initState() {
print('Runde ${widget.roundNumber} geöffnet'); print('Runde ${widget.roundNumber} geöffnet');
print('Neuste Runde: ${widget.gameSession.playerScores[0].length}'); print('Neuste Runde: ${widget.gameSession.round}');
if (widget.gameSession.round < widget.gameSession.playerScores[0].length) { if (widget.roundNumber < widget.gameSession.round) {
print('Die Runde ${widget.gameSession.round} ist kleiner als die neuste ' print('Die Runde ${widget.roundNumber} ist kleiner als die neuste '
'Runde ${widget.gameSession.playerScores[0].length}, somit werden ' 'Runde ${widget.gameSession.round}, somit werden '
'die bereits eingetragenen Punkte angezeigt.'); 'die bereits eingetragenen Punkte angezeigt.');
// If the current round has already been played, the text fields // If the current round has already been played, the text fields
@@ -66,9 +66,7 @@ class _RoundViewState extends State<RoundView> {
previousPageTitle: 'Übersicht', previousPageTitle: 'Übersicht',
leading: CupertinoButton( leading: CupertinoButton(
padding: EdgeInsets.zero, padding: EdgeInsets.zero,
onPressed: () { onPressed: () => Navigator.pop(context, widget.gameSession),
Navigator.pop(context);
},
child: Text('Abbrechen'), child: Text('Abbrechen'),
), ),
), ),
@@ -259,7 +257,10 @@ class _RoundViewState extends State<RoundView> {
children: [ children: [
CupertinoButton( CupertinoButton(
onPressed: _areRoundInputsValid() onPressed: _areRoundInputsValid()
? () => {_finishRound(), Navigator.pop(context)} ? () => {
_finishRound(),
Navigator.pop(context, widget.gameSession)
}
: null, : null,
child: const Text('Fertig'), child: const Text('Fertig'),
), ),
@@ -337,6 +338,7 @@ class _RoundViewState extends State<RoundView> {
} }
_calculateScoredPoints(); _calculateScoredPoints();
widget.gameSession.sumPoints(); widget.gameSession.sumPoints();
widget.gameSession.increaseRound();
print('Die Punktesummen wurden aktualisiert'); print('Die Punktesummen wurden aktualisiert');
} }
@@ -357,12 +359,22 @@ class _RoundViewState extends State<RoundView> {
/// Every player with the lowest score gets 0 points. /// Every player with the lowest score gets 0 points.
/// Every other player gets their round score. /// Every other player gets their round score.
void _calculateScoredPoints() { void _calculateScoredPoints() {
print('Spieler: ${gameSession.players}');
// 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!,
List.generate(widget.gameSession.players.length, (index) => 0));
} else {
// List of the scores of the current round // List of the scores of the current round
List<int> roundScores = []; List<int> roundScores = [];
for (TextEditingController c in _scoreControllerList) { for (TextEditingController c in _scoreControllerList) {
roundScores.add(int.parse(c.text)); if (c.text.isNotEmpty) roundScores.add(int.parse(c.text));
} }
print('Spieler: ${gameSession.players}');
print('Punkte: $roundScores'); print('Punkte: $roundScores');
print('${gameSession.players[_caboPlayerIndex]} hat CABO gesagt'); print('${gameSession.players[_caboPlayerIndex]} hat CABO gesagt');
print('${gameSession.players[_caboPlayerIndex]} hat ' print('${gameSession.players[_caboPlayerIndex]} hat '
@@ -370,23 +382,21 @@ class _RoundViewState extends State<RoundView> {
/// List of the index of the player(s) with the lowest score /// List of the index of the player(s) with the lowest score
List<int> lowestScoreIndex = _getLowestScoreIndex(roundScores); List<int> lowestScoreIndex = _getLowestScoreIndex(roundScores);
// A player has Kamikaze print('Folgende Spieler haben die niedrigsten Punte:');
if (_kamikazePlayerIndex != null) { for (int i in lowestScoreIndex) {
print('${widget.gameSession.players[_kamikazePlayerIndex!]} hat Kamikaze ' print('${widget.gameSession.players[i]} (${roundScores[i]} Punkte)');
'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 // The player who said CABO is one of the players which have the
// fewest points. // fewest points.
else if (lowestScoreIndex.contains(_caboPlayerIndex)) { if (lowestScoreIndex.contains(_caboPlayerIndex)) {
print('${widget.gameSession.players[_caboPlayerIndex]} hat CABO gesagt ' print('${widget.gameSession.players[_caboPlayerIndex]} hat CABO gesagt '
'und bekommt 0 Punkte'); 'und bekommt 0 Punkte');
print('Alle anderen Spieler bekommen ihre Punkte'); print('Alle anderen Spieler bekommen ihre Punkte');
_assignPoints([_caboPlayerIndex], -1, roundScores); _assignPoints([_caboPlayerIndex], -1, roundScores);
} else { } else {
// A player other than the one who said CABO has the fewest points. // A player other than the one who said CABO has the fewest points.
print('${widget.gameSession.players[_caboPlayerIndex]} hat CABO gesagt, ' print(
'${widget.gameSession.players[_caboPlayerIndex]} hat CABO gesagt, '
'jedoch nicht die wenigsten Punkte.'); 'jedoch nicht die wenigsten Punkte.');
print('Folgende:r Spieler haben die wenigsten Punkte:'); print('Folgende:r Spieler haben die wenigsten Punkte:');
for (int i in lowestScoreIndex) { for (int i in lowestScoreIndex) {
@@ -395,32 +405,23 @@ class _RoundViewState extends State<RoundView> {
_assignPoints(lowestScoreIndex, _caboPlayerIndex, roundScores); _assignPoints(lowestScoreIndex, _caboPlayerIndex, roundScores);
} }
} }
}
/// Returns the index of the player with the lowest score. If there are /// Returns the index of the player with the lowest score. If there are
/// multiple players with the same lowest score, all of them are returned. /// 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. /// [roundScores] is a list of the scores of all players in the current round.
List<int> _getLowestScoreIndex(List<int> roundScores) { List<int> _getLowestScoreIndex(List<int> roundScores) {
print('_getLowestScoreIndex() aufgerufen');
int lowestScore = roundScores[0]; int lowestScore = roundScores[0];
List<int> lowestScoreIndex = [0]; List<int> lowestScoreIndex = [0];
print('Niedrigster Score: ${gameSession.players[lowestScoreIndex[0]]} '
'($lowestScore Punkte)');
for (int i = 1; i < roundScores.length; i++) { for (int i = 1; i < roundScores.length; i++) {
if (roundScores[i] < lowestScore) { if (roundScores[i] < lowestScore) {
print('Neuer niedrigster Score: ${gameSession.players[i]} '
'(${roundScores[i]} Punkte)');
lowestScore = roundScores[i]; lowestScore = roundScores[i];
lowestScoreIndex = [i]; lowestScoreIndex = [i];
} else if (roundScores[i] == lowestScore) { } else if (roundScores[i] == lowestScore) {
print('${gameSession.players[i]} hat ebenfalls am wenigsten Punkte '
'(${roundScores[i]} Punkte)');
lowestScoreIndex.add(i); 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; return lowestScoreIndex;
} }
@@ -444,19 +445,20 @@ class _RoundViewState extends State<RoundView> {
/// current round. /// current round.
void _assignPoints( void _assignPoints(
List<int> winnnerIndex, int loserIndex, List<int> roundScores) { List<int> winnnerIndex, int loserIndex, List<int> roundScores) {
print('Punkte der Spieler'); print('Folgende Punkte wurden aus der Runde übernommen:');
for (int i = 0; i < roundScores.length; i++) { for (int i = 0; i < roundScores.length; i++) {
print('${widget.gameSession.players[i]}: ${roundScores[i]}'); print('${widget.gameSession.players[i]}: ${roundScores[i]}');
} }
for (int i in winnnerIndex) { 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; roundScores[i] = 0;
} }
if (loserIndex != -1) { if (loserIndex != -1) {
print('${widget.gameSession.players[loserIndex]} bekommt 5 Fehlerpunkte'); print('${widget.gameSession.players[loserIndex]} bekommt 5 Fehlerpunkte');
roundScores[loserIndex] += 5; roundScores[loserIndex] += 5;
} }
print('Aktualisierte Punkte'); print('Aktualisierte Punkte:');
for (int i = 0; i < roundScores.length; i++) { for (int i = 0; i < roundScores.length; i++) {
print('${widget.gameSession.players[i]}: ${roundScores[i]}'); print('${widget.gameSession.players[i]}: ${roundScores[i]}');
} }