|
| 1 | +using System.Collections; |
| 2 | +using System.Collections.Generic; |
| 3 | +using UnityEngine; |
| 4 | +using SaveData; |
| 5 | +using UnityEngine.UI; |
| 6 | + |
| 7 | +public class SaveManager : MonoBehaviour |
| 8 | +{ |
| 9 | + public List<QuestionCatalogue> questionCatalogueList; |
| 10 | + public PlayerProfile playerProfile; |
| 11 | + public QuestionCatalogue createdQuestionCatalogue; |
| 12 | + public Canvas globalCanvas; |
| 13 | + |
| 14 | + private void Start() |
| 15 | + { |
| 16 | + playerProfile = SaveSystem.instance.loadPlayerProfileFromJson("Player"); |
| 17 | + questionCatalogueList = SaveSystem.instance.loadQuestionCataloguesFromJson(); |
| 18 | + |
| 19 | + if (playerProfile.userName == null) |
| 20 | + { |
| 21 | + openUsernamePanel(); |
| 22 | + } |
| 23 | + playerProfileStatistics(); |
| 24 | + } |
| 25 | + |
| 26 | + public void savePlayerProfileToFile() |
| 27 | + { |
| 28 | + SaveSystem.instance.savePlayerProfileToJson(playerProfile); |
| 29 | + Debug.Log("Saved Active PlayerProfile to FileSystem in " + Application.persistentDataPath); |
| 30 | + } |
| 31 | + |
| 32 | + public void addQuestionToOwnQuestionCatalogue() |
| 33 | + { |
| 34 | + string questionName; |
| 35 | + int rightAnswerPosition = 0; //By Default the First Answer is always the correct answer |
| 36 | + List<string> answers = new List<string>(); //The Starting Index of such a List is 0. The First Element of this List can be accessed via "answers[0]". |
| 37 | + |
| 38 | + questionName = GameObject.Find("InputField_QuestionName").GetComponentInChildren<Text>().text; //UI-InputField for QuestionName must be named "InputField_QuestionName" |
| 39 | + GameObject[] answerInputFields = GameObject.FindGameObjectsWithTag("Created_Question_Answer"); //UI-InputFields for Question-Answers must have Tag "Created_Question_Answer" |
| 40 | + |
| 41 | + foreach (GameObject answerInputField in answerInputFields) |
| 42 | + { |
| 43 | + string answerText = answerInputField.GetComponentInChildren<Text>().text; |
| 44 | + if (string.IsNullOrWhiteSpace(answerText)) |
| 45 | + { |
| 46 | + Debug.Log("An Answer was found to be Empty or unusable.\nQuestion Adding Process has been stopped.\nPlease Fill Answer InputFields with reasonable Information."); |
| 47 | + return; //Exits the Function |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + answers.Add(answerText); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + GameObject[] answerToggles = GameObject.FindGameObjectsWithTag("Created_Question_Answer_Toggle"); //UI-InputFields for Question-Answers must have Tag "Created_Question_Answer" |
| 56 | + //The Toggles must all belong to the same Toggle-Group and an option to only nable one Toggle must be activated |
| 57 | + int answerID = 0; |
| 58 | + foreach (GameObject answerToggle in answerToggles) |
| 59 | + { |
| 60 | + bool active = answerToggle.GetComponent<Toggle>().isOn; |
| 61 | + if (active == true) |
| 62 | + { |
| 63 | + rightAnswerPosition = answerID; |
| 64 | + break; |
| 65 | + } |
| 66 | + else |
| 67 | + { |
| 68 | + answerID++; |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + Question question = new Question(questionName, answers, rightAnswerPosition); |
| 73 | + createdQuestionCatalogue.questions.Add(question); |
| 74 | + Debug.Log("A new Question has been added to the currently active QuestionCatalogue."); |
| 75 | + } |
| 76 | + |
| 77 | + public void displayQuestionCount() |
| 78 | + { |
| 79 | + int questionCount = createdQuestionCatalogue.questions.Count; |
| 80 | + GameObject.Find("Label_QuestionCount").GetComponentInChildren<Text>().text = questionCount.ToString(); //UI-Label for the Display of the current Question Count has to be named "Label_QuestionCount" |
| 81 | + } |
| 82 | + |
| 83 | + public void saveQuestionCatalogueToFileSystem() |
| 84 | + { |
| 85 | + string fileName = GameObject.Find("InputField_QuestionCatalogueName").GetComponentInChildren<Text>().text; |
| 86 | + if (string.IsNullOrWhiteSpace(fileName)) |
| 87 | + { |
| 88 | + Debug.Log("Catalogue Name was found to be Empty or unusable.\nCatalogue Saving Process has been stopped.\nPlease Fill the Catalogue Name InputField with reasonable Information."); |
| 89 | + return; //Exits the Function |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + createdQuestionCatalogue.fileName = fileName; |
| 94 | + } |
| 95 | + |
| 96 | + if (createdQuestionCatalogue.questions.Count < 5) |
| 97 | + { |
| 98 | + Debug.Log("A Catalogue has to have a minimum of 5 added Questions.\nCatalogue Saving Process has been stopped.\nPlease add More Questions."); |
| 99 | + return; //Exits the Function |
| 100 | + } |
| 101 | + SaveSystem.instance.saveQuestionCatalogueToJson(createdQuestionCatalogue); |
| 102 | + |
| 103 | + |
| 104 | + } |
| 105 | + public void setUsername() |
| 106 | + { |
| 107 | + string username = GameObject.Find("InputField_Username").GetComponentInChildren<Text>().text; |
| 108 | + if (string.IsNullOrWhiteSpace(username)) |
| 109 | + { |
| 110 | + Debug.Log("Username was found to be Empty or unusable.\nUsername Saving Process has been stopped.\nPlease Fill the Username InputField with reasonable Information."); |
| 111 | + return; //Exits the Function |
| 112 | + } |
| 113 | + else |
| 114 | + { |
| 115 | + playerProfile.userName = username; |
| 116 | + SaveSystem.instance.savePlayerProfileToJson(playerProfile); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + public void openUsernamePanel() |
| 121 | + { |
| 122 | + //Open a UI-Panel with an InputField for the User to Input his Username |
| 123 | + GameObject usernamePanel = globalCanvas.transform.Find("Panel_Username").gameObject; //The Panel named "Panel_Username" must be a direct child of the global UI canvas |
| 124 | + usernamePanel.SetActive(true); |
| 125 | + } |
| 126 | + |
| 127 | + public void playerProfileStatistics() |
| 128 | + { |
| 129 | + bool newStatistic = false; |
| 130 | + //Compares Available Statistics to Available Catalogues |
| 131 | + foreach (QuestionCatalogue questionCatalogue in questionCatalogueList) |
| 132 | + { |
| 133 | + bool found = false; |
| 134 | + foreach (Statistic statistic in playerProfile.statistics) |
| 135 | + { |
| 136 | + if (string.Equals(questionCatalogue.fileName, statistic.label)) |
| 137 | + { |
| 138 | + found = true; |
| 139 | + } |
| 140 | + } |
| 141 | + if (found == false) |
| 142 | + { |
| 143 | + addStatistic(questionCatalogue.fileName, 0); |
| 144 | + newStatistic = true; |
| 145 | + } |
| 146 | + |
| 147 | + } |
| 148 | + if (newStatistic == true) |
| 149 | + { |
| 150 | + SaveSystem.instance.savePlayerProfileToJson(playerProfile); |
| 151 | + } |
| 152 | + |
| 153 | + } |
| 154 | + private void addStatistic(string label, int score) |
| 155 | + { |
| 156 | + Statistic statistic = new Statistic(label, score); |
| 157 | + playerProfile.statistics.Add(statistic); |
| 158 | + Debug.Log("A new Statistic " + statistic.label + " has been added." ); |
| 159 | + } |
| 160 | + |
| 161 | + public void updateStatistic(string label, int score) //Can be called after every QuestionCatalogue-Session |
| 162 | + { |
| 163 | + bool changed = false; |
| 164 | + foreach (Statistic statistic in playerProfile.statistics) |
| 165 | + { |
| 166 | + if (string.Equals(label, statistic.label) && score > statistic.score) |
| 167 | + { |
| 168 | + statistic.score = score; |
| 169 | + changed = true; |
| 170 | + } |
| 171 | + } |
| 172 | + if (changed == true) |
| 173 | + { |
| 174 | + SaveSystem.instance.savePlayerProfileToJson(playerProfile); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | +} |
0 commit comments