You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
5.1 KiB

1 month ago
using System.Linq;
using Sog;
using ProtoCSStruct;
namespace Game
{
public static class LevelUtils
{
// 临时代码, 兼容老接口
public static void AddExp(PlayerOnGame player, int exp, string reason)
{
AddExp(player, ref player.RoleData.Knight.DbHero, exp);
}
// 增加经验请调用上面的AddExp接口
private static void AddExp(PlayerOnGame player, ref DBHero dbHero, int exp)
{
if (exp < 0)
{
return;
}
var r = MonthlyCardSvc.GetMonthlyCardBuff(player, MonthlyCardBuffId.ExpGetUp);
exp += (exp * r / 10000);
TraceLog.Trace("LevelUtils.AddExp uid {0} hero {1} old level {2} old exp {3} addExp {4}"
, player.UserID, dbHero.DescId, dbHero.Level, dbHero.Exp, exp);
bool isDirty = false;
bool isLvUp = false;
var maxLevelDesc = LevelDescMgr.Instance.ItemTable.Last().Value;
var maxLevel = maxLevelDesc.level;
if(dbHero.Level >= maxLevel)
{
return;
}
var desc = LevelDescMgr.Instance.GetConfig(dbHero.Level);
if (desc == null)
{
TraceLog.Error("LevelUtils.AddExp get LevelDesc is null! id:{0}", dbHero.Level);
return;
}
dbHero.Exp += exp;
while (dbHero.Exp >= desc.levelUpExp && dbHero.Level < maxLevel)
{
dbHero.Exp -= desc.levelUpExp;
if (dbHero.Level >= maxLevelDesc.level)
{
dbHero.Exp = maxLevelDesc.levelUpExp;
break;
}
dbHero.Level += 1;
BattlePassSvc.AddPoint(player, ReasonType.Level, 1);
// 兼容下老代码, 设置rolebase的等级
player.RoleBase.Level = dbHero.Level;
GameServerUtils.GetEventHandlerMgr().TriggerPlayerAddLevel(player);
isDirty = true;
isLvUp = true;
GameBillLogUtils.LogAddLevel(player.UserID, LevelType.RoleLevel, dbHero.Level);
// 升级付出的时间, 注意不是在线时长
long time = player.RoleData.OtherData.OnlineExpAllTime + GameServerUtils.GetTimeSecond() - player.RoleData.OtherData.OnlineExpStartTime;
player.RoleData.OtherData.OnlineExpStartTime = GameServerUtils.GetTimeSecond();
player.RoleData.OtherData.OnlineExpAllTime = 0;
GameBDCLogUtils.ExpChange(player, (int)LevelType.RoleLevel, dbHero.Level - 1, dbHero.Level, (int)time);
PopPackageSvc.TriggerPopPackage(player,PackPopType.Level);
PlayerNotify.NotifyPlayerShowInfoChange(player);
desc = LevelDescMgr.Instance.GetConfig(dbHero.Level);
if (desc == null) break;
}
if (isLvUp)
{
TraceLog.Trace("LevelUtils.AddExp uid {0} hero {1} new level {2} new exp {3}"
, player.UserID, dbHero.DescId, dbHero.Level, dbHero.Exp);
}
SyncHeroLevel(player, ref dbHero);
player.RoleBase.Exp = dbHero.Exp;
player.RoleBase.Level = dbHero.Level;
if (isDirty)
{
player.MakeDirty();
}
}
public static void AddLevel(this PlayerOnGame player, int level)
{
if (level < 0)
{
SubLevel(player, level);
return;
}
while ((level--) > 0)
{
Add1Level(player);
}
}
public static void Add1Level(PlayerOnGame player)
{
ref var dbHero = ref player.RoleData.Knight.DbHero;
var desc = LevelDescMgr.Instance.GetConfig(dbHero.Level);
if (desc == null)
{
return;
}
AddExp(player, desc.levelUpExp - dbHero.Exp, "Add1Level");
}
public static void SubLevel(PlayerOnGame player, int level)
{
ref var dbHero = ref player.RoleData.Knight.DbHero;
if (dbHero.Level <= level)
{
dbHero.Level = 1;
}
else
{
dbHero.Level -= level;
}
dbHero.Exp = 0;
player.MakeDirty();
SyncHeroLevel(player, ref dbHero);
}
public static void SyncHeroLevel(PlayerOnGame player, ref DBHero dbHero)
{
var info = new CSSyncLevelInfo { LevelType = LevelType.RoleLevel, Level = dbHero.Level, Exp = dbHero.Exp };
player.SendToClient((int)CSGameMsgID.SyncLevelInfo, ref info);
}
public static void SyncVipLevel(PlayerOnGame player)
{
var info = new CSSyncLevelInfo { LevelType = LevelType.VipLevel, Level = player.GetVipLevel(), Exp = player.GetVipExp() };
player.SendToClient((int)CSGameMsgID.SyncLevelInfo, ref info);
}
}
}