From e490646e5d07bca391f22b31c228b83c7dd564d5 Mon Sep 17 00:00:00 2001 From: CWX Date: Sun, 26 Jan 2025 19:08:16 +0000 Subject: [PATCH] Start chatBots --- .../Dialogue/Commando/SptCommandoCommands.cs | 47 +++- .../Helpers/Dialogue/CommandoDialogChatBot.cs | 2 +- .../Core/Helpers/Dialogue/IDialogueChatBot.cs | 2 +- .../Helpers/Dialogue/SptDialogueChatBot.cs | 208 +++++++++++++++++- Libraries/Core/Helpers/UtilityHelper.cs | 5 +- .../Core/Models/Spt/Config/CoreConfig.cs | 2 +- 6 files changed, 248 insertions(+), 18 deletions(-) diff --git a/Libraries/Core/Helpers/Dialogue/Commando/SptCommandoCommands.cs b/Libraries/Core/Helpers/Dialogue/Commando/SptCommandoCommands.cs index 2f15ef44..0f725d37 100644 --- a/Libraries/Core/Helpers/Dialogue/Commando/SptCommandoCommands.cs +++ b/Libraries/Core/Helpers/Dialogue/Commando/SptCommandoCommands.cs @@ -2,34 +2,67 @@ using Core.Helpers.Dialog.Commando.SptCommands; using Core.Models.Eft.Dialog; using Core.Models.Eft.Profile; +using Core.Models.Spt.Config; +using Core.Servers; +using Core.Services; namespace Core.Helpers.Dialog.Commando; [Injectable] public class SptCommandoCommands : IChatCommand { + protected List _sptCommands; + protected LocalisationService _localisationService; + public SptCommandoCommands( + ConfigServer configServer, + LocalisationService localisationService, + IEnumerable sptCommands + ) + { + _sptCommands = sptCommands.ToList(); + _localisationService = localisationService; + var coreConfigs = configServer.GetConfig(); + var commandoId = coreConfigs.Features?.ChatbotFeatures.Ids.GetValueOrDefault("commando"); + if (!(coreConfigs.Features.ChatbotFeatures.CommandoFeatures.GiveCommandEnabled && + coreConfigs.Features.ChatbotFeatures.EnabledBots.ContainsKey(commandoId))) + { + var giveCommand = _sptCommands.FirstOrDefault(x => x.GetCommand().ToLower() == "give"); + _sptCommands.Remove(giveCommand); + } + } + public void RegisterSptCommandoCommand(ISptCommand command) { - throw new NotImplementedException(); + if (_sptCommands.Any((c) => c.GetCommand() == command.GetCommand())) { + throw new Exception( + _localisationService.GetText( + "chat-unable_to_register_command_already_registered", + command.GetCommand() + ) + ); + } + _sptCommands.Add(command); } - + public string GetCommandPrefix() { - throw new NotImplementedException(); + return "spt"; } public string GetCommandHelp(string command) { - throw new NotImplementedException(); + return _sptCommands.FirstOrDefault(c => c.GetCommand() == command)?.GetCommandHelp(); } public List GetCommands() { - throw new NotImplementedException(); + return _sptCommands.Select(c => c.GetCommand()).ToList(); } - public string Handle(string command, UserDialogInfo commandHandler, string sesssionId, SendMessageRequest request) + public string Handle(string command, UserDialogInfo commandHandler, string sessionId, SendMessageRequest request) { - throw new NotImplementedException(); + return _sptCommands + .Find((c) => c.GetCommand() == command) + .PerformAction(commandHandler, sessionId, request); } } diff --git a/Libraries/Core/Helpers/Dialogue/CommandoDialogChatBot.cs b/Libraries/Core/Helpers/Dialogue/CommandoDialogChatBot.cs index 067f192a..243e44d5 100644 --- a/Libraries/Core/Helpers/Dialogue/CommandoDialogChatBot.cs +++ b/Libraries/Core/Helpers/Dialogue/CommandoDialogChatBot.cs @@ -38,6 +38,6 @@ public class CommandoDialogChatBot( protected string GetUnrecognizedCommandMessage() { - throw new NotImplementedException(); + return "I'm sorry soldier, I don't recognize the command you are trying to use! Type \"help\" to see available commands."; } } diff --git a/Libraries/Core/Helpers/Dialogue/IDialogueChatBot.cs b/Libraries/Core/Helpers/Dialogue/IDialogueChatBot.cs index d50269ef..b4f221db 100644 --- a/Libraries/Core/Helpers/Dialogue/IDialogueChatBot.cs +++ b/Libraries/Core/Helpers/Dialogue/IDialogueChatBot.cs @@ -6,5 +6,5 @@ namespace Core.Helpers.Dialogue; public interface IDialogueChatBot { public UserDialogInfo GetChatBot(); - public string HandleMessage(string sessionId, SendMessageRequest request); + public string? HandleMessage(string sessionId, SendMessageRequest request); } diff --git a/Libraries/Core/Helpers/Dialogue/SptDialogueChatBot.cs b/Libraries/Core/Helpers/Dialogue/SptDialogueChatBot.cs index 04b01a8f..dab7acc3 100644 --- a/Libraries/Core/Helpers/Dialogue/SptDialogueChatBot.cs +++ b/Libraries/Core/Helpers/Dialogue/SptDialogueChatBot.cs @@ -7,18 +7,26 @@ using Core.Models.Spt.Config; using Core.Models.Utils; using Core.Servers; using Core.Services; +using Core.Utils; namespace Core.Helpers.Dialogue; [Injectable] public class SptDialogueChatBot( ISptLogger _logger, - MailSendService mailSendService, - IEnumerable chatCommands, - ConfigServer configServer -) : AbstractDialogChatBot(_logger, mailSendService, chatCommands) + MailSendService _mailSendService, + IEnumerable _chatCommands, + ConfigServer _configServer, + ProfileHelper _profileHelper, + RandomUtil _randomUtil, + SeasonalEventService _seasonalEventService, + GiftService _giftService, + LocalisationService _localisationService +) : AbstractDialogChatBot(_logger, _mailSendService, _chatCommands) { - protected CoreConfig _coreConfig = configServer.GetConfig(); + protected CoreConfig _coreConfig = _configServer.GetConfig(); + protected WeatherConfig _weatherConfig = _configServer.GetConfig(); + protected List _listOfMessages = ["hello", "hi", "sup", "yo", "hey"]; public override UserDialogInfo GetChatBot() { @@ -37,8 +45,194 @@ public class SptDialogueChatBot( }; } - public string HandleMessage(string sessionId, SendMessageRequest request) + public string? HandleMessage(string sessionId, SendMessageRequest request) { - throw new NotImplementedException(); + var sender = _profileHelper.GetPmcProfile(sessionId); + + var sptFriendUser = GetChatBot(); + var requestInput = request.Text.ToLower(); + + // only check if entered text is gift code when feature enabled + if (_coreConfig.Features.ChatbotFeatures.SptFriendGiftsEnabled) { + var giftSent = _giftService.SendGiftToPlayer(sessionId, request.Text); + if (giftSent == GiftSentResult.SUCCESS) { + _mailSendService.SendUserMessageToPlayer( + sessionId, + sptFriendUser, + _randomUtil.GetArrayValue([ + "Hey! you got the right code!", + "A secret code, how exciting!", + "You found a gift code!", + "A gift code! incredible", + "A gift! what could it be!", + ]), + [], + null + ); + + return null; + } + + if (giftSent == GiftSentResult.FAILED_GIFT_ALREADY_RECEIVED) { + _mailSendService.SendUserMessageToPlayer( + sessionId, + sptFriendUser, + _randomUtil.GetArrayValue(["Looks like you already used that code", "You already have that!!"]), + [], + null + ); + + return null; + } + } + + if (requestInput.Contains("love you")) { + _mailSendService.SendUserMessageToPlayer( + sessionId, + sptFriendUser, + _randomUtil.GetArrayValue([ + "That's quite forward but i love you too in a purely chatbot-human way", + "I love you too buddy :3!", + "uwu", + $"love you too {sender?.Info?.Nickname}", + ]), + [], + null + ); + } + + if (requestInput == "spt") { + _mailSendService.SendUserMessageToPlayer( + sessionId, + sptFriendUser, + _randomUtil.GetArrayValue(["Its me!!", "spt? i've heard of that project"]), + [], + null + ); + } + + if (requestInput == "fish") { + _mailSendService.SendUserMessageToPlayer( + sessionId, + sptFriendUser, + _randomUtil.GetArrayValue(["blub"]), + [], + null + ); + } + + if (_listOfMessages.Contains(requestInput)) { + _mailSendService.SendUserMessageToPlayer( + sessionId, + sptFriendUser, + _randomUtil.GetArrayValue([ + "Howdy", + "Hi", + "Greetings", + "Hello", + "bonjor", + "Yo", + "Sup", + "Heyyyyy", + "Hey there", + $"Hello {sender?.Info?.Nickname}", + ]), + [], null + ); + } + + if (requestInput == "nikita") { + _mailSendService.SendUserMessageToPlayer( + sessionId, + sptFriendUser, + _randomUtil.GetArrayValue([ + "I know that guy!", + "Cool guy, he made EFT!", + "Legend", + "Remember when he said webel-webel-webel-webel, classic Nikita moment", + ]), [], null + ); + } + + if (requestInput == "are you a bot") { + _mailSendService.SendUserMessageToPlayer( + sessionId, + sptFriendUser, + _randomUtil.GetArrayValue(["beep boop", "**sad boop**", "probably", "sometimes", "yeah lol"]), + [], null + ); + } + + if (requestInput == "itsonlysnowalan") { + _weatherConfig.OverrideSeason = Season.WINTER; + + _mailSendService.SendUserMessageToPlayer( + sessionId, + sptFriendUser, + _randomUtil.GetArrayValue([_localisationService.GetText("chatbot-snow_enabled")]), [], null + ); + } + + if (requestInput == "givemesunshine") { + _weatherConfig.OverrideSeason = Season.SUMMER; + + _mailSendService.SendUserMessageToPlayer( + sessionId, + sptFriendUser, + _randomUtil.GetArrayValue([_localisationService.GetText("chatbot-summer_enabled")]), [], null + ); + } + + if (requestInput == "veryspooky") { + var enableEventResult = _seasonalEventService.ForceSeasonalEvent(SeasonalEventType.Halloween); + if (enableEventResult) { + _mailSendService.SendUserMessageToPlayer( + sessionId, + sptFriendUser, + _randomUtil.GetArrayValue([ + _localisationService.GetText("chatbot-forced_event_enabled", SeasonalEventType.Halloween) + ]), [], null + ); + } + } + + if (requestInput == "hohoho") { + var enableEventResult = _seasonalEventService.ForceSeasonalEvent(SeasonalEventType.Christmas); + if (enableEventResult) { + _mailSendService.SendUserMessageToPlayer( + sessionId, + sptFriendUser, + _randomUtil.GetArrayValue([ + _localisationService.GetText("chatbot-forced_event_enabled", SeasonalEventType.Christmas) + ]), [], null + ); + } + } + + if (requestInput == "givemespace") { + var stashRowGiftId = "StashRows"; + var maxGiftsToSendCount = _coreConfig.Features.ChatbotFeatures.CommandUseLimits[stashRowGiftId] ?? 5; + if (_profileHelper.PlayerHasRecievedMaxNumberOfGift(sessionId, stashRowGiftId, maxGiftsToSendCount)) { + _mailSendService.SendUserMessageToPlayer( + sessionId, + sptFriendUser, + _localisationService.GetText("chatbot-cannot_accept_any_more_of_gift"), [], null + ); + } else { + _profileHelper.AddStashRowsBonusToProfile(sessionId, 2); + + _mailSendService.SendUserMessageToPlayer( + sessionId, + sptFriendUser, + _randomUtil.GetArrayValue([ + _localisationService.GetText("chatbot-added_stash_rows_please_restart"), + ]), [], null + ); + + _profileHelper.FlagGiftReceivedInProfile(sessionId, stashRowGiftId, maxGiftsToSendCount); + } + } + + return request.DialogId; } } diff --git a/Libraries/Core/Helpers/UtilityHelper.cs b/Libraries/Core/Helpers/UtilityHelper.cs index 46cbe672..4e0f205e 100644 --- a/Libraries/Core/Helpers/UtilityHelper.cs +++ b/Libraries/Core/Helpers/UtilityHelper.cs @@ -7,6 +7,9 @@ public class UtilityHelper { public List ArrayIntersect(List a, List b) { - throw new NotImplementedException(); + //a.Intersect(x => b.Contains(x)).ToList(); + // gives error Delegate type could not be infered + + return a.Where(x => b.Contains(x)).ToList(); } } diff --git a/Libraries/Core/Models/Spt/Config/CoreConfig.cs b/Libraries/Core/Models/Spt/Config/CoreConfig.cs index 8bc77765..f778b638 100644 --- a/Libraries/Core/Models/Spt/Config/CoreConfig.cs +++ b/Libraries/Core/Models/Spt/Config/CoreConfig.cs @@ -179,7 +179,7 @@ public record ChatbotFeatures public CommandoFeatures CommandoFeatures { get; set; } [JsonPropertyName("commandUseLimits")] - public Dictionary CommandUseLimits { get; set; } + public Dictionary CommandUseLimits { get; set; } [JsonPropertyName("ids")] public Dictionary Ids { get; set; }