301 lines
9.1 KiB
C#
301 lines
9.1 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using Cal.DataTable;
|
||
|
||
namespace ET
|
||
{
|
||
public class StarSoulBagAwakeSystem: AwakeSystem<StarSoulBag>
|
||
{
|
||
public override void Awake(StarSoulBag self)
|
||
{
|
||
self.InitData();
|
||
}
|
||
}
|
||
|
||
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)
|
||
{
|
||
#if UNITY
|
||
GetDataFromServer(self);
|
||
|
||
#endif
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
private static void Sync(this StarSoulBag self, long Id, StarSoulItem item)
|
||
{
|
||
M2C_SyncStarSoulBag proto = new();
|
||
StarSoulNetItem netItem = new StarSoulNetItem();
|
||
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;
|
||
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);
|
||
}
|
||
|
||
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;
|
||
StarSoulLevelConfig soulLevelConfig = StarSoulLevelConfigCategory.Instance.GetByQualityAndLevel(item.quality, nextLevel);
|
||
int needExp = soulLevelConfig.NeedExp;
|
||
var (exp, needGold) = expList[0];
|
||
totalExp += exp;
|
||
expList.RemoveAt(0);
|
||
needCount++;
|
||
if (totalExp >= needExp)
|
||
{
|
||
var payRet = CharacterHelper.ReduceMoney(self.Parent, CharacterHelper.MoneyType.Coin, needGold * 100_00);
|
||
if (payRet != null)
|
||
{
|
||
AddLevel(self, item, addLevel, totalExp);
|
||
return payRet;
|
||
}
|
||
|
||
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)
|
||
{
|
||
if (addLevel > 0)
|
||
{
|
||
item.exp = 0;
|
||
}
|
||
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 "系统错误";
|
||
}
|
||
|
||
EquipType equipType = item.posType;
|
||
Bag bag = self.Parent.GetComponent<Bag>();
|
||
long oldId = 0;
|
||
if (item.isUsed)
|
||
{
|
||
var ret = bag.PutonStarSoulItem(equipType,0,out oldId);
|
||
if (ret != null)
|
||
{
|
||
return ret;
|
||
}
|
||
|
||
item.isUsed = false;
|
||
}
|
||
else
|
||
{
|
||
var ret = bag.PutonStarSoulItem(equipType,item.Id,out oldId);
|
||
if (ret != null)
|
||
{
|
||
return ret;
|
||
}
|
||
|
||
item.isUsed = true;
|
||
}
|
||
|
||
CharacterHelper.SyncNumeric(self.Parent);
|
||
var oldItem = self.Get(oldId);
|
||
if (oldItem != null)
|
||
{
|
||
oldItem.isUsed = false;
|
||
self.Sync(oldItem.Id,oldItem);
|
||
}
|
||
self.Sync(item.Id,item);
|
||
return null;
|
||
}
|
||
}
|
||
} |