public static IEnumerator GetQuestion(Action <QuestionDataServer> callback) { if (string.IsNullOrEmpty(currentSessionToken)) { yield return(GetKey()); } UnityWebRequest webRequest = UnityWebRequest.Get("https://opentdb.com/api.php?amount=1&encode=base64&token=" + currentSessionToken); yield return(webRequest.SendWebRequest()); string cleanedRequest = preRemove.Replace(webRequest.downloadHandler.text, "").Replace("]}]}", "]}"); QuestionDataServer questionData = JsonUtility.FromJson <QuestionDataServer>(cleanedRequest); questionData.question = DecodeBase64(questionData.question); questionData.correct_answer = DecodeBase64(questionData.correct_answer); for (int i = 0; i < questionData.incorrect_answers.Length; i++) { questionData.incorrect_answers[i] = DecodeBase64(questionData.incorrect_answers[i]); } /*new QuestionDataServer(); * * questionData.question = "What is the meaning of life?"; * * questionData.incorrect_answers = new string[3] { "Calculating...", "Lots of beer.", "We mortals cannot fanthom that." }; * questionData.correct_answer = "42";*/ List <string> answers = new List <string>(); for (int i = 0; i < questionData.incorrect_answers.Length + 1; i++) { if (i == 0) { answers.Add(questionData.correct_answer); } else { answers.Add(questionData.incorrect_answers[i - 1]); } } Shuffle(answers); questionData.shuffled_answers = answers.ToArray(); questionData.correct_answer_index = answers.IndexOf(questionData.correct_answer); callback(questionData); }
private IEnumerator SendQuestionToClients() { yield return(QuestionReceiver.GetQuestion((returnedQuestionData) => { currentQuestionData = returnedQuestionData; })); currentQuestionDataClient = new QuestionDataClient(currentQuestionData.question, currentQuestionData.shuffled_answers); playersAnswered = 0; for (int i = 0; i < playerIds.Count; i++) { if (players[playerIds[i]] == null) { continue; } int playerIndex = playerIds[i]; SendQuestionToClient(players[playerIndex], currentQuestionDataClient); } }