ZoeAppClass/Assets/Scripts/Class/Question/QuestionsController.cs

382 lines
12 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class QuestionsController : MonoBehaviour
{
bool teacher = false;
public TypesClass.Questions question = new TypesClass.Questions();
private QuestionFix betterArray = new QuestionFix();
public Transform allignerNoImg, allignerImg;
public static int QAcertos = 0;
public static int QTotal = 0;
public static QuestionsController instancia;
public List<string> sentences; bool completeSentence; string rightAnswerStr;
public bool Answered = false; public string ipToImg;
int indexQuestion;
public bool inAnime = false;
private void Awake()
{
instancia = this;
}
public class QuestionFix
{
public List<string> ops = new List<string>();
public List<int> answers = new List<int>();
public List<string> photos = new List<string>();
}
public void Answer(bool rightt, int answerID) {
if (!inAnime)
{
GeneralScript.instance.questions[indexQuestion].answeredRight = rightt;
GeneralScript generalScript = GeneralScript.instance;
generalScript.addanswerdb(answerID.ToString());
generalScript.LiberarAvanco(rightt);
}
else
{
AnimeControl anime = GetComponentInParent<AnimeControl>();
anime.endQuestion(answerID);
}
QAcertos += (rightt) ? 1 : 0;
QTotal++;
ReproduzirAudio(rightt);
if (rightt && completeSentence)
{
Enunciado thisEnunciado = GetComponentInChildren<Enunciado>();
string newEnunciado = sentences[0] + rightAnswerStr + sentences[1];
thisEnunciado.ChangeText(newEnunciado);
}
}
public void ReproduzirAudio(bool Correto)
{
this.GetComponent<AudioSource>().clip = Resources.Load<AudioClip>("Questions/" + ((Correto) ? "res_correto" : "res_errado"));
this.GetComponent<AudioSource>().Play();
}
public void create(GeneralScript.GroupColors colors, TypesClass.Questions thisquestion, int index, bool isAnime = false)
{
teacher = ReadingFile.AllClassesUsers.Contains(ReadingFile.instance.userType);
indexQuestion = index;
question = thisquestion;
betterArray.ops.Add(question.op1);
betterArray.ops.Add(question.op2);
betterArray.ops.Add(question.op3);
betterArray.ops.Add(question.op4);
betterArray.answers.Add(question.answerid1);
betterArray.answers.Add(question.answerid2);
betterArray.answers.Add(question.answerid3);
betterArray.answers.Add(question.answerid4);
ipToImg = "";
question.enunciado = RemoverTagsHtml(question.enunciado);
Enunciado thisEnunciado = GetComponentInChildren<Enunciado>();
string enunciado = GlobalClasses.ImgOnSentence(question.enunciado);
if (enunciado != question.enunciado)
{
ipToImg = TakeIpFromHTML(question.enunciado);
}
CheckSentence(enunciado);
if (completeSentence)
{
enunciado = sentences[0] + "____" + sentences[1];
}
if (ipToImg == "")
{
thisEnunciado.NoImage(enunciado, colors.clara, teacher, isAnime);
if (!question.hasmedia)
{
StartCoroutine(createNoImg(colors.clara, isAnime, false));
}
else
{
StartCoroutine(CreateWithImg(colors.clara, isAnime));
}
}
else
{
if (isAnime)
{
thisEnunciado.WithImage(enunciado, colors.clara, ipToImg, teacher);
}
else
{
thisEnunciado.WithImage(enunciado, colors.escura, ipToImg, teacher);
}
if (!question.hasmedia)
{
StartCoroutine(createNoImg(colors.clara, isAnime, true));
}
else
{
StartCoroutine(CreateWithImg(colors.clara, isAnime));
}
}
}
void CheckSentence(string enunciado)
{
if (enunciado.Contains("~"))
{
Debug.Log("Enunciado has ~");
completeSentence = true;
string rawText = enunciado;
List<string> exploded = new List<string>();
exploded.AddRange(rawText.Split('~'));
Debug.Log(exploded);
for (int i = 0; i < exploded.Count; i++)
{
}
}
}
#region Image on header
string ImgOnSentence(string sent)
{
int theresImg = sent.IndexOf("src=");
int inicioImg = sent.IndexOf("<img");
if (theresImg != -1)
{
ipToImg = TakeIpFromHTML(sent);
int endImg = sent.IndexOf("\" />") + 4;
string newsent = sent;
newsent = newsent.Remove(inicioImg, endImg - inicioImg);
int BeginImg = sent.IndexOf("<img ");
int EndImg = sent.IndexOf("\" />") + 4;
try
{
newsent = RemoverTagsHtml(sent.Remove(BeginImg, EndImg - BeginImg));
}
catch (Exception e)
{
}
return newsent;
}
else
{
return sent;
}
}
string RemoverTagsHtml(string Texto)
{
Texto = Texto.Replace("<p>", "");
Texto = Texto.Replace("</p>", "");
Texto = Texto.Replace("<br />", "\n");
Texto = Texto.Replace("&gt;", ">");
Texto = Texto.Replace("&lt;", "<");
Texto = Texto.Replace("<strong>", "<b>");
Texto = Texto.Replace("</strong>", "</b>");
Texto = Texto.Replace("<em>", "<i>");
Texto = Texto.Replace("</em>", "</i>");
return Texto;
}
string TakeIpFromHTML(string textImg)
{
int startindex = textImg.IndexOf("http");
if (startindex != -1)
{
string newtext = textImg;
newtext = newtext.Remove(0, startindex);
startindex = newtext.IndexOf("\"");
newtext = newtext.Remove(startindex);
return newtext;
}
else
{
return "";
}
}
#endregion
IEnumerator createNoImg(Color color, bool isAnime, bool rectWithImage) {
GameObject prefab;
if (isAnime)
{
prefab = Resources.Load<GameObject>("Questions/OptionNoImgAnime");
}
else
{
prefab = Resources.Load<GameObject>("Questions/OptionNoImg");
}
yield return new WaitForFixedUpdate();
#region Questions
/*if (question.op1 != "" && question.op1 != null)
{
GameObject obj1 = Instantiate(prefab, allignerNoImg);
obj1.transform.localScale = new Vector2(.75f, .75f);
QuestOptionControl thisOption = obj1.GetComponent<QuestOptionControl>();
thisOption.SetInfoNoImage(question.op1, question.answerid1);
thisOption.SetColor(color);
if (question.rightQuestion == 1)
{
thisOption.SetRightQuestion();
}
}
yield return new WaitForFixedUpdate();
if (question.op2 != "" && question.op2 != null)
{
GameObject obj2 = Instantiate(prefab, allignerNoImg);
obj2.transform.localScale = new Vector2(.75f, .75f);
QuestOptionControl thisOption = obj2.GetComponent<QuestOptionControl>();
thisOption.SetInfoNoImage(question.op2, question.answerid2);
thisOption.SetColor(color);
if (question.rightQuestion == 2)
{
thisOption.SetRightQuestion();
}
}
yield return new WaitForFixedUpdate();
if (question.op3 != "" && question.op3 != null)
{
GameObject obj3 = Instantiate(prefab, allignerNoImg);
obj3.transform.localScale = new Vector2(.75f, .75f);
QuestOptionControl thisOption = obj3.GetComponent<QuestOptionControl>();
thisOption.SetInfoNoImage(question.op3, question.answerid3);
thisOption.SetColor(color);
if (question.rightQuestion == 3)
{
thisOption.SetRightQuestion();
}
}
yield return new WaitForFixedUpdate();
if (question.op4 != "" && question.op4 != null)
{
GameObject obj4 = Instantiate(prefab, allignerNoImg);
obj4.transform.localScale = new Vector2(.75f, .75f);
QuestOptionControl thisOption = obj4.GetComponent<QuestOptionControl>();
thisOption.SetInfoNoImage(question.op4, question.answerid4);
thisOption.SetColor(color);
if (question.rightQuestion == 4)
{
thisOption.SetRightQuestion();
}
}*/
#endregion
for (int i = 0; i < betterArray.ops.Count; i++)
{
int correctTest = i + 1;
CreateOption(prefab, allignerNoImg, correctTest, color, betterArray.ops[i], betterArray.answers[i], rectWithImage);
yield return new WaitForFixedUpdate();
}
}
public void CreateOption(GameObject prefab, Transform alligner, int nbr, Color color, string opstr, int id, bool imgOnsentence)
{
if (opstr != "" && opstr != null)
{
GameObject obj4 = Instantiate(prefab, alligner);
obj4.transform.localScale = new Vector2(.75f, .75f);
QuestOptionControl thisOption = obj4.GetComponent<QuestOptionControl>();
thisOption.SetInfoNoImage(opstr, id);
thisOption.SetColor(color, teacher);
if (question.rightQuestion == nbr)
{
thisOption.SetRightQuestion(teacher);
rightAnswerStr = opstr;
}
if (imgOnsentence)
{
RectTransform rect = obj4.GetComponent<RectTransform>();
rect.sizeDelta = new Vector2(300, 50);
}
}
}
IEnumerator CreateWithImg(Color color, bool isAnime) {
GameObject prefab;
if (!isAnime)
{
prefab = Resources.Load<GameObject>("Questions/OptionImg");
}
else
{
prefab = Resources.Load<GameObject>("Questions/Anime/OptionImgAnime");
}
yield return new WaitForFixedUpdate();
for (int i = 0; i < betterArray.ops.Count; i++)
{
if (betterArray.ops[i] != "" && betterArray.ops[i] != null)
{
GameObject obj1 = Instantiate(prefab, allignerImg);
if (!isAnime) { obj1.transform.localScale = new Vector2(.5f, .5f); }
else { obj1.transform.localScale = new Vector2(.75f, .75f); }
QuestOptionControl thisOption = obj1.GetComponent<QuestOptionControl>();
if (question.photoId1 != null) { thisOption.SetInfoImage(betterArray.ops[i], betterArray.photos[i]); }
else { thisOption.SetInfoNoImage(betterArray.ops[i], betterArray.answers[i]); }
thisOption.SetColor(color, teacher);
int correctTest = i + 1;
Debug.Log("This answer has the index of " + correctTest + " and the correct answer is " + question.rightQuestion);
if (question.rightQuestion == correctTest)
{
thisOption.SetRightQuestion(teacher);
}
}
yield return new WaitForFixedUpdate();
}
yield return new WaitForFixedUpdate();
}
public IEnumerator downloadImage(string id) {
Image image = GetComponentsInChildren<Image>()[2];
image.sprite = MenuController.loadsprite;
string url = "http://" + ReadingFile.iploc + "/images/back_" + id + ".jpg";
using (WWW www = new WWW(url))
{
yield return www;
image.sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height),
new Vector2(0, 0));
}
}
}