CTT/Unity/Assets/Hotfix/Logic/Behaviour/Game/System/StarSoulBagSystem.cs

154 lines
4.5 KiB
C#
Raw Normal View History

2021-05-01 22:06:12 +08:00
using System;
using System.Collections.Generic;
using Cal;
using ET.EventType;
using UnityEngine;
namespace ET
{
public class StarSoulBagAwakeSystem: AwakeSystem<StarSoulBag>
{
public override void Awake(StarSoulBag self)
{
}
}
public class StarSoulBagDeserializeSystem: DeserializeSystem<StarSoulBag>
{
public override void Deserialize(StarSoulBag self)
{
}
}
public class StarSoulBagDestroySystem: DestroySystem<StarSoulBag>
{
public override void Destroy(StarSoulBag self)
{
self.ItemCount = 0;
self.itemDic.Clear();
}
}
public static class StarSoulBagSystem
{
public static void InitData(this StarSoulBag self)
{
2021-05-14 15:28:23 +08:00
#if UNITY
2021-05-01 22:06:12 +08:00
GetDataFromServer(self).Coroutine();
#endif
}
private static async ETVoid GetDataFromServer(StarSoulBag self)
{
var ret = await self.ZoneScene().GetComponent<SessionComponent>().Call<M2C_GetStarSoulBag>(new C2M_GetStarSoulBag());
if (!ret.Message.IsNullOrEmpty())
{
Game.EventSystem.Publish(new ShowTipUI() { tip = ret.Message, tipType = TipType.Single, isClearIpt = true, });
return;
}
2021-05-14 15:28:23 +08:00
self.FillData(ret.itemList, ret.usedIdMap, ret.suitKVs);
}
public static void FillData(this StarSoulBag self, List<StarSoulNetItem> itemList, List<long> usedIdMap, List<KV_int32_int32> suitKVs)
{
for (var i = 0; i < usedIdMap.Count; i++)
2021-05-05 13:36:19 +08:00
{
2021-05-14 15:28:23 +08:00
self.usedStarSoulDic[(byte) i] = usedIdMap[i];
2021-05-05 13:36:19 +08:00
}
2021-05-14 15:28:23 +08:00
2021-05-05 13:36:19 +08:00
self.Suits.Clear();
2021-05-14 15:28:23 +08:00
foreach (KV_int32_int32 kv in suitKVs)
2021-05-05 13:36:19 +08:00
{
2021-05-14 15:28:23 +08:00
self.Suits.Add(new StarSoulSuit() { Id = kv.key, type = (StarSoulSuit.StarSoulSuitType) kv.value });
2021-05-05 13:36:19 +08:00
}
2021-05-14 15:28:23 +08:00
foreach (StarSoulNetItem starSoulNetItem in itemList)
2021-05-01 22:06:12 +08:00
{
try
{
var item = new StarSoulItem()
{
Id = starSoulNetItem.Id,
isUsed = starSoulNetItem.isUsed,
posType = (EquipType) starSoulNetItem.posType,
quality = (Quality) starSoulNetItem.quality,
typeId = starSoulNetItem.typeId,
level = (byte) starSoulNetItem.level,
exp = starSoulNetItem.exp,
2021-05-05 13:36:19 +08:00
isLocked = starSoulNetItem.isLocked,
mainAttribute = starSoulNetItem.main,
2021-05-01 22:06:12 +08:00
};
for (var i = 0; i < starSoulNetItem.vice.Count; i++)
{
item.viceAttribute[i] = starSoulNetItem.vice[i];
2021-05-14 15:28:23 +08:00
}
2021-05-05 13:36:19 +08:00
for (var i = 0; i < starSoulNetItem.viceAdd.Count; i++)
2021-05-01 22:06:12 +08:00
{
item.viceAdd[i] = starSoulNetItem.viceAdd[i];
}
2021-05-14 15:28:23 +08:00
2021-05-01 22:06:12 +08:00
self.Add(item);
}
catch (Exception e)
{
2021-05-14 15:28:23 +08:00
Log.Error(e);
2021-05-01 22:06:12 +08:00
}
}
}
public static bool CanAdd(this StarSoulBag self)
{
return self.ItemCount < StarSoulBag.MaxCount;
}
public static string Add(this StarSoulBag self, StarSoulItem item)
{
if (!self.CanAdd()) return "星魂背包已满";
if (self.itemDic.ContainsKey(item.Id))
{
2021-05-14 15:28:23 +08:00
#if SERVER
2021-05-01 22:06:12 +08:00
Log.Error($"{self.Id.GetPlayerFormatName()} serverId 重复 {item.Id}");
#endif
return "系统错误";
}
self.itemDic.Add(item.Id, item);
self.ItemCount++;
return null;
}
2021-05-14 15:28:23 +08:00
public static string Update(this StarSoulBag self, StarSoulItem item)
2021-05-02 01:19:35 +08:00
{
if (!self.itemDic.ContainsKey(item.Id))
{
2021-05-14 15:28:23 +08:00
return Add(self, item);
2021-05-02 01:19:35 +08:00
}
2021-05-14 15:28:23 +08:00
2021-05-02 01:19:35 +08:00
self.itemDic[item.Id] = item;
return null;
}
2021-05-01 22:06:12 +08:00
public static bool Remove(this StarSoulBag self, long Id)
{
2021-05-14 15:28:23 +08:00
if (!self.itemDic.Remove(Id))
2021-05-01 22:06:12 +08:00
{
return false;
}
self.ItemCount--;
return true;
}
public static StarSoulItem Get(this StarSoulBag self, long Id)
{
if (!self.itemDic.TryGetValue(Id, out var itemBase))
{
return null;
}
2021-05-14 15:28:23 +08:00
2021-05-01 22:06:12 +08:00
return itemBase;
}
}
}