267 lines
8.7 KiB
C#
267 lines
8.7 KiB
C#
using asap.core;
|
||
using cfg;
|
||
using game;
|
||
using GameCore;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using UniRx;
|
||
using UnityEngine;
|
||
/// <summary>
|
||
/// 这是一个处理幸运任务数据的工具类,只有一个监听事件的状态
|
||
/// </summary>
|
||
public class LuckyTaskData
|
||
{
|
||
ILuckyTaskGetData luckyModel;
|
||
IDisposable disposables;
|
||
Tables tables;
|
||
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 (curTasks[taskId] == taskParam || curTasks[taskId] == -1)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
curTasks[taskId] += conditionTypeEvent.count;
|
||
if (curTasks[taskId] >= taskParam)
|
||
{
|
||
curTasks[taskId] = taskParam;
|
||
}
|
||
|
||
isUpdata = true;
|
||
}
|
||
}
|
||
if (isUpdata)
|
||
{
|
||
luckyModel.SaveTaskData();
|
||
SetRedPoint();
|
||
}
|
||
}
|
||
|
||
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<LuckyTaskPanel>();
|
||
if (panel != null)
|
||
{
|
||
panel.Init(dicTasks);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 为了各个数据类不再重复写这些代码
|
||
/// 初始化任务数据 , 将任务字典清空并根据传入的任务轮次列表重新填充任务ID,初始进度为0
|
||
/// </summary>
|
||
/// <param name="taskRound"></param>
|
||
public void InitDicTasks(List<int> taskRound)
|
||
{
|
||
if (taskRound != null)
|
||
{
|
||
var tasks = tables.TbCommonTask;
|
||
var curTasks = luckyModel.DicTasks;
|
||
int round = curTasks.GetValueOrDefault(-1, 0);
|
||
int level = curTasks.GetValueOrDefault(-2, 0);
|
||
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;
|
||
}
|
||
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; // 保留轮次信息
|
||
luckyModel.SaveTaskData();
|
||
//SetRedPoint();
|
||
RedPointManager.Instance.SetRedPointState(luckyModel.LuckyTaskRedPointKey, true);
|
||
}
|
||
}
|
||
public void SetRedPoint()
|
||
{
|
||
var tasks = tables.TbCommonTask;
|
||
var curTasks = luckyModel.DicTasks;
|
||
//每日任务
|
||
int count = 0;
|
||
int level = curTasks.GetValueOrDefault(-2, 0);
|
||
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;
|
||
}
|
||
if (item.Value == taskParam)
|
||
{
|
||
count++;
|
||
}
|
||
}
|
||
RedPointManager.Instance.SetRedPointState(luckyModel.LuckyTaskRedPointKey, count > 0, count);
|
||
}
|
||
/// <summary>
|
||
/// 为了各个数据类不再重复写这些代码
|
||
/// 领取任务奖励处理任务数据,将任务状态标记为完成 -1,全部完成后重新初始化任务数据
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <param name="taskRound2"></param>
|
||
/// <returns></returns>
|
||
public bool OnGetTaskReward(int id, List<int> taskRound2)
|
||
{
|
||
if (luckyModel.DicTasks.ContainsKey(id))
|
||
{
|
||
luckyModel.DicTasks[id] = -1;
|
||
luckyModel.SaveTaskData();
|
||
SetRedPoint();
|
||
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>
|
||
/// 给 LuckyTaskData 使用的接口
|
||
/// </summary>
|
||
|
||
public interface ILuckyTaskGetData
|
||
{
|
||
/// <summary>
|
||
/// 任务进度数据 key:任务ID value:任务进度 -1表示已完成
|
||
/// key=-1 value=轮次
|
||
/// key=-2 value=付费等级
|
||
/// </summary>
|
||
public Dictionary<int, int> DicTasks { get; set; }
|
||
public string TaskPanelName { get; }
|
||
public string LuckyTaskRedPointKey { get; }//luckgame "eventluckmagic.entrance"
|
||
public void SaveTaskData();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 任务面板获取数据接口
|
||
/// </summary>
|
||
public interface ILuckyTaskUIGetData
|
||
{
|
||
/// <summary>
|
||
/// 任务进度数据
|
||
/// </summary>
|
||
public Dictionary<int, int> DicTasks { get; }
|
||
/// <summary>
|
||
/// 领取奖励,标记任务为完成状态 -1
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <returns></returns>
|
||
public bool OnGetTaskReward(int id);
|
||
/// <summary>
|
||
/// 获取任务面板名称
|
||
/// </summary>
|
||
public string TaskPanelName { get; }
|
||
/// <summary>
|
||
/// 获取幸运无限购买礼包ID
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public int GetLuckyPackID();
|
||
public int EventId { get; }
|
||
}
|
||
|
||
|