1
0
Fork 0
LaboratoryProtection/Assets/UnityTest/功能模块/_3综合考核/Scripts/QuestionDatabase.cs

299 lines
7.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Sirenix.OdinInspector;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
[CreateAssetMenu(fileName = "QuestionDatabase", menuName = "Database/QuestionDatabase", order = 1)]
public partial class QuestionDatabase : SerializedScriptableObject
{
public enum QusetionType
{
Single, //单选
Mulitiple, //多选
}
[Flags]
public enum Option
{
Nil = 0,
_0 = 1 << 0,
_1 = 1 << 1,
_2 = 1 << 2,
_3 = 1 << 3,
_4 = 1 << 4,
_5 = 1 << 5,
_6 = 1 << 6,
_7 = 1 << 7,
_8 = 1 << 8,
_9 = 1 << 9,
_10 = 1 << 10,
}
private Option Switch(int num)
{
return (Option)(1 << num);
}
private char[] Switch2ABC(Option option)
{
var value = new List<char>();
for (int i = 0; i < 10; i++)
{
var temp = (Option)(1 << i);
if ((option & temp) == temp)
{
value.Add((char)(65 + i));
}
}
return value.ToArray();
}
}
#if UNITY_EDITOR
public partial class QuestionDatabase
{
[TextArea(4, 100)]
public string allStrs;
[TableList(ShowIndexLabels = true)]
public List<Data> datas = new List<Data>();
public class Data
{
[TextArea(7, 10)]
public string title;
[TextArea(7, 10)]
public string tempOptions;
public List<string> options;
[TableColumnWidth(80, resizable: false)]
public QusetionType type;
[TableColumnWidth(80, resizable: false)]
public Option rightAnswer;
[TableColumnWidth(80, resizable: false)]
public int score;
public Data()
{
title = string.Empty;
options = new List<string>(4);
type = QusetionType.Single;
rightAnswer = Option.Nil;
score = 0;
}
[TableColumnWidth(30)]
[Button, VerticalGroup("Actions")]
public void CreateOption()
{
if (tempOptions != string.Empty)
{
options = tempOptions
.Split('\n')
.Where(_ => _ != string.Empty)
.Select(_ => {
if (_.Length > 1 && _[1] == '.' || _[1] == '')
{
var newStr = new string(_.Skip(3).ToArray());
newStr = newStr.Replace(" ", "");
return newStr;
}
return _;
})
.ToList();
tempOptions = string.Empty;
}
}
[TableColumnWidth(30)]
[Button, VerticalGroup("Actions")]
public void CreateAll()
{
var all = tempOptions
.Split('\n')
.Select(_ => _.Replace(" ", ""))
.Select(_ => _.Replace("\t", ""))
.Where(_ => _ != string.Empty)
.Select(_ => {
if (_.Length > 1)
{
var first = _.IndexOfAny(new[] { '.', '' });
if (_[first + 1] == ' ')
{
first += 1;
}
var newStr = new string(_.Substring(first + 1));
return newStr;
}
return _;
})
.ToList();
this.title = all[0];
all.RemoveAt(0);
var answer = all[all.Count - 1];
var abc = answer.Replace("正确答案:", "");
all.RemoveAt(all.Count - 1);
if (abc.Length > 1)
{
type = QusetionType.Mulitiple;
}
else
{
type = QusetionType.Single;
}
Debug.Log(this.Switch(abc));
rightAnswer = this.Switch(abc);
options = all.ToList();
}
private Option Switch(string str)
{
var chars = str.ToCharArray().Select(_ => (int)_ - 65);
var options = chars.Select(_ => (int)Mathf.Pow(2, _));
var result = 0;
foreach (var item in options)
{
result |= item;
}
return (Option)result;
}
}
[Button]
private void HandleAll()
{
var all = this.allStrs
.Split('\n')
.Where(x => x.Length > 0)
.Select(_ => _.Replace(" ", ""))
.Select(_ => _.Replace("\t", ""));
var list = new List<StringBuilder>();
var temp = new StringBuilder();
foreach (var item in all)
{
temp.AppendLine(item);
if (item.Contains("正确答案") == true)
{
list.Add(temp);
temp = new StringBuilder();
}
}
var array = new QuestionDatabase.Data[list.Count];
datas = array.ToList();
for (int i = 0; i < list.Count; i++)
{
datas[i] = new QuestionDatabase.Data();
datas[i].tempOptions = list[i].ToString();
datas[i].CreateAll();
}
this.SetType();
this.Sync();
}
[Button]
private void AddData()
{
if (datas == null)
{
return;
}
datas.Add(new Data()
{
options = { "", "", "", "" },
});
}
[Button]
private void AddTemplate()
{
if (datas == null)
{
return;
}
var template = datas[0];
var data = new Data()
{
title = template.title,
options = template.options,
rightAnswer = template.rightAnswer,
score = template.score,
tempOptions = template.tempOptions,
type = template.type,
};
datas.Add(data);
this.Sync();
}
[Button]
private void SimplifyData()
{
if (datas == null)
{
return;
}
foreach (var item in datas)
{
item.options = item.options.Where(_ => _ != string.Empty).ToList();
}
this.datas.RemoveAll(_ => string.IsNullOrEmpty(_.title) == true);
this.Sync();
}
[Button()]
private void Sync()
{
#if UNITY_EDITOR
UnityEditor.EditorUtility.SetDirty(this);
#endif
}
[Button()]
private void SetType()
{
if (datas == null)
{
return;
}
foreach (var item in datas)
{
var bitArray = new BitArray(BitConverter.GetBytes((int)item.rightAnswer).ToArray());
var count = 0;
for (int i = 0; i < bitArray.Length; i++)
{
if (bitArray[i] == true)
{
count++;
}
}
Debug.Log(count);
if (count > 1)
{
item.type = QusetionType.Mulitiple;
}
else
{
item.type = QusetionType.Single;
}
}
this.Sync();
}
[Button()]
private void ShowAllCount()
{
var databases = Resources.FindObjectsOfTypeAll<QuestionDatabase>();
var count = 0;
foreach (var item in databases)
{
count += item.datas.Count;
}
Debug.Log(count);
}
}
#endif