Files
ft/Client/Assets/Scripts/LuckyTask/LuckyTaskData.cs
2026-06-29 21:18:33 +08:00

349 lines
12 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using asap.core;
using cfg;
using GameCore;
using System;
using System.Collections.Generic;
using System.Linq;
using UniRx;
using UnityEngine;
public class TbEventLuckyTask
{
public List<int> taskID = new List<int>();
public List<int> adReward = new List<int>();
}
/// <summary>
/// 这是一个处理幸运任务数据的工具类,只有一个监听事件的状态
/// </summary>
public class LuckyTaskData
{
public const string ADTaskADKey = "LuckyTaskADKey";
ILuckyTaskGetData luckyModel;
IDisposable disposables;
Tables tables;
int _redCount;
public int RedCount
{
get { return _redCount; }
}
List<ConditionType> conditionTypeEvents = new List<ConditionType>();
public LuckyTaskData(ILuckyTaskGetData luckyModel)
{
tables = GContext.container.Resolve<Tables>();
this.luckyModel = luckyModel;
disposables = GContext.OnEvent<ConditionTypeEvent>().Subscribe(UpdateTaskData);
SetRedPoint();
}
void UpdateTaskData(ConditionTypeEvent conditionTypeEvent)
{
if (conditionTypeEvents.Count > 0 && !conditionTypeEvents.Contains(conditionTypeEvent.type))
{
return;
}
//Debug.Log($"[LuckyTaskData] UpdateTaskData {conditionTypeEvent.type} {conditionTypeEvent.count}");
var tasks = tables.TbCommonTask;
var curTasks = luckyModel.DicTasks;
var curTasksId = curTasks.Keys.ToList();
bool isUpdata = false;
int level = curTasks.GetValueOrDefault(-2, 0);
//每日任务
foreach (var taskId in curTasksId)
{
if (taskId <= 0)
{
continue;
}
var task = tasks.GetOrDefault(taskId);
if (task == null)
continue;
if (!conditionTypeEvents.Contains(task.TaskType))
{
conditionTypeEvents.Add(task.TaskType);
}
if (task.TaskType == conditionTypeEvent.type)
{
int taskParam = task.TaskParam[level];
if (task.TaskType == ConditionType.RechargeAmount)
{
var iap = tables.TbIAPItemList.GetOrDefault(taskParam);
taskParam = iap.VIPPoint;
}
if (task.TaskType == ConditionType.ConsumeEnergy)
{
float inflationRate = curTasks.GetValueOrDefault(-3, 0) / 100f;
taskParam = GContext.container.Resolve<PlayerItemData>().IntInflation(taskParam, inflationRate);
}
if (curTasks[taskId] == taskParam || curTasks[taskId] == -1)
{
continue;
}
curTasks[taskId] += conditionTypeEvent.count;
if (curTasks[taskId] >= taskParam)
{
curTasks[taskId] = taskParam;
}
isUpdata = true;
}
}
if (isUpdata)
{
SetRedPoint();
luckyModel.SaveTaskData();
}
}
public async void OpenTaskPanel(Dictionary<int, int> dicTasks)
{
GameObject taskPanelName = await UIManager.Instance.ShowUI(new UIType(luckyModel.TaskPanelName));
if (taskPanelName != null)
{
var panel = taskPanelName.GetComponent<LuckyTaskPanelBase>();
if (panel != null)
{
panel.InitOpen(luckyModel, dicTasks);
}
}
}
public void RefreshTasks()
{
InitDicTasks();
}
public void InitTasks()
{
int inflationRate = (int)(GContext.container.Resolve<PlayerData>().InflationRate * 100);
int group = FishingEventData.Condition_NoRechargeDay() ? 1 : 0;
luckyModel.DicTasks[-3] = inflationRate; // 保留轮次信息
luckyModel.DicTasks[-4] = group; // 保留轮次信息
InitDicTasks();
}
/// <summary>
/// 为了各个数据类不再重复写这些代码
/// 初始化任务数据 将任务字典清空并根据传入的任务轮次列表重新填充任务ID初始进度为0
/// </summary>
void InitDicTasks()
{
var tasks = tables.TbCommonTask;
var curTasks = luckyModel.DicTasks;
int round = curTasks.GetValueOrDefault(-1, 0);
int level = curTasks.GetValueOrDefault(-2, 0);
int inflationRate = curTasks.GetValueOrDefault(-3, 0);
int group = curTasks.GetValueOrDefault(-4, 0);
TbEventLuckyTask eventLuckyTask = GetTbEventLuckyTask(luckyModel.EventId, round + 1, group);
var taskRound = eventLuckyTask.taskID;
if (curTasks.Count > 0 && round > 0)
{
foreach (var item in curTasks)
{
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;
}
else if (commonTask.TaskType == ConditionType.ConsumeEnergy)
{
taskParam = GContext.container.Resolve<PlayerItemData>().IntInflation(taskParam, inflationRate / 100f);
}
if (item.Value >= taskParam)
{
Dictionary<int, int> newDicTasks = new Dictionary<int, int>(curTasks);
OpenTaskPanel(newDicTasks);
break;
}
}
}
}
}
curTasks.Clear();
level = GContext.container.Resolve<PlayerData>().PriceLv;
if (level >= tables.TbCommonTask.DataList[0].TaskParam.Count)
{
level = tables.TbCommonTask.DataList[0].TaskParam.Count - 1;
}
curTasks[-2] = level;
conditionTypeEvents.Clear();
for (int i = 0; i < taskRound.Count; i++)
{
var taskId = taskRound[i];
if (taskId > 0 && !curTasks.ContainsKey(taskId))
{
curTasks[taskId] = 0; // 初始化任务进度为0
var task = tasks.GetOrDefault(taskId);
if (!conditionTypeEvents.Contains(task.TaskType))
{
conditionTypeEvents.Add(task.TaskType);
}
}
}
curTasks[-1] = ++round; // 保留轮次信息
curTasks[-3] = inflationRate; // 保留轮次信息
curTasks[-4] = group; // 保留轮次信息
_redCount = 0;
luckyModel.SaveTaskData();
RedPointManager.Instance.SetRedPointState(luckyModel.LuckyTaskRedPointKey, true);
}
public void SetRedPoint()
{
var tasks = tables.TbCommonTask;
var curTasks = luckyModel.DicTasks;
//每日任务
_redCount = 0;
int level = curTasks.GetValueOrDefault(-2, 0);
float inflationRate = curTasks.GetValueOrDefault(-3, 0) / 100f;
foreach (var item in curTasks)
{
if (item.Key <= 0 || item.Value <= 0)
{
continue;
}
var task = tasks.GetOrDefault(item.Key);
if (task == null)
continue;
int taskParam = task.TaskParam[level];
if (task.TaskType == ConditionType.RechargeAmount)
{
var iap = tables.TbIAPItemList.GetOrDefault(taskParam);
taskParam = iap.VIPPoint;
}
else if (task.TaskType == ConditionType.ConsumeEnergy)
{
taskParam = GContext.container.Resolve<PlayerItemData>().IntInflation(taskParam, inflationRate);
}
if (item.Value == taskParam)
{
_redCount++;
}
}
RedPointManager.Instance.SetRedPointState(luckyModel.LuckyTaskRedPointKey, _redCount > 0, _redCount);
}
/// <summary>
/// 为了各个数据类不再重复写这些代码
/// 领取任务奖励处理任务数据,将任务状态标记为完成 -1全部完成后重新初始化任务数据
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public bool OnGetTaskReward(int id)
{
if (luckyModel.DicTasks.ContainsKey(id))
{
luckyModel.DicTasks[id] = -1;
SetRedPoint();
luckyModel.SaveTaskData();
return true;
}
else
{
Debug.LogError($" The current task list does not include ID : {id} ");
return false;
}
}
public void Dispose()
{
if (disposables != null)
{
disposables.Dispose();
disposables = null;
}
Debug.Log($"[LuckyTaskData] {this.GetType().Name} Dispose");
}
/// <summary>
///
/// </summary>
/// <param name="eventID"></param>
/// <param name="DicTasks">
/// key=1 value=广告组完成ID
/// key=-1 value=轮次
/// key=-2 value=付费等级
/// key=-3 value=体力膨胀系数
/// key=-4 value=普通组还是广告组</param>
/// <returns></returns>
public static TbEventLuckyTask GetTbEventLuckyTask(int eventID, int round, int group)
{
TbEventLuckyTask tbEventLuckyTask = new TbEventLuckyTask();
Tables tables = GContext.container.Resolve<Tables>();
FishingEvent fishingEvent = tables.TbFishingEvent.GetOrDefault(eventID);
if (fishingEvent.Type == 8)
{
if (round > 2)
{
round = 2;
}
switch (fishingEvent.SubType)
{
case 3:
return GetTbEventLuckMagicTask(round, group);
case 4:
return GetTbEventLuckyCardsTask(round, group);
default:
break;
}
}
return tbEventLuckyTask;
}
static TbEventLuckyTask GetTbEventLuckMagicTask(int round, int group)
{
TbEventLuckyTask tbEventLuckyTask = new TbEventLuckyTask();
Tables tables = GContext.container.Resolve<Tables>();
List<EventLuckMagicTask> taskDataList = tables.TbEventLuckMagicTask.DataList;
var taskList = taskDataList.Where(t => t.Round == round && t.Type == group).ToList();
tbEventLuckyTask.taskID = taskList.Where(t => t.CommonTask > 0).Select(t => t.CommonTask).ToList();
tbEventLuckyTask.adReward = taskDataList.Where(t => t.AdReward > 0).Select(t => t.AdReward).ToList();
return tbEventLuckyTask;
}
static TbEventLuckyTask GetTbEventLuckyCardsTask(int round, int group)
{
TbEventLuckyTask tbEventLuckyTask = new TbEventLuckyTask();
Tables tables = GContext.container.Resolve<Tables>();
List<EventLuckyCardsTask> taskDataList = tables.TbEventLuckyCardsTask.DataList;
var taskList = taskDataList.Where(t => t.Round == round && t.Type == group).ToList();
tbEventLuckyTask.taskID = taskList.Where(t => t.CommonTask > 0).Select(t => t.CommonTask).ToList();
tbEventLuckyTask.adReward = taskDataList.Where(t => t.AdReward > 0).Select(t => t.AdReward).ToList();
return tbEventLuckyTask;
}
}
/// <summary>
/// 给 LuckyTaskData 使用的接口
/// </summary>
public interface ILuckyTaskGetData
{
/// <summary>
/// 任务进度数据 key:任务ID value:任务进度 -1表示已完成
/// key=-1 value=轮次
/// key=-2 value=付费等级
/// key=-3 value=体力膨胀系数
/// key=-4 value=普通组还是广告组
/// </summary>
public Dictionary<int, int> DicTasks { get; set; }
public string TaskPanelName { get; }
public string LuckyTaskRedPointKey { get; }//luckgame "eventluckmagic.entrance"
public void SaveTaskData();
public int EventId { get; }
/// <summary>
/// 领取奖励,标记任务为完成状态 -1
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public bool OnGetTaskReward(int id);
/// <summary>
/// 获取幸运无限购买礼包ID
/// </summary>
/// <returns></returns>
public int GetLuckyPackID();
}