抽签分组软件添加抽签滚动效果,并完成导出excel

ruanjian2
zc 2023-12-20 19:31:00 +08:00
parent 0a3300e5c7
commit 9f436f192a
18 changed files with 537 additions and 1304 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using OfficeOpenXml;
using Script.UI;
using UnityEngine;
using UnityEngine.UI;
@ -15,6 +18,7 @@ public class ExcelManager : SingleManager<ExcelManager>
public Player_DataInfo GetPlayerInfo => _playerData.GetAndRemove();
public GroupData_DataInfo GetGroupInfoA => _groupData.GetA();
public GroupData_DataInfo GetGroupInfoB => _groupData.GetB();
public GroupData_DataInfo GetGroupRandomInfo => _groupData.RandomGet();
public ExcelManager()
{
@ -26,6 +30,38 @@ public class ExcelManager : SingleManager<ExcelManager>
Debug.Log("excel data load finish !!");
}
}
//模拟数据写入
#region 存入内容并导出Excel文件
public void SaveAndExp(SaveExcelData data)
{
string path = Application.streamingAssetsPath + "/程序输出数据" + DateTime.Now.Ticks + ".xlsx";
FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
using (var pck = new ExcelPackage(fs))
{
var sheet = pck.Workbook.Worksheets.Add("Sheet1");
for (int i = 0; i < data.hands.Length; i++)
{
sheet.SetValue(1, i + 2, data.hands[i]);
}
for (var i = 0; i < data.infos.Count; i++)
{
for (var j = 0; j < data.infos[i].strings.Length; j++)
{
//第0行、列留白
// sheet.Cells[i + 2, j + 2].Value = data.infos[i].strings[j];
sheet.SetValue(i + 2, j + 2, data.infos[i].strings[j]);
}
}
pck.Save();
}
fs.Dispose();
}
#endregion
}

View File

@ -0,0 +1,28 @@
using System.Collections.Generic;
public class SaveExcelData
{
public class SaveExcelDataInfo
{
public string[] strings;
public SaveExcelDataInfo(string[] str)
{
strings = str;
}
}
public List<SaveExcelDataInfo> infos;
public string[] hands;
public SaveExcelData()
{
hands = new[] { "选手ID", "选手(如没有内容属于正常情况)", "分组" };
this.infos = new List<SaveExcelDataInfo>();
}
public void AddInfoData(string id, string name, string team)
{
infos.Add(new SaveExcelDataInfo(new[] { id, name, team }));
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8c0768e23cf7447cae0689c6b00366ed
timeCreated: 1703060804

View File

@ -22,7 +22,7 @@ namespace Script
public FileManager()
{
dataPath = Application.streamingAssetsPath + $"/data{GenerateGlobalID.GenerateIntID()}.txt";
dataPath = Application.streamingAssetsPath + $"/Data.txt";
if (!File.Exists(dataPath))
{
var fileStream = File.Create(dataPath);

View File

@ -6,6 +6,11 @@ namespace Script
public class GlobalManager : SingleManager<GlobalManager>
{
private List<SummaryInfo> _list = new List<SummaryInfo>();
public List<SummaryInfo> list => _list;
public List<SummaryInfo> list
{
get { return _list; }
set { _list = value; }
}
}
}

View File

@ -13,6 +13,20 @@ namespace Script.UI
btn_Exit.onClick.AddListener(ClickExit);
}
public override void Open()
{
base.Open();
var data = new SaveExcelData();
foreach (var summaryInfo in GlobalManager.Instance.list)
{
data.AddInfoData(summaryInfo.id.ToString(), "", summaryInfo.group);
}
ExcelManager.Instance.SaveAndExp(data);
}
public override void Dispose()
{
base.Dispose();

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
@ -10,17 +11,55 @@ namespace Script.UI
{
public GameObject item;
public Button btn_Close;
public Button btn_Stop;
class GroupPanelData
{
private GameObject go;
private Text txt_ID;
private Text txt_Name;
public GroupPanelData(GameObject go, string idStr, string nameStr = "")
{
txt_ID = go.transform.GetChild(0).GetComponent<Text>();
txt_Name = go.transform.GetChild(1).GetComponent<Text>();
txt_ID.text = idStr;
txt_Name.text = nameStr;
}
public void SetCultureName(string name)
{
this.txt_Name.text = name;
}
}
private Dictionary<int, GroupPanelData> groups = new Dictionary<int, GroupPanelData>();
private bool isRolling;
public override void Init()
{
base.Init();
btn_Close.onClick.AddListener(ClickClose);
btn_Stop.onClick.AddListener(ClickStop);
btn_Close.gameObject.SetActive(false);
btn_Stop.gameObject.SetActive(true);
}
private void ClickStop()
{
btn_Close.gameObject.SetActive(true);
btn_Stop.gameObject.SetActive(false);
//
isRolling = false;
}
public override void Dispose()
{
base.Dispose();
btn_Close.onClick.RemoveListener(ClickClose);
btn_Stop.onClick.RemoveListener(ClickStop);
}
public override void Open()
@ -66,31 +105,108 @@ namespace Script.UI
}
// 排个序
var list = end.OrderBy(t=>t.id).ToList();
var list = end.OrderBy(t => t.id).ToList();
StringBuilder sb = new StringBuilder();
// StringBuilder sb = new StringBuilder();
foreach (var summaryInfo in list)
{
item.SetActive(false);
GenItem(summaryInfo.id, summaryInfo.group);
sb.AppendLine($"选手ID{summaryInfo.id}");
sb.AppendLine($"选手分组:{summaryInfo.group}");
sb.AppendLine($"\n");
// sb.AppendLine($"选手ID{summaryInfo.id}");
// sb.AppendLine($"选手分组:{summaryInfo.group}");
// sb.AppendLine($"\n");
}
FileManager.Instance.SavePlayerData(sb.ToString());
isRolling = true;
StartCoroutine(RandomRoll());
// FileManager.Instance.SavePlayerData(sb.ToString());
}
private void GenItem(string id, string group)
{
var go = GameObject.Instantiate(item, item.transform.parent);
go.name = id;
var txt_ID = go.transform.Find("txt_ID").GetComponent<Text>();
var txt_Group = go.transform.Find("txt_Group").GetComponent<Text>();
txt_ID.text = id;
txt_Group.text = group;
// var txt_ID = go.transform.Find("txt_ID").GetComponent<Text>();
// var txt_Group = go.transform.Find("txt_Group").GetComponent<Text>();
// txt_ID.text = id;
// txt_Group.text = group;
go.SetActive(true);
groups.Add(int.Parse(id), new GroupPanelData(go, id, group));
}
IEnumerator RandomRoll()
{
while (isRolling)
{
yield return new WaitForSeconds(0.02f);
var info = ExcelManager.Instance.GetGroupRandomInfo;
foreach (var culturePanelData in groups.Values)
{
culturePanelData.SetCultureName(info.groupName);
}
}
ShowResult();
}
void ShowResult()
{
List<SummaryInfo> cache = new List<SummaryInfo>(GlobalManager.Instance.list);
List<SummaryInfo> end = new List<SummaryInfo>();
var num = cache.Count % 2; // 平均后多出来的
var listNum = cache.Count / 2; //
var groupAName = ExcelManager.Instance.GetGroupInfoA.groupName;
var groupBName = ExcelManager.Instance.GetGroupInfoB.groupName;
// A
var count = listNum + num;
for (int i = 0; i < count; i++)
{
var range = Random.Range(0, cache.Count - 1);
var summaryInfo = cache[range];
summaryInfo.group = groupAName;
end.Add(summaryInfo);
cache.Remove(summaryInfo);
}
// B
for (int i = 0; i < listNum; i++)
{
var range = Random.Range(0, cache.Count - 1);
var summaryInfo = cache[range];
summaryInfo.group = groupBName;
end.Add(summaryInfo);
cache.Remove(summaryInfo);
}
// 排个序
var list = end.OrderBy(t => t.id).ToList();
StringBuilder sb = new StringBuilder();
foreach (var summaryInfo in list)
{
item.SetActive(false);
SetItem(summaryInfo.id, summaryInfo.group);
sb.AppendLine($"选手ID{summaryInfo.id}");
sb.AppendLine($"选手分组:{summaryInfo.group}");
sb.AppendLine($"\n");
}
GlobalManager.Instance.list = list;
FileManager.Instance.SavePlayerData(sb.ToString());
}
private void SetItem(string id, string name)
{
var i = int.Parse(id);
if (groups.TryGetValue(i, out var data))
{
data.SetCultureName(name);
}
}
}
}

