Numeric detection limit on a string. +REP
#1

I am stocked on a function, that get always bug when i try to test and script it.
A function that detects the numbers in a string, on OnPlayerText, main chat.
If the player input in the chat-text (string) more than 5 numbers the chat return 0;, also if numbers are separate between spaces or other symbols.

How can i implement this idea? Any helps?

Thanks.
+ REP to all helpers.
Reply
#2

Hi!

It sounds that you have already a function, haven't you?
Can you show us this function, please?
Reply
#3

So basically an anti advertisement system?
Reply
#4

Unfortunately, this is an older idea that i tried to implement many times on the past weeks; for some days i leaved it, then when i saw more ip spam on my server, that can't be handled by the anti adv system, i started thinking again. Btw i was trying to make this using strfind.

Thanks to be interest.
Reply
#5

https://sampforum.blast.hk/showthread.php?tid=568668

PAWN version:
pawn Код:
// ** INCLUDES

#include <a_samp>
#include <sscanf>

// ** DEFINES

// *** FUNCTIONS

#define strcpy(%0,%1,%2) strcat((%0[0] = '\0', %0), %1, %2)
#define isnull(%1) ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))

// ** MAIN

main()
{
    print("Loaded \"anti_advert.amx\".");
}

// ** CALLBACKS

public OnGameModeInit()
{  
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

public OnPlayerText(playerid, text[])
{
    if(IsAdvertisement(text))
    {
        SendClientMessage(playerid, 0xFF0000FF, "Your message is considered an advertisement, please review it.");
        return 0;
    }
    return 1;
}

// ** FUNCTIONS

forward bool:IsAdvertisement(text[]);
public bool:IsAdvertisement(text[])
{
    new message[128], extract[2], element[4][4], count_1, count_2, temp, bool:number_next = false, bool:next_number = false, bool:advert = false;
    strcpy(message, text, sizeof(message));

    for(new i = 0, j = strlen(message); i < j; i ++)
    {
        switch(message[i])
        {
            case '0'..'9':
            {
                if(next_number) continue;

                number_next = false;

                strmid(extract, message[i], 0, 1);
                strcat(element[count_1], extract);
               
                count_2 ++;

                if(count_2 == 3 || message[i + 1] == EOS)
                {
                    strmid(extract, message[i + 1], 0, 1);

                    if(IsNumeric(extract))
                    {
                        element[0][0] = EOS;
                        element[1][0] = EOS;
                        element[2][0] = EOS;
                        element[3][0] = EOS;

                        count_1 = 0;
                        count_2 = 0;

                        next_number = true;
                        continue;
                    }

                    temp = strval(element[count_1]);

                    if(count_1 == 0)
                    {
                        if(temp <= 255)
                        {
                            count_1 ++;
                            count_2 = 0;
                        }
                        else
                        {
                            element[count_1][0] = EOS;

                            count_2 = 0;

                            next_number = true;
                        }
                    }
                    else
                    {
                        if(temp <= 255)
                        {
                            count_1 ++;
                            count_2 = 0;
                        }
                        else
                        {
                            element[0][0] = EOS;
                            element[1][0] = EOS;
                            element[2][0] = EOS;
                            element[3][0] = EOS;

                            count_1 = 0;
                            count_2 = 0;

                            next_number = true;
                        }
                    }
                }

                if(count_1 == 4)
                {
                    advert = true;
                    break;
                }
            }
            default:
            {
                next_number = false;

                if(number_next) continue;

                if(!isnull(element[count_1]))
                {
                    temp = strval(element[count_1]);

                    if(count_1 == 0)
                    {
                        if(temp <= 255)
                        {
                            count_1 ++;
                            count_2 = 0;

                            number_next = true;
                        }
                        else
                        {
                            element[count_1][0] = EOS;

                            count_2 = 0;
                        }
                    }
                    else
                    {
                        if(temp <= 255)
                        {
                            count_1 ++;
                            count_2 = 0;

                            number_next = true;
                        }
                        else
                        {
                            element[0][0] = EOS;
                            element[1][0] = EOS;
                            element[2][0] = EOS;
                            element[3][0] = EOS;

                            count_1 = 0;
                            count_2 = 0;
                        }
                    }

                    if(count_1 == 4)
                    {
                        advert = true;
                        break;
                    }
                }
            }
        }
    }
    return advert;
}

stock IsNumeric(const string[])
{
    return !sscanf(string, "{d}");
}
Reply
#6

Your code detects something such as an ip, or have numeric symbol limit for every text string?

Because my idea was to make a numeric limit on every message, that the player sends to the main server chat.

Thanks.
Reply
#7

It detects VALID IPs and blocks them from being sent to the chat. If an invalid IP is detected, it just sends the message to the chat, as it can be anything. Just note that it's a strict detection.
Reply
#8

And what about the 5 numeric symbols limit for every sent string?

It's more easy to implement?

Thanks.
Reply
#9

This will not work, and it's just the mind most simple idea, but something like this, naturally implemed.

Код:
OnPlayerText{

if(IsNumeric[text] >= 5) return 0;

return 1;
}
Thanks again.
Reply
#10

That's a stupid idea. It will be annoying when chatting.

Hello, I am 55 years-old. When I was 22, I moved to Canada. But then moved back to my native country when I was 32. How about you guys?

Guess, what...
Blocked

And, no that won't work. You gotta loop through the whole string, and count the numbers.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)