Files
back_cantanBuilding/Assets/Scripts/EventGatherCore/EventGatherCoreSystem.cs
2026-05-26 16:15:54 +08:00

100 lines
4.1 KiB
C#

using System.Linq;
using asap.core;
using UnityEngine;
using System.Collections.Generic;
using GameCore;
public static class EventGatherCoreSystem
{
public readonly static int Cost = 1;
public static int PickRandomRewardIdx()
{
var stage = GContext.container.Resolve<EventGatherCoreData>().StageData;
var ctx = EventGatherCoreAct.Ctx;
var rewardIds = stage.RewardIdList;
var weightList = rewardIds
.Select((id, idx) =>
!stage.IsRewardClaimed(idx) &&
!IsDoomed(id) &&
ctx.TryGetRewardWeight(id, out var weight) ? weight : 0)
.ToList();
var idx = FtMathUtils.GetRandomIdxFromWeightList(weightList);
return idx;
}
private static bool IsDoomed(int rewardId)
{
var stage = GContext.container.Resolve<EventGatherCoreData>().StageData;
var ctx = EventGatherCoreAct.Ctx;
return stage.IsEarlyTry(ctx) && stage.IsCloseToFinish(ctx) && ctx.IsCoreItem(rewardId);
}
public static void GrantRewards(int rewardId)
{
var rewardItem = EventGatherCoreAct.Ctx.GetReward(rewardId, out _);
if (rewardItem == null)
return;
if (!EventGatherCoreAct.Ctx.IsCoreItem(rewardId))
GContext.Publish(new DeferredRewardStashService.EventStashItem { Item = rewardItem });
}
public static void GrantTaskReward(int taskDropId, List<int> remainingRewardIds)
{
GContext.Publish(new DeferredRewardStashService.EventStashDrop { DropId = taskDropId});
if (remainingRewardIds != null)
foreach (var rewardId in remainingRewardIds)
GrantRewards(rewardId);
}
public static bool Draw(int triggerIdx, out int rewardIdx, out int rewardId)
{
int evRoundCount, evStageCount, evPlayCount, evRewardId, evRewardCount, evEndOfStage, evCombineId;
rewardId = 0;
rewardIdx = 0;
var data = GContext.container.Resolve<EventGatherCoreData>();
if (data.TicketCount <= 0)
{
Debug.Log("[EventGatherCore]Insufficient tickets");
return false;
}
data.AddTicket(-Cost);
rewardIdx = PickRandomRewardIdx();
data.StageData.SetTriggerUsed(triggerIdx);
data.StageData.SetRewardClaimed(rewardIdx);
var ctx = EventGatherCoreAct.Ctx;
data.Save();
evRoundCount = data.StageCount / ctx.GetTotalStageCount() + 1;
evStageCount = data.StageCount % ctx.GetTotalStageCount() + 1;
evPlayCount = data.StageData.PlayCount;
rewardId = data.StageData.RewardIdList[rewardIdx];
var rewardItem = EventGatherCoreAct.Ctx.GetReward(rewardId, out _);
GrantRewards(rewardId);
evRewardId = rewardItem.id;
evRewardCount = rewardItem.count;
evEndOfStage = data.StageData.IsStageCleared(ctx)? 1 : 0;
evCombineId = evRoundCount * 10000 + evStageCount * 100 + evPlayCount;
// Debug.Log($"<color=red>[EventGatherCore] -------------------Event Tracking---------------------</color>");
// Debug.Log($"[EventGatherCore] round: {evRoundCount}");
// Debug.Log($"[EventGatherCore] stage: {evStageCount}");
// Debug.Log($"[EventGatherCore] count: {evPlayCount}");
// Debug.Log($"[EventGatherCore] item_id: {evRewardId}");
// Debug.Log($"[EventGatherCore] item_count: {evRewardCount}");
// Debug.Log($"[EventGatherCore] is_done: {evEndOfStage}");
// Debug.Log($"[EventGatherCore] combine_id: {evCombineId}");
// Debug.Log($"[EventGatherCore] -------------------End of Event Tracking---------------------");
#if AGG
using (var e = GEvent.GameEvent("event_gathercore"))
{
e.AddContent("round", evRoundCount)
.AddContent("stage", evStageCount)
.AddContent("count", evPlayCount)
.AddContent("item_id", evRewardId)
.AddContent("item_count", evRewardCount)
.AddContent("is_done", evEndOfStage)
.AddContent("combine_id", evCombineId);
}
#endif
return true;
}
}