• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

C++ Max - Min - symbols for name c++ / seconds to minutesLUA

Sigoles

Discord: @sigoles
Joined
Nov 20, 2015
Messages
1,209
Solutions
2
Reaction score
154
How I can avoid names above 30 words and avoid symbols, avoid some words like (GOD)?

C++:
if (text.substr(0, 5) == "!nick" && text.length() > 6) {
        std::string newName = text.substr(6);
        if (client) {
            g_dispatcher.addTask(createTask(std::bind(&ProtocolGame::broadcastSpectatorMessage, client, "", (spectatorName.empty() ? "spectator" : spectatorName) + " changed nick to " + newName)));
        }
        spectatorName = newName;
        return;
    }


How if its above 60 seconds, transforms it to show in minutes?
LUA:
player:sendCancelMessage(string.format("%d seconds to next wave.", math.ceil(player:getStorageValue(90906) - os.time())))

tfs 1.1
thanks
 
Solution
C++:
if (text.substr(0, 5) == "!nick" && text.length() > 6)
{
   std::string newName = text.substr(6);

   if(newName.length() > 30)
       return;

   std::vector<std::string> forbiddenNames;
   forbiddenNames.push_back("Forbidden Name 1");
   forbiddenNames.push_back("Forbidden Name 2");
   forbiddenNames.push_back("Forbidden Name 3");
   // and so on

   for(int i=0; i<forbiddenNames.size(); i++)
   {
       if(newName == forbiddenNames[i])
           return;
   }

    if(client)
       g_dispatcher.addTask(createTask(std::bind(&ProtocolGame::broadcastSpectatorMessage, client, "", (spectatorName.empty() ? "spectator" : spectatorName) + " changed nick to " + newName)));
    spectatorName = newName;
    return;
}
That vector with...
C++:
if (text.substr(0, 5) == "!nick" && text.length() > 6)
{
   std::string newName = text.substr(6);

   if(newName.length() > 30)
       return;

   std::vector<std::string> forbiddenNames;
   forbiddenNames.push_back("Forbidden Name 1");
   forbiddenNames.push_back("Forbidden Name 2");
   forbiddenNames.push_back("Forbidden Name 3");
   // and so on

   for(int i=0; i<forbiddenNames.size(); i++)
   {
       if(newName == forbiddenNames[i])
           return;
   }

    if(client)
       g_dispatcher.addTask(createTask(std::bind(&ProtocolGame::broadcastSpectatorMessage, client, "", (spectatorName.empty() ? "spectator" : spectatorName) + " changed nick to " + newName)));
    spectatorName = newName;
    return;
}
That vector with forbidden names should probably be initialized outside of this function (otherwise it will be created every time someone calls this function, which is unnecessary), but since I do not see the entire code, I am unable to tell you where exactly you should place it. Also there's a chance you'll need to place #include<vector> somewhere in your header file (I'm not sure if it's already included).

Edit 1: And keep in mind this is a very simple solution – it doesn't display any error message, so the user might be confused why his command didn't work, so you should probably add it. Unfortunately I have no knowledge about this broadcast system you're using, so I cannot really help you any further with this.
Edit 2: It also doesn't filter specific words, but only checks against full nicknames. If you were to block specific words, you would need to use a more advanced solution, especially since you'd have to be careful not to block nicks like "stegodon".

LUA:
seconds = math.ceil(player:getStorageValue(90906) - os.time()
minutes = math.floor(seconds/60)
seconds = seconds%60
player:sendCancelMessage(string.format("%d minutes and %d seconds to the next wave.", minutes, seconds)))

Anyway, both of these changes required very minimal coding knowledge, which, judging by your post, you lack. I believe you should try to learn at least the basics – otherwise you'll have to seek help even for the smallest obstacles that may arise on your way to hosting a server.
 
