using System;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using UnityEngine.UI;
|
|
|
namespace vnxbqy.UI
|
{
|
public class HolidayWishPoolModel : Model, IBeforePlayerDataInitialize, IPlayerLoginOk, IOpenServerActivity
|
{
|
public event Action<int> onStateUpdate;
|
public const int redPointID = 28002;
|
public Redpoint redPoint = new Redpoint(MainRedDot.RedPoint_HolidayWishes, redPointID);
|
Operation operationType = Operation.HolidayWish;
|
|
//节日祝福 id从101开始和精彩活动区分
|
public const int activityType = (int)OpenServerActivityCenter.ActivityType.AT_JRZF;
|
public const int activityID = 102;
|
|
//瓶子编号对应的int2<祝福值,领取情况>
|
public Dictionary<byte, Int2> bottleDict = new Dictionary<byte, Int2>();
|
public event Action UpdateBottleInfo; // 瓶子信息变更 祝福值和领奖情况
|
public event Action UpdateLegendMsg;
|
public event Action<int> UpdateWishNum;
|
|
public byte bottleNumSelect;
|
public List<WishLegend> wishLegends = new List<WishLegend>();
|
|
public int wishItemID; //祝福需要的物品ID
|
public int wishCount1; // 祝福次数
|
public int wishSingleNeedNum = 1; //单次祝福需要的物品数量
|
public int wishCount2;
|
|
bool IsRequestLegend = false; //首次启动界面请求
|
int viewType = 17;
|
|
PackModel playerPack { get { return ModelCenter.Instance.GetModel<PackModel>(); } }
|
|
public override void Init()
|
{
|
playerPack.refreshItemCountEvent += RefreshItemCountEvent;
|
DTCA003_tagUniversalGameRecInfo.onGetUniversalGameInfo += OnGetUniversalGameInfo;
|
OperationTimeHepler.Instance.operationStartEvent += OperationStartEvent;
|
OperationTimeHepler.Instance.operationEndEvent += OperationEndEvent;
|
OperationTimeHepler.Instance.operationAdvanceEvent += OperationAdvanceEvent;
|
OpenServerActivityCenter.Instance.Register(activityID, this, activityType);
|
|
var funcCfg = FuncConfigConfig.Get("FeastWishCfg");
|
wishItemID = int.Parse(funcCfg.Numerical1);
|
var _jsonDataC = LitJson.JsonMapper.ToObject(funcCfg.Numerical2);
|
if (_jsonDataC.Count > 0)
|
{
|
wishCount1 = int.Parse(_jsonDataC[0].ToString());
|
wishCount2 = int.Parse(_jsonDataC[1].ToString());
|
}
|
}
|
|
|
|
public void OnBeforePlayerDataInitialize()
|
{
|
bottleDict.Clear();
|
IsRequestLegend = false;
|
wishLegends.Clear();
|
}
|
|
public void OnPlayerLoginOk()
|
{
|
}
|
|
public override void UnInit()
|
{
|
playerPack.refreshItemCountEvent -= RefreshItemCountEvent;
|
DTCA003_tagUniversalGameRecInfo.onGetUniversalGameInfo -= OnGetUniversalGameInfo;
|
OperationTimeHepler.Instance.operationStartEvent -= OperationStartEvent;
|
OperationTimeHepler.Instance.operationEndEvent -= OperationEndEvent;
|
OperationTimeHepler.Instance.operationAdvanceEvent -= OperationAdvanceEvent;
|
}
|
|
public bool IsOpen {
|
get {
|
return OperationTimeHepler.Instance.SatisfyOpenCondition(operationType);
|
}
|
}
|
|
public bool priorityOpen {
|
get {
|
return redPoint.state == RedPointState.Simple;
|
}
|
}
|
|
public bool IsAdvance {
|
get {
|
return OperationTimeHepler.Instance.SatisfyAdvanceCondition(operationType);
|
}
|
}
|
|
private void OperationEndEvent(Operation type, int state)
|
{
|
if (type == operationType && state == 0)
|
{
|
if (onStateUpdate != null)
|
{
|
onStateUpdate(activityID);
|
}
|
UpdateRedpoint();
|
}
|
}
|
|
private void OperationAdvanceEvent(Operation type)
|
{
|
if (type == operationType)
|
{
|
if (onStateUpdate != null)
|
{
|
onStateUpdate(activityID);
|
}
|
}
|
}
|
|
private void OperationStartEvent(Operation type, int state)
|
{
|
if (type == operationType && state == 0)
|
{
|
|
if (onStateUpdate != null)
|
{
|
onStateUpdate(activityID);
|
}
|
}
|
}
|
|
//0 可领取 1 已领取
|
public int GetBottleAwardState(byte bottleNum, int index)
|
{
|
|
OperationHolidayWish holiday;
|
OperationTimeHepler.Instance.TryGetOperation(operationType, out holiday);
|
|
int recordIndex = holiday.wishBottles[bottleNumSelect].ChoosePrizeList[index].RecordIndex;
|
if (((int)Math.Pow(2, recordIndex) & bottleDict[bottleNum].y) == 0)
|
{
|
return 0;
|
}
|
|
return 1;
|
}
|
|
void UpdateRedpoint()
|
{
|
OperationHolidayWish holiday;
|
OperationTimeHepler.Instance.TryGetOperation(operationType, out holiday);
|
redPoint.state = RedPointState.None;
|
|
if (holiday == null) return; //封包顺序如果有问题 此处为空
|
|
// 瓶子的祝福值满 和 有祝福值可祈愿的情况 增加红点
|
foreach (var bottle in bottleDict)
|
{
|
var state = GetBottleProcessState(bottle.Key, holiday);
|
if (state >= 1)
|
{
|
redPoint.state = RedPointState.Simple;
|
}
|
}
|
|
if (playerPack.GetItemCountByID(PackType.Item, wishItemID) >= wishCount1)
|
{
|
redPoint.state = RedPointState.Simple;
|
}
|
|
}
|
|
//瓶子进度0到1, -1代表已领完
|
public float GetBottleProcessState(byte bottleNum, OperationHolidayWish holiday)
|
{
|
if (holiday == null) return 0;
|
if (!bottleDict.ContainsKey(bottleNum) || !holiday.wishBottles.ContainsKey(bottleNum)) return 0;
|
|
if (NumberOf1(bottleDict[bottleNum].y) >= holiday.wishBottles[bottleNum].ChooseTimeMax)
|
return -1;
|
|
return bottleDict[bottleNum].x / (float)holiday.wishBottles[bottleNum].NeedWishValue;
|
}
|
|
//遍历每次消除一个1,有多少次就多少个1
|
private int NumberOf1(int n)
|
{
|
int count = 0;
|
while (n != 0)
|
{
|
count++;
|
n &= n - 1;
|
}
|
return count;
|
}
|
|
public void UpdateInfo(HAA44_tagMCFeastWishPlayerInfo netPack)
|
{
|
for (int i = 0; i < netPack.PlayerBottleInfo.Length; i++)
|
{
|
byte bottleNum = netPack.PlayerBottleInfo[i].BottleNum;
|
if (bottleDict.ContainsKey(bottleNum))
|
{
|
bottleDict[bottleNum] = new Int2(netPack.PlayerBottleInfo[i].WishValue, (int)netPack.PlayerBottleInfo[i].ChooseRecord);
|
}
|
else
|
{
|
bottleDict.Add(bottleNum, new Int2(netPack.PlayerBottleInfo[i].WishValue, (int)netPack.PlayerBottleInfo[i].ChooseRecord));
|
}
|
}
|
|
if (UpdateBottleInfo != null)
|
UpdateBottleInfo();
|
|
UpdateRedpoint();
|
}
|
|
public void SendWish(byte wishCount)
|
{
|
CAA11_tagCMFeastWishPoolWish wish = new CAA11_tagCMFeastWishPoolWish();
|
wish.WishCount = wishCount;
|
GameNetSystem.Instance.SendInfo(wish);
|
}
|
|
private void RefreshItemCountEvent(PackType packType, int index, int itemId)
|
{
|
if (itemId != wishItemID) return;
|
UpdateRedpoint();
|
}
|
|
|
//每次启动游戏 只在打开界面做一次请求,后续会收到服务端单条通知
|
public void RequestLegend()
|
{
|
if (IsRequestLegend) return;
|
IsRequestLegend = true;
|
CA001_tagViewUniversalGameRec legend = new CA001_tagViewUniversalGameRec();
|
legend.ViewType = (byte)viewType;
|
GameNetSystem.Instance.SendInfo(legend);
|
}
|
|
public void OnAddWish(HAA45_tagMCFeastWishResult netPack)
|
{
|
if (UpdateWishNum != null)
|
{
|
UpdateWishNum((int)netPack.AddWishValue);
|
}
|
}
|
|
private void OnGetUniversalGameInfo(HA003_tagUniversalGameRecInfo package)
|
{
|
if (!IsRequestLegend)
|
{
|
//界面首次启动后会获取一次总信息
|
return;
|
}
|
|
if (package.Type != viewType) return;
|
for (int i = 0; i < package.Count; i++)
|
{
|
WishLegend msg = new WishLegend()
|
{
|
name = package.UniversalGameRec[i].StrValue1,
|
itemID = (int)package.UniversalGameRec[i].Value1,
|
itemCount = (int)package.UniversalGameRec[i].Value2,
|
source = (int)package.UniversalGameRec[i].Value3,
|
};
|
|
wishLegends.Add(msg);
|
}
|
if (wishLegends.Count > 35)
|
wishLegends.RemoveRange(0, 5);
|
|
if (UpdateLegendMsg != null)
|
UpdateLegendMsg();
|
|
}
|
|
public struct WishLegend
|
{
|
public string name;
|
public int itemID;
|
public int itemCount;
|
public int source;
|
}
|
}
|
}
|