From 573cf786a9c63ea44e2dea7da1b750d689fe0953 Mon Sep 17 00:00:00 2001 From: CWX Date: Sat, 25 Jan 2025 20:13:30 +0000 Subject: [PATCH 1/3] cast to int, change type and lock before accessing bots --- Libraries/Core/Generators/BotGenerator.cs | 2 +- .../Core/Models/Eft/Common/Tables/BotBase.cs | 2 +- .../Services/BotGenerationCacheService.cs | 48 ++++++++++++------- SptCommon/Extensions/ListExtensions.cs | 6 +-- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/Libraries/Core/Generators/BotGenerator.cs b/Libraries/Core/Generators/BotGenerator.cs index 3514c13e..208ffe6a 100644 --- a/Libraries/Core/Generators/BotGenerator.cs +++ b/Libraries/Core/Generators/BotGenerator.cs @@ -230,7 +230,7 @@ public class BotGenerator( bot.Info.Experience = botLevel.Exp; bot.Info.Level = botLevel.Level; - bot.Info.Settings.Experience = GetExperienceRewardForKillByDifficulty( + bot.Info.Settings.Experience = (int)GetExperienceRewardForKillByDifficulty( botJsonTemplate.BotExperience.Reward, botGenerationDetails.BotDifficulty, botGenerationDetails.Role diff --git a/Libraries/Core/Models/Eft/Common/Tables/BotBase.cs b/Libraries/Core/Models/Eft/Common/Tables/BotBase.cs index ae67cdef..e12d0b1f 100644 --- a/Libraries/Core/Models/Eft/Common/Tables/BotBase.cs +++ b/Libraries/Core/Models/Eft/Common/Tables/BotBase.cs @@ -196,7 +196,7 @@ public record BotInfoSettings { public string? Role { get; set; } public string? BotDifficulty { get; set; } - public double? Experience { get; set; } + public int? Experience { get; set; } public double? StandingForKill { get; set; } public double? AggressorBonus { get; set; } public bool? UseSimpleAnimator { get; set; } diff --git a/Libraries/Core/Services/BotGenerationCacheService.cs b/Libraries/Core/Services/BotGenerationCacheService.cs index ec47911b..c64ccfd6 100644 --- a/Libraries/Core/Services/BotGenerationCacheService.cs +++ b/Libraries/Core/Services/BotGenerationCacheService.cs @@ -13,6 +13,7 @@ public class BotGenerationCacheService( { protected Dictionary> _storedBots = new Dictionary>(); protected Queue _activeBotsInRaid = []; + protected Lock _lock = new Lock(); /** @@ -21,11 +22,14 @@ public class BotGenerationCacheService( */ public void StoreBots(string key, List botsToStore) { - foreach (var bot in botsToStore) + lock (_lock) { - if (!_storedBots.TryAdd(key, [bot])) + foreach (var bot in botsToStore) { - _storedBots[key].Add(bot); + if (!_storedBots.TryAdd(key, [bot])) + { + _storedBots[key].Add(bot); + } } } } @@ -38,21 +42,24 @@ public class BotGenerationCacheService( */ public BotBase? GetBot(string key) { - if (_storedBots.TryGetValue(key, out var bots)) + lock (_lock) { - if (bots.Count > 0) + if (_storedBots.TryGetValue(key, out var bots)) { - try + if (bots.Count > 0) { - return bots.Pop(); - } - catch (Exception _) - { - _logger.Error(_localisationService.GetText("bot-cache_has_zero_bots_of_requested_type", key)); + return bots.PopFirst(); + try + { + } + catch (Exception _) + { + _logger.Error(_localisationService.GetText("bot-cache_has_zero_bots_of_requested_type", key)); + } } } } - + _logger.Error(_localisationService.GetText("bot-no_bot_type_in_cache", key)); return null; } @@ -63,7 +70,10 @@ public class BotGenerationCacheService( */ public void StoreUsedBot(BotBase botToStore) { - _activeBotsInRaid.Enqueue(botToStore); + lock (_lock) + { + _activeBotsInRaid.Enqueue(botToStore); + } } /** @@ -74,7 +84,10 @@ public class BotGenerationCacheService( */ public BotBase? GetUsedBot(string profileId) { - return _activeBotsInRaid.FirstOrDefault(x => x.Id == profileId); + lock (_lock) + { + return _activeBotsInRaid.FirstOrDefault(x => x.Id == profileId); + } } /** @@ -82,8 +95,11 @@ public class BotGenerationCacheService( */ public void ClearStoredBots() { - _storedBots.Clear(); - _activeBotsInRaid = []; + lock (_lock) + { + _storedBots.Clear(); + _activeBotsInRaid = []; + } } /** diff --git a/SptCommon/Extensions/ListExtensions.cs b/SptCommon/Extensions/ListExtensions.cs index c5833bdc..5c6e764b 100644 --- a/SptCommon/Extensions/ListExtensions.cs +++ b/SptCommon/Extensions/ListExtensions.cs @@ -9,10 +9,10 @@ public static class ListExtensions return items; } - public static T Pop(this List source) + public static T PopFirst(this List source) { - T r = source.Last(); - source.Remove(source.Last()); + T r = source.First(); + source.Remove(source.First()); return r; } } From 8871827c921203104051ed0cf9f0a34a80730dc9 Mon Sep 17 00:00:00 2001 From: Valens <8889280+VforValens@users.noreply.github.com> Date: Sat, 25 Jan 2025 15:14:22 -0500 Subject: [PATCH 2/3] Update NotifierController.cs Start implementation of the NotifyAsync function from node server. Partially completed. --- .../Core/Controllers/NotifierController.cs | 52 +++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/Libraries/Core/Controllers/NotifierController.cs b/Libraries/Core/Controllers/NotifierController.cs index b10ee4e5..656fbd8e 100644 --- a/Libraries/Core/Controllers/NotifierController.cs +++ b/Libraries/Core/Controllers/NotifierController.cs @@ -1,14 +1,20 @@ using SptCommon.Annotations; using Core.Helpers; using Core.Models.Eft.Notifier; +using static System.Runtime.InteropServices.JavaScript.JSType; +using Core.Services; +using System.Diagnostics.Tracing; namespace Core.Controllers; [Injectable] public class NotifierController( HttpServerHelper _httpServerHelper, - NotifierHelper _notifierHelper -) + NotifierHelper _notifierHelper, + NotificationService _notificationService, + _timeout, + _pollInterval + ) { /// /// Resolve an array of session notifications. @@ -20,7 +26,47 @@ public class NotifierController( /// public async Task NotifyAsync(string sessionId) { - throw new NotImplementedException(); + // TODO: Finish implementation of the NotifyAsync method + // + //return new Promise((resolve) => { + // // keep track of our timeout + // let counter = 0; + + // /** + // * Check for notifications, resolve if any, otherwise poll + // * intermittently for a period of time. + // */ + // var checkNotifications = () => { + // /** + // * If there are no pending messages we should either check again later + // * or timeout now with a default response. + // */ + // if (!_notificationService.Has(sessionID)) { + // // have we exceeded timeout? if so reply with default ping message + // if (counter > _timeout) { + // return resolve([_notifierHelper.getDefaultNotification()]); + // } + + // // check again + // setTimeout(checkNotifications, _pollInterval); + + // // update our timeout counter + // counter += _pollInterval; + // return; + // } + + // /** + // * Maintaining array reference is not necessary, so we can just copy and reinitialize + // */ + // var messages = _notificationService.Get(sessionID); + + // _notificationService.UpdateMessageOnQueue(sessionID, []); + // resolve(messages); + }; + + // immediately check + checkNotifications(); + }); } /// From 7fcd72e8d3f8b795d06cd07d1f3ab51dd5ef6d89 Mon Sep 17 00:00:00 2001 From: Valens <8889280+VforValens@users.noreply.github.com> Date: Sat, 25 Jan 2025 15:16:16 -0500 Subject: [PATCH 3/3] Update NotifierController.cs Remove unused parameters and clean up the controller so it doesn't break the build KEK. --- Libraries/Core/Controllers/NotifierController.cs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Libraries/Core/Controllers/NotifierController.cs b/Libraries/Core/Controllers/NotifierController.cs index 656fbd8e..52c55f7f 100644 --- a/Libraries/Core/Controllers/NotifierController.cs +++ b/Libraries/Core/Controllers/NotifierController.cs @@ -10,10 +10,7 @@ namespace Core.Controllers; [Injectable] public class NotifierController( HttpServerHelper _httpServerHelper, - NotifierHelper _notifierHelper, - NotificationService _notificationService, - _timeout, - _pollInterval + NotifierHelper _notifierHelper ) { /// @@ -62,11 +59,11 @@ public class NotifierController( // _notificationService.UpdateMessageOnQueue(sessionID, []); // resolve(messages); - }; + //}; // immediately check - checkNotifications(); - }); + // checkNotifications(); + //}); } ///