Improved SptDialogChatBot code abstraction

This commit is contained in:
Chomp
2025-01-28 17:21:34 +00:00
parent 0fcfa99134
commit 21a811b488
14 changed files with 558 additions and 188 deletions
@@ -8,6 +8,7 @@ using Core.Models.Utils;
using Core.Servers;
using Core.Services;
using Core.Utils;
using Core.Helpers.Dialogue.SptMessageHandlers;
namespace Core.Helpers.Dialogue;
@@ -18,15 +19,21 @@ public class SptDialogueChatBot(
IEnumerable<IChatCommand> _chatCommands,
ConfigServer _configServer,
ProfileHelper _profileHelper,
RandomUtil _randomUtil,
SeasonalEventService _seasonalEventService,
GiftService _giftService,
LocalisationService _localisationService
IEnumerable<IChatMessageHandler> chatMessageHandlers
) : IDialogueChatBot
{
protected IEnumerable<IChatMessageHandler> _chatMessageHandlers = ChatMessageHandlerSetup(chatMessageHandlers);
private static List<IChatMessageHandler> ChatMessageHandlerSetup(IEnumerable<IChatMessageHandler> components)
{
var chatMessageHandlers = components.ToList();
chatMessageHandlers.Sort((a, b) => a.GetPriority() - b.GetPriority());
return chatMessageHandlers;
}
protected CoreConfig _coreConfig = _configServer.GetConfig<CoreConfig>();
protected WeatherConfig _weatherConfig = _configServer.GetConfig<WeatherConfig>();
protected List<string> _listOfMessages = ["hello", "hi", "sup", "yo", "hey"];
public UserDialogInfo GetChatBot()
{
@@ -48,190 +55,10 @@ public class SptDialogueChatBot(
public string? HandleMessage(string sessionId, SendMessageRequest request)
{
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);
}
}
_chatMessageHandlers.FirstOrDefault((v) => v.CanHandle(request.Text))
.Process(sessionId, sptFriendUser, sender);
return request.DialogId;
}
@@ -0,0 +1,34 @@
using Core.Models.Eft.Common;
using Core.Models.Eft.Profile;
using Core.Services;
using Core.Utils;
using SptCommon.Annotations;
namespace Core.Helpers.Dialogue.SptMessageHandlers
{
[Injectable]
public class AreYouABotMessageHandler(
MailSendService _mailSendService,
RandomUtil _randomUtil) : IChatMessageHandler
{
public int GetPriority()
{
return 100;
}
public bool CanHandle(string message)
{
return message.ToLower() == "are you a bot";
}
public void Process(string sessionId, UserDialogInfo sptFriendUser, PmcData sender)
{
_mailSendService.SendUserMessageToPlayer(
sessionId,
sptFriendUser,
_randomUtil.GetArrayValue(["beep boop", "**sad boop**", "probably", "sometimes", "yeah lol"]),
[], null
);
}
}
}
@@ -0,0 +1,32 @@
using Core.Models.Eft.Common;
using Core.Models.Eft.Profile;
using Core.Services;
using SptCommon.Annotations;
namespace Core.Helpers.Dialogue.SptMessageHandlers;
[Injectable]
public class FishMessageHandler(
MailSendService _mailSendService) : IChatMessageHandler
{
public int GetPriority()
{
return 100;
}
public bool CanHandle(string message)
{
return message.ToLower() == "fish";
}
public void Process(string sessionId, UserDialogInfo sptFriendUser, PmcData sender)
{
_mailSendService.SendUserMessageToPlayer(
sessionId,
sptFriendUser,
"blub",
[],
null
);
}
}
@@ -0,0 +1,42 @@
using Core.Models.Eft.Common;
using Core.Models.Eft.Profile;
using Core.Models.Enums;
using Core.Services;
using Core.Utils;
using SptCommon.Annotations;
namespace Core.Helpers.Dialogue.SptMessageHandlers
{
[Injectable]
public class ForceChristmasMessageHandler(
LocalisationService _localisationService,
MailSendService _mailSendService,
RandomUtil _randomUtil,
SeasonalEventService _seasonalEventService) : IChatMessageHandler
{
public int GetPriority()
{
return 99;
}
public bool CanHandle(string message)
{
return message.ToLower() == "hohoho";
}
public void Process(string sessionId, UserDialogInfo sptFriendUser, PmcData sender)
{
var enableEventResult = _seasonalEventService.ForceSeasonalEvent(SeasonalEventType.Christmas);
if (enableEventResult)
{
_mailSendService.SendUserMessageToPlayer(
sessionId,
sptFriendUser,
_randomUtil.GetArrayValue([
_localisationService.GetText("chatbot-forced_event_enabled", SeasonalEventType.Christmas)
]), [], null
);
}
}
}
}
@@ -0,0 +1,44 @@
using Core.Models.Eft.Common;
using Core.Models.Eft.Profile;
using Core.Models.Enums;
using Core.Services;
using Core.Utils;
using SptCommon.Annotations;
namespace Core.Helpers.Dialogue.SptMessageHandlers
{
[Injectable]
public class ForceHalloweenMessageHandler(
LocalisationService _localisationService,
MailSendService _mailSendService,
RandomUtil _randomUtil,
SeasonalEventService _seasonalEventService) : IChatMessageHandler
{
public int GetPriority()
{
return 99;
}
public bool CanHandle(string message)
{
return message.ToLower() == "veryspooky";
}
public void Process(string sessionId, UserDialogInfo sptFriendUser, PmcData sender)
{
var enableEventResult = _seasonalEventService.ForceSeasonalEvent(SeasonalEventType.Halloween);
if (enableEventResult)
{
_mailSendService.SendUserMessageToPlayer(
sessionId,
sptFriendUser,
_randomUtil.GetArrayValue([
_localisationService.GetText("chatbot-forced_event_enabled", SeasonalEventType.Halloween)
]),
[],
null
);
}
}
}
}
@@ -0,0 +1,44 @@
using Core.Models.Eft.Common;
using Core.Models.Eft.Profile;
using Core.Models.Enums;
using Core.Models.Spt.Config;
using Core.Servers;
using Core.Services;
using Core.Utils;
using SptCommon.Annotations;
namespace Core.Helpers.Dialogue.SptMessageHandlers
{
[Injectable]
public class ForceSnowMessageHandler(
LocalisationService _localisationService,
MailSendService _mailSendService,
RandomUtil _randomUtil,
ConfigServer _configServer) : IChatMessageHandler
{
private WeatherConfig _weatherConfig = _configServer.GetConfig<WeatherConfig>();
public int GetPriority()
{
return 99;
}
public bool CanHandle(string message)
{
return message.ToLower() == "itsonlysnowalan";
}
public void Process(string sessionId, UserDialogInfo sptFriendUser, PmcData sender)
{
_weatherConfig.OverrideSeason = Season.WINTER;
_mailSendService.SendUserMessageToPlayer(
sessionId,
sptFriendUser,
_randomUtil.GetArrayValue([_localisationService.GetText("chatbot-snow_enabled")]),
[],
null
);
}
}
}
@@ -0,0 +1,44 @@
using Core.Models.Eft.Common;
using Core.Models.Eft.Profile;
using Core.Models.Enums;
using Core.Models.Spt.Config;
using Core.Servers;
using Core.Services;
using Core.Utils;
using SptCommon.Annotations;
namespace Core.Helpers.Dialogue.SptMessageHandlers
{
[Injectable]
public class ForceSummerMessageHandler(
LocalisationService _localisationService,
MailSendService _mailSendService,
RandomUtil _randomUtil,
ConfigServer _configServer) : IChatMessageHandler
{
private WeatherConfig _weatherConfig = _configServer.GetConfig<WeatherConfig>();
public int GetPriority()
{
return 99;
}
public bool CanHandle(string message)
{
return message.ToLower() == "givemesunshine";
}
public void Process(string sessionId, UserDialogInfo sptFriendUser, PmcData sender)
{
_weatherConfig.OverrideSeason = Season.SUMMER;
_mailSendService.SendUserMessageToPlayer(
sessionId,
sptFriendUser,
_randomUtil.GetArrayValue([_localisationService.GetText("chatbot-summer_enabled")]),
[],
null
);
}
}
}
@@ -0,0 +1,58 @@
using Core.Models.Eft.Common;
using Core.Models.Eft.Profile;
using Core.Models.Spt.Config;
using Core.Servers;
using Core.Services;
using Core.Utils;
using SptCommon.Annotations;
namespace Core.Helpers.Dialogue.SptMessageHandlers;
[Injectable]
public class GiveMeSpaceMessageHandler(
ProfileHelper _profileHelper,
LocalisationService _localisationService,
MailSendService _mailSendService,
RandomUtil _randomUtil,
ConfigServer _configServer) : IChatMessageHandler
{
private CoreConfig _coreConfig = _configServer.GetConfig<CoreConfig>();
public int GetPriority()
{
return 100;
}
public bool CanHandle(string message)
{
return message.ToLower() == "givemespace";
}
public void Process(string sessionId, UserDialogInfo sptFriendUser, PmcData sender)
{
const string 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);
}
}
}
@@ -0,0 +1,48 @@
using Core.Models.Eft.Common;
using Core.Models.Eft.Profile;
using Core.Services;
using Core.Utils;
using SptCommon.Annotations;
namespace Core.Helpers.Dialogue.SptMessageHandlers
{
[Injectable]
public class HelloMessageHandler(
MailSendService _mailSendService,
RandomUtil _randomUtil) : IChatMessageHandler
{
protected List<string> _listOfMessages = ["hello", "hi", "sup", "yo", "hey", "bonjour"];
public int GetPriority()
{
return 100;
}
public bool CanHandle(string message)
{
return _listOfMessages.Contains(message, StringComparer.OrdinalIgnoreCase);
}
public void Process(string sessionId, UserDialogInfo sptFriendUser, PmcData sender)
{
_mailSendService.SendUserMessageToPlayer(
sessionId,
sptFriendUser,
_randomUtil.GetArrayValue([
"Howdy",
"Hi",
"Greetings",
"Hello",
"Bonjor",
"Yo",
"Sup",
"Heyyyyy",
"Hey there",
"OH its you",
$"Hello {sender?.Info?.Nickname}",
]),
[], null
);
}
}
}
@@ -0,0 +1,13 @@
using Core.Models.Eft.Common;
using Core.Models.Eft.Profile;
namespace Core.Helpers.Dialogue.SptMessageHandlers;
public interface IChatMessageHandler
{
// Lower = More priority
int GetPriority();
public abstract bool CanHandle(string message);
public abstract void Process(string sessionId, UserDialogInfo sptFriendUser, PmcData sender);
}
@@ -0,0 +1,40 @@
using Core.Models.Eft.Common;
using Core.Models.Eft.Profile;
using Core.Services;
using Core.Utils;
using SptCommon.Annotations;
namespace Core.Helpers.Dialogue.SptMessageHandlers;
[Injectable]
public class LoveYouChatMessageHandler(
MailSendService _mailSendService,
RandomUtil _randomUtil
) : IChatMessageHandler
{
public int GetPriority()
{
return 100;
}
public bool CanHandle(string message)
{
return message.ToLower() == "love you";
}
public void Process(string sessionId, UserDialogInfo sptFriendUser, PmcData sender)
{
_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
);
}
}
@@ -0,0 +1,39 @@
using Core.Models.Eft.Common;
using Core.Models.Eft.Profile;
using Core.Services;
using Core.Utils;
using SptCommon.Annotations;
namespace Core.Helpers.Dialogue.SptMessageHandlers
{
[Injectable]
public class NikitaMessageHandler(
MailSendService _mailSendService,
RandomUtil _randomUtil) : IChatMessageHandler
{
public int GetPriority()
{
return 100;
}
public bool CanHandle(string message)
{
return message.ToLower() == "nikita";
}
public void Process(string sessionId, UserDialogInfo sptFriendUser, PmcData sender)
{
_mailSendService.SendUserMessageToPlayer(
sessionId,
sptFriendUser,
_randomUtil.GetArrayValue([
"I know that guy!",
"Cool guy, he made EFT!",
"Legend",
"The mastermind of my suffering",
"Remember when he said webel-webel-webel-webel, classic Nikita moment",
]), [], null
);
}
}
}
@@ -0,0 +1,71 @@
using Core.Models.Eft.Common;
using Core.Models.Eft.Profile;
using Core.Models.Enums;
using Core.Models.Spt.Config;
using Core.Servers;
using Core.Services;
using Core.Utils;
using SptCommon.Annotations;
namespace Core.Helpers.Dialogue.SptMessageHandlers
{
[Injectable]
public class SendGiftMessageHandler(
MailSendService _mailSendService,
RandomUtil _randomUtil,
GiftService _giftService,
ConfigServer _configServer) : IChatMessageHandler
{
private CoreConfig _coreConfig = _configServer.GetConfig<CoreConfig>();
private string commandSent = string.Empty;
public int GetPriority()
{
return 1;
}
public bool CanHandle(string message)
{
return _giftService.GiftExists(message.ToLower());
}
public void Process(string sessionId, UserDialogInfo sptFriendUser, PmcData sender)
{
// Gifts may be disabled via config
if (!_coreConfig.Features.ChatbotFeatures.SptFriendGiftsEnabled)
{
return;
}
var giftSent = _giftService.SendGiftToPlayer(sessionId, commandSent);
switch (giftSent)
{
case 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;
case GiftSentResult.FAILED_GIFT_ALREADY_RECEIVED:
_mailSendService.SendUserMessageToPlayer(
sessionId,
sptFriendUser,
_randomUtil.GetArrayValue(["Looks like you already used that code", "You already have that!!"]),
[],
null
);
return;
}
}
}
}
@@ -0,0 +1,34 @@
using Core.Models.Eft.Common;
using Core.Models.Eft.Profile;
using Core.Services;
using Core.Utils;
using SptCommon.Annotations;
namespace Core.Helpers.Dialogue.SptMessageHandlers;
[Injectable]
public class SptMessageHandler(
MailSendService _mailSendService,
RandomUtil _randomUtil) : IChatMessageHandler
{
public int GetPriority()
{
return 100;
}
public bool CanHandle(string message)
{
return message.ToLower() == "spt";
}
public void Process(string sessionId, UserDialogInfo sptFriendUser, PmcData sender)
{
_mailSendService.SendUserMessageToPlayer(
sessionId,
sptFriendUser,
_randomUtil.GetArrayValue(["Its me!!", "spt? i've heard of that project"]),
[],
null
);
}
}