mirror of
https://github.com/ConjureETS/GameOff2024.git
synced 2026-03-24 05:00:59 +00:00
85 lines
2.6 KiB
C#
85 lines
2.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using GameOff.Peasant;
|
|
using GameOff.Quiz;
|
|
using UnityEngine;
|
|
using Random = System.Random;
|
|
|
|
namespace GameOff.Core
|
|
{
|
|
[DefaultExecutionOrder(-60)]
|
|
public class QuizHandler : MonoBehaviour
|
|
{
|
|
public static QuizHandler Instance { get; private set; }
|
|
|
|
[SerializeField] private int questionAmount = 20;
|
|
[SerializeField] private int quizTakerAmount = 8;
|
|
[SerializeField] private List<Sprite> questionVisual;
|
|
[SerializeField] private int seed;
|
|
[SerializeField] private bool randomGeneration;
|
|
|
|
private readonly List<string> ChoicePossibles = new List<string>() { "a", "b", "c", "d", "e", "f" };
|
|
|
|
private Random _random;
|
|
private List<QuestionInfo> _questionInfos;
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance)
|
|
{
|
|
Debug.LogError($"QuizManager already exist! {transform}");
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
Instance = this;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (randomGeneration)
|
|
seed = UnityEngine.Random.Range(0, Int32.MaxValue);
|
|
_random = new Random(seed);
|
|
|
|
GenerateQuiz();
|
|
}
|
|
|
|
private void GenerateQuiz()
|
|
{
|
|
_questionInfos = new List<QuestionInfo>();
|
|
|
|
for (int i = 0; i < questionAmount; i++)
|
|
{
|
|
int amount = _random.Next(2, ChoicePossibles.Count);
|
|
string answer = ChoicePossibles[_random.Next(amount)];
|
|
List<string> tmp = new List<string>();
|
|
|
|
for (int x = 0; x < amount; x++)
|
|
tmp.Add(ChoicePossibles[x]);
|
|
|
|
_questionInfos.Add(new QuestionInfo(i + 1, questionVisual[_random.Next(questionVisual.Count)], tmp, answer));
|
|
}
|
|
}
|
|
|
|
public string GetAnswerAtIndex(int index, float successRate) {
|
|
if(_random.Next(100) <= successRate * 100)
|
|
return _questionInfos[index].Answer;
|
|
return _questionInfos[index].Choices[_random.Next(_questionInfos[index].Count)];
|
|
}
|
|
|
|
public float GetResultPercent(string[] answer)
|
|
{
|
|
float succeed = 0;
|
|
|
|
for (int i = 0; i < answer.Length; i++)
|
|
if (answer[i] == _questionInfos[i].Answer)
|
|
succeed++;
|
|
|
|
return succeed / questionAmount * 100;
|
|
}
|
|
|
|
public List<QuestionInfo> QuestionInfos => _questionInfos;
|
|
public int QuestionAmount => questionAmount;
|
|
}
|
|
}
|