498 lines
17 KiB
C#
498 lines
17 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using Cal;
|
||
using Cal.DataTable;
|
||
|
||
namespace ET
|
||
{
|
||
public class StarSoulBagAwakeSystem: AwakeSystem<StarSoulBag>
|
||
{
|
||
public override void Awake(StarSoulBag self)
|
||
{
|
||
self.InitData();
|
||
self.getAttributeFunc = self.GetAttribute;
|
||
}
|
||
}
|
||
|
||
public class StarSoulBagDeserializeSystem: DeserializeSystem<StarSoulBag>
|
||
{
|
||
public override void Deserialize(StarSoulBag self)
|
||
{
|
||
self.getAttributeFunc = self.GetAttribute;
|
||
if (self.usedStarSoulDic.Count == 0)
|
||
self.InitData();
|
||
}
|
||
}
|
||
|
||
public class StarSoulBagDestroySystem: DestroySystem<StarSoulBag>
|
||
{
|
||
public override void Destroy(StarSoulBag self)
|
||
{
|
||
self.ItemCount = 0;
|
||
self.itemDic.Clear();
|
||
self.Suits.Clear();
|
||
self.typeStarSoulCount.Clear();
|
||
self.usedStarSoulDic.Clear();
|
||
self.getAttributeFunc = null;
|
||
}
|
||
}
|
||
|
||
public static class StarSoulBagSystem
|
||
{
|
||
public static void InitData(this StarSoulBag self)
|
||
{
|
||
for (int i = 0; i < 12; i++)
|
||
{
|
||
self.usedStarSoulDic[(byte) i] = 0;
|
||
}
|
||
#if UNITY
|
||
GetDataFromServer(self);
|
||
#endif
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
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++;
|
||
self.Sync(item.Id, item);
|
||
return null;
|
||
}
|
||
|
||
public static bool Remove(this StarSoulBag self, long Id)
|
||
{
|
||
if (!self.itemDic.Remove(Id))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
self.ItemCount--;
|
||
self.Sync(Id, null);
|
||
return true;
|
||
}
|
||
|
||
public static StarSoulItem Get(this StarSoulBag self, long Id)
|
||
{
|
||
if (!self.itemDic.TryGetValue(Id, out var itemBase))
|
||
{
|
||
return null;
|
||
}
|
||
|
||
return itemBase;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 同步已经使用的星魂
|
||
/// </summary>
|
||
/// <param name="self"></param>
|
||
/// <param name="key"></param>
|
||
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);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 同步背包
|
||
/// </summary>
|
||
/// <param name="self"></param>
|
||
/// <param name="Id"></param>
|
||
/// <param name="item"></param>
|
||
private static void Sync(this StarSoulBag self, long Id, StarSoulItem item)
|
||
{
|
||
M2C_SyncStarSoulBag proto = new();
|
||
StarSoulNetItem netItem = new();
|
||
if (item == null)
|
||
{
|
||
netItem.Id = 0;
|
||
}
|
||
else
|
||
{
|
||
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);
|
||
}
|
||
}
|
||
|
||
proto.Id = Id;
|
||
proto.item = netItem;
|
||
MessageHelper.SendActor(self.GetParent<Unit>(), proto);
|
||
}
|
||
|
||
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 });
|
||
}
|
||
}
|
||
|
||
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;
|
||
nextLevel = (byte) (item.level + addLevel + 1);
|
||
StarSoulLevelConfig soulLevelConfig = StarSoulLevelConfigCategory.Instance.GetByQualityAndLevel(item.quality, nextLevel);
|
||
int needExp = soulLevelConfig.NeedExp;
|
||
var (exp, needGold) = expList[0];
|
||
var payRet = CharacterHelper.ReduceMoney(self.Parent, CharacterHelper.MoneyType.Coin, needGold * 100_00);
|
||
if (payRet != null)
|
||
{
|
||
AddLevel(self, item, addLevel, totalExp);
|
||
return payRet;
|
||
}
|
||
|
||
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);
|
||
item.exp = totalExp;
|
||
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 "系统错误";
|
||
}
|
||
|
||
byte key = (byte) item.posType;
|
||
if (item.isUsed)
|
||
{
|
||
self.usedStarSoulDic.TryGetValue(key, out var oldId);
|
||
if (oldId == 0)
|
||
{
|
||
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}");
|
||
}
|
||
|
||
item.isUsed = false;
|
||
}
|
||
//放置
|
||
else
|
||
{
|
||
int count = 0;
|
||
self.usedStarSoulDic.TryGetValue(key, out var oldId);
|
||
self.usedStarSoulDic[key] = item.Id;
|
||
if (oldId != 0)
|
||
{
|
||
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);
|
||
}
|
||
}
|
||
|
||
self.typeStarSoulCount.TryGetValue(item.typeId, out count);
|
||
self.typeStarSoulCount[item.typeId] = ++count;
|
||
item.isUsed = true;
|
||
}
|
||
|
||
CheckSuit(self);
|
||
CharacterHelper.SyncNumeric(self.Parent);
|
||
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)
|
||
{
|
||
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);
|
||
}
|
||
}
|
||
|
||
public static string Resolve(this StarSoulBag self, List<long> ids,List<RewardItem> _list)
|
||
{
|
||
foreach (var id in ids)
|
||
{
|
||
try
|
||
{
|
||
var item = self.Get(id);
|
||
if (item != null)
|
||
{
|
||
StarSoulResolveConfig config = StarSoulResolveConfigCategory.Instance.Get((long) item.quality);
|
||
DropHelper.Drop(self.GetParent<Unit>(),self.Parent.GetComponent<PlayerData>(), BattleType.None,config.DropId,null,1,"星魂分解",list: _list);
|
||
self.Remove(id);
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Log.Error(e);
|
||
return "系统错误";
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
}
|
||
} |