82 lines
2.3 KiB
C#
82 lines
2.3 KiB
C#
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using cfg.BlacksmithData;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.UI;
|
||
|
using Random = UnityEngine.Random;
|
||
|
|
||
|
public class PrizeDrawItem : MonoBehaviour
|
||
|
{
|
||
|
[SerializeField] private int id;
|
||
|
[SerializeField] private Text name;
|
||
|
[SerializeField] private Text num;
|
||
|
[SerializeField] private List<LotteryPrizesData> _lotteryPrizesDatas;
|
||
|
[SerializeField] private Button _button;
|
||
|
private PrizeDrawData paizeData;
|
||
|
|
||
|
private void OnEnable()
|
||
|
{
|
||
|
_button.onClick.AddListener(ButtonEvent);
|
||
|
}
|
||
|
|
||
|
private void OnDisable()
|
||
|
{
|
||
|
_button.onClick.RemoveListener(ButtonEvent);
|
||
|
}
|
||
|
|
||
|
public void SetData()
|
||
|
{
|
||
|
paizeData = JsonTab.Instance.tables.PrizeDraw.Get(id);
|
||
|
num.text = paizeData.Boxcost.ToString();
|
||
|
_lotteryPrizesDatas = new List<LotteryPrizesData>();
|
||
|
foreach (var prizesData in JsonTab.Instance.tables.LotteryPrizes.DataList)
|
||
|
{
|
||
|
for (int i = 0; i < prizesData.Awardboxid.Count; i++)
|
||
|
{
|
||
|
if (prizesData.Awardboxid[i]==id)
|
||
|
{
|
||
|
_lotteryPrizesDatas.Add(prizesData);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
private void ButtonEvent()
|
||
|
{
|
||
|
if (DataManager.GetItem(paizeData.Boxtype)>=paizeData.Boxcost)
|
||
|
{
|
||
|
int w = _lotteryPrizesDatas.Sum(prizesData => prizesData.AwardProbability);
|
||
|
var dataList = new List<ItemDataNum>();
|
||
|
for (int i = 0; i < paizeData.Boxcost; i++)
|
||
|
{
|
||
|
AddData(w,ref dataList);
|
||
|
}
|
||
|
StorePanel.ins.OpenRewardPanel(dataList);
|
||
|
DataManager.RemoveItem(paizeData.Boxtype,paizeData.Boxcost);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void AddData( int wight,ref List<ItemDataNum> itemDatas)
|
||
|
{
|
||
|
var r = Random.Range(0, wight);
|
||
|
for (int i = 0; i < _lotteryPrizesDatas.Count; i++)
|
||
|
{
|
||
|
r -= _lotteryPrizesDatas[i].AwardProbability;
|
||
|
if (r >= 0) continue;
|
||
|
foreach (var data in itemDatas.Where(data => data.id==_lotteryPrizesDatas[i].AwardID))
|
||
|
{
|
||
|
data.num += 1;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
var itemNum = new ItemDataNum(_lotteryPrizesDatas[i].AwardID, 1);
|
||
|
itemDatas.Add(itemNum);
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
}
|