Converted CalculateLevel to extension method

Removed `PlayerService`
This commit is contained in:
Chomp
2025-06-28 17:05:56 +01:00
parent d4dd309252
commit 1c83014ed2
5 changed files with 38 additions and 52 deletions
@@ -171,5 +171,34 @@ namespace SPTarkov.Server.Core.Extensions
b.BanType == BanType.RagFair && currentTimestamp < b.DateTime
) ?? false;
}
/// <summary>
/// Calculates the current level of a player based on their accumulated experience points.
/// This method iterates through an experience table to determine the highest level achieved
/// by comparing the player's experience against cumulative thresholds.
/// </summary>
/// <param name="pmcData"> Player profile </param>
/// <param name="expTable">Experience table from globals.json</param>
/// <returns>
/// The calculated level of the player as an integer, or null if the level cannot be determined.
/// This value is also assigned to <see cref="PmcData.Info.Level" /> within the provided profile.
/// </returns>
public static int? CalculateLevel(this PmcData pmcData, ExpTable[] expTable)
{
var accExp = 0;
for (var i = 0; i < expTable.Length; i++)
{
accExp += expTable[i].Experience ?? 0;
if (pmcData.Info.Experience < accExp)
{
break;
}
pmcData.Info.Level = i + 1;
}
return pmcData.Info.Level;
}
}
}
@@ -1,4 +1,5 @@
using SPTarkov.DI.Annotations;
using SPTarkov.Server.Core.Extensions;
using SPTarkov.Server.Core.Models.Eft.Common;
using SPTarkov.Server.Core.Models.Eft.Common.Tables;
using SPTarkov.Server.Core.Models.Eft.Hideout;
@@ -81,7 +82,9 @@ public class RewardHelper(
int.Parse(reward.Value.ToString())
); // this must occur first as the output object needs to take the modified profile exp value
// Recalculate level in event player leveled up
pmcProfile.Info.Level = _playerService.CalculateLevel(pmcProfile);
pmcProfile.Info.Level = pmcProfile.CalculateLevel(
_databaseService.GetGlobals().Configuration.Exp.Level.ExperienceTable
);
break;
case RewardType.TraderStanding:
_traderHelper.AddStandingToTrader(sessionId, reward.Target, reward.Value.Value);
@@ -336,7 +336,9 @@ public class TraderHelper(
var loyaltyLevels = _databaseService.GetTrader(traderID).Base.LoyaltyLevels;
// Level up player
pmcData.Info.Level = _playerService.CalculateLevel(pmcData);
pmcData.Info.Level = pmcData.CalculateLevel(
_databaseService.GetGlobals().Configuration.Exp.Level.ExperienceTable
);
// Level up traders
var targetLevel = 0;
@@ -1,38 +0,0 @@
using SPTarkov.DI.Annotations;
using SPTarkov.Server.Core.Models.Eft.Common;
namespace SPTarkov.Server.Core.Services;
[Injectable(InjectionType.Singleton)]
public class PlayerService(DatabaseService _databaseService)
{
/// <summary>
/// Calculates the current level of a player based on their accumulated experience points.
/// This method iterates through an experience table to determine the highest level achieved
/// by comparing the player's experience against cumulative thresholds.
/// </summary>
/// <param name="pmcData"> Player profile </param>
/// <returns>
/// The calculated level of the player as an integer, or null if the level cannot be determined.
/// This value is also assigned to <see cref="PmcData.Info.Level" /> within the provided profile.
/// </returns>
public int? CalculateLevel(PmcData pmcData)
{
var accExp = 0;
var expTable = _databaseService.GetGlobals().Configuration.Exp.Level.ExperienceTable;
for (var i = 0; i < expTable.Length; i++)
{
accExp += expTable[i].Experience ?? 0;
if (pmcData.Info.Experience < accExp)
{
break;
}
pmcData.Info.Level = i + 1;
}
return pmcData.Info.Level;
}
}
@@ -33,17 +33,7 @@ public class TraderPurchasePersisterService(
{
var profile = _profileHelper.GetFullProfile(sessionId);
if (profile.TraderPurchases is null)
{
return null;
}
if (profile.TraderPurchases.ContainsKey(traderId))
{
return profile.TraderPurchases[traderId];
}
return null;
return profile?.TraderPurchases?.GetValueOrDefault(traderId);
}
/// <summary>
@@ -97,7 +87,7 @@ public class TraderPurchasePersisterService(
continue;
}
// Skip if no trader-speicifc purchases
// Skip if no trader-specific purchases
if (!profile.Value.TraderPurchases.TryGetValue(traderId, out _))
{
continue;