Cleaned up file

This commit is contained in:
2025-07-20 20:30:20 +02:00
parent 1c70cbeb5a
commit cadaf4ce99

View File

@@ -230,7 +230,7 @@ class _CreateGameViewState extends State<CreateGameView> {
.add(TextEditingController()); .add(TextEditingController());
}); });
} else { } else {
showFeedbackDialog(CreateStatus.maxPlayers); _showFeedbackDialog(CreateStatus.maxPlayers);
} }
}, },
), ),
@@ -268,37 +268,60 @@ class _CreateGameViewState extends State<CreateGameView> {
))); )));
} }
Future<void> _createGame() async { /// Returns a widget that displays the currently selected game mode in the View.
var uuid = const Uuid(); Text _getDisplayedGameMode() {
final String id = uuid.v1(); if (gameMode == GameMode.none) {
return Text(AppLocalizations.of(context).no_mode_selected);
} else if (gameMode == GameMode.pointLimit) {
return Text(
'${ConfigService.getPointLimit()} ${AppLocalizations.of(context).points}',
style: TextStyle(color: CustomTheme.primaryColor));
} else {
return Text(AppLocalizations.of(context).unlimited,
style: TextStyle(color: CustomTheme.primaryColor));
}
}
List<String> players = []; /// Checks all game attributes before creating a new game.
for (var controller in _playerNameTextControllers) { /// If any attribute is invalid, it shows a feedback dialog.
players.add(controller.text); /// If all attributes are valid, it calls the `_createGame` method.
void _checkAllGameAttributes() {
if (_gameTitleTextController.text == '') {
_showFeedbackDialog(CreateStatus.noGameTitle);
return;
} }
bool isPointsLimitEnabled = gameMode == GameMode.pointLimit; if (gameMode == GameMode.none) {
_showFeedbackDialog(CreateStatus.noModeSelected);
return;
}
GameSession gameSession = GameSession( if (_playerNameTextControllers.length < 2) {
id: id, _showFeedbackDialog(CreateStatus.minPlayers);
createdAt: DateTime.now(), return;
gameTitle: _gameTitleTextController.text, }
players: players,
pointLimit: ConfigService.getPointLimit(),
caboPenalty: ConfigService.getCaboPenalty(),
isPointsLimitEnabled: isPointsLimitEnabled,
);
gameManager.addGameSession(gameSession);
final session = gameManager.getGameSessionById(id)!;
Navigator.pushReplacement( if (!_everyPlayerHasAName()) {
context, _showFeedbackDialog(CreateStatus.noPlayerName);
CupertinoPageRoute( return;
builder: (context) => ActiveGameView(gameSession: session))); }
_createGame();
}
/// Checks if every player has a name.
/// Returns true if all players have a name, false otherwise.
bool _everyPlayerHasAName() {
for (var controller in _playerNameTextControllers) {
if (controller.text == '') {
return false;
}
}
return true;
} }
/// Displays a feedback dialog based on the [CreateStatus]. /// Displays a feedback dialog based on the [CreateStatus].
void showFeedbackDialog(CreateStatus status) { void _showFeedbackDialog(CreateStatus status) {
final (title, message) = _getDialogContent(status); final (title, message) = _getDialogContent(status);
showCupertinoDialog( showCupertinoDialog(
@@ -317,63 +340,6 @@ class _CreateGameViewState extends State<CreateGameView> {
}); });
} }
void _checkAllGameAttributes() {
if (_gameTitleTextController.text == '') {
_showDialog((
AppLocalizations.of(context).no_gameTitle_title,
AppLocalizations.of(context).no_gameTitle_message
));
return;
}
if (gameMode == GameMode.none) {
_showDialog(
(
AppLocalizations.of(context).no_mode_title,
AppLocalizations.of(context).no_mode_message
),
);
return;
}
if (_playerNameTextControllers.length < 2) {
_showDialog(
(
AppLocalizations.of(context).min_players_title,
AppLocalizations.of(context).min_players_message
),
);
return;
}
if (!everyPlayerHasAName()) {
_showDialog((
AppLocalizations.of(context).no_name_title,
AppLocalizations.of(context).no_name_message
));
return;
}
_createGame();
}
void _showDialog((String, String) content) {
final (title, message) = content;
showCupertinoDialog(
context: context,
builder: (context) => CupertinoAlertDialog(
title: Text(title),
content: Text(message),
actions: [
CupertinoDialogAction(
child: Text(AppLocalizations.of(context).ok),
onPressed: () => Navigator.pop(context),
),
],
),
);
}
/// Returns the title and message for the dialog based on the [CreateStatus]. /// Returns the title and message for the dialog based on the [CreateStatus].
(String, String) _getDialogContent(CreateStatus status) { (String, String) _getDialogContent(CreateStatus status) {
switch (status) { switch (status) {
@@ -406,28 +372,36 @@ class _CreateGameViewState extends State<CreateGameView> {
} }
} }
/// Checks if every player has a name. /// Creates a new gameSession and navigates to the active game view.
/// Returns true if all players have a name, false otherwise. /// This method creates a new gameSession object with the provided attributes in the text fields.
bool everyPlayerHasAName() { /// It then adds the game session to the game manager and navigates to the active game view.
for (var controller in _playerNameTextControllers) { void _createGame() {
if (controller.text == '') { var uuid = const Uuid();
return false; final String id = uuid.v1();
}
}
return true;
}
Text _getDisplayedGameMode() { List<String> players = [];
if (gameMode == GameMode.none) { for (var controller in _playerNameTextControllers) {
return Text(AppLocalizations.of(context).no_mode_selected); players.add(controller.text);
} else if (gameMode == GameMode.pointLimit) {
return Text(
'${ConfigService.getPointLimit()} ${AppLocalizations.of(context).points}',
style: TextStyle(color: CustomTheme.primaryColor));
} else {
return Text(AppLocalizations.of(context).unlimited,
style: TextStyle(color: CustomTheme.primaryColor));
} }
bool isPointsLimitEnabled = gameMode == GameMode.pointLimit;
GameSession gameSession = GameSession(
id: id,
createdAt: DateTime.now(),
gameTitle: _gameTitleTextController.text,
players: players,
pointLimit: ConfigService.getPointLimit(),
caboPenalty: ConfigService.getCaboPenalty(),
isPointsLimitEnabled: isPointsLimitEnabled,
);
gameManager.addGameSession(gameSession);
final session = gameManager.getGameSessionById(id)!;
Navigator.pushReplacement(
context,
CupertinoPageRoute(
builder: (context) => ActiveGameView(gameSession: session)));
} }
@override @override