Refactoring & toString method added

This commit is contained in:
Felix Kirchner
2025-05-01 13:06:49 +02:00
parent 6ad69671db
commit cd6931093e
5 changed files with 24 additions and 18 deletions

View File

@@ -3,7 +3,7 @@ import 'package:cabo_counter/data/round.dart';
/// This class represents a game session for Cabo game. /// This class represents a game session for Cabo game.
/// [createdAt] is the timestamp of when the game session was created. /// [createdAt] is the timestamp of when the game session was created.
/// [gameTitle] is the title of the game. /// [gameTitle] is the title of the game.
/// [gameHasPointLimit] is a boolean indicating if the game has the default /// [isPointsLimitEnabled] is a boolean indicating if the game has the default
/// point limit of 101 points or not. /// point limit of 101 points or not.
/// [players] is a string list of player names. /// [players] is a string list of player names.
/// [playerScores] is a list of the summed scores of all players. /// [playerScores] is a list of the summed scores of all players.
@@ -13,7 +13,7 @@ import 'package:cabo_counter/data/round.dart';
class GameSession { class GameSession {
final DateTime createdAt = DateTime.now(); final DateTime createdAt = DateTime.now();
final String gameTitle; final String gameTitle;
final bool gameHasPointLimit; final bool isPointsLimitEnabled;
final List<String> players; final List<String> players;
late List<int> playerScores; late List<int> playerScores;
List<Round> roundList = []; List<Round> roundList = [];
@@ -23,16 +23,16 @@ class GameSession {
GameSession({ GameSession({
required this.gameTitle, required this.gameTitle,
required this.gameHasPointLimit, required this.isPointsLimitEnabled,
required this.players, required this.players,
}) { }) {
playerScores = List.filled(players.length, 0); playerScores = List.filled(players.length, 0);
} }
@override @override
String toString() { toString() {
return ('GameSession: [createdAt: $createdAt, gameTitle: $gameTitle, ' return ('GameSession: [createdAt: $createdAt, gameTitle: $gameTitle, '
'gameHasPointLimit: $gameHasPointLimit, players: $players, ' 'isPointsLimitEnabled: $isPointsLimitEnabled, players: $players, '
'playerScores: $playerScores, roundList: $roundList, ' 'playerScores: $playerScores, roundList: $roundList, '
'roundNumber: $roundNumber, isGameFinished: $isGameFinished, ' 'roundNumber: $roundNumber, isGameFinished: $isGameFinished, '
'winner: $winner]'); 'winner: $winner]');
@@ -41,7 +41,7 @@ class GameSession {
/// Converts the GameSession object to a JSON map. /// Converts the GameSession object to a JSON map.
Map<String, dynamic> toJson() => { Map<String, dynamic> toJson() => {
'gameTitle': gameTitle, 'gameTitle': gameTitle,
'gameHasPointLimit': gameHasPointLimit, 'gameHasPointLimit': isPointsLimitEnabled,
'players': players, 'players': players,
'playerScores': playerScores, 'playerScores': playerScores,
'roundNumber': roundNumber, 'roundNumber': roundNumber,
@@ -53,7 +53,7 @@ class GameSession {
/// Creates a GameSession object from a JSON map. /// Creates a GameSession object from a JSON map.
GameSession.fromJson(Map<String, dynamic> json) GameSession.fromJson(Map<String, dynamic> json)
: gameTitle = json['gameTitle'], : gameTitle = json['gameTitle'],
gameHasPointLimit = json['gameHasPointLimit'], isPointsLimitEnabled = json['gameHasPointLimit'],
players = List<String>.from(json['players']), players = List<String>.from(json['players']),
playerScores = List<int>.from(json['playerScores']), playerScores = List<int>.from(json['playerScores']),
roundNumber = json['roundNumber'], roundNumber = json['roundNumber'],
@@ -208,7 +208,7 @@ class GameSession {
/// the winner. /// the winner.
void updatePoints() { void updatePoints() {
_sumPoints(); _sumPoints();
if (gameHasPointLimit) { if (isPointsLimitEnabled) {
_checkHundredPointsReached(); _checkHundredPointsReached();
for (int i = 0; i < playerScores.length; i++) { for (int i = 0; i < playerScores.length; i++) {

View File

@@ -19,6 +19,12 @@ class Round {
required this.scoreUpdates, required this.scoreUpdates,
this.kamikazePlayerIndex}); this.kamikazePlayerIndex});
@override
toString() {
return 'Round $roundNum: scores: $scores, scoreUpdates: $scoreUpdates, '
'kamikazePlayerIndex: $kamikazePlayerIndex';
}
/// Converts the Round object to a JSON map. /// Converts the Round object to a JSON map.
Map<String, dynamic> toJson() => { Map<String, dynamic> toJson() => {
'roundNum': roundNum, 'roundNum': roundNum,

View File

@@ -11,36 +11,36 @@ void main() {
Globals.addGameSession(GameSession( Globals.addGameSession(GameSession(
gameTitle: 'Spiel am 27.02.2025', gameTitle: 'Spiel am 27.02.2025',
players: ['Clara', 'Tobias', 'Yannik', 'Lena', 'Lekaia'], players: ['Clara', 'Tobias', 'Yannik', 'Lena', 'Lekaia'],
gameHasPointLimit: true)); isPointsLimitEnabled: true));
Globals.addGameSession(GameSession( Globals.addGameSession(GameSession(
gameTitle: 'Freundschaftsrunde', gameTitle: 'Freundschaftsrunde',
players: ['Felix', 'Jonas', 'Nils'], players: ['Felix', 'Jonas', 'Nils'],
gameHasPointLimit: false)); isPointsLimitEnabled: false));
Globals.addGameSession(GameSession( Globals.addGameSession(GameSession(
gameTitle: 'Familienabend', gameTitle: 'Familienabend',
players: ['Mama', 'Papa', 'Lisa'], players: ['Mama', 'Papa', 'Lisa'],
gameHasPointLimit: true, isPointsLimitEnabled: true,
)); ));
Globals.addGameSession(GameSession( Globals.addGameSession(GameSession(
gameTitle: 'Turnier 1. Runde', gameTitle: 'Turnier 1. Runde',
players: ['Tim', 'Max', 'Sophie', 'Lena'], players: ['Tim', 'Max', 'Sophie', 'Lena'],
gameHasPointLimit: false)); isPointsLimitEnabled: false));
Globals.addGameSession(GameSession( Globals.addGameSession(GameSession(
gameTitle: '2 Namen max length', gameTitle: '2 Namen max length',
players: ['Heinrich', 'Johannes'], players: ['Heinrich', 'Johannes'],
gameHasPointLimit: true)); isPointsLimitEnabled: true));
Globals.addGameSession(GameSession( Globals.addGameSession(GameSession(
gameTitle: '3 Namen max length', gameTitle: '3 Namen max length',
players: ['Benjamin', 'Stefanie', 'Wolfgang'], players: ['Benjamin', 'Stefanie', 'Wolfgang'],
gameHasPointLimit: false)); isPointsLimitEnabled: false));
Globals.addGameSession(GameSession( Globals.addGameSession(GameSession(
gameTitle: '4 Namen max length', gameTitle: '4 Namen max length',
players: ['Leonhard', 'Mathilde', 'Bernhard', 'Gerlinde'], players: ['Leonhard', 'Mathilde', 'Bernhard', 'Gerlinde'],
gameHasPointLimit: true)); isPointsLimitEnabled: true));
Globals.addGameSession(GameSession( Globals.addGameSession(GameSession(
gameTitle: '5 Namen max length', gameTitle: '5 Namen max length',
players: ['Hartmuth', 'Elisabet', 'Rosalind', 'Theresia', 'Karoline'], players: ['Hartmuth', 'Elisabet', 'Rosalind', 'Theresia', 'Karoline'],
gameHasPointLimit: false)); isPointsLimitEnabled: false));
runApp(const App()); runApp(const App());
} }

View File

@@ -284,7 +284,7 @@ class _CreateGameState extends State<CreateGame> {
GameSession gameSession = GameSession( GameSession gameSession = GameSession(
gameTitle: _gameTitleTextController.text, gameTitle: _gameTitleTextController.text,
players: players, players: players,
gameHasPointLimit: selectedMode!, isPointsLimitEnabled: selectedMode!,
); );
Globals.addGameSession(gameSession); Globals.addGameSession(gameSession);
LocalStorageService.saveGameSessions(); LocalStorageService.saveGameSessions();

View File

@@ -97,7 +97,7 @@ class _MainMenuViewState extends State<MainMenuView> {
style: const TextStyle(fontSize: 14), style: const TextStyle(fontSize: 14),
) )
: Text( : Text(
'Modus: ${_translateGameMode(session.gameHasPointLimit)}', 'Modus: ${_translateGameMode(session.isPointsLimitEnabled)}',
style: const TextStyle(fontSize: 14), style: const TextStyle(fontSize: 14),
), ),
trailing: Row( trailing: Row(