From 44f880a28551e4cd24a32e4792b39a279f99296b Mon Sep 17 00:00:00 2001 From: Mmokane Date: Wed, 24 Apr 2024 02:35:31 +0100 Subject: [PATCH] trying to add a bot command, almost done --- .vscode/settings.json | 3 +++ Server.cpp | 56 ++++++++++++++++++++++++++++++++++++------- Server.hpp | 3 +++ channel.cpp | 4 +++- channel.hpp | 2 ++ 5 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c37a50e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "C_Cpp.errorSquiggles": "enabled" +} \ No newline at end of file diff --git a/Server.cpp b/Server.cpp index 1736b9c..24c64a1 100644 --- a/Server.cpp +++ b/Server.cpp @@ -527,7 +527,8 @@ int stringToInt(const std::string& str) { // ........AND FINISH HERE ......////// -void Server::handleClientData(int fd) { +void Server::handleClientData(int fd) +{ std::string command; char buffer[BUFFER_SIZE]; memset(buffer, 0, sizeof(buffer)); @@ -858,8 +859,31 @@ void Server::handleClientData(int fd) { send(fd, errorMessage.c_str(), errorMessage.size(), 0); } } - - + else if (startsWith(command, "BOT ")) + { + std::string channelName, botname, start, end, guessed; + std::istringstream iss(command.substr(4)); + iss >> channelName >> botname >> start >> end >> guessed; + channelName = trim(channelName); + botname = trim(botname); + start = trim(start); + end = trim(end); + guessed = trim(guessed); + if (channels.find(channelName) != channels.end() && channels[channelName].isOperator(fd)) + { + int random = randomInRange(stringToInt(start), stringToInt(end)); + if (random == stringToInt(guessed)) + { + std::string modeChangeMessage = ":server.host PRIVMSG #" + channelName + " :Congratulations " + botname + " you have guessed the number " + guessed + " correctly\n"; + send(fd, modeChangeMessage.c_str(), modeChangeMessage.size(), 0); + } + else + { + std::string modeChangeMessage = ":server.host PRIVMSG #" + channelName + " :Sorry " + botname + " you have guessed the number " + guessed + " incorrectly\n"; + send(fd, modeChangeMessage.c_str(), modeChangeMessage.size(), 0); + } + } + } else if (startsWith(command, "MODE ")) { std::string channelName, mode , nick; @@ -868,7 +892,7 @@ void Server::handleClientData(int fd) { iss >> channelName >> mode >> nick; channelName = trim(channelName); mode = trim(mode); - if (mode == "+l") + if (mode == "+l") // what is this ? nick = trim(nick); std::map::iterator it = channels.find(channelName); @@ -1027,6 +1051,7 @@ void Server::handleClientData(int fd) { } } } + //**************** STOOOOOOP HERE TOP G ... break; } @@ -1041,7 +1066,6 @@ void Server::handleClientData(int fd) { } } - void Server::clientCleanup(int fd) { for (std::vector::iterator it = _fds.begin(); it != _fds.end(); ++it) { if (it->fd == fd) { @@ -1059,7 +1083,24 @@ void Server::clientCleanup(int fd) { } } -void Server::closeFds() { +int randomInRange(int min, int max) { + // Check for invalid range + if (min > max) { + return -1; // Or throw an exception + } + + // Calculate range size (inclusive) + int range_size = max - min + 1; + + // Generate random number between 0 and RAND_MAX (scaled by range size) + double random_double = (double)rand() / (RAND_MAX + 1.0) * range_size; + + // Convert to integer and shift by minimum value + return (int)random_double + min; +} + +void Server::closeFds() +{ for (size_t i = 0; i < _clients.size(); i++){ int fd = _clients[i].getFd(); std::cout << "Client <" << fd << "> Disconnected" << std::endl; @@ -1068,6 +1109,5 @@ void Server::closeFds() { if (_serverSocketFd != -1) close(_serverSocketFd); - _fds.clear(); -} \ No newline at end of file +} diff --git a/Server.hpp b/Server.hpp index 8e36c55..aaddc6b 100644 --- a/Server.hpp +++ b/Server.hpp @@ -89,6 +89,9 @@ class Server { void handleClientData(int fd); void clientCleanup(int fd); void closeFds(); + }; +int randomInRange(int min, int max); + #endif \ No newline at end of file diff --git a/channel.cpp b/channel.cpp index a5a2c8b..26fb056 100644 --- a/channel.cpp +++ b/channel.cpp @@ -184,4 +184,6 @@ void Channel::removeOperator(const std::string& operatorName ) return; // Exit the function after removing the operator } } -} \ No newline at end of file +} + + diff --git a/channel.hpp b/channel.hpp index 0c9997c..131249a 100644 --- a/channel.hpp +++ b/channel.hpp @@ -71,6 +71,7 @@ public: void ejectUserfromivited(std::string nickname); std::string getOperatorNickname(int fd) const; void removeOperator(const std::string& operatorName ); + void setPass(const std::string &Newpass) { @@ -85,4 +86,5 @@ public: + #endif \ No newline at end of file