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

299 lines
9.5 KiB
C#

using asap.core;
using cfg;
using GameCore;
using System;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 同一系列的活动使用同一个Model
/// 礼包、代币、进度奖励等数据结构都相同
/// 后续活动只用关系自己的玩法数据即可
/// </summary>
public class EventLuckyGameModel : ILuckyTaskGetData, ILuckyTaskUIGetData
{
public static EventLuckyGameModel Get()
{
var model = GContext.container.Resolve<EventLuckyGameModel>();
if (model == null)
{
GContext.container.Register<EventLuckyGameModel>().AsSingleton();
model = GContext.container.Resolve<EventLuckyGameModel>();
}
return model;
}
public const string Key = "EventLuckGameRecord";
public const string PackRedPointKey = "luckgame.pack";
public const string RedPointKey = "luckgame";
EventLuckGameRecord eventLuckGameRecord = new EventLuckGameRecord();
FishingEventData fishingEventData;
Tables tables;
public FishingEvent fishingEvent { get; private set; }
public EventLuckGameInfo eventInfo { get; private set; } = new EventLuckGameInfo();
public FishingEventCycleItem3 cycle { get; private set; }
public int EventId => eventLuckGameRecord.EventId;
public int Progress => eventLuckGameRecord.Progress;
public string EventGameData => eventLuckGameRecord.EventGameData;
public TimeSpan RemainingTime
{
get
{
if (fishingEvent == null || fishingEvent.TimeDefinition is not LimitedTime limitedTime)
{
return TimeSpan.Zero;
}
return System.DateTime.Parse(limitedTime.EndTime) - ZZTimeHelper.UtcNow();
}
}
public EventLuckyGameModel(FishingEventData fishingEventData, Tables tables)
{
this.fishingEventData = fishingEventData ?? GContext.container.Resolve<FishingEventData>();
this.tables = tables ?? GContext.container.Resolve<Tables>();
luckyTaskData = new LuckyTaskData(this);
}
public void Save()
{
string s = Newtonsoft.Json.JsonConvert.SerializeObject(eventLuckGameRecord);
//Debug.Log($"[EventLuckGameModel] Save EventLuckGameRecord: {s}");
PlayFabMgr.Instance.UpdateUserDataValue(Key, s);
}
//=========================以下是活动数据=========================
public int TicketCount
{
get
{
if (!IsActive)
{
return 0;
}
//_ticketCount = fishingEventData.GetTransitionDataCount(EventId);
return eventLuckGameRecord.TicketCount;
}
private set
{
if (!IsActive)
{
return;
}
eventLuckGameRecord.TicketCount = value;
if (eventLuckGameRecord.TicketCount < 0)
{
eventLuckGameRecord.TicketCount = 0;
}
fishingEventData.SaveTransitionData(EventId, eventLuckGameRecord.TicketCount);
Save();
RedPointManager.Instance.SetRedPointState(RedPointKey, eventLuckGameRecord.TicketCount >= cycle.RedDot);
}
}
public bool IsActive
{
get
{
if (fishingEvent == null)
{
return false;
}
if (fishingEvent.TimeDefinition is LimitedTime limitedTime)
{
return ZZTimeHelper.UtcNow() < System.DateTime.Parse(limitedTime.EndTime) && ZZTimeHelper.UtcNow() > System.DateTime.Parse(limitedTime.StartTime);
}
return false;
}
}
public void AddTicket(int count)
{
if (fishingEvent == null || cycle == null || !IsActive)
{
Debug.LogError("[EventLuckGameModel] _tableEvent is null, cannot add ticket.");
return;
}
TicketCount += count;
}
#region
public void AddProgress(int progress)
{
if (fishingEvent == null)
{
Debug.LogError("[EventLuckGameModel] _tableEvent is null, cannot add progress.");
return;
}
eventLuckGameRecord.Progress += progress;
if (eventLuckGameRecord.Progress < 0)
{
eventLuckGameRecord.Progress = 0;
}
Save();
}
#endregion
public void OnEvent(FishingEvent t)
{
fishingEvent = t;
if (t == null)
{
return;
}
string s = PlayFabMgr.Instance.GetLocalData(Key);
if (string.IsNullOrEmpty(s))
{
eventLuckGameRecord = new EventLuckGameRecord();
}
else
{
try
{
eventLuckGameRecord = Newtonsoft.Json.JsonConvert.DeserializeObject<EventLuckGameRecord>(s);
}
catch (Exception e)
{
Debug.LogError($"[EventLuckGameModel] Deserialize Error: {e.Message}");
eventLuckGameRecord = new EventLuckGameRecord();
}
}
DicTasks = eventLuckGameRecord.DicTasks;
cycle = tables.TbFishingEventCycleItem3.GetOrDefault(t.RedirectID);
if (cycle == null)
{
Debug.LogError($"[EventLuckGameModel] CycleItem3 not found for RedirectID: {t.RedirectID}");
return;
}
if (eventLuckGameRecord.EventId != t.ID)
{
eventLuckGameRecord.EventId = t.ID;
eventLuckGameRecord.ChainProgress = 0;
eventLuckGameRecord.Progress = 0;
eventLuckGameRecord.EventGameData = string.Empty;
TicketCount = cycle.WelcomeGift;
DicTasks.Clear();
}
SetEventInfo();
}
//=========================以上是活动数据=========================
//=========================以下是玩法数据=========================
public void SetEventGameData(string data)
{
if (fishingEvent == null)
{
Debug.LogError("[EventLuckyGameModel] fishingEvent is null, cannot set event game data.");
return;
}
eventLuckGameRecord.EventGameData = data;
Save();
}
void SetEventInfo()
{
if (fishingEvent == null)
{
Debug.LogError("[EventLuckyGameModel] fishingEvent is null, cannot set event info.");
return;
}
eventInfo = GetEventLuckGameInfo();
if (DicTasks.Count == 0 && eventInfo.TaskRound1 != null && eventInfo.TaskRound1.Count > 0)
{
luckyTaskData.InitDicTasks(eventInfo.TaskRound1);
}
else
{
luckyTaskData.SetRedPoint();
}
RedPointManager.Instance.SetRedPointState(RedPointKey, eventLuckGameRecord.TicketCount >= cycle.RedDot);
}
EventLuckGameInfo GetEventLuckGameInfo()
{
var eventInfo = new EventLuckGameInfo();
if (fishingEvent.SubType == 4)
{
EventLuckyCardsMain _tableMain = tables.TbEventLuckyCardsMain.GetOrDefault(cycle.RedirectID);
if (_tableMain == null)
{
Debug.LogError($"[EventLuckyCardsMain] EventLuckyCardsMain is Null RedirectID {cycle.RedirectID}");
fishingEvent = null;
return null;
}
eventInfo.IconName = _tableMain.Icon;
eventInfo.MainPrefab = _tableMain.Prefab;
eventInfo.TaskPanelName = _tableMain.TaskPrefab;
eventInfo.ItemID = _tableMain.Item;
eventInfo.PackId = _tableMain.PackId;
eventInfo.TaskRound1 = _tableMain.TaskRound1;
eventInfo.TaskRound2 = _tableMain.TaskRound2;
}
return eventInfo;
}
#region
//这是一个工具类,只有一个监听任务触发的状态,处理任务数据
private LuckyTaskData luckyTaskData;
/// 获取任务面板名称,配表
public string TaskPanelName => eventInfo.TaskPanelName;
public string LuckyTaskRedPointKey => "luckgame.luckytask";
//任务数据存储
public Dictionary<int, int> DicTasks { get; set; } = new Dictionary<int, int>();
public void InitDicTasks()
{
luckyTaskData.InitDicTasks(eventInfo.TaskRound2);
}
public void SaveTaskData()
{
eventLuckGameRecord.DicTasks = DicTasks;
Save();
}
public bool OnGetTaskReward(int id)
{
return luckyTaskData.OnGetTaskReward(id, eventInfo.TaskRound2);
}
public int GetLuckyPackID()
{
if (fishingEvent == null || cycle == null || eventInfo == null)
{
Debug.LogError("[EventLuckyGameModel] fishingEvent or cycle is null, cannot get lucky gift info.");
return 0;
}
return eventInfo.PackId;
}
#endregion
//=========================以上是玩法数据=========================
public void Dispose()
{
luckyTaskData.Dispose();
}
}
public class EventLuckGameRecord
{
public int EventId { get; set; }
public int ChainProgress { get; set; }
public int Progress { get; set; }
public int TicketCount { get; set; } = 0;
public string EventGameData { get; set; } = "";
public Dictionary<int, int> DicTasks { get; set; } = new Dictionary<int, int>();
}
public class EventLuckGameInfo
{
public string ActName = "EventLuckyCardsAct";
public string IconName;
public string MainPrefab;
public string TaskPanelName;
public int ItemID;
public int PackId;
public List<int> TaskRound1;
public List<int> TaskRound2;
}