From b3dfa3054ccd0d7ea5d2f9b0ac9c692a2f140595 Mon Sep 17 00:00:00 2001 From: Chomp Date: Fri, 31 Jan 2025 12:33:06 +0000 Subject: [PATCH] Improved spt give command --- .../SptCommands/GiveCommand/GiveSptCommand.cs | 46 ++++++++++--------- Libraries/Core/Utils/FileUtil.cs | 11 +++-- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/Libraries/Core/Helpers/Dialogue/Commando/SptCommands/GiveCommand/GiveSptCommand.cs b/Libraries/Core/Helpers/Dialogue/Commando/SptCommands/GiveCommand/GiveSptCommand.cs index 89b061f9..d183a003 100644 --- a/Libraries/Core/Helpers/Dialogue/Commando/SptCommands/GiveCommand/GiveSptCommand.cs +++ b/Libraries/Core/Helpers/Dialogue/Commando/SptCommands/GiveCommand/GiveSptCommand.cs @@ -26,7 +26,7 @@ public class GiveSptCommand( ) { protected Dictionary _savedCommand = new(); - //private const Regex _commandRegex = new Regex(@"/^spt give(((([a - z]{ 2,5}) )?")(.+)"|\w+) )?([0 - 9]+)$/"; + private static readonly Regex _commandRegex = new("""/^spt give(((([a - z]{ 2,5}) )?"(.+)"|\w+) )?([0 - 9]+)$/"""); private const double _acceptableConfidence = 0.9d; // Exception for flares @@ -52,9 +52,6 @@ public class GiveSptCommand( public string PerformAction(UserDialogInfo commandHandler, string sessionId, SendMessageRequest request) { - throw new NotImplementedException(); - /** - if (!_commandRegex.IsMatch(request.Text)) { _mailSendService.SendUserMessageToPlayer( sessionId, @@ -113,7 +110,7 @@ public class GiveSptCommand( if (isItemName) { try { - locale = result.Groups[4] ?? _localeService.GetDesiredGameLocale() ?? "en"; + locale = result.Groups[4].Value ?? _localeService.GetDesiredGameLocale() ?? "en"; } catch (Exception ex) { _mailSendService.SendUserMessageToPlayer( sessionId, @@ -125,34 +122,43 @@ public class GiveSptCommand( } localizedGlobal = GetGlobalsLocale(locale); - - var closestItemsMatchedByName = _itemHelper + var allAllowedItemNames = _itemHelper .GetItems() - .Where((i) => IsItemAllowed(i)) - .Select((i) => localizedGlobal[$"{i?.Id} Name"]?.ToLower() ?? i.Properties.Name) - .Where((i) => !string.IsNullOrEmpty(i)) - .Select((i) => ({ Match = StringSimilarity(item.ToLower(), i.ToLower())ItemName = i })) // We need to find a similar system to string-similarity-js - .Sort((a1, a2) => a2.match - a1.match); + .Where(IsItemAllowed) + .Select(i => localizedGlobal + .GetValueOrDefault($"{i.Id} Name", i.Properties.Name)?.ToLower()) + .Where(i => !string.IsNullOrEmpty(i)); - if (closestItemsMatchedByName[0].match >= _acceptableConfidence) { + var closestItemsMatchedByName = allAllowedItemNames + .Select( + i => new + { + Match = StringSimilarity.Match(item.ToLower(), i.ToLower()), + ItemName = i + } + ).ToList(); + + + closestItemsMatchedByName.Sort((a1, a2) => (int)a2.Match - (int)a1.Match); // We need to find a similar system to string-similarity-js + + if (closestItemsMatchedByName[0].Match >= _acceptableConfidence) { item = closestItemsMatchedByName[0].ItemName; } else { var i = 1; var slicedItems = closestItemsMatchedByName.Slice(0, 10); // max 10 item names and map them var itemList = slicedItems - .map((match) => $"{i++}. {match.ItemName} (conf: ${(match.match * 100).toFixed(2)})") - .Join("\n"); + .Select(match => $"{i++}. {match.ItemName} (conf: ${Math.Round(match.Match * 100), 2})"); _savedCommand.Add( sessionId, new SavedCommand( quantity, - slicedItems.map((item) => item.ItemName), + slicedItems.Select(item => item.ItemName).ToList(), locale)); _mailSendService.SendUserMessageToPlayer( sessionId, commandHandler, - "Could not find exact match. Closest matches are:\n\n${itemList}\n\nUse 'spt give [number]' to select one."); + $"Could not find exact match. Closest matches are:\n\n${itemList}\n\nUse 'spt give [number]' to select one."); return request.DialogId; } @@ -165,8 +171,8 @@ public class GiveSptCommand( var tplId = isItemName ? _itemHelper .GetItems() - .Where((i) => IsItemAllowed(i)) - .FirstOrDefault((i) => (localizedGlobal[$"{i?.Id} Name"]?.ToLower() ?? i.Properties.Name) == item).Id + .Where(i => IsItemAllowed(i)) + .FirstOrDefault(i => (localizedGlobal[$"{i?.Id} Name"]?.ToLower() ?? i.Properties.Name) == item).Id : item; var checkedItem = _itemHelper.GetItem(tplId); @@ -227,8 +233,6 @@ public class GiveSptCommand( _mailSendService.SendSystemMessageToPlayer(sessionId, "SPT GIVE", itemsToSend); return request.DialogId; - - */ } protected Dictionary GetGlobalsLocale(string desiredLocale) diff --git a/Libraries/Core/Utils/FileUtil.cs b/Libraries/Core/Utils/FileUtil.cs index a72e8cab..dbf169c7 100644 --- a/Libraries/Core/Utils/FileUtil.cs +++ b/Libraries/Core/Utils/FileUtil.cs @@ -92,20 +92,21 @@ public class FileUtil( /// Copy a file from one path to another /// /// Source file to copy from - /// + /// /// Should destination file be overwritten - public void CopyFile(string copyFromPath, string destinationPath, bool overwrite = false) + public void CopyFile(string copyFromPath, string destinationFilePath, bool overwrite = false) { // Check it exists first if (!FileExists(copyFromPath)) { - _logger.Error($"Source file not found: {copyFromPath}. Cannot copy to: {destinationPath}"); + _logger.Error($"Source file not found: {copyFromPath}. Cannot copy to: {destinationFilePath}"); } + // Ensure dir exists - Directory.CreateDirectory(destinationPath); + Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath)); // Copy the file - File.Copy(copyFromPath, destinationPath, overwrite); + File.Copy(copyFromPath, destinationFilePath, overwrite); } }