CTT/Server/Hotfix/Game/System/Bag/StarSoulBagSystem.cs

473 lines
16 KiB
C#
Raw Normal View History

2021-05-01 22:06:12 +08:00
using System;
2021-05-02 23:18:14 +08:00
using System.Collections;
2021-05-01 22:06:12 +08:00
using System.Collections.Generic;
2021-05-05 13:36:19 +08:00
using Cal;
2021-05-02 23:18:14 +08:00
using Cal.DataTable;
2021-05-01 22:06:12 +08:00
namespace ET
{
public class StarSoulBagAwakeSystem: AwakeSystem<StarSoulBag>
{
public override void Awake(StarSoulBag self)
{
self.InitData();
2021-05-05 13:36:19 +08:00
self.getAttributeFunc = self.GetAttribute;
2021-05-01 22:06:12 +08:00
}
}
public class StarSoulBagDeserializeSystem: DeserializeSystem<StarSoulBag>
{
public override void Deserialize(StarSoulBag self)
{
2021-05-05 13:36:19 +08:00
self.getAttributeFunc = self.GetAttribute;
if(self.usedStarSoulDic.Count==0)
self.InitData();
2021-05-01 22:06:12 +08:00
}
}
public class StarSoulBagDestroySystem: DestroySystem<StarSoulBag>
{
public override void Destroy(StarSoulBag self)
{
self.ItemCount = 0;
self.itemDic.Clear();
2021-05-05 13:36:19 +08:00
self.Suits.Clear();
self.typeStarSoulCount.Clear();
self.usedStarSoulDic.Clear();
self.getAttributeFunc = null;
2021-05-01 22:06:12 +08:00
}
}
public static class StarSoulBagSystem
{
public static void InitData(this StarSoulBag self)
{
2021-05-05 13:36:19 +08:00
for (int i = 0; i < 12; i++)
{
self.usedStarSoulDic[(byte) i] = 0;
}
2021-05-02 01:19:35 +08:00
#if UNITY
2021-05-01 22:06:12 +08:00
GetDataFromServer(self);
#endif
}
2021-05-05 13:36:19 +08:00
public static float GetAttribute(this StarSoulBag self, AttributeType attributeType)
{
var bound = 0f;
try
{
foreach (var kv in self.usedStarSoulDic)
{
long starSoulId = kv.Value;
if (starSoulId != 0)
{
if(starSoulId==0)
return bound;
StarSoulBag starSoulBag = self;
StarSoulItem item = starSoulBag.Get(starSoulId);
if (item == null)
{
return bound;
}
StarSoulAttributeConfig soulAttributeConfig = StarSoulAttributeConfigCategory.Instance.Get(item.mainId);
if (soulAttributeConfig.Key == (int) attributeType)
bound += ItemHelper.GetRealMainValue(soulAttributeConfig.Value,item.level);
for (var i = 0; i < item.viceIds.Length; i++)
{
if(item.viceIds[i]==0)
break;
soulAttributeConfig = StarSoulAttributeConfigCategory.Instance.Get(item.viceIds[i]);
if (soulAttributeConfig.Key == (int) attributeType)
bound += ItemHelper.GetRealViceValue(soulAttributeConfig.Value*(1+item.viceAdd[i]),item.quality);
}
}
}
}
catch (Exception e)
{
Log.Error(e);
}
return bound;
}
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.TryAdd(item.Id, item))
{
Log.Error($"{self.Id.GetPlayerFormatName()} serverId 重复 {item.Id}");
return "系统错误";
}
self.ItemCount++;
2021-05-02 01:19:35 +08:00
self.Sync(item.Id, item);
2021-05-01 22:06:12 +08:00
return null;
}
public static bool Remove(this StarSoulBag self, long Id)
{
2021-05-02 01:19:35 +08:00
if (!self.itemDic.Remove(Id))
2021-05-01 22:06:12 +08:00
{
return false;
}
self.ItemCount--;
2021-05-02 01:19:35 +08:00
self.Sync(Id, null);
2021-05-01 22:06:12 +08:00
return true;
}
public static StarSoulItem Get(this StarSoulBag self, long Id)
{
if (!self.itemDic.TryGetValue(Id, out var itemBase))
{
return null;
}
2021-05-02 01:19:35 +08:00
2021-05-01 22:06:12 +08:00
return itemBase;
}
2021-05-02 01:19:35 +08:00
2021-05-14 15:28:23 +08:00
/// <summary>
/// 同步已经使用的星魂
/// </summary>
/// <param name="self"></param>
/// <param name="key"></param>
2021-05-05 13:36:19 +08:00
private static void Sync(this StarSoulBag self,byte key)
{
self.usedStarSoulDic.TryGetValue(key, out long value);
M2C_SyncStarSoulBagItemUsed proto = new();
proto.key = key;
proto.value = value;
foreach (StarSoulSuit starSoulSuit in self.Suits)
{
proto.suitKVs.Add(new KV_int32_int32()
{
key = starSoulSuit.Id,
value = (int) starSoulSuit.type
});
}
MessageHelper.SendActor(self.GetParent<Unit>(), proto);
}
2021-05-14 15:28:23 +08:00
/// <summary>
/// 同步背包
/// </summary>
/// <param name="self"></param>
/// <param name="Id"></param>
/// <param name="item"></param>
2021-05-02 01:19:35 +08:00
private static void Sync(this StarSoulBag self, long Id, StarSoulItem item)
{
M2C_SyncStarSoulBag proto = new();
2021-05-14 15:28:23 +08:00
StarSoulNetItem netItem = new();
2021-05-02 01:19:35 +08:00
if (item == null)
{
netItem.Id = 0;
}
2021-05-02 23:18:14 +08:00
else
2021-05-02 01:19:35 +08:00
{
2021-05-02 23:18:14 +08:00
netItem.Id = item.Id;
netItem.level = item.level;
netItem.exp = item.exp;
netItem.quality = (int) item.quality;
netItem.posType = (int) item.posType;
netItem.typeId = item.typeId;
netItem.isUsed = item.isUsed;
netItem.main = item.mainId;
2021-05-05 13:36:19 +08:00
netItem.isLocked = item.isLocked;
2021-05-02 23:18:14 +08:00
foreach (int id in item.viceIds)
{
netItem.vice.Add(id);
}
foreach (float id in item.viceAdd)
{
netItem.viceAdd.Add(id);
}
2021-05-02 01:19:35 +08:00
}
proto.Id = Id;
proto.item = netItem;
MessageHelper.SendActor(self.GetParent<Unit>(), proto);
}
2021-05-02 23:18:14 +08:00
2021-05-14 15:28:23 +08:00
public static void GetBagInfo(this StarSoulBag self, List<StarSoulNetItem> itemList, List<long> usedIdMap, List<KV_int32_int32> suitKVs)
{
foreach (var kv in self.itemDic)
{
try
{
StarSoulNetItem netItem = new();
StarSoulItem item = kv.Value;
netItem.Id = item.Id;
netItem.level = item.level;
netItem.exp = item.exp;
netItem.quality = (int) item.quality;
netItem.posType = (int) item.posType;
netItem.typeId = item.typeId;
netItem.isUsed = item.isUsed;
netItem.main = item.mainId;
netItem.isLocked = item.isLocked;
foreach (int id in item.viceIds)
{
netItem.vice.Add(id);
}foreach (float id in item.viceAdd)
{
netItem.viceAdd.Add(id);
}
itemList.Add(netItem);
}
catch (Exception e)
{
Log.Error(e);
}
}
usedIdMap.AddRange(self.usedStarSoulDic.Values);
foreach (StarSoulSuit starSoulSuit in self.Suits)
{
suitKVs.Add(new KV_int32_int32()
{
key = starSoulSuit.Id,
value = (int) starSoulSuit.type
});
}
}
2021-05-02 23:18:14 +08:00
private static string AddExp(this StarSoulBag self, StarSoulItem item, List<(int, int)> expList, ref int needCount)
{
if (item == null)
{
Log.Error($"{self.Id.GetPlayerFormatName()} item == null");
return "系统错误";
}
byte nextLevel = (byte) (item.level + 1);
if (nextLevel > 20) return "已经达到最大等级";
needCount = 0;
int totalExp = item.exp;
int addLevel = 0;
do
{
if (item.level + addLevel >= 20) break;
2021-05-05 13:36:19 +08:00
nextLevel = (byte) (item.level + addLevel + 1);
2021-05-02 23:18:14 +08:00
StarSoulLevelConfig soulLevelConfig = StarSoulLevelConfigCategory.Instance.GetByQualityAndLevel(item.quality, nextLevel);
int needExp = soulLevelConfig.NeedExp;
var (exp, needGold) = expList[0];
2021-05-05 13:36:19 +08:00
var payRet = CharacterHelper.ReduceMoney(self.Parent, CharacterHelper.MoneyType.Coin, needGold * 100_00);
if (payRet != null)
{
AddLevel(self, item, addLevel, totalExp);
return payRet;
}
2021-05-02 23:18:14 +08:00
totalExp += exp;
expList.RemoveAt(0);
needCount++;
if (totalExp >= needExp)
{
totalExp -= needExp;
addLevel++;
}
}
while (expList.Count > 0);
AddLevel(self, item, addLevel, totalExp);
return null;
static void AddLevel(StarSoulBag self, StarSoulItem item, int addLevel, int totalExp)
{
byte old = item.level;
item.level = (byte) (item.level + addLevel);
AddAttribute(item, old, 4);
2021-05-05 13:36:19 +08:00
item.exp = totalExp;
2021-05-02 23:18:14 +08:00
self.Sync(item.Id, item);
}
}
private static void AddAttribute(StarSoulItem item, byte old, int perLevel)
{
int addAttibuteCount = CharacterHelper.GetCharacterPoint(old, item.level, 4);
if (addAttibuteCount <= 0) return;
int attributeCount = 0;
foreach (int itemViceId in item.viceIds)
{
if (itemViceId != 0)
attributeCount++;
}
do
{
if (attributeCount < 4)
{
if (MathHelper.IsHit(0.1f))
{
//升级旧的
ItemHelper.UpgradeStarSoulItemVice(item, attributeCount);
}
else
{
//添加新的
ItemHelper.AddStarSoulItemVice(item);
attributeCount++;
}
}
else if (attributeCount == 4)
{
//升级旧的
ItemHelper.UpgradeStarSoulItemVice(item, attributeCount);
}
addAttibuteCount--;
}
while (addAttibuteCount > 0);
}
public static string Upgrade(this StarSoulBag self, long id, List<long> deleteIds)
{
int count = deleteIds.Count;
if (count == 0) return "系统错误";
if (count > 20) return "最多选择20个";
using var list = ListComponent<(int, int)>.Create();
foreach (long deleteId in deleteIds)
{
StarSoulItem item = self.Get(deleteId);
if (item == null)
{
Log.Error($"{self.Id.GetPlayerFormatName()} item == null where id = {deleteId}");
continue;
}
StarSoulLevelConfig soulLevelConfig = StarSoulLevelConfigCategory.Instance.GetByQualityAndLevel(item.quality, item.level);
list.List.Add((soulLevelConfig.Exp, soulLevelConfig.NeedCoin));
}
int needCount = count;
try
{
var ret = self.AddExp(self.Get(id), list.List, ref needCount);
if (ret != null)
return ret;
}
catch (Exception e)
{
Log.Error(e);
}
for (int i = 0; i < needCount; i++)
{
long deleteId = deleteIds[i];
StarSoulItem item = self.Get(deleteId);
if (item == null)
{
Log.Error($"{self.Id.GetPlayerFormatName()} item == null where id = {deleteId}");
continue;
}
self.Remove(deleteId);
}
return null;
}
public static string PutonItem(this StarSoulBag self, long id)
{
var item = self.Get(id);
if (item == null)
{
Log.Error($"{self.Id.GetPlayerFormatName()} 没有星魂 id={id}");
return "系统错误";
}
2021-05-05 13:36:19 +08:00
byte key = (byte) item.posType;
2021-05-02 23:18:14 +08:00
if (item.isUsed)
{
2021-05-05 13:36:19 +08:00
self.usedStarSoulDic.TryGetValue(key, out var oldId);
if (oldId == 0)
2021-05-02 23:18:14 +08:00
{
2021-05-05 13:36:19 +08:00
Log.Error($"{self.Id.GetPlayerFormatName()} {item}");
item.isUsed = false;
self.Sync(item.Id, item);
return "未找到星魂";
}
self.usedStarSoulDic[key] = 0;
self.typeStarSoulCount.TryGetValue(item.typeId, out int count);
if (count > 0)
self.typeStarSoulCount[item.typeId] = --count;
else
{
Log.Error($"{self.Id.GetPlayerFormatName()} {item}");
2021-05-02 23:18:14 +08:00
}
item.isUsed = false;
}
2021-05-05 13:36:19 +08:00
//放置
2021-05-02 23:18:14 +08:00
else
{
2021-05-05 13:36:19 +08:00
int count = 0;
self.usedStarSoulDic.TryGetValue(key, out var oldId);
self.usedStarSoulDic[key] = item.Id;
if (oldId != 0)
2021-05-02 23:18:14 +08:00
{
2021-05-05 13:36:19 +08:00
StarSoulItem oldItem = self.Get(oldId);
if (oldItem != null)
{
self.typeStarSoulCount.TryGetValue(oldItem.typeId, out count);
if (count > 0)
self.typeStarSoulCount[oldItem.typeId] = --count;
else
{
Log.Error($"{self.Id.GetPlayerFormatName()} {item}");
}
oldItem.isUsed = false;
self.Sync(oldItem.Id, oldItem);
}
2021-05-02 23:18:14 +08:00
}
2021-05-05 13:36:19 +08:00
self.typeStarSoulCount.TryGetValue(item.typeId, out count);
self.typeStarSoulCount[item.typeId] = ++count;
2021-05-02 23:18:14 +08:00
item.isUsed = true;
}
2021-05-03 01:54:31 +08:00
2021-05-05 13:36:19 +08:00
CheckSuit(self);
2021-05-03 01:54:31 +08:00
CharacterHelper.SyncNumeric(self.Parent);
2021-05-05 13:36:19 +08:00
self.Sync(key);
self.Sync(item.Id, item);
return null;
}
private static void CheckSuit(this StarSoulBag self)
{
self.Suits.Clear();
foreach (var kv in self.typeStarSoulCount)
2021-05-03 01:54:31 +08:00
{
2021-05-05 13:36:19 +08:00
if (kv.Value >= 8)
{
AddSuit(self, kv.Key, StarSoulSuit.StarSoulSuitType.Suit4And8);
}
else if (kv.Value >= 4)
{
AddSuit(self, kv.Key, StarSoulSuit.StarSoulSuitType.Suit4);
}
}
static void AddSuit(StarSoulBag self, int id, StarSoulSuit.StarSoulSuitType type)
{
StarSoulSuit suit = new() { Id = id, type = type };
self.Suits.Add(suit);
}
}
public static void Lock(this StarSoulBag self, long id, bool messageIsLock)
{
var item = self.Get(id);
if (item != null)
{
item.isLocked = messageIsLock;
self.Sync(item.Id, item);
2021-05-03 01:54:31 +08:00
}
2021-05-02 23:18:14 +08:00
}
2021-05-01 22:06:12 +08:00
}
}