Пример #1
0
    public void updateUserScore(string world, string chap, string difficulty, string userid, StudentScores studentscore)
    {
        Debug.Log("Update user score");
        Debug.Log("Call Score manager to update data");
        crudscore.updateUserScore(world, chap, difficulty, userid, studentscore);

        crudscore.getUserScore(world, chap, difficulty, userid, printSuccessMsg);
    }
Пример #2
0
    IEnumerator GenerateQuestions()
    {
        var getTask = FirebaseDatabase.DefaultInstance
                      .GetReference("question").Child(world).Child(chap).Child(difficulty)
                      .GetValueAsync();

        yield return(new WaitUntil(() => getTask.IsCompleted || getTask.IsFaulted));

        DataSnapshot snapshot = null;

        if (getTask.IsCompleted)
        {
            snapshot = getTask.Result;
        }

        if (difficulty == "easy")
        {
            NumQuestions = 10;
        }
        else if (difficulty == "normal")
        {
            NumQuestions = 15;
        }
        else
        {
            NumQuestions = 20;
        }

        int totalQuestion = int.Parse(snapshot.ChildrenCount.ToString());

        if (NumQuestions > totalQuestion) //If not enough questions from the database
        {
            question.text  = "The lecturer has not set enough question for this chapter.";
            Time.timeScale = 0;
        }
        else
        {
            GetQuestion[] questionList = new GetQuestion[snapshot.ChildrenCount];
            int           index        = 0;
            //Get question list
            foreach (DataSnapshot s in snapshot.Children)
            {
                questionList[index]            = new GetQuestion();
                questionList[index].UniqueKey  = s.Key;
                questionList[index++].question = JsonUtility.FromJson <UploadQuestion>(s.GetRawJsonValue());
            }

            GetQuestion[] randomQuestion = new GetQuestion[NumQuestions];
            for (int i = 0; i < NumQuestions; i++)
            {
                randomQuestion[i] = questionList[i];
                while (randomQuestion[i] == null)
                {
                    int random = Random.Range(0, NumQuestions - 1);
                    if (questionList[random] != null)
                    {
                        randomQuestion[i]    = questionList[random];
                        questionList[random] = null;
                    }
                }
            }


            int rightOption = 0;
            for (int i = 0; i < NumQuestions; i++)
            {
                question.text = randomQuestion[i].question.question;
                int random = Random.Range(1, 4);
                if (random == 1)
                {
                    answer1.text = randomQuestion[i].question.ans1;
                    answer2.text = randomQuestion[i].question.ans2;
                    answer3.text = randomQuestion[i].question.ans3;
                    answer4.text = randomQuestion[i].question.ans4;
                }
                else if (random == 2)
                {
                    answer1.text = randomQuestion[i].question.ans2;
                    answer2.text = randomQuestion[i].question.ans3;
                    answer3.text = randomQuestion[i].question.ans1;
                    answer4.text = randomQuestion[i].question.ans4;
                }
                else if (random == 3)
                {
                    answer1.text = randomQuestion[i].question.ans3;
                    answer2.text = randomQuestion[i].question.ans2;
                    answer3.text = randomQuestion[i].question.ans4;
                    answer4.text = randomQuestion[i].question.ans1;
                }
                else
                {
                    answer1.text = randomQuestion[i].question.ans3;
                    answer2.text = randomQuestion[i].question.ans1;
                    answer3.text = randomQuestion[i].question.ans4;
                    answer4.text = randomQuestion[i].question.ans2;
                }

                string rightAnswer = randomQuestion[i].question.correctAns;

                if (rightAnswer == answer1.text)
                {
                    rightOption = 1;
                }
                else if (rightAnswer == answer2.text)
                {
                    rightOption = 2;
                }
                else if (rightAnswer == answer3.text)
                {
                    rightOption = 3;
                }
                else if (rightAnswer == answer4.text)
                {
                    rightOption = 4;
                }
                yield return(new WaitForSeconds(4.2f));

                if (rightOption == userChoice)
                {
                    score++;
                    Score.text = score.ToString();
                    point.Play();
                    // playSound();
                    yield return(new WaitForSeconds(1.8f));
                }
                else
                {
                    setCharacterMovement(character, true);

                    fail.Play();
                    yield return(new WaitForSeconds(0.6f));

                    setCharacterMovement(character, false);
                    yield return(new WaitForSeconds(1.2f));
                }
            }
            // GETTING attempt  from scores

            int latestAttempt = 0;

            /* var task = FirebaseDatabase.DefaultInstance
             * .GetReference("scores").Child(world).Child(chap).Child(difficulty).Child(userID)
             * .GetValueAsync();
             *
             * yield return new WaitUntil(() => task.IsCompleted || task.IsFaulted);
             *
             *
             * if (task.IsFaulted)
             * {
             *   Debug.Log("Failed to connect");
             *   // Handle the error...
             * }
             * else if (task.IsCompleted)
             * {
             *
             *   Debug.Log("Code Runs");
             *   snapshot = task.Result;
             *
             *
             *   index = 0;
             *
             *   //currently order is ascending
             *   StudentScores[] scoresList = new StudentScores[snapshot.ChildrenCount];
             *
             *   foreach (DataSnapshot s in snapshot.Children)
             *   {
             *       scoresList[index] = new StudentScores();
             *       scoresList[index++] = JsonUtility.FromJson<StudentScores>(s.GetRawJsonValue());
             *   }
             *
             *   Debug.Log("Total record found " + snapshot.ChildrenCount);
             *
             *
             *   foreach (var item in scoresList)
             *   {
             *       if (item != null)
             *       {
             *           //get the student name here
             *           if (item.attempt > latestAttempt)
             *           {
             *               latestAttempt = item.attempt;
             *           }
             *
             *           if (item.scores > score)
             *           {
             *               score = item.scores;
             *           }
             *       }
             *   }
             * //}*/
            // To Insert student score into DB
            Debug.Log(scoreList.scores);
            if (scoreList != null)
            {
                if (scoreList.scores > score)
                {
                    score = scoreList.scores;
                }
                latestAttempt = scoreList.attempt;
            }


            crudscore.getUserScore(world, chap, difficulty, userID, callbackFunc);


            StudentScores sc = new StudentScores();

            sc.name    = "";
            sc.attempt = latestAttempt + 1;
            sc.scores  = score;
            crudscore.updateUserScore(world, chap, difficulty, userID, sc);

            Time.timeScale = 0;
            question.text  = "Game Completed! Click back to attempt another game!";
        }
    }