Last edited:
Solution
C++:
if (text.substr(0, 5) == "!nick" && text.length() > 6)
{
    std::string newName = text.substr(6);

    std::vector<std::string> forbiddenNames;
    forbiddenNames.push_back("Forbidden Name 1");
    forbiddenNames.push_back("Forbidden Name 2");
    forbiddenNames.push_back("Forbidden Name 3");
    // and so on

    for(int i=0; i<forbiddenNames.size(); i++)
    {
        if(newName == forbiddenNames[i])
            return;
    }

    if(client)
        g_dispatcher.addTask(createTask(std::bind(&ProtocolGame::broadcastSpectatorMessage, client, "", (spectatorName.empty() ? "spectator" : spectatorName) + " changed nick to " + newName)));
    spectatorName = newName;
    return;
}
That vector with forbidden names should probably be initialized outside of this function (otherwise it will be created every time someone calls this function, which is unnecessary), but since I do not see the entire code, I am unable to tell you where exactly you should place it. Also there's a chance you'll need to place #include<vector> somewhere in your header file (I'm not sure if it's already included).
And keep in mind this is a very simple solution – it doesn't display any error message, so the user might be confused why his command didn't work, so you should probably add it. Unfortunately I have no knowledge about this broadcast system you're using, so I cannot really help you any further with this.

LUA:
seconds = math.ceil(player:getStorageValue(90906) - os.time()
minutes = math.floor(seconds/60)
seconds = seconds%60
player:sendCancelMessage(string.format("%d minutes and %d seconds to the next wave.", minutes, seconds)))

Anyway, both of these changes required very minimal coding knowledge, which, judging by your post, you lack. I believe you should try to learn at least the basics – otherwise you'll have to seek help even for the smallest obstacles that may arise on your way to hosting a server.


hey bro, thanks!!
can u tell me where put?
C++:
void ProtocolSpectator::parseSpectatorSay(NetworkMessage& msg)
{
    SpeakClasses type = (SpeakClasses)msg.getByte();
    uint16_t channelId = 0;
    if (type == TALKTYPE_CHANNEL_Y) {
        channelId = msg.get<uint16_t>();
    }
    else {
        return;
    }
    const std::string text = msg.getString();
    if (text.length() > 255 || channelId != CHANNEL_CAST || !client) {
        return;
    }
   if (text.substr(0, 5) == "!nick" && text.length() > 6) {
        std::string newName = text.substr(6);
        if (client) {
            g_dispatcher.addTask(createTask(std::bind(&ProtocolGame::broadcastSpectatorMessage, client, "", (spectatorName.empty() ? "spectator" : spectatorName) + " changed nick to " + newName)));
        }
        spectatorName = newName;
        return;
    }

if (client) {
        g_dispatcher.addTask(createTask(std::bind(&ProtocolGame::broadcastSpectatorMessage, client, spectatorName, text)));
    }

}
 
hey bro, thanks!!
can u tell me where put?
C++:
void ProtocolSpectator::parseSpectatorSay(NetworkMessage& msg)
{
    SpeakClasses type = (SpeakClasses)msg.getByte();
    uint16_t channelId = 0;
    if (type == TALKTYPE_CHANNEL_Y) {
        channelId = msg.get<uint16_t>();
    }
    else {
        return;
    }
    const std::string text = msg.getString();
    if (text.length() > 255 || channelId != CHANNEL_CAST || !client) {
        return;
    }
   if (text.substr(0, 5) == "!nick" && text.length() > 6) {
        std::string newName = text.substr(6);
        if (client) {
            g_dispatcher.addTask(createTask(std::bind(&ProtocolGame::broadcastSpectatorMessage, client, "", (spectatorName.empty() ? "spectator" : spectatorName) + " changed nick to " + newName)));
        }
        spectatorName = newName;
        return;
    }

if (client) {
        g_dispatcher.addTask(createTask(std::bind(&ProtocolGame::broadcastSpectatorMessage, client, spectatorName, text)));
    }

}

Simply replace the lines you've posted with ones I've posted.
 
