From e46f6f6181ed07582095658b76119c9b67c2f8d4 Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Mon, 26 Aug 2024 14:14:23 +0800 Subject: [PATCH] Prepare simple warlist mode --- CMakeLists.txt | 2 + src/engine/shared/chillerbot/variables.h | 1 + .../components/chillerbot/chatcommand.cpp | 13 +- .../client/components/chillerbot/warlist.cpp | 412 +---------------- .../client/components/chillerbot/warlist.h | 3 + .../chillerbot/warlist_commands_advanced.cpp | 424 ++++++++++++++++++ .../chillerbot/warlist_commands_simple.cpp | 98 ++++ 7 files changed, 539 insertions(+), 414 deletions(-) create mode 100644 src/game/client/components/chillerbot/warlist_commands_advanced.cpp create mode 100644 src/game/client/components/chillerbot/warlist_commands_simple.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7cc535fbafc..59e968d4aa3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2409,6 +2409,8 @@ if(CLIENT) components/chillerbot/vibebot.h components/chillerbot/warlist.cpp components/chillerbot/warlist.h + components/chillerbot/warlist_commands_advanced.cpp + components/chillerbot/warlist_commands_simple.cpp components/console.cpp components/console.h components/controls.cpp diff --git a/src/engine/shared/chillerbot/variables.h b/src/engine/shared/chillerbot/variables.h index adbddca7fc0..ad56081a718 100644 --- a/src/engine/shared/chillerbot/variables.h +++ b/src/engine/shared/chillerbot/variables.h @@ -64,6 +64,7 @@ MACRO_CONFIG_STR(ClSavedDummySkin, saved_dummy_skin, 24, "default", CFGFLAG_CLIE // warlist MACRO_CONFIG_INT(ClWarList, cl_war_list, 1, 0, 1, CFGFLAG_CLIENT | CFGFLAG_SAVE, "Display nameplate color based on chillerbot/warlist directory") +MACRO_CONFIG_INT(ClWarListAdvanced, cl_war_list_advanced, 0, 0, 1, CFGFLAG_CLIENT | CFGFLAG_SAVE, "Activates more complex warlist mode") MACRO_CONFIG_INT(ClNameplatesWarReason, cl_nameplates_war_reason, 0, 0, 1, CFGFLAG_CLIENT | CFGFLAG_SAVE, "Show war reason in name plates") MACRO_CONFIG_INT(ClWarListAutoReload, cl_war_list_auto_reload, 10, 0, 600, CFGFLAG_CLIENT | CFGFLAG_SAVE, "Reload warlist every x seconds 0=off") diff --git a/src/game/client/components/chillerbot/chatcommand.cpp b/src/game/client/components/chillerbot/chatcommand.cpp index 33f94272042..064f79f250d 100644 --- a/src/game/client/components/chillerbot/chatcommand.cpp +++ b/src/game/client/components/chillerbot/chatcommand.cpp @@ -8,8 +8,8 @@ #include -#include "chatcommand.h" #include "base/system.h" +#include "chatcommand.h" void CChatCommand::OnServerMsg(const char *pMsg) { @@ -51,7 +51,7 @@ bool CChatCommand::OnChatCmd(char Prefix, int ClientId, int Team, const char *pC bool CChatCommand::ParseChatCmd(char Prefix, int ClientId, int Team, const char *pCmdWithArgs) { char aRawArgLine[512]; - aRawArgLine[0] = '\0'; + str_copy(aRawArgLine, pCmdWithArgs); const int MaxArgLen = 256; char aCmd[MaxArgLen]; int i; @@ -61,9 +61,12 @@ bool CChatCommand::ParseChatCmd(char Prefix, int ClientId, int Team, const char break; aCmd[i] = pCmdWithArgs[i]; } - const char *pArgStart = str_skip_whitespaces(aCmd + i); - if(pArgStart) - str_copy(aRawArgLine, pArgStart); + + int Skip = i; + while(pCmdWithArgs[Skip] && str_isspace(pCmdWithArgs[Skip])) + Skip++; + str_copy(aRawArgLine, pCmdWithArgs + Skip); + aCmd[i] = '\0'; int ROffset = m_pClient->m_ChatHelper.ChatCommandGetROffset(aCmd); diff --git a/src/game/client/components/chillerbot/warlist.cpp b/src/game/client/components/chillerbot/warlist.cpp index 44b80e184cc..b63b669c90d 100644 --- a/src/game/client/components/chillerbot/warlist.cpp +++ b/src/game/client/components/chillerbot/warlist.cpp @@ -881,414 +881,8 @@ bool CWarList::OnChatCmd(char Prefix, int ClientId, int Team, const char *pCmd, return false; if(ClientId != m_pClient->m_Snap.m_LocalClientId) return false; - char aBuf[512]; - if(!str_comp(pCmd, "search")) // "search " - { - if(NumArgs != 1) - { - str_format(aBuf, sizeof(aBuf), "Error: expected 1 argument but got %d", NumArgs); - m_pClient->m_Chat.AddLine(-2, 0, aBuf); - return true; - } - m_pClient->m_Chat.AddLine(-2, 0, "[search] fullmatch:"); - if(!SearchName(ppArgs[0], false)) - { - m_pClient->m_Chat.AddLine(-2, 0, "[search] partial:"); - SearchName(ppArgs[0], true); - } - } - else if(!str_comp(pCmd, "help")) - { - m_pClient->m_Chat.AddLine(-2, 0, "=== chillerbot-ux warlist ==="); - m_pClient->m_Chat.AddLine(-2, 0, "!addwar "); - m_pClient->m_Chat.AddLine(-2, 0, "!addteam "); - m_pClient->m_Chat.AddLine(-2, 0, "!peace "); - m_pClient->m_Chat.AddLine(-2, 0, "!war "); - m_pClient->m_Chat.AddLine(-2, 0, "!team "); - m_pClient->m_Chat.AddLine(-2, 0, "!unfriend "); - m_pClient->m_Chat.AddLine(-2, 0, "!addreason [--force] "); - m_pClient->m_Chat.AddLine(-2, 0, "!search "); - m_pClient->m_Chat.AddLine(-2, 0, "!create [name]"); - } - else if(!str_comp(pCmd, "create")) // "create [name]" - { - if(NumArgs < 1) - { - m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); - return true; - } - if(NumArgs < 2) - { - m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); - return true; - } - char aType[512]; - char aFolder[512]; - str_copy(aType, ppArgs[0], sizeof(aType)); - str_copy(aFolder, ppArgs[1], sizeof(aFolder)); - char aName[64]; - aName[0] = '\0'; - if(NumArgs > 2) - str_copy(aName, ppArgs[2], sizeof(aName)); - if(str_comp(aType, "war") && - str_comp(aType, "team") && - str_comp(aType, "neutral") && - str_comp(aType, "traitor")) - { - m_pClient->m_Chat.AddLine(-2, 0, "Error type has to be one of those: "); - return true; - } - const char *aTypes[] = {"war", "team", "neutral", "traitor"}; - for(const auto *CheckType : aTypes) - { - str_format(aBuf, sizeof(aBuf), "chillerbot/warlist/%s/%s", CheckType, aFolder); - if(Storage()->FolderExists(aBuf, IStorage::TYPE_SAVE)) - { - char aError[256]; - str_format(aError, sizeof(aError), "Error folder already exists: %s", aBuf); - m_pClient->m_Chat.AddLine(-2, 0, aError); - return true; - } - } - str_format(aBuf, sizeof(aBuf), "chillerbot/warlist/%s", aType); - if(!Storage()->CreateFolder(aBuf, IStorage::TYPE_SAVE)) - { - str_format(aBuf, sizeof(aBuf), "Failed to create folder %s/%s", aType, aFolder); - m_pClient->m_Chat.AddLine(-2, 0, aBuf); - return true; - } - str_format(aBuf, sizeof(aBuf), "chillerbot/warlist/%s/%s", aType, aFolder); - if(!Storage()->CreateFolder(aBuf, IStorage::TYPE_SAVE)) - { - str_format(aBuf, sizeof(aBuf), "Failed to create folder %s/%s", aType, aFolder); - m_pClient->m_Chat.AddLine(-2, 0, aBuf); - return true; - } - if(aName[0]) - { - if(SearchName(aName, false, true)) - { - str_format(aBuf, sizeof(aBuf), "Error: name '%s' is already used in different folder", aName); - m_pClient->m_Chat.AddLine(-2, 0, aBuf); - return true; - } - str_format(aBuf, sizeof(aBuf), "Created folder %s/%s and add name '%s'", aType, aFolder, aName); - AddWar(aFolder, aName); - } - else - { - str_format(aBuf, sizeof(aBuf), "Created folder %s/%s", aType, aFolder); - } - m_pClient->m_Chat.AddLine(-2, 0, aBuf); - } - else if(!str_comp(pCmd, "addwar")) // "addwar " - { - if(NumArgs < 1) - { - m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); - return true; - } - if(NumArgs < 2) - { - m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); - return true; - } - char aFolder[512]; - char aName[512]; - str_copy(aFolder, ppArgs[0], sizeof(aFolder)); - str_copy(aName, ppArgs[1], sizeof(aName)); - AddWar(aFolder, aName); - } - else if(!str_comp(pCmd, "addteam")) // "addteam " - { - if(NumArgs < 1) - { - m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); - return true; - } - if(NumArgs < 2) - { - m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); - return true; - } - char aFolder[512]; - char aName[512]; - str_copy(aFolder, ppArgs[0], sizeof(aFolder)); - str_copy(aName, ppArgs[1], sizeof(aName)); - char aFilename[1024]; - str_format(aFilename, sizeof(aFilename), "chillerbot/warlist/team/%s/names.txt", aFolder); - IOHANDLE File = Storage()->OpenFile(aFilename, IOFLAG_APPEND, IStorage::TYPE_SAVE); - if(!File) - { - str_format(aBuf, sizeof(aBuf), "failed to open war list file '%s'", aFilename); - m_pClient->m_Chat.AddLine(-2, 0, aBuf); - return true; - } - io_write(File, aName, str_length(aName)); - io_write_newline(File); - io_close(File); - - str_format(aBuf, sizeof(aBuf), "Added '%s' to the folder %s", aName, aFolder); - ReloadList(); - m_pClient->m_Chat.AddLine(-2, 0, aBuf); - } - else if(!str_comp(pCmd, "addtraitor")) // "addtraitor " - { - if(NumArgs < 1) - { - m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); - return true; - } - if(NumArgs < 2) - { - m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); - return true; - } - char aFolder[512]; - char aName[512]; - str_copy(aFolder, ppArgs[0], sizeof(aFolder)); - str_copy(aName, ppArgs[1], sizeof(aName)); - char aFilename[1024]; - str_format(aFilename, sizeof(aFilename), "chillerbot/warlist/traitor/%s/names.txt", aFolder); - IOHANDLE File = Storage()->OpenFile(aFilename, IOFLAG_APPEND, IStorage::TYPE_SAVE); - if(!File) - { - str_format(aBuf, sizeof(aBuf), "failed to open war list file '%s'", aFilename); - m_pClient->m_Chat.AddLine(-2, 0, aBuf); - return true; - } - - io_write(File, aName, str_length(aName)); - io_write_newline(File); - io_close(File); - - str_format(aBuf, sizeof(aBuf), "Added '%s' to the folder %s", aName, aFolder); - ReloadList(); - m_pClient->m_Chat.AddLine(-2, 0, aBuf); - } - else if(!str_comp(pCmd, "peace")) // "peace " - { - if(NumArgs < 1) - { - m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); - return true; - } - char aFolder[512]; - str_copy(aFolder, ppArgs[0], sizeof(aFolder)); - char aPath[1024]; - char aPeacePath[1024]; - str_format(aPath, sizeof(aPath), "chillerbot/warlist/war/%s/names.txt", aFolder); - str_format(aPeacePath, sizeof(aPeacePath), "chillerbot/warlist/neutral/%s/names.txt", aFolder); - IOHANDLE File = Storage()->OpenFile(aPath, IOFLAG_READ, IStorage::TYPE_SAVE); - if(!File) - { - str_format(aBuf, sizeof(aBuf), "failed to open war list file '%s'", aPath); - m_pClient->m_Chat.AddLine(-2, 0, aBuf); - return true; - } - io_close(File); - File = Storage()->OpenFile(aPeacePath, IOFLAG_READ, IStorage::TYPE_SAVE); - if(File) - { - str_format(aBuf, sizeof(aBuf), "Peace entry already exists '%s'", aPeacePath); - m_pClient->m_Chat.AddLine(-2, 0, aBuf); - io_close(File); - return true; - } - - str_format(aPath, sizeof(aPath), "chillerbot/warlist/war/%s", aFolder); - str_format(aPeacePath, sizeof(aPeacePath), "chillerbot/warlist/neutral/%s", aFolder); - Storage()->RenameFile(aPath, aPeacePath, IStorage::TYPE_SAVE); - - str_format(aBuf, sizeof(aBuf), "Moved folder %s from war/ to neutral/", aFolder); - ReloadList(); - m_pClient->m_Chat.AddLine(-2, 0, aBuf); - } - else if(!str_comp(pCmd, "unfriend")) // "unfriend " - { - if(NumArgs < 1) - { - m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); - return true; - } - char aFolder[512]; - str_copy(aFolder, ppArgs[0], sizeof(aFolder)); - char aPath[1024]; - char aNeutralPath[1024]; - str_format(aPath, sizeof(aPath), "chillerbot/warlist/team/%s/names.txt", aFolder); - str_format(aNeutralPath, sizeof(aNeutralPath), "chillerbot/warlist/neutral/%s/names.txt", aFolder); - IOHANDLE File = Storage()->OpenFile(aPath, IOFLAG_READ, IStorage::TYPE_SAVE); - if(!File) - { - str_format(aBuf, sizeof(aBuf), "failed to open war list file '%s'", aPath); - m_pClient->m_Chat.AddLine(-2, 0, aBuf); - return true; - } - io_close(File); - File = Storage()->OpenFile(aNeutralPath, IOFLAG_READ, IStorage::TYPE_SAVE); - if(File) - { - str_format(aBuf, sizeof(aBuf), "Neutral entry already exists '%s'", aNeutralPath); - m_pClient->m_Chat.AddLine(-2, 0, aBuf); - io_close(File); - return true; - } - - str_format(aPath, sizeof(aPath), "chillerbot/warlist/team/%s", aFolder); - str_format(aNeutralPath, sizeof(aNeutralPath), "chillerbot/warlist/neutral/%s", aFolder); - Storage()->RenameFile(aPath, aNeutralPath, IStorage::TYPE_SAVE); - - str_format(aBuf, sizeof(aBuf), "Moved folder %s from team/ to neutral/", aFolder); - ReloadList(); - m_pClient->m_Chat.AddLine(-2, 0, aBuf); - } - else if(!str_comp(pCmd, "team")) // "team " - { - if(NumArgs < 1) - { - m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); - return true; - } - char aFolder[512]; - str_copy(aFolder, ppArgs[0], sizeof(aFolder)); - char aPath[1024]; - char aTeamPath[1024]; - str_format(aPath, sizeof(aPath), "chillerbot/warlist/neutral/%s/names.txt", aFolder); - str_format(aTeamPath, sizeof(aTeamPath), "chillerbot/warlist/team/%s/names.txt", aFolder); - IOHANDLE File = Storage()->OpenFile(aPath, IOFLAG_READ, IStorage::TYPE_SAVE); - if(!File) - { - str_format(aBuf, sizeof(aBuf), "failed to open war list file '%s'", aPath); - m_pClient->m_Chat.AddLine(-2, 0, aBuf); - return true; - } - io_close(File); - File = Storage()->OpenFile(aTeamPath, IOFLAG_READ, IStorage::TYPE_SAVE); - if(File) - { - str_format(aBuf, sizeof(aBuf), "Peace entry already exists '%s'", aTeamPath); - m_pClient->m_Chat.AddLine(-2, 0, aBuf); - io_close(File); - return true; - } - - str_format(aPath, sizeof(aPath), "chillerbot/warlist/neutral/%s", aFolder); - str_format(aTeamPath, sizeof(aTeamPath), "chillerbot/warlist/team/%s", aFolder); - Storage()->RenameFile(aPath, aTeamPath, IStorage::TYPE_SAVE); - - str_format(aBuf, sizeof(aBuf), "Moved folder %s from neutral/ to team/", aFolder); - ReloadList(); - m_pClient->m_Chat.AddLine(-2, 0, aBuf); - } - else if(!str_comp(pCmd, "war")) // "war " - { - if(NumArgs < 1) - { - m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); - return true; - } - char aFolder[512]; - str_copy(aFolder, ppArgs[0], sizeof(aFolder)); - char aPath[1024]; - char aWarPath[1024]; - str_format(aPath, sizeof(aPath), "chillerbot/warlist/neutral/%s/names.txt", aFolder); - str_format(aWarPath, sizeof(aWarPath), "chillerbot/warlist/war/%s/names.txt", aFolder); - IOHANDLE File = Storage()->OpenFile(aPath, IOFLAG_READ, IStorage::TYPE_SAVE); - if(!File) - { - str_format(aBuf, sizeof(aBuf), "failed to open war list file '%s'", aPath); - m_pClient->m_Chat.AddLine(-2, 0, aBuf); - return true; - } - io_close(File); - File = Storage()->OpenFile(aWarPath, IOFLAG_READ, IStorage::TYPE_SAVE); - if(File) - { - str_format(aBuf, sizeof(aBuf), "War entry already exists '%s'", aWarPath); - m_pClient->m_Chat.AddLine(-2, 0, aBuf); - io_close(File); - return true; - } - - str_format(aPath, sizeof(aPath), "chillerbot/warlist/neutral/%s", aFolder); - str_format(aWarPath, sizeof(aWarPath), "chillerbot/warlist/war/%s", aFolder); - Storage()->RenameFile(aPath, aWarPath, IStorage::TYPE_SAVE); - - str_format(aBuf, sizeof(aBuf), "Moved folder %s from neutral/ to war/", aFolder); - ReloadList(); - m_pClient->m_Chat.AddLine(-2, 0, aBuf); - } - else if(!str_comp(pCmd, "addreason")) // "addreason " - { - if(NumArgs < 1) - { - m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); - return true; - } - if(NumArgs < 2) - { - m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); - return true; - } - char aFolder[512]; - char aReason[512]; - str_copy(aFolder, ppArgs[0], sizeof(aFolder)); - str_copy(aReason, ppArgs[1], sizeof(aReason)); - const char *pReason = aReason; - bool Force = true; - if(str_startswith(aReason, "-f ")) - pReason += str_length("-f "); - else if(str_startswith(aReason, "--force ")) - pReason += str_length("--force "); - else - Force = false; - char aFilename[1024]; - str_format(aFilename, sizeof(aFilename), "chillerbot/warlist/war/%s/reason.txt", aFolder); - IOHANDLE File = Storage()->OpenFile(aFilename, IOFLAG_READ, IStorage::TYPE_SAVE); - if(File) - { - const char *pLine; - CLineReader Reader; - - if(!Reader.OpenFile(File)) - { - str_format(aBuf, sizeof(aBuf), "failed to open '%s'", aFilename); - Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "warlist", aBuf); - return true; - } - - // read one line only - pLine = Reader.Get(); - if(pLine && pLine[0] != '\0' && !Force) - { - str_format(aBuf, sizeof(aBuf), "Folder %s already has a reason. Use -f to overwrite reason: %s", aFolder, pLine); - m_pClient->m_Chat.AddLine(-2, 0, aBuf); - return true; - } - io_close(File); - } - File = Storage()->OpenFile(aFilename, IOFLAG_WRITE, IStorage::TYPE_SAVE); - if(!File) - { - str_format(aBuf, sizeof(aBuf), "Failed to open file for writing chillerbot/warlist/war/%s/reason.txt", aFolder); - m_pClient->m_Chat.AddLine(-2, 0, aBuf); - return true; - } - str_format(aBuf, sizeof(aBuf), "Adding reason to folder='%s' reason='%s'", aFolder, pReason); - io_write(File, pReason, str_length(pReason)); - char aDate[32]; - str_timestamp(aDate, sizeof(aDate)); - str_format(aBuf, sizeof(aBuf), " (%s)", aDate); - io_write(File, aBuf, str_length(aBuf)); - io_write_newline(File); - io_close(File); - ReloadList(); - m_pClient->m_Chat.AddLine(-1, 0, aBuf); - } - else - { - return false; - } - return true; + if(g_Config.m_ClWarListAdvanced) + return OnChatCmdAdvanced(Prefix, ClientId, Team, pCmd, NumArgs, ppArgs, pRawArgLine); + return OnChatCmdSimple(Prefix, ClientId, Team, pCmd, NumArgs, ppArgs, pRawArgLine); } diff --git a/src/game/client/components/chillerbot/warlist.h b/src/game/client/components/chillerbot/warlist.h index 28c08fd7047..3f9874b74c5 100644 --- a/src/game/client/components/chillerbot/warlist.h +++ b/src/game/client/components/chillerbot/warlist.h @@ -121,6 +121,9 @@ class CWarList : public CComponent bool OnChatCmd(char Prefix, int ClientId, int Team, const char *pCmd, int NumArgs, const char **ppArgs, const char *pRawArgLine); + bool OnChatCmdSimple(char Prefix, int ClientId, int Team, const char *pCmd, int NumArgs, const char **ppArgs, const char *pRawArgLine); + bool OnChatCmdAdvanced(char Prefix, int ClientId, int Team, const char *pCmd, int NumArgs, const char **ppArgs, const char *pRawArgLine); + void GetWarReason(const char *pName, char *pReason, int ReasonSize); void GetWarClansStr(char *pBuf, int Size); diff --git a/src/game/client/components/chillerbot/warlist_commands_advanced.cpp b/src/game/client/components/chillerbot/warlist_commands_advanced.cpp new file mode 100644 index 00000000000..cafb5e0921b --- /dev/null +++ b/src/game/client/components/chillerbot/warlist_commands_advanced.cpp @@ -0,0 +1,424 @@ +// ChillerDragon 2021 - chillerbot ux + +#include +#include +#include +#include + +#include + +#include "warlist.h" + +bool CWarList::OnChatCmdAdvanced(char Prefix, int ClientId, int Team, const char *pCmd, int NumArgs, const char **ppArgs, const char *pRawArgLine) +{ + char aBuf[512]; + if(!str_comp(pCmd, "search")) // "search " + { + if(NumArgs != 1) + { + str_format(aBuf, sizeof(aBuf), "Error: expected 1 argument but got %d", NumArgs); + m_pClient->m_Chat.AddLine(-2, 0, aBuf); + return true; + } + m_pClient->m_Chat.AddLine(-2, 0, "[search] fullmatch:"); + if(!SearchName(ppArgs[0], false)) + { + m_pClient->m_Chat.AddLine(-2, 0, "[search] partial:"); + SearchName(ppArgs[0], true); + } + } + else if(!str_comp(pCmd, "help")) + { + m_pClient->m_Chat.AddLine(-2, 0, "=== chillerbot-ux warlist ==="); + m_pClient->m_Chat.AddLine(-2, 0, "!addwar "); + m_pClient->m_Chat.AddLine(-2, 0, "!addteam "); + m_pClient->m_Chat.AddLine(-2, 0, "!peace "); + m_pClient->m_Chat.AddLine(-2, 0, "!war "); + m_pClient->m_Chat.AddLine(-2, 0, "!team "); + m_pClient->m_Chat.AddLine(-2, 0, "!unfriend "); + m_pClient->m_Chat.AddLine(-2, 0, "!addreason [--force] "); + m_pClient->m_Chat.AddLine(-2, 0, "!search "); + m_pClient->m_Chat.AddLine(-2, 0, "!create [name]"); + } + else if(!str_comp(pCmd, "create")) // "create [name]" + { + if(NumArgs < 1) + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); + return true; + } + if(NumArgs < 2) + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); + return true; + } + char aType[512]; + char aFolder[512]; + str_copy(aType, ppArgs[0], sizeof(aType)); + str_copy(aFolder, ppArgs[1], sizeof(aFolder)); + char aName[64]; + aName[0] = '\0'; + if(NumArgs > 2) + str_copy(aName, ppArgs[2], sizeof(aName)); + if(str_comp(aType, "war") && + str_comp(aType, "team") && + str_comp(aType, "neutral") && + str_comp(aType, "traitor")) + { + m_pClient->m_Chat.AddLine(-2, 0, "Error type has to be one of those: "); + return true; + } + const char *aTypes[] = {"war", "team", "neutral", "traitor"}; + for(const auto *CheckType : aTypes) + { + str_format(aBuf, sizeof(aBuf), "chillerbot/warlist/%s/%s", CheckType, aFolder); + if(Storage()->FolderExists(aBuf, IStorage::TYPE_SAVE)) + { + char aError[256]; + str_format(aError, sizeof(aError), "Error folder already exists: %s", aBuf); + m_pClient->m_Chat.AddLine(-2, 0, aError); + return true; + } + } + str_format(aBuf, sizeof(aBuf), "chillerbot/warlist/%s", aType); + if(!Storage()->CreateFolder(aBuf, IStorage::TYPE_SAVE)) + { + str_format(aBuf, sizeof(aBuf), "Failed to create folder %s/%s", aType, aFolder); + m_pClient->m_Chat.AddLine(-2, 0, aBuf); + return true; + } + str_format(aBuf, sizeof(aBuf), "chillerbot/warlist/%s/%s", aType, aFolder); + if(!Storage()->CreateFolder(aBuf, IStorage::TYPE_SAVE)) + { + str_format(aBuf, sizeof(aBuf), "Failed to create folder %s/%s", aType, aFolder); + m_pClient->m_Chat.AddLine(-2, 0, aBuf); + return true; + } + if(aName[0]) + { + if(SearchName(aName, false, true)) + { + str_format(aBuf, sizeof(aBuf), "Error: name '%s' is already used in different folder", aName); + m_pClient->m_Chat.AddLine(-2, 0, aBuf); + return true; + } + str_format(aBuf, sizeof(aBuf), "Created folder %s/%s and add name '%s'", aType, aFolder, aName); + AddWar(aFolder, aName); + } + else + { + str_format(aBuf, sizeof(aBuf), "Created folder %s/%s", aType, aFolder); + } + m_pClient->m_Chat.AddLine(-2, 0, aBuf); + } + else if(!str_comp(pCmd, "addwar")) // "addwar " + { + if(NumArgs < 1) + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); + return true; + } + if(NumArgs < 2) + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); + return true; + } + char aFolder[512]; + char aName[512]; + str_copy(aFolder, ppArgs[0], sizeof(aFolder)); + str_copy(aName, ppArgs[1], sizeof(aName)); + AddWar(aFolder, aName); + } + else if(!str_comp(pCmd, "addteam")) // "addteam " + { + if(NumArgs < 1) + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); + return true; + } + if(NumArgs < 2) + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); + return true; + } + char aFolder[512]; + char aName[512]; + str_copy(aFolder, ppArgs[0], sizeof(aFolder)); + str_copy(aName, ppArgs[1], sizeof(aName)); + char aFilename[1024]; + str_format(aFilename, sizeof(aFilename), "chillerbot/warlist/team/%s/names.txt", aFolder); + IOHANDLE File = Storage()->OpenFile(aFilename, IOFLAG_APPEND, IStorage::TYPE_SAVE); + if(!File) + { + str_format(aBuf, sizeof(aBuf), "failed to open war list file '%s'", aFilename); + m_pClient->m_Chat.AddLine(-2, 0, aBuf); + return true; + } + + io_write(File, aName, str_length(aName)); + io_write_newline(File); + io_close(File); + + str_format(aBuf, sizeof(aBuf), "Added '%s' to the folder %s", aName, aFolder); + ReloadList(); + m_pClient->m_Chat.AddLine(-2, 0, aBuf); + } + else if(!str_comp(pCmd, "addtraitor")) // "addtraitor " + { + if(NumArgs < 1) + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); + return true; + } + if(NumArgs < 2) + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); + return true; + } + char aFolder[512]; + char aName[512]; + str_copy(aFolder, ppArgs[0], sizeof(aFolder)); + str_copy(aName, ppArgs[1], sizeof(aName)); + char aFilename[1024]; + str_format(aFilename, sizeof(aFilename), "chillerbot/warlist/traitor/%s/names.txt", aFolder); + IOHANDLE File = Storage()->OpenFile(aFilename, IOFLAG_APPEND, IStorage::TYPE_SAVE); + if(!File) + { + str_format(aBuf, sizeof(aBuf), "failed to open war list file '%s'", aFilename); + m_pClient->m_Chat.AddLine(-2, 0, aBuf); + return true; + } + + io_write(File, aName, str_length(aName)); + io_write_newline(File); + io_close(File); + + str_format(aBuf, sizeof(aBuf), "Added '%s' to the folder %s", aName, aFolder); + ReloadList(); + m_pClient->m_Chat.AddLine(-2, 0, aBuf); + } + else if(!str_comp(pCmd, "peace")) // "peace " + { + if(NumArgs < 1) + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); + return true; + } + char aFolder[512]; + str_copy(aFolder, ppArgs[0], sizeof(aFolder)); + char aPath[1024]; + char aPeacePath[1024]; + str_format(aPath, sizeof(aPath), "chillerbot/warlist/war/%s/names.txt", aFolder); + str_format(aPeacePath, sizeof(aPeacePath), "chillerbot/warlist/neutral/%s/names.txt", aFolder); + IOHANDLE File = Storage()->OpenFile(aPath, IOFLAG_READ, IStorage::TYPE_SAVE); + if(!File) + { + str_format(aBuf, sizeof(aBuf), "failed to open war list file '%s'", aPath); + m_pClient->m_Chat.AddLine(-2, 0, aBuf); + return true; + } + io_close(File); + File = Storage()->OpenFile(aPeacePath, IOFLAG_READ, IStorage::TYPE_SAVE); + if(File) + { + str_format(aBuf, sizeof(aBuf), "Peace entry already exists '%s'", aPeacePath); + m_pClient->m_Chat.AddLine(-2, 0, aBuf); + io_close(File); + return true; + } + + str_format(aPath, sizeof(aPath), "chillerbot/warlist/war/%s", aFolder); + str_format(aPeacePath, sizeof(aPeacePath), "chillerbot/warlist/neutral/%s", aFolder); + Storage()->RenameFile(aPath, aPeacePath, IStorage::TYPE_SAVE); + + str_format(aBuf, sizeof(aBuf), "Moved folder %s from war/ to neutral/", aFolder); + ReloadList(); + m_pClient->m_Chat.AddLine(-2, 0, aBuf); + } + else if(!str_comp(pCmd, "unfriend")) // "unfriend " + { + if(NumArgs < 1) + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); + return true; + } + char aFolder[512]; + str_copy(aFolder, ppArgs[0], sizeof(aFolder)); + char aPath[1024]; + char aNeutralPath[1024]; + str_format(aPath, sizeof(aPath), "chillerbot/warlist/team/%s/names.txt", aFolder); + str_format(aNeutralPath, sizeof(aNeutralPath), "chillerbot/warlist/neutral/%s/names.txt", aFolder); + IOHANDLE File = Storage()->OpenFile(aPath, IOFLAG_READ, IStorage::TYPE_SAVE); + if(!File) + { + str_format(aBuf, sizeof(aBuf), "failed to open war list file '%s'", aPath); + m_pClient->m_Chat.AddLine(-2, 0, aBuf); + return true; + } + io_close(File); + File = Storage()->OpenFile(aNeutralPath, IOFLAG_READ, IStorage::TYPE_SAVE); + if(File) + { + str_format(aBuf, sizeof(aBuf), "Neutral entry already exists '%s'", aNeutralPath); + m_pClient->m_Chat.AddLine(-2, 0, aBuf); + io_close(File); + return true; + } + + str_format(aPath, sizeof(aPath), "chillerbot/warlist/team/%s", aFolder); + str_format(aNeutralPath, sizeof(aNeutralPath), "chillerbot/warlist/neutral/%s", aFolder); + Storage()->RenameFile(aPath, aNeutralPath, IStorage::TYPE_SAVE); + + str_format(aBuf, sizeof(aBuf), "Moved folder %s from team/ to neutral/", aFolder); + ReloadList(); + m_pClient->m_Chat.AddLine(-2, 0, aBuf); + } + else if(!str_comp(pCmd, "team")) // "team " + { + if(NumArgs < 1) + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); + return true; + } + char aFolder[512]; + str_copy(aFolder, ppArgs[0], sizeof(aFolder)); + char aPath[1024]; + char aTeamPath[1024]; + str_format(aPath, sizeof(aPath), "chillerbot/warlist/neutral/%s/names.txt", aFolder); + str_format(aTeamPath, sizeof(aTeamPath), "chillerbot/warlist/team/%s/names.txt", aFolder); + IOHANDLE File = Storage()->OpenFile(aPath, IOFLAG_READ, IStorage::TYPE_SAVE); + if(!File) + { + str_format(aBuf, sizeof(aBuf), "failed to open war list file '%s'", aPath); + m_pClient->m_Chat.AddLine(-2, 0, aBuf); + return true; + } + io_close(File); + File = Storage()->OpenFile(aTeamPath, IOFLAG_READ, IStorage::TYPE_SAVE); + if(File) + { + str_format(aBuf, sizeof(aBuf), "Peace entry already exists '%s'", aTeamPath); + m_pClient->m_Chat.AddLine(-2, 0, aBuf); + io_close(File); + return true; + } + + str_format(aPath, sizeof(aPath), "chillerbot/warlist/neutral/%s", aFolder); + str_format(aTeamPath, sizeof(aTeamPath), "chillerbot/warlist/team/%s", aFolder); + Storage()->RenameFile(aPath, aTeamPath, IStorage::TYPE_SAVE); + + str_format(aBuf, sizeof(aBuf), "Moved folder %s from neutral/ to team/", aFolder); + ReloadList(); + m_pClient->m_Chat.AddLine(-2, 0, aBuf); + } + else if(!str_comp(pCmd, "war")) // "war " + { + if(NumArgs < 1) + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); + return true; + } + char aFolder[512]; + str_copy(aFolder, ppArgs[0], sizeof(aFolder)); + char aPath[1024]; + char aWarPath[1024]; + str_format(aPath, sizeof(aPath), "chillerbot/warlist/neutral/%s/names.txt", aFolder); + str_format(aWarPath, sizeof(aWarPath), "chillerbot/warlist/war/%s/names.txt", aFolder); + IOHANDLE File = Storage()->OpenFile(aPath, IOFLAG_READ, IStorage::TYPE_SAVE); + if(!File) + { + str_format(aBuf, sizeof(aBuf), "failed to open war list file '%s'", aPath); + m_pClient->m_Chat.AddLine(-2, 0, aBuf); + return true; + } + io_close(File); + File = Storage()->OpenFile(aWarPath, IOFLAG_READ, IStorage::TYPE_SAVE); + if(File) + { + str_format(aBuf, sizeof(aBuf), "War entry already exists '%s'", aWarPath); + m_pClient->m_Chat.AddLine(-2, 0, aBuf); + io_close(File); + return true; + } + + str_format(aPath, sizeof(aPath), "chillerbot/warlist/neutral/%s", aFolder); + str_format(aWarPath, sizeof(aWarPath), "chillerbot/warlist/war/%s", aFolder); + Storage()->RenameFile(aPath, aWarPath, IStorage::TYPE_SAVE); + + str_format(aBuf, sizeof(aBuf), "Moved folder %s from neutral/ to war/", aFolder); + ReloadList(); + m_pClient->m_Chat.AddLine(-2, 0, aBuf); + } + else if(!str_comp(pCmd, "addreason")) // "addreason " + { + if(NumArgs < 1) + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); + return true; + } + if(NumArgs < 2) + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); + return true; + } + char aFolder[512]; + char aReason[512]; + str_copy(aFolder, ppArgs[0], sizeof(aFolder)); + str_copy(aReason, ppArgs[1], sizeof(aReason)); + const char *pReason = aReason; + bool Force = true; + if(str_startswith(aReason, "-f ")) + pReason += str_length("-f "); + else if(str_startswith(aReason, "--force ")) + pReason += str_length("--force "); + else + Force = false; + char aFilename[1024]; + str_format(aFilename, sizeof(aFilename), "chillerbot/warlist/war/%s/reason.txt", aFolder); + IOHANDLE File = Storage()->OpenFile(aFilename, IOFLAG_READ, IStorage::TYPE_SAVE); + if(File) + { + const char *pLine; + CLineReader Reader; + + if(!Reader.OpenFile(File)) + { + str_format(aBuf, sizeof(aBuf), "failed to open '%s'", aFilename); + Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "warlist", aBuf); + return true; + } + + // read one line only + pLine = Reader.Get(); + if(pLine && pLine[0] != '\0' && !Force) + { + str_format(aBuf, sizeof(aBuf), "Folder %s already has a reason. Use -f to overwrite reason: %s", aFolder, pLine); + m_pClient->m_Chat.AddLine(-2, 0, aBuf); + return true; + } + io_close(File); + } + File = Storage()->OpenFile(aFilename, IOFLAG_WRITE, IStorage::TYPE_SAVE); + if(!File) + { + str_format(aBuf, sizeof(aBuf), "Failed to open file for writing chillerbot/warlist/war/%s/reason.txt", aFolder); + m_pClient->m_Chat.AddLine(-2, 0, aBuf); + return true; + } + str_format(aBuf, sizeof(aBuf), "Adding reason to folder='%s' reason='%s'", aFolder, pReason); + io_write(File, pReason, str_length(pReason)); + char aDate[32]; + str_timestamp(aDate, sizeof(aDate)); + str_format(aBuf, sizeof(aBuf), " (%s)", aDate); + io_write(File, aBuf, str_length(aBuf)); + io_write_newline(File); + io_close(File); + ReloadList(); + m_pClient->m_Chat.AddLine(-1, 0, aBuf); + } + else + { + return false; + } + return true; +} diff --git a/src/game/client/components/chillerbot/warlist_commands_simple.cpp b/src/game/client/components/chillerbot/warlist_commands_simple.cpp new file mode 100644 index 00000000000..f1a13d1ddf0 --- /dev/null +++ b/src/game/client/components/chillerbot/warlist_commands_simple.cpp @@ -0,0 +1,98 @@ +// ChillerDragon 2021 - chillerbot ux + +#include +#include +#include +#include + +#include + +#include "warlist.h" + +bool CWarList::OnChatCmdSimple(char Prefix, int ClientId, int Team, const char *pCmd, int NumArgs, const char **ppArgs, const char *pRawArgLine) +{ + if(!str_comp(pCmd, "search")) // "search " + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: search only works in advanced warlist mode"); + return true; + } + else if(!str_comp(pCmd, "help")) + { + m_pClient->m_Chat.AddLine(-2, 0, "=== chillerbot-ux warlist ==="); + m_pClient->m_Chat.AddLine(-2, 0, "!addwar "); + m_pClient->m_Chat.AddLine(-2, 0, "!addteam "); + m_pClient->m_Chat.AddLine(-2, 0, "!peace "); + m_pClient->m_Chat.AddLine(-2, 0, "!war "); + m_pClient->m_Chat.AddLine(-2, 0, "!team "); + m_pClient->m_Chat.AddLine(-2, 0, "!unfriend "); + m_pClient->m_Chat.AddLine(-2, 0, "!addreason [--force] "); + m_pClient->m_Chat.AddLine(-2, 0, "!search "); + m_pClient->m_Chat.AddLine(-2, 0, "!create [name]"); + } + else if(!str_comp(pCmd, "create")) // "create [name]" + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: create only works in advanced warlist mode"); + return true; + } + else if(!str_comp(pCmd, "addwar")) // "addwar " + { + if(NumArgs < 1) + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: missing argument "); + return true; + } + + if(!Storage()->CreateFolder("chillerbot/warlist/war", IStorage::TYPE_SAVE)) + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: failed to create war folder"); + return true; + } + if(!Storage()->CreateFolder("chillerbot/warlist/war/war", IStorage::TYPE_SAVE)) + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: failed to create war/war folder"); + return true; + } + + AddWar("war", pRawArgLine); + } + else if(!str_comp(pCmd, "addteam")) // "addteam " + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: addteam only works in advanced warlist mode"); + return true; + } + else if(!str_comp(pCmd, "addtraitor")) // "addtraitor " + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: addtraitor only works in advanced warlist mode"); + return true; + } + else if(!str_comp(pCmd, "peace")) // "peace " + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: peace only works in advanced warlist mode"); + return true; + } + else if(!str_comp(pCmd, "unfriend")) // "unfriend " + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: unfriend only works in advanced warlist mode"); + return true; + } + else if(!str_comp(pCmd, "team")) // "team " + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: team only works in advanced warlist mode"); + return true; + } + else if(!str_comp(pCmd, "war")) // "war " + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: war only works in advanced warlist mode"); + return true; + } + else if(!str_comp(pCmd, "addreason")) // "addreason " + { + m_pClient->m_Chat.AddLine(-2, 0, "Error: addreason only works in advanced warlist mode"); + return true; + } + else + { + return false; + } + return true; +}