From 36c784da63456c42aeaf2ef4845269b2a4d26b1c Mon Sep 17 00:00:00 2001
From: Turid Dahl <turidcd@stud.ntnu.no>
Date: Thu, 16 Apr 2020 10:10:51 +0200
Subject: [PATCH] #90 game can't crash when something goes wrong with creating
 a tournament

---
 .../game/states/CreateNewTournamentState.java | 24 ++++++++++++-------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/frontend/core/src/com/gameware/game/states/CreateNewTournamentState.java b/frontend/core/src/com/gameware/game/states/CreateNewTournamentState.java
index 4b5df66..cb40661 100644
--- a/frontend/core/src/com/gameware/game/states/CreateNewTournamentState.java
+++ b/frontend/core/src/com/gameware/game/states/CreateNewTournamentState.java
@@ -60,6 +60,10 @@ public class CreateNewTournamentState extends State {
     private final String createBtnText = "Create";
     private final String nextBtnText = "Next";
 
+    private final String noNameText = "No name given";
+    private final String noGamesSelectedText = "No games selected";
+    private final String IOExceptionText = "Something went wrong when\ncreating the tournament";
+
 
 //    Input fields
     private TextField nameInputField = new TextField("",skin);
@@ -80,7 +84,7 @@ public class CreateNewTournamentState extends State {
     private final int inputFieldHeight = Gdx.graphics.getHeight()/15;
     private final int nameFieldWidth = Gdx.graphics.getWidth()/3;
     private final float checkBoxSize = Gdx.graphics.getWidth()/15;
-    private final float scrollPaneWidth = Gdx.graphics.getWidth()/1.13f;
+    private final float scrollPaneWidth = Gdx.graphics.getWidth()/1.14f;
     private final float scrollPaneHeight = Gdx.graphics.getHeight()/2.3f;
 
     private Table rootTable;
@@ -263,7 +267,7 @@ public class CreateNewTournamentState extends State {
         dialog.getContentTable().add(label).width(Gdx.graphics.getWidth()/1.5f).pad(dialog.getHeight()/5f).padBottom(0);
 
         dialog.button("Okay", true); //sends "true" as the result
-        dialog.getButtonTable().pad(dialog.getHeight()/5f);
+        dialog.getButtonTable().pad(dialog.getHeight()/2f);
 
         return dialog;
     }
@@ -287,8 +291,14 @@ public class CreateNewTournamentState extends State {
             public void clicked(InputEvent e, float x, float y){
                 if(GameWare.getInstance().isSoundEffectsOn()){ buttonPressSound.play(); }
 
+                if(getTournamentName().equals("")){
+                    errorLabel.setText(noNameText);
+                    return;
+                }
+
                 page ++;
 
+                errorLabel.setText("");
                 nameUserInput = getTournamentName();
                 timePerRoundUserInput = getTimePerRound();
                 maxPlayersUserInput = getMaxPlayers();
@@ -363,6 +373,7 @@ public class CreateNewTournamentState extends State {
             stage.clear();
             makeStage();
 
+            errorLabel.setText("");
             nameInputField.setText(nameUserInput);
             timePerRoundSelectBox.setSelected(timePerRoundUserInput);
             maxPlayersSelectBox.setSelected(maxPlayersUserInput);
@@ -374,21 +385,18 @@ public class CreateNewTournamentState extends State {
     private void handleCreateBtnClick(){
         if(GameWare.getInstance().isSoundEffectsOn()){ buttonPressSound.play(); }
         if(chosenGames.isEmpty()){
-            errorLabel.setText("No games chosen.");
-            return;
-        }
-        if(getTournamentName().equals("")){
-            errorLabel.setText("No name given.");
+            errorLabel.setText(noGamesSelectedText);
             return;
         }
         try {
             tournament = new Tournament(GameWare.getInstance().getPlayer().getId(),chosenGames,nameUserInput, timePerRoundUserInput, maxPlayersUserInput, roundsPerGameUserInput,1,chosenGames.size(),startDelayUserInput);
             tournamentFromDB = QueryIntermediate.createNewTournament(tournament);
             round = QueryIntermediate.getRoundFromTournament(tournamentFromDB.get_id(), GameWare.getInstance().getPlayer().getId(), tournamentFromDB.getCurrentRound());
+            gsm.set(new ViewTournamentState(gsm,tournamentFromDB,round));
         } catch (IOException e) {
             e.printStackTrace();
+            errorLabel.setText(IOExceptionText);
         }
-        gsm.set(new ViewTournamentState(gsm,tournamentFromDB,round));
     }
 
     private String getTournamentName(){
-- 
GitLab