Since you using tfs 1.1 why not use C++11 hashmap for searching forbidden names? It should be much faster than doing the comparison for every string.
Here are example with hashmap + vector for checking sub strings:
Code:
void ProtocolSpectator::parseSpectatorSay(NetworkMessage& msg)
{
    SpeakClasses type = (SpeakClasses)msg.getByte();
    uint16_t channelId = 0;
    if (type == TALKTYPE_CHANNEL_Y) {
        channelId = msg.get<uint16_t>();
    } else {
        return;
    }
    const std::string text = msg.getString();
    if (text.length() > 255 || channelId != CHANNEL_CAST || !client) {
        return;
    }
    if (text.substr(0, 5) == "!nick" && text.length() > 6) {
        std::string newName = text.substr(6);
        if (newName.length() > 30) {
            return;
        }

        static const std::unordered_map<std::string, bool> forbiddenNames = { /*{"forbidden name", true - disallow/false - allow}*/{"god of pain", true} };
        auto forbiddenName = forbiddenNames.find(asLowerCaseString(newName));
        if (forbiddenName != forbiddenNames.end() && forbiddenName->second) {
            return;
        }

        static const std::vector<std::string> forbiddenSubStrings = { "(GOD)", "[GOD]" };
        for (const auto& subString : forbiddenSubStrings) {
            if (newName.find(subString) != std::string::npos) {
                return;
            }
        }

        if (client) {
            g_dispatcher.addTask(createTask(std::bind(&ProtocolGame::broadcastSpectatorMessage, client, "", (spectatorName.empty() ? "spectator" : spectatorName) + " changed nick to " + newName)));
        }
        spectatorName = newName;
        return;
    }

    if (client) {
        g_dispatcher.addTask(createTask(std::bind(&ProtocolGame::broadcastSpectatorMessage, client, spectatorName, text)));
    }
}
 
C++:
if (text.substr(0, 5) == "!nick" && text.length() > 6)
{
   std::string newName = text.substr(6);

   if(newName.length() > 30)
       return;

   vector<std::string> forbiddenNames;
   forbiddenNames.push_back("Forbidden Name 1");
   forbiddenNames.push_back("Forbidden Name 2");
   forbiddenNames.push_back("Forbidden Name 3");
   // and so on

   for(int i=0; i<forbiddenNames.size(); i++)
   {
       if(newName == forbiddenNames[i])
           return;
   }

    if(client)
       g_dispatcher.addTask(createTask(std::bind(&ProtocolGame::broadcastSpectatorMessage, client, "", (spectatorName.empty() ? "spectator" : spectatorName) + " changed nick to " + newName)));
    spectatorName = newName;
    return;
}
That vector with forbidden names should probably be initialized outside of this function (otherwise it will be created every time someone calls this function, which is unnecessary), but since I do not see the entire code, I am unable to tell you where exactly you should place it. Also there's a chance you'll need to place #include<vector> somewhere in your header file (I'm not sure if it's already included).

Edit 1: And keep in mind this is a very simple solution – it doesn't display any error message, so the user might be confused why his command didn't work, so you should probably add it. Unfortunately I have no knowledge about this broadcast system you're using, so I cannot really help you any further with this.
Edit 2: It also doesn't filter specific words, but only checks against full nicknames. If you were to block specific words, you would need to use a more advanced solution, especially since you'd have to be careful not to block nicks like "stegodon".

LUA:
seconds = math.ceil(player:getStorageValue(90906) - os.time()
minutes = math.floor(seconds/60)
seconds = seconds%60
player:sendCancelMessage(string.format("%d minutes and %d seconds to the next wave.", minutes, seconds)))

Anyway, both of these changes required very minimal coding knowledge, which, judging by your post, you lack. I believe you should try to learn at least the basics – otherwise you'll have to seek help even for the smallest obstacles that may arise on your way to hosting a server.

hey, and how to avoide symbols, you know?
 
hey, and how to avoide symbols, you know?
Madzix's solution lets you block names containing specific words (although you need to be very careful with that, because of what I mentioned in my first post), and is better optimised in general, so I would just use it instead of what I proposed. So to block certain symbols, you'd just add them to the list of blocked words (forbiddenSubStrings).
 
Back
Top