根据积分来获取奖品,奖品类型有大、中、小三种可兑换奖品。
前提,保证最少有4个玩家,大于4个玩家时最多有一个大奖,其余都是中奖来设计。
注:导入所需的配置文件 链接:http://pan.baidu.com/s/1mh56PDm 密码:6mu0
引入:
using NPOI.HSSF.UserModel;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
public class ReadExcelXsl : MonoBehaviour
{
//单例模式,方便调用
private static ReadExcelXsl _instance;
public static ReadExcelXsl GetInstance
{
get
{
if (_instance == null)
{
_instance = new ReadExcelXsl();
}
return _instance;
}
}
private string currentPath;//Excel文件存放地址
private HSSFWorkbook book;//创建一个worbook,对应一个Excel文件
private ISheet sheet;//对应Excel文件中的sheet
private bool isEnough;//判断兑换码是否足够
void Start()
{
currentPath = System.IO.Directory.GetCurrentDirectory();
CheckExcelFileCount();
}
/// <summary>
///
/// </summary>
/// <param name="type">
/// 文件类型
/// </param>
/// <param name="count">
/// 取得多少个数据
/// </param>
public List<string> GetAwardCode(int type, int count)
{
List<string> tmpArr = new List<string>();
using (
FileStream file = new FileStream(Path.Combine(currentPath, type + ".xls"), FileMode.Open,
FileAccess.ReadWrite))
{
book = new HSSFWorkbook(file);
sheet = book.GetSheetAt(0);//对应到Excel中的第一页sheet
file.Close();
int total = sheet.LastRowNum;//取得第一页Excel表格的行数
//从后往前取数据
for (int i = total; i > total - count; i--)
{
Debug.Log("type:" + type + " | code :" + sheet.GetRow(i).GetCell(0));
tmpArr.Add(sheet.GetRow(i).GetCell(0).ToString());//把取得的兑换码添加到集合中
sheet.RemoveRow(sheet.GetRow(i));//从前往后取依次从Excel表格中删除数据
}
}
Debug.Log("total:" + sheet.LastRowNum);
//删除完数据后再从新写入(相当于保存)
using (
FileStream editFile = new FileStream(Path.Combine(currentPath, type + ".xls"), FileMode.Open,
FileAccess.ReadWrite))
{
book.Write(editFile);
editFile.Close();
}
return tmpArr;//返回数据
}
/// <summary>
/// 启动程序时检查Excel中的数据量
/// </summary>
/// <param name="type">
/// 文件类型
/// </param>
/// <returns></returns>
private int CheckAwardCodeCount(int type)
{
int total = 0;
using (
FileStream file = new FileStream(Path.Combine(currentPath, type + ".xls"), FileMode.Open,
FileAccess.ReadWrite))
{
book = new HSSFWorkbook(file);
sheet = book.GetSheetAt(0);
file.Close();
total = sheet.LastRowNum;
}
return total;
}
//校验兑换码是否少于等于四个(至少要有4个玩家)
public void CheckExcelFileCount()
{
int total = 0;
string str = "";
for (int i = 1; i <= 4; i++)
{
total = CheckAwardCodeCount(i);
Debug.Log("<color=red> Total:" + total + "</color>");
switch (i)
{
case 1:
str = "大奖";
break;
case 2:
str = "中奖";
break;
case 3:
str = "小奖";
break;
default:
break;
}
if (total <= 4)
{
isEnough = true;
Debug.Log(str + "兑换码数量不足,请更新!\n");
}
else
{
Debug.Log("兑换码数量充足!剩余:" + total + "\n");
}
}
if (isEnough)
{
//兑换码足够
}
else
{
//兑换码不足够
}
}
}
注:保证最少有4个玩家,大于4个玩家时最多有一个大奖,其余都是中奖来设计。
//用来存储三种类型奖品
List<string> smallAwardArr = new List<string>();
List<string> bigAwardArr = new List<string>();
List<string> midAwardArr = new List<string>();
private int CurrentPlayerCount = 4;//玩家数量(不少于4个)
/// <summary>
/// 获取兑换码(CurrentPlayerCount>=4)
/// </summary>
void GetRedeemCode()
{
//get 4 small award code.
smallAwardArr = ReadExcelXsl.GetInstance.GetAwardCode(3, 4);
for (int i = CurrentPlayerCount - 1; i >= 0; i--)
{
// PlayerBehavior playerBehavior = ......//取得玩家身上的脚本,把兑换码给到他们
if (i >= CurrentPlayerCount - 4)
{
//playerBehavior.code = smallAwardArr[0];//"small";
smallAwardArr.RemoveAt(0);
Debug.Log("small");
}
else if (CurrentPlayerCount > 4)
{
int midCount = CurrentPlayerCount - 4;
//get one big award code.
bigAwardArr = ReadExcelXsl.GetInstance.GetAwardCode(1, 1);
//get midCount mid award code.
if (midCount > 0)
midAwardArr = ReadExcelXsl.GetInstance.GetAwardCode(2, midCount);
if (i == 0)
{
Debug.Log("big");
//playerBehavior.code = bigAwardArr[0];// "big";
bigAwardArr.RemoveAt(0);
}
else
{
if (midCount > 0)
{
Debug.Log("middle");
// playerBehavior.code = midAwardArr[0];// "middle";
midAwardArr.RemoveAt(0);
}
}
}
}
}