View File

@ -0,0 +1,112 @@
选手ID01
选手分组B组
选手ID02
选手分组A组
选手ID07
选手分组B组
选手ID09
选手分组B组
选手ID10
选手分组A组
选手ID11
选手分组A组
选手ID01
选手分组A组
选手ID02
选手分组A组
选手ID04
选手分组A组
选手ID08
选手分组B组
选手ID10
选手分组B组
选手ID11
选手分组B组
选手ID01
选手分组A组
选手ID02
选手分组B组
选手ID04
选手分组B组
选手ID05
选手分组B组
选手ID06
选手分组A组
选手ID07
选手分组B组
选手ID09
选手分组A组
选手ID10
选手分组A组
选手ID01
选手分组B组
选手ID02
选手分组A组
选手ID03
选手分组B组
选手ID05
选手分组B组
选手ID07
选手分组A组
选手ID08
选手分组A组
选手ID10
选手分组A组

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 12dc9ac2beec49c40a52d833c7e02ef2
guid: 2c84cb78a48446f41886dce73e077ba2
DefaultImporter:
externalObjects: {}
userData:

View File

@ -1,25 +0,0 @@
选手ID01
选手分组B组
选手ID04
选手分组A组
选手ID05
选手分组B组
选手ID07
选手分组B组
选手ID08
选手分组A组
选手ID09
选手分组A组

View File

@ -1,25 +0,0 @@
选手ID09
选手分组B组
选手ID08
选手分组A组
选手ID07
选手分组A组
选手ID06
选手分组B组
选手ID05
选手分组A组
选手ID04
选手分组B组

View File

@ -1,7 +0,0 @@
fileFormatVersion: 2
guid: 837215d7031790a42bb6830a88546ccd
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,25 +0,0 @@
选手ID03
选手分组A组
选手ID06
选手分组A组
选手ID05
选手分组A组
选手ID01
选手分组A组
选手ID09
选手分组A组
选手ID08
选手分组A组
选手ID04
选手分组B组
选手ID11
选手分组B组
选手ID02
选手分组B组
选手ID10
选手分组B组
选手ID07
选手分组B组
选手ID12
选手分组B组

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 9f07e35e28c0a6c4388ce36936d38ecd
guid: 5d311f9d1f531c840a317247115c4119
DefaultImporter:
externalObjects: {}
userData: