using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
|
using UnityEngine;
|
using LitJson;
|
using System.Collections;
|
|
|
|
public class ShopItemInfo
|
{
|
public int itemId;
|
public int count;
|
|
public ShopItemInfo(int id, int count)
|
{
|
this.itemId = id;
|
this.count = count;
|
}
|
}
|
|
public class StoreModel : GameSystemManager<StoreModel>
|
{
|
public Dictionary<int, List<StoreData>> storeTypeDict { get; private set; } //所有显示商品
|
public Dictionary<int, int> shopRefreshCntDict = new Dictionary<int, int>(); //刷新次数
|
private Dictionary<int, int> shopItemlimitDict = new Dictionary<int, int>(); //已购买次数
|
public event Action RefreshBuyShopLimitEvent;
|
public event Action RefreshShopEvent;
|
|
public StoreFunc selectStoreFuncType = StoreFunc.Normal;
|
|
|
public int buyShopID;
|
|
//配置
|
public int heroSoulRefreshMoneyType;
|
public int heroSoulRefreshMoney;
|
public int heroSoulRefreshFreeCount;
|
|
Dictionary<int, List<int>> freeShopDict = new Dictionary<int, List<int>>();
|
public Dictionary<int, int> shopMoneyTypeDict = new Dictionary<int, int>();
|
|
public override void Init()
|
{
|
RedpointCenter.Instance.AddParent(MainRedDot.MainGuildRedpoint, MainRedDot.Store_REDPOINT);
|
FuncOpen.Instance.OnFuncStateChangeEvent += FuncStateChange;
|
TimeMgr.Instance.OnDayEvent += RefreshDay;
|
GuildManager.Instance.EnterOrQuitGuildEvent += EnterOrQuitGuildEvent;
|
DTC0102_tagCDBPlayer.beforePlayerDataInitializeEventOnRelogin += OnBeforePlayerDataInitializeRelogin;
|
DTC0102_tagCDBPlayer.beforePlayerDataInitializeEvent += OnBeforePlayerDataInitialize;
|
|
ParseFuncConfig();
|
ParseStoreConfig();
|
}
|
|
public override void Release()
|
{
|
FuncOpen.Instance.OnFuncStateChangeEvent -= FuncStateChange;
|
TimeMgr.Instance.OnDayEvent -= RefreshDay;
|
GuildManager.Instance.EnterOrQuitGuildEvent -= EnterOrQuitGuildEvent;
|
DTC0102_tagCDBPlayer.beforePlayerDataInitializeEventOnRelogin -= OnBeforePlayerDataInitializeRelogin;
|
DTC0102_tagCDBPlayer.beforePlayerDataInitializeEvent -= OnBeforePlayerDataInitialize;
|
}
|
|
void OnBeforePlayerDataInitialize()
|
{
|
shopItemlimitDict.Clear();
|
storeTypeDict.Remove((int)StoreFunc.Hero); //由服务器通知
|
selectStoreFuncType = StoreFunc.Normal;
|
}
|
|
void OnBeforePlayerDataInitializeRelogin()
|
{
|
buyItemCheckDict.Clear();
|
|
}
|
|
|
#region 解析本地数据
|
private void ParseFuncConfig()
|
{
|
var config = FuncConfigConfig.Get("StoreHeroSoul");
|
var tmpArr = ConfigParse.GetMultipleStr<int>(config.Numerical2);
|
heroSoulRefreshMoneyType = tmpArr[0];
|
heroSoulRefreshMoney = tmpArr[1];
|
heroSoulRefreshFreeCount = int.Parse(config.Numerical3);
|
}
|
|
|
private void ParseStoreConfig()
|
{
|
freeShopDict.Clear();
|
storeTypeDict = new Dictionary<int, List<StoreData>>();
|
List<StoreConfig> storeConfigs = StoreConfig.GetValues();
|
for (int i = 0; i < storeConfigs.Count; i++)
|
{
|
var config = storeConfigs[i];
|
//UnlockType 2 由服务端通知
|
if (config.ShopType != 0 && config.UnlockType != 2)
|
{
|
//构建商品数据
|
StoreData storeData = new StoreData();
|
storeData.shopId = config.ID;
|
storeData.storeConfig = config;
|
if (!storeTypeDict.ContainsKey(config.ShopType))
|
{
|
List<StoreData> storeDatas = new List<StoreData>();
|
storeDatas.Add(storeData);
|
storeTypeDict.Add(config.ShopType, storeDatas);
|
}
|
else
|
{
|
storeTypeDict[config.ShopType].Add(storeData);
|
}
|
|
//构建免费商品数据
|
if (config.MoneyNum == 0)
|
{
|
if (!freeShopDict.ContainsKey(config.ShopType))
|
{
|
freeShopDict.Add(config.ShopType, new List<int>());
|
}
|
freeShopDict[config.ShopType].Add(config.ID);
|
}
|
}
|
|
//构建商品价格类型
|
if (!shopMoneyTypeDict.ContainsKey(config.ShopType))
|
{
|
shopMoneyTypeDict[config.ShopType] = config.MoneyType;
|
}
|
}
|
|
foreach (var type in storeTypeDict.Keys)
|
{
|
var configs = storeTypeDict[type];
|
configs.Sort(CompareByShopSort);
|
}
|
}
|
|
|
|
public int CompareByShopSort(StoreData start, StoreData end)
|
{
|
int sort1 = start.storeConfig.ShopSort;
|
int sort2 = end.storeConfig.ShopSort;
|
|
return sort1.CompareTo(sort2);
|
}
|
|
public class StoreData
|
{
|
public int shopId;
|
public StoreConfig storeConfig;
|
}
|
#endregion
|
|
#region 获取数据
|
|
|
private bool IsSellOut(StoreData storeData)
|
{
|
int remainNum = 0;
|
if (TryGetIsSellOut(storeData.storeConfig, out remainNum))
|
return true;
|
|
return false;
|
}
|
|
|
public List<StoreData> TryGetStoreDatas(StoreFunc type)
|
{
|
List<StoreData> datas = null;
|
if (storeTypeDict.TryGetValue((int)type, out datas))
|
{
|
return datas;
|
}
|
|
return datas;
|
}
|
|
|
|
|
public StoreData GetStoreData(int shopId)
|
{
|
StoreConfig storeConfig = StoreConfig.Get(shopId);
|
if (storeConfig == null) return null;
|
|
List<StoreData> storeDatas = null;
|
storeTypeDict.TryGetValue(storeConfig.ShopType, out storeDatas);
|
if (storeDatas != null)
|
{
|
for (int i = 0; i < storeDatas.Count; i++)
|
{
|
var storeData = storeDatas[i];
|
if (storeData.shopId == shopId)
|
{
|
return storeData;
|
}
|
}
|
}
|
return null;
|
}
|
|
|
|
|
|
public bool TryGetIsSellOut(StoreConfig storeConfig, out int remainCnt)
|
{
|
int canBuyCnt = storeConfig.LimitCnt;
|
remainCnt = 0;
|
|
//不限购
|
if (canBuyCnt == 0) return false;
|
|
remainCnt = canBuyCnt;
|
var buyCnt = GetShopLimitBuyCount(storeConfig.ID);
|
remainCnt -= buyCnt;
|
|
if (remainCnt <= 0)
|
{
|
return true;
|
}
|
return false;
|
}
|
|
|
|
|
#endregion
|
|
|
|
private void RefreshDay()
|
{
|
UpdateFreeShopRedpoint();
|
}
|
|
void EnterOrQuitGuildEvent(bool isEnter)
|
{
|
UpdateFreeShopRedpoint();
|
}
|
|
|
|
/// <summary>
|
/// 得到物品购买次数
|
/// </summary>
|
/// <param name="buylimit"></param>
|
public void RefreshBuyShopLimitModel(HA802_tagSCShopItemBuyCntInfo buylimit)
|
{
|
int i = 0;
|
for (i = 0; i < buylimit.Count; i++)
|
{
|
shopItemlimitDict[(int)buylimit.BuyCntList[i].ShopID] = (int)buylimit.BuyCntList[i].BuyCnt;
|
}
|
|
UpdateFreeShopRedpoint();
|
if (RefreshBuyShopLimitEvent != null)
|
RefreshBuyShopLimitEvent();
|
}
|
|
// 限购商品已购买次数
|
public int GetShopLimitBuyCount(int shopID)
|
{
|
if (shopItemlimitDict.ContainsKey(shopID))
|
{
|
return shopItemlimitDict[shopID];
|
}
|
return 0;
|
}
|
|
public void UpdateRefreshItemInfo(HA803_tagSCShopRefreshItemInfo netPack)
|
{
|
storeTypeDict.Remove(netPack.ShopType);
|
shopRefreshCntDict[netPack.ShopType] = netPack.RefreshCnt;
|
List<int> resortList = new List<int>();
|
for (int i = 0; i < netPack.ShopIDList.Length; i++)
|
{
|
var config = StoreConfig.Get((int)netPack.ShopIDList[i]);
|
//UnlockType 2 由服务端通知
|
StoreData storeData = new StoreData();
|
storeData.shopId = config.ID;
|
storeData.storeConfig = config;
|
if (!storeTypeDict.ContainsKey(config.ShopType))
|
{
|
List<StoreData> storeDatas = new List<StoreData>();
|
storeDatas.Add(storeData);
|
storeTypeDict.Add(config.ShopType, storeDatas);
|
}
|
else
|
{
|
storeTypeDict[config.ShopType].Add(storeData);
|
}
|
}
|
|
if (storeTypeDict.ContainsKey(netPack.ShopType))
|
{
|
var configs = storeTypeDict[netPack.ShopType];
|
configs.Sort(CompareByShopSort);
|
}
|
|
RefreshShopEvent?.Invoke();
|
}
|
|
|
|
//和GetShopItemlistEx 一样 获取商店物品列表,只是返回类型不同
|
public List<ShopItemInfo> GetShopItemlistByIndex(StoreConfig storeConfig)
|
{
|
if (storeConfig == null) return null;
|
List<ShopItemInfo> shopItemlist = new List<ShopItemInfo>();
|
|
if (storeConfig.ItemID != 0)
|
{
|
ShopItemInfo shopItem = new ShopItemInfo(storeConfig.ID, storeConfig.ItemCnt);
|
shopItemlist.Add(shopItem);
|
}
|
var extraItem = storeConfig.ItemListEx;
|
if (extraItem != null)
|
{
|
for (int i = 0; i < extraItem.Length; i++)
|
{
|
ShopItemInfo shop = new ShopItemInfo(extraItem[i][0], extraItem[i][1]);
|
shopItemlist.Add(shop);
|
}
|
}
|
return shopItemlist;
|
}
|
|
//和GetShopItemlistByIndex 一样 获取商店物品列表,只是返回类型不同
|
public List<int[]> GetShopItemlistEx(StoreConfig storeConfig)
|
{
|
if (storeConfig == null) return null;
|
|
List<int[]> shopItemlist = new List<int[]>();
|
if (storeConfig.ItemID != 0)
|
{
|
shopItemlist.Add(new int[] { storeConfig.ID, storeConfig.ItemCnt});
|
}
|
var extraItem = storeConfig.ItemListEx;
|
for (int i = 0; i < extraItem.Length; i++)
|
{
|
shopItemlist.Add(extraItem[i]);
|
}
|
return shopItemlist;
|
}
|
|
public bool TryGetShopItemInfo(int _id, out List<ShopItemInfo> _shopItems)
|
{
|
_shopItems = GetShopItemlistByIndex(StoreConfig.Get(_id));
|
return _shopItems != null;
|
}
|
|
|
public void SendBuyShopItem(StoreConfig model, int count)
|
{
|
if (UIHelper.CheckMoneyCount(model.MoneyType, model.MoneyNum * count, 2))
|
{
|
CA310_tagCSBuyItem buyShop = new CA310_tagCSBuyItem();
|
buyShop.ShopID = (ushort)model.ID;
|
buyShop.BuyCount = (uint)count;
|
GameNetSystem.Instance.SendInfo(buyShop);
|
}
|
|
}
|
|
//仙玉购买物品的二次确认框,一级货币只有仙玉 默认为仙玉即可
|
Dictionary<int, bool> buyItemCheckDict = new Dictionary<int, bool>(); //记录勾选信息
|
//eventType 二次确认框类型,对应枚举 BuyStoreItemCheckType
|
public void SendBuyShopItemWithPopCheck(StoreConfig model, int count, int eventType = 0)
|
{
|
if (model.MoneyNum == 0)
|
{
|
//免费的
|
SendBuyShopItem(model, count);
|
return;
|
}
|
|
if (buyItemCheckDict.ContainsKey(eventType) && buyItemCheckDict[eventType])
|
{
|
SendBuyShopItem(model, count);
|
return;
|
}
|
|
ConfirmCancel.ToggleConfirmCancel(Language.Get("Mail101"), Language.Get("CostMoney", model.MoneyNum, model.MoneyType),
|
Language.Get("ConfirmCancel102"), (bool isOk, bool isToggle) =>
|
{
|
if (isOk)
|
{
|
SendBuyShopItem(model, count);
|
buyItemCheckDict[eventType] = isToggle;
|
}
|
|
});
|
}
|
|
//花仙玉购买的二次确认框(本次登录)
|
//eventType 二次确认框类型,对应枚举 BuyStoreItemCheckType
|
public void UseMoneyCheck(int money, int moneyType, Action func, int eventType = 0, string tip = "CostMoney", string fullTip = "")
|
{
|
if (money == 0)
|
{
|
//免费的
|
func?.Invoke();
|
return;
|
}
|
|
if (buyItemCheckDict.ContainsKey(eventType) && buyItemCheckDict[eventType])
|
{
|
func?.Invoke();
|
return;
|
}
|
|
ConfirmCancel.ToggleConfirmCancel(Language.Get("Mail101"), fullTip == "" ? Language.Get(tip, money, moneyType) : fullTip,
|
Language.Get("ConfirmCancel102"), (bool isOk, bool isToggle) =>
|
{
|
if (isOk)
|
{
|
func?.Invoke();
|
buyItemCheckDict[eventType] = isToggle;
|
}
|
|
});
|
}
|
|
|
|
private void FuncStateChange(int funcId)
|
{
|
switch ((FuncOpenEnum)funcId)
|
{
|
case FuncOpenEnum.Store:
|
UpdateFreeShopRedpoint();
|
break;
|
}
|
}
|
|
|
|
#region 商城红点
|
Redpoint storeRedpoint = new Redpoint(MainRedDot.RightFuncRedpoint, MainRedDot.Store_REDPOINT);
|
Redpoint normalRedpoint = new Redpoint(MainRedDot.Store_REDPOINT, MainRedDot.Store_REDPOINT * 10 + 1);
|
Redpoint guildRedpoint = new Redpoint(MainRedDot.Store_REDPOINT, MainRedDot.Store_REDPOINT * 10 + 2);
|
|
public void UpdateFreeShopRedpoint()
|
{
|
if (!FuncOpen.Instance.IsFuncOpen((int)FuncOpenEnum.Store)) return;
|
|
normalRedpoint.state = RedPointState.None;
|
guildRedpoint.state = RedPointState.None;
|
|
if (freeShopDict.Count == 0) return;
|
|
if (freeShopDict.ContainsKey((int)StoreFunc.Normal))
|
{
|
var shopList = freeShopDict[(int)StoreFunc.Normal];
|
|
for (int i = 0; i < shopList.Count; i++)
|
{
|
var shopID = shopList[i];
|
var config = StoreConfig.Get(shopID);
|
if (shopItemlimitDict.ContainsKey(shopID))
|
{
|
if (shopItemlimitDict[shopID] < config.LimitCnt)
|
{
|
normalRedpoint.state = RedPointState.Simple;
|
break;
|
}
|
}
|
else
|
{
|
normalRedpoint.state = RedPointState.Simple;
|
break;
|
}
|
}
|
}
|
|
if (PlayerDatas.Instance.fairyData.HasFairy && freeShopDict.ContainsKey((int)StoreFunc.Guild))
|
{
|
var shopList = freeShopDict[(int)StoreFunc.Guild];
|
|
for (int i = 0; i < shopList.Count; i++)
|
{
|
var shopID = shopList[i];
|
var config = StoreConfig.Get(shopID);
|
if (shopItemlimitDict.ContainsKey(shopID))
|
{
|
if (shopItemlimitDict[shopID] < config.LimitCnt)
|
{
|
guildRedpoint.state = RedPointState.Simple;
|
break;
|
}
|
}
|
else
|
{
|
guildRedpoint.state = RedPointState.Simple;
|
break;
|
}
|
}
|
}
|
|
}
|
|
|
|
#endregion
|
|
public void RefreshStore(int shopType)
|
{
|
var pack = new CA232_tagCSRefreshShop();
|
pack.ShopType = (ushort)shopType;
|
GameNetSystem.Instance.SendInfo(pack);
|
}
|
|
//获取商品状态 0可购买 1已售罄 2免费 3未解锁
|
public int GetShopIDState(int shopID)
|
{
|
var config = StoreConfig.Get(shopID);
|
if (config.UnlockType == 1)
|
{
|
if (!PlayerDatas.Instance.fairyData.HasFairy ||
|
PlayerDatas.Instance.fairyData.fairy.FamilyLV < config.UnlockValue)
|
{
|
return 3;
|
}
|
}
|
|
if (config.LimitCnt != 0 && GetShopLimitBuyCount(shopID) >= config.LimitCnt)
|
{
|
return 1;
|
}
|
|
if (config.MoneyNum == 0)
|
{
|
return 2;
|
}
|
|
return 0;
|
}
|
}
|
|
public enum StoreFunc
|
{
|
Normal = 1, //1:坊市
|
Guild = 2, //2:公会
|
Hero = 3, //3:将魂
|
|
}
|
|
|