Advertisement
NomadicWarrior

QuestioData

Feb 22nd, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using System.IO;
  4. using UnityEngine.UI;
  5.  
  6. public class QuestionData : MonoBehaviour
  7. {
  8. public static QuestionData instance { get; private set; }
  9.  
  10. private string[] questionFiles = { "rank_0.json", "rank_1.json", "rank_2.json" }; // All question JSON files
  11. private string currentQuestionsFile = "current.json"; // Current empty JSON file
  12.  
  13.  
  14. private string questionJsonFilePath; // Path of question files
  15. private string emptyJsonFilePath; // Path of empty file
  16. private bool answerCheck; // check current question's answer
  17. private int randomizedIndex;
  18.  
  19. public ListQuestions listQuestions;
  20.  
  21. [SerializeField] Text questionDisplayText = default;
  22. [SerializeField] Button yesButton = default;
  23. [SerializeField] Button noButton = default;
  24. [SerializeField] private int totalQuestionsLeft;
  25.  
  26.  
  27. private void Awake()
  28. {
  29. if(instance == null)
  30. {
  31. instance = this;
  32. DontDestroyOnLoad(gameObject);
  33. }
  34. else
  35. {
  36. Destroy(gameObject);
  37. }
  38. }
  39.  
  40. private void Start()
  41. {
  42.  
  43. }
  44.  
  45. public void LoadQuestion(int playerRank)
  46. {
  47. // Load Questions according on player rank
  48. questionJsonFilePath = Path.Combine(Application.streamingAssetsPath, questionFiles[playerRank]);
  49. emptyJsonFilePath = Path.Combine(Application.streamingAssetsPath, currentQuestionsFile);
  50.  
  51. ValidateJson(questionJsonFilePath, emptyJsonFilePath);
  52. }
  53.  
  54. private void ValidateJson(string fullOfQuestionJson, string emptyJson)
  55. {
  56. if (File.Exists(fullOfQuestionJson))
  57. {
  58. string dataAsJson = File.ReadAllText(fullOfQuestionJson);
  59. listQuestions = JsonUtility.FromJson<ListQuestions>(dataAsJson);
  60.  
  61. if (File.Exists(emptyJson))
  62. {
  63. string toJson = JsonUtility.ToJson(listQuestions);
  64. File.WriteAllText(emptyJson, toJson);
  65. }
  66.  
  67. totalQuestionsLeft = listQuestions.questions.Count;
  68. DispalyQuestion();
  69. }
  70. }
  71.  
  72. // Display questions randomly
  73. private void DispalyQuestion()
  74. {
  75. if (totalQuestionsLeft > 0)
  76. {
  77. int questionIndex = Random.Range(0, listQuestions.questions.Count);
  78. randomizedIndex = questionIndex;
  79. questionDisplayText.text = listQuestions.questions[questionIndex].question;
  80. }
  81. else if (totalQuestionsLeft <= 0)
  82. {
  83. GamePanel.instance.SuccessPanel();
  84. Debug.Log("Level Has Finished");
  85. Player.instance.playerData.rankNumber += 1;
  86. Player.instance.SavePlayerData();
  87. }
  88. }
  89.  
  90. // Check True Answers
  91. public void YesAnswer(bool isTrue)
  92. {
  93. if (totalQuestionsLeft > 0)
  94. {
  95. if (listQuestions.questions[randomizedIndex].isTrue == isTrue)
  96. {
  97. print("Yes answer is correct");
  98. UpdateQuestion(randomizedIndex);
  99. }
  100. else if (listQuestions.questions[randomizedIndex].isTrue != isTrue)
  101. {
  102. print("Yes answer is incorrect");
  103. GamePanel.instance.FailPanel();
  104. }
  105. }
  106.  
  107. }
  108.  
  109. // Check False Answers
  110. public void NoAnswer(bool isFalse)
  111. {
  112. if (totalQuestionsLeft > 0)
  113. {
  114. if (listQuestions.questions[randomizedIndex].isTrue == isFalse)
  115. {
  116. print("No answer is correct");
  117. UpdateQuestion(randomizedIndex);
  118. }
  119. else if (listQuestions.questions[randomizedIndex].isTrue != isFalse)
  120. {
  121. print("No answer is incorrect");
  122. GamePanel.instance.FailPanel();
  123. }
  124. }
  125. }
  126.  
  127.  
  128. private void UpdateQuestion(int removalElement)
  129. {
  130. questionDisplayText.text = listQuestions.questions[removalElement].question;
  131. listQuestions.questions.RemoveAt(removalElement);
  132. totalQuestionsLeft = listQuestions.questions.Count;
  133. string newJson = JsonUtility.ToJson(listQuestions);
  134. File.WriteAllText(emptyJsonFilePath, newJson);
  135. DispalyQuestion();
  136. GamePanel.instance.Rotate();
  137. }
  138.  
  139. }
  140.  
  141. [System.Serializable]
  142. public class Questions
  143. {
  144. public string question;
  145. public bool isTrue;
  146. }
  147.  
  148. [System.Serializable]
  149. public class ListQuestions
  150. {
  151. public List<Questions> questions;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement