From 116cfe9a6a2dca0c5638d283e6862772fec2e505 Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Wed, 23 Apr 2025 23:06:31 +0200 Subject: [PATCH] Replaced emojis with unicode and implemented winner string --- lib/data/game_session.dart | 23 ++++++++++++++++++++++- lib/views/active_game_view.dart | 15 +++++++-------- lib/views/main_menu_view.dart | 23 ++++++++++++----------- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/lib/data/game_session.dart b/lib/data/game_session.dart index 7f5d788..680b9b2 100644 --- a/lib/data/game_session.dart +++ b/lib/data/game_session.dart @@ -17,13 +17,14 @@ class GameSession { Duration(days: 21).inMilliseconds + 1))); // DEBUG: Random Timestamp int round = 1; bool finished = false; + String winner = ''; GameSession({ required this.gameTitle, required this.players, required this.gameMode, }); - List> playerScores = List.generate(5, (_) => [0]); + List> playerScores = List.generate(5, (_) => [0, 0]); @override String toString() { @@ -45,6 +46,22 @@ class GameSession { return length; } + void _determineWinner() { + int score = playerScores[0][0]; + String lowestPlayer = players[0]; + for (int i = 0; i < players.length; i++) { + print('Player: ${players[i]}, Score: ${playerScores[i][0]}'); + if (playerScores[i][0] < score) { + print( + 'New lowest player: ${players[i]} - Score: ${playerScores[i][0]}'); + score = playerScores[i][0]; + lowestPlayer = players[i]; + } + } + print('Der Gewinner ist: $lowestPlayer'); + winner = lowestPlayer; + } + /// Returns a string representation of the scores for a specific round. /// The method takes a round number as a parameter and returns a string /// containing the name of each player and their corressponding score in @@ -72,7 +89,10 @@ class GameSession { /// playerScores. Its important that each index of the [roundScores] list /// corresponds to the index of the player in the [playerScores] list. void addRoundScoresToScoreList(List roundScores, int roundNumber) { + print('addRoundScoresToScoreList: $roundScores'); for (int i = 0; i < roundScores.length; i++) { + print( + 'i: $i, roundNumber: $roundNumber, playerScores[i].length: ${playerScores[i].length}'); playerScores[i][roundNumber] = (roundScores[i]); } } @@ -93,6 +113,7 @@ class GameSession { finished = true; print('${players[i]} hat die 101 Punkte ueberschritten, ' 'deswegen wurde das Spiel beendet'); + _determineWinner(); } } } diff --git a/lib/views/active_game_view.dart b/lib/views/active_game_view.dart index eff2bc5..b22100d 100644 --- a/lib/views/active_game_view.dart +++ b/lib/views/active_game_view.dart @@ -74,10 +74,10 @@ class _ActiveGameViewState extends State { title: Text( 'Runde ${index + 1}', ), - trailing: - index + 1 == widget.gameSession.playerScores[0].length - ? Text('⏳', style: TextStyle(fontSize: 22)) - : Text('✅', style: TextStyle(fontSize: 22)), + trailing: index + 1 != widget.gameSession.round || + widget.gameSession.finished == true + ? (Text('\u{2705}', style: TextStyle(fontSize: 22))) + : Text('\u{23F3}', style: TextStyle(fontSize: 22)), onTap: () async { // ignore: unused_local_variable final val = await Navigator.push( @@ -105,7 +105,6 @@ 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]; @@ -122,17 +121,17 @@ class _ActiveGameViewState extends State { switch (index) { case 0: return Text( - '🥇', + '\u{1F947}', style: TextStyle(fontSize: 22), ); case 1: return Text( - '🥈', + '\u{1F948}', style: TextStyle(fontSize: 22), ); case 2: return Text( - '🥉', + '\u{1F949}', style: TextStyle(fontSize: 22), ); default: diff --git a/lib/views/main_menu_view.dart b/lib/views/main_menu_view.dart index a53c503..280f503 100644 --- a/lib/views/main_menu_view.dart +++ b/lib/views/main_menu_view.dart @@ -53,7 +53,6 @@ class _MainMenuViewState extends State { @override Widget build(BuildContext context) { gameSessionArray.sort((b, a) => a.createdAt.compareTo(b.createdAt)); - calculateRoundNumbers(); return CupertinoPageScaffold( navigationBar: CupertinoNavigationBar( @@ -89,8 +88,15 @@ class _MainMenuViewState extends State { padding: EdgeInsets.symmetric(vertical: 10.0), child: CupertinoListTile( title: Text(session.gameTitle), - subtitle: - Text('Modus: ${_translateGameMode(session.gameMode)}'), + subtitle: session.finished == true + ? Text( + '\u{1F947} ${session.winner}', + style: TextStyle(fontSize: 14), + ) + : Text( + 'Modus: ${_translateGameMode(session.gameMode)}', + style: TextStyle(fontSize: 14), + ), trailing: Row( children: [ Text('${session.round}'), @@ -102,14 +108,15 @@ class _MainMenuViewState extends State { Icon(CupertinoIcons.person_2_fill), ], ), - onTap: () { - Navigator.push( + onTap: () async { + final val = await Navigator.push( context, CupertinoPageRoute( builder: (context) => ActiveGameView( gameSession: gameSessionArray[index]), ), ); + setState(() {}); }, )); }, @@ -129,10 +136,4 @@ class _MainMenuViewState extends State { return '-'; } } - - void calculateRoundNumbers() { - for (var s in gameSessionArray) { - s.round = s.playerScores[0].length; - } - } }