备份CatanBuilding瘦身独立工程
This commit is contained in:
311
Assets/Scripts/LuckyTask/UI/LuckyTaskPanel.cs
Normal file
311
Assets/Scripts/LuckyTask/UI/LuckyTaskPanel.cs
Normal file
@@ -0,0 +1,311 @@
|
||||
using asap.core;
|
||||
using cfg;
|
||||
using game;
|
||||
using GameCore;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class LuckyTaskPanel : MonoBehaviour
|
||||
{
|
||||
ILuckyTaskUIGetData luckyModel;
|
||||
Tables tables;
|
||||
GameObject itemPrefab;
|
||||
CanvasGroup canvasGroup;
|
||||
Transform content;
|
||||
List<LuckyTaskItem> taskItems = new List<LuckyTaskItem>();
|
||||
Dictionary<int, int> taskData;
|
||||
private void Awake()
|
||||
{
|
||||
canvasGroup = GetComponent<CanvasGroup>();
|
||||
itemPrefab = transform.Find("root/ScrollView/Viewport/Content/item1").gameObject;
|
||||
itemPrefab.SetActive(false);
|
||||
content = transform.Find("root/ScrollView/Viewport/Content");
|
||||
}
|
||||
public void Init(ILuckyTaskUIGetData panelCallTaskData)
|
||||
{
|
||||
tables = GContext.container.Resolve<Tables>();
|
||||
this.luckyModel = panelCallTaskData;
|
||||
taskData = luckyModel.DicTasks;
|
||||
SetItem();
|
||||
}
|
||||
public void Init(Dictionary<int, int> dicTasks)
|
||||
{
|
||||
tables = GContext.container.Resolve<Tables>();
|
||||
taskData = dicTasks;
|
||||
SetItem();
|
||||
}
|
||||
void SetItem()
|
||||
{
|
||||
List<LuckyTaskItemData> taskList = new List<LuckyTaskItemData>();
|
||||
int level = taskData.GetValueOrDefault(-2, 0);
|
||||
foreach (var item in taskData)
|
||||
{
|
||||
if (item.Key <= 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
CommonTask commonTask = tables.TbCommonTask.Get(item.Key);
|
||||
if (commonTask != null)
|
||||
{
|
||||
LuckyTaskItemData luckyTaskData = new LuckyTaskItemData();
|
||||
luckyTaskData.id = item.Key;
|
||||
luckyTaskData.reward = commonTask.TaskReward;
|
||||
|
||||
string desc = LocalizationMgr.GetText(commonTask.TaskDesc_l10n_key);
|
||||
string progressText;
|
||||
if (commonTask.TaskType == ConditionType.RechargeAmount)
|
||||
{
|
||||
//sku.ProdPrice
|
||||
int iapid = commonTask.TaskParam[level];
|
||||
var iap = tables.TbIAPItemList.GetOrDefault(iapid);
|
||||
SKUDetailDataEvent sKUDetailDataEvent = new SKUDetailDataEvent(iap);
|
||||
GContext.Publish(sKUDetailDataEvent);
|
||||
desc = desc.SafeFormat(sKUDetailDataEvent.price);
|
||||
float TaskParam = iap.VIPPoint;
|
||||
float value = item.Value;
|
||||
float ratio = value / TaskParam;
|
||||
float ProdPrice = sKUDetailDataEvent.prodPrice;
|
||||
float truncatedPrice = (float)Mathf.Ceil(ratio * ProdPrice * 100) / 100;
|
||||
progressText = $"({truncatedPrice:F2}/{ProdPrice})";
|
||||
if (item.Value == -1)
|
||||
{
|
||||
progressText = $"({ProdPrice}/{ProdPrice})";
|
||||
luckyTaskData.state = -1;
|
||||
}
|
||||
else if (value >= TaskParam)
|
||||
{
|
||||
luckyTaskData.state = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
luckyTaskData.state = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int taskParam = commonTask.TaskParam[level];
|
||||
progressText = $"({item.Value}/{taskParam})";
|
||||
if (item.Value == -1)
|
||||
{
|
||||
progressText = $"({taskParam}/{taskParam})";
|
||||
luckyTaskData.state = -1;
|
||||
}
|
||||
else if (item.Value >= taskParam)
|
||||
{
|
||||
luckyTaskData.state = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
luckyTaskData.state = 0;
|
||||
}
|
||||
desc = desc.SafeFormat(taskParam);
|
||||
}
|
||||
luckyTaskData.desc = desc + progressText;
|
||||
taskList.Add(luckyTaskData);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"Task ID {item.Key} not found in CommonTask table.");
|
||||
}
|
||||
}
|
||||
taskList.Sort((a, b) => b.state.CompareTo(a.state)); // 按state排序
|
||||
for (int i = 0; i < taskList.Count; i++)
|
||||
{
|
||||
LuckyTaskItem luckyTaskItem = Instantiate(itemPrefab, content).AddComponent<LuckyTaskItem>();
|
||||
luckyTaskItem.gameObject.SetActive(true);
|
||||
luckyTaskItem.Init(taskList[i], OnClickLuckyTaskItem);
|
||||
taskItems.Add(luckyTaskItem);
|
||||
}
|
||||
}
|
||||
|
||||
public async void TaskItemPlay(LuckyTaskItem luckyTaskItem)
|
||||
{
|
||||
canvasGroup.blocksRaycasts = false;
|
||||
try
|
||||
{
|
||||
await luckyTaskItem.Play();
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Debug.LogError($"Error during TaskItemPlay: {ex.Message}");
|
||||
}
|
||||
canvasGroup.blocksRaycasts = true;
|
||||
GContext.Publish(new ShowData());
|
||||
}
|
||||
|
||||
void OnClickLuckyTaskItem(LuckyTaskItem luckyTaskItem)
|
||||
{
|
||||
if (luckyModel == null)
|
||||
{
|
||||
//奖励补领
|
||||
OnClickLuckyTaskItem1(luckyTaskItem);
|
||||
return;
|
||||
}
|
||||
if (luckyTaskItem.data.state == 1)
|
||||
{
|
||||
bool result = luckyModel.OnGetTaskReward(luckyTaskItem.data.id);
|
||||
if (result)
|
||||
{
|
||||
CommonTask commonTask = tables.TbCommonTask.GetOrDefault(luckyTaskItem.data.id);
|
||||
if (commonTask != null)
|
||||
{
|
||||
// 任务奖励
|
||||
List<ItemData> rewardItem = GContext.container.Resolve<PlayerItemData>().AddItemByDrop(commonTask.TaskReward, false);
|
||||
if (rewardItem != null)
|
||||
{
|
||||
TaskItemPlay(luckyTaskItem);
|
||||
}
|
||||
}
|
||||
#if AGG
|
||||
using (var e = GEvent.GameEvent("event_lucky_task"))
|
||||
{
|
||||
int round = luckyModel.DicTasks.GetValueOrDefault(-1, 0);
|
||||
e.AddContent("task_id", luckyTaskItem.data.id)
|
||||
.AddContent("round", round)
|
||||
.AddContent("combine_id", round * 100000 + luckyTaskItem.data.id % 10000)
|
||||
.AddContent("task_reward", commonTask.TaskReward);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("任务奖励领取失败");
|
||||
}
|
||||
}
|
||||
else if (luckyTaskItem.data.state == 0)
|
||||
{
|
||||
OnClickGo(luckyTaskItem.data.id);
|
||||
Debug.Log("任务未完成,无法领取奖励");
|
||||
}
|
||||
else if (luckyTaskItem.data.state == -1)
|
||||
{
|
||||
Debug.Log("任务已领取,无法重复领取");
|
||||
}
|
||||
}
|
||||
|
||||
void OnClickLuckyTaskItem1(LuckyTaskItem luckyTaskItem)
|
||||
{
|
||||
if (luckyTaskItem.data.state == 1)
|
||||
{
|
||||
CommonTask commonTask = tables.TbCommonTask.GetOrDefault(luckyTaskItem.data.id);
|
||||
taskData[luckyTaskItem.data.id] = -1; // 标记为已领取
|
||||
if (commonTask != null)
|
||||
{
|
||||
// 任务奖励
|
||||
List<ItemData> rewardItem = GContext.container.Resolve<PlayerItemData>().AddItemByDrop(commonTask.TaskReward, false);
|
||||
if (rewardItem != null)
|
||||
{
|
||||
TaskItemPlay(luckyTaskItem);
|
||||
}
|
||||
}
|
||||
#if AGG
|
||||
using (var e = GEvent.GameEvent("event_lucky_task"))
|
||||
{
|
||||
int round = luckyModel.DicTasks.GetValueOrDefault(-1, 0);
|
||||
e.AddContent("task_id", luckyTaskItem.data.id)
|
||||
.AddContent("round", round)
|
||||
.AddContent("combine_id", round * 100000 + luckyTaskItem.data.id % 10000)
|
||||
.AddContent("task_reward", commonTask.TaskReward);
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
UIManager.Instance.DestroyUI(gameObject.name);
|
||||
}
|
||||
}
|
||||
|
||||
async void GetReward()
|
||||
{
|
||||
List<int> dropID = new List<int>();
|
||||
int level = taskData.GetValueOrDefault(-2, 0);
|
||||
foreach (var item in taskData)
|
||||
{
|
||||
if (item.Key > 0 && item.Value != -1)
|
||||
{
|
||||
CommonTask commonTask = tables.TbCommonTask.Get(item.Key);
|
||||
if (commonTask != null)
|
||||
{
|
||||
int taskParam = commonTask.TaskParam[level];
|
||||
if (commonTask.TaskType == ConditionType.RechargeAmount)
|
||||
{
|
||||
var iap = tables.TbIAPItemList.GetOrDefault(taskParam);
|
||||
taskParam = iap.VIPPoint;
|
||||
}
|
||||
if (item.Value >= taskParam)
|
||||
{
|
||||
dropID.Add(commonTask.TaskReward);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (dropID.Count > 0)
|
||||
{
|
||||
var itemDatas = GContext.container.Resolve<PlayerItemData>().AddItemByDropList(dropID);
|
||||
GContext.Publish(new ShowData());
|
||||
await Awaiters.Seconds(0.5f);
|
||||
ToastPanel.Show(LocalizationMgr.GetText("UI_COMMON_missions_refreshed"));
|
||||
}
|
||||
}
|
||||
|
||||
async void OnClickGo(int id)
|
||||
{
|
||||
await Awaiters.NextFrame;
|
||||
var task = tables.TbCommonTask.Get(id);
|
||||
switch (task.TaskType)
|
||||
{
|
||||
case ConditionType.Complete1v1Count:
|
||||
case ConditionType.ConsumeEnergy:
|
||||
GContext.Publish(new UnloadActToNextAct());
|
||||
break;
|
||||
case ConditionType.CollectFishCardCount:
|
||||
case ConditionType.ConsumeDiamondCount:
|
||||
await UIManager.Instance.ShowUI(UITypes.FishingShopPanel);
|
||||
GContext.Publish(new LackOfResourceConfirmEvent() { state = 0 });
|
||||
break;
|
||||
case ConditionType.MakeInAppPurchase:
|
||||
case ConditionType.RechargeAmount:
|
||||
int packID = luckyModel.GetLuckyPackID();
|
||||
if (packID <= 0)
|
||||
{
|
||||
Debug.LogError("Lucky Pack ID is invalid or not set.");
|
||||
return;
|
||||
}
|
||||
EventPackManager eventPackManager = tables.TbEventPackManager.GetOrDefault(packID);
|
||||
if (eventPackManager == null)
|
||||
{
|
||||
Debug.LogError($"EventPackManager with ID {packID} not found.");
|
||||
return;
|
||||
}
|
||||
|
||||
GameObject go = await UIManager.Instance.ShowUI(new UIType(eventPackManager.Panel));
|
||||
if (go != null)
|
||||
{
|
||||
EventLuckyTriplePackPanel triple = go.GetComponent<EventLuckyTriplePackPanel>();
|
||||
triple.Init(EventLuckyTriplePackData.Create(luckyModel.EventId, packID));
|
||||
}
|
||||
break;
|
||||
}
|
||||
UIManager.Instance.DestroyUI(gameObject.name);
|
||||
}
|
||||
private void OnDisable()
|
||||
{
|
||||
if (luckyModel == null)
|
||||
{
|
||||
GetReward();
|
||||
}
|
||||
}
|
||||
}
|
||||
public class LuckyTaskItemData
|
||||
{
|
||||
public int id;
|
||||
public string desc;
|
||||
public int target;
|
||||
public int reward;
|
||||
public int progress;
|
||||
/// <summary>
|
||||
/// // 0:未完成 1:可领取 -1:已领取
|
||||
/// </summary>
|
||||
public int state;
|
||||
}
|
||||
Reference in New Issue
Block a user