trying to add a bot command, almost done
This commit is contained in:
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"C_Cpp.errorSquiggles": "enabled"
|
||||||
|
}
|
54
Server.cpp
54
Server.cpp
@@ -527,7 +527,8 @@ int stringToInt(const std::string& str) {
|
|||||||
// ........AND FINISH HERE ......//////
|
// ........AND FINISH HERE ......//////
|
||||||
|
|
||||||
|
|
||||||
void Server::handleClientData(int fd) {
|
void Server::handleClientData(int fd)
|
||||||
|
{
|
||||||
std::string command;
|
std::string command;
|
||||||
char buffer[BUFFER_SIZE];
|
char buffer[BUFFER_SIZE];
|
||||||
memset(buffer, 0, sizeof(buffer));
|
memset(buffer, 0, sizeof(buffer));
|
||||||
@@ -858,8 +859,31 @@ void Server::handleClientData(int fd) {
|
|||||||
send(fd, errorMessage.c_str(), errorMessage.size(), 0);
|
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 "))
|
else if (startsWith(command, "MODE "))
|
||||||
{
|
{
|
||||||
std::string channelName, mode , nick;
|
std::string channelName, mode , nick;
|
||||||
@@ -868,7 +892,7 @@ void Server::handleClientData(int fd) {
|
|||||||
iss >> channelName >> mode >> nick;
|
iss >> channelName >> mode >> nick;
|
||||||
channelName = trim(channelName);
|
channelName = trim(channelName);
|
||||||
mode = trim(mode);
|
mode = trim(mode);
|
||||||
if (mode == "+l")
|
if (mode == "+l") // what is this ?
|
||||||
nick = trim(nick);
|
nick = trim(nick);
|
||||||
std::map<std::string, Channel>::iterator it = channels.find(channelName);
|
std::map<std::string, Channel>::iterator it = channels.find(channelName);
|
||||||
|
|
||||||
@@ -1027,6 +1051,7 @@ void Server::handleClientData(int fd) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//**************** STOOOOOOP HERE TOP G ...
|
//**************** STOOOOOOP HERE TOP G ...
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -1041,7 +1066,6 @@ void Server::handleClientData(int fd) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Server::clientCleanup(int fd) {
|
void Server::clientCleanup(int fd) {
|
||||||
for (std::vector<pollfd>::iterator it = _fds.begin(); it != _fds.end(); ++it) {
|
for (std::vector<pollfd>::iterator it = _fds.begin(); it != _fds.end(); ++it) {
|
||||||
if (it->fd == fd) {
|
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++){
|
for (size_t i = 0; i < _clients.size(); i++){
|
||||||
int fd = _clients[i].getFd();
|
int fd = _clients[i].getFd();
|
||||||
std::cout << "Client <" << fd << "> Disconnected" << std::endl;
|
std::cout << "Client <" << fd << "> Disconnected" << std::endl;
|
||||||
@@ -1068,6 +1109,5 @@ void Server::closeFds() {
|
|||||||
|
|
||||||
if (_serverSocketFd != -1)
|
if (_serverSocketFd != -1)
|
||||||
close(_serverSocketFd);
|
close(_serverSocketFd);
|
||||||
|
|
||||||
_fds.clear();
|
_fds.clear();
|
||||||
}
|
}
|
@@ -89,6 +89,9 @@ class Server {
|
|||||||
void handleClientData(int fd);
|
void handleClientData(int fd);
|
||||||
void clientCleanup(int fd);
|
void clientCleanup(int fd);
|
||||||
void closeFds();
|
void closeFds();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int randomInRange(int min, int max);
|
||||||
|
|
||||||
#endif
|
#endif
|
@@ -185,3 +185,5 @@ void Channel::removeOperator(const std::string& operatorName )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -72,6 +72,7 @@ public:
|
|||||||
std::string getOperatorNickname(int fd) const;
|
std::string getOperatorNickname(int fd) const;
|
||||||
void removeOperator(const std::string& operatorName );
|
void removeOperator(const std::string& operatorName );
|
||||||
|
|
||||||
|
|
||||||
void setPass(const std::string &Newpass)
|
void setPass(const std::string &Newpass)
|
||||||
{
|
{
|
||||||
pass = Newpass;
|
pass = Newpass;
|
||||||
@@ -85,4 +86,5 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
Reference in New Issue
Block a user