AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Plugin/Gameplay Ideas and Requests (https://forums.alliedmods.net/forumdisplay.php?f=60)
-   -   Solved Ignore color input (https://forums.alliedmods.net/showthread.php?t=295274)

sneaK 03-20-2017 19:14

Ignore color input
 
1 Attachment(s)
Hi guys, I'm currently working with this plugin (also attached below): https://forums.alliedmods.net/showpo...&postcount=538 Unfortunately the author has not been online in 3 months, and I haven't been able to contact him.

This plugin allows players to answer a math question and gain store credits. The store plugin I am using also allows players to colorize their chat messages (using {blue}, {red}, {green}, etc) which is not registering properly with the above plugin. I was wondering if it is possible to ignore the chat colors, and only get the proper string (the numbers) that the player is typing into chat.

Thanks!

Chaosxk 03-21-2017 00:25

Re: Ignore color input
 
Try this, uses regex.
Replace this function and include <regex>

Code:

#include <regex>
public Action OnChatMessage(&author, Handle recipients, char[] name, char[] message)
{
        if(inQuizz)
        {
                Regex regex = new Regex("\\{.+?\\}\\s*");
                int count = regex.Match(message);
                for (int i = 0; i < count; i++)
                {
                        char buffer[16];
                        regex.GetSubString(i, buffer, sizeof(buffer));
                        ReplaceString(message, 256, buffer, "");
                }
                char bit[1][5];
                ExplodeString(message, " ", bit, sizeof bit, sizeof bit[]);
                delete regex;
               
                if(ProcessSolution(author, StringToInt(bit[0])))
                        SendEndQuestion(author);
        }
}


sneaK 03-21-2017 01:13

Re: Ignore color input
 
That works with no chat colors, but when I have chat colors enabled, still no dice. :(

Chaosxk 03-21-2017 01:25

Re: Ignore color input
 
If this is for CS:GO then i don't think its {red}, {blue}, instead it uses \x02, \x03..etc... i think?

Can you link to the color chat plugin your using?

Timocop 03-21-2017 02:39

Re: Ignore color input
 
Should remove all unprintable characters from the message, including color, if the message is ASCII. (untested but should work)
PHP Code:

public Action OnChatMessage(&authorHandle recipientschar[] namechar[] message)
{
    if(
inQuizz)
    {
        
char chr[1];
        for(
int i 132i++) {
            
FormatEx(chrsizeof chr"%c"i);
            
ReplaceString(message256chr"");
        }
        for(
int i 128256i++) {
            
FormatEx(chrsizeof chr"%c"i);
            
ReplaceString(message256chr"");
        }
        
        
char bit[1][5];
        
ExplodeString(message" "bitsizeof bitsizeof bit[]);

        if(
ProcessSolution(authorStringToInt(bit[0])))
            
SendEndQuestion(author);
    }


Btw. Memory Leak @162 :P
Spoiler

sneaK 03-21-2017 03:03

Re: Ignore color input
 
Thanks for the tip on the memory leak!

Unfortunately with that solution, the plugin now sees no answers from anyone, regardless of color or no color. Error log:

Code:

L 03/21/2017 - 02:00:29: [SM] Call stack trace:
L 03/21/2017 - 02:00:29: [SM]  [0] ReplaceString
L 03/21/2017 - 02:00:29: [SM]  [1] Line 127, G:\sourcemod 1.8\scripting\zeph_mathcredits3.sp::OnChatMessage
L 03/21/2017 - 02:00:29: [SM]  [3] Call_Finish
L 03/21/2017 - 02:00:29: [SM]  [4] Line 367, C:\Users\Administrator\Documents\Repositories\Simple Chat Processor\scripting\simple-chatprocessor.sp::OnSayText2
L 03/21/2017 - 02:00:33: [SM] Exception reported: Cannot replace searches of empty strings

I'm using this colors include, by the way: https://github.com/Maxximou5/csgo-pl...csgocolors.inc

Timocop 03-21-2017 03:14

Re: Ignore color input
 
Wops, better clone it :oops: I was dumb, forgot to add +1 size for the null terminator :oops: You're better off with the regex one, works better and less code.

PHP Code:

public Action OnChatMessage(&authorHandle recipientschar[] namechar[] message)
{
    if(
inQuizz)
    {
        
char filteredMessage[256];
        
strcopy(filteredMessagesizeof filteredMessagemessage);
        
        
char chr[2];
        for(
int i 132i++) {
            
FormatEx(chrsizeof chr"%c"i);
            
ReplaceString(filteredMessagesizeof filteredMessagechr"");
        }
        for(
int i 128256i++) {
            
FormatEx(chrsizeof chr"%c"i);
            
ReplaceString(filteredMessagesizeof filteredMessagechr"");
        }
        
        
char bit[1][5];
        
ExplodeString(filteredMessage" "bitsizeof bitsizeof bit[]);

        if(
ProcessSolution(authorStringToInt(bit[0])))
            
SendEndQuestion(author);
    }


Regex whitelist version (tested):
PHP Code:

public Action OnChatMessage(&authorHandle recipientschar[] namechar[] message)
{
    if(
inQuizz)
    {
        
char filteredMessage[256];
        
strcopy(filteredMessagesizeof filteredMessagemessage);
        
        
Regex regex = new Regex("[\\-\\d]+");        //Positive/Negative number
//        Regex regex = new Regex("[\\d]+");            //Positive number
//        Regex regex = new Regex("[\\-\\d\\.]+");    //Positive/Negative number/float
        
if(regex.Match(filteredMessage) > 0)
        {
            
regex.GetSubString(0filteredMessagesizeof filteredMessage);
            if(
ProcessSolution(authorStringToInt(filteredMessage)))
                
SendEndQuestion(author);            
        }
        
delete regex;
    }


Testing Code with Output:
Spoiler

headline 03-21-2017 04:02

Re: Ignore color input
 
I was unable to compile so it might have an error I couldn't catch, but this might be what you're looking for lol.

PHP Code:

#include <sourcemod>
#include <sdktools>
#include <store>
#include <scp>
#include <multicolors>

#define PLUGIN_NAME         "[ANY] Math Credits (Zephyrus-Store)"
#define PLUGIN_DESCRIPTION     "Give credits on correct math answer."
#define PLUGIN_AUTHOR         "Arkarr & Simon"
#define PLUGIN_VERSION         "1.02z"
#define PLUGIN_TAG            "[{red}Math{blue}Credits]"
#define PLUS                "+"
#define MINUS                "-"
#define DIVISOR                "/"
#define MULTIPL                "*"

bool inQuizz;

char op[32];
char operators[4][2] = {"+""-""/""*"};

int nbrmin;
int nbrmax;
int maxcredits;
int mincredits;
int questionResult;
int credits;
int minplayers;

Handle timerQuestionEnd;
Handle CVAR_MinimumNumber;
Handle CVAR_MaximumNumber;
Handle CVAR_MaximumCredits;
Handle CVAR_MinimumCredits;
Handle CVAR_TimeBetweenQuestion;
Handle CVAR_TimeAnswer;
Handle CVAR_MinimumPlayers;

public 
Plugin myinfo 
{
    
name PLUGIN_NAME,
    
author PLUGIN_AUTHOR,
    
description PLUGIN_DESCRIPTION,
    
version PLUGIN_VERSION,
    
url "[email protected]"
};

public 
void OnPluginStart()
{
    
inQuizz false;
    
    
CVAR_MinimumNumber CreateConVar("sm_MathCredits_minimum_number""1""What should be the minimum number for questions ?");
    
CVAR_MaximumNumber CreateConVar("sm_MathCredits_maximum_number""100""What should be the maximum number for questions ?");
    
CVAR_MaximumCredits CreateConVar("sm_MathCredits_maximum_credits""50""What should be the maximum number of credits earned for a correct answers ?");
    
CVAR_MinimumCredits CreateConVar("sm_MathCredits_minimum_credits""5""What should be the minimum number of credits earned for a correct answers ?");
    
CVAR_TimeBetweenQuestion CreateConVar("sm_MathCredits_time_between_questions""50""Time in seconds between each questions.");
    
CVAR_TimeAnswer CreateConVar("sm_MathCredits_time_answer_questions""10""Time in seconds to give a answer to a question.");
    
CVAR_MinimumPlayers CreateConVar("sm_MathCredits_minimum_players""10""What should be the minimum number of players ?");
    
    
AutoExecConfig(true"MathCredits");
}

public 
void OnMapStart()
{
    
CreateTimer(GetConVarFloat(CVAR_TimeBetweenQuestion) + GetConVarFloat(CVAR_TimeAnswer), CreateQuestion_TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
}

public 
void OnConfigsExecuted()
{
    
nbrmin GetConVarInt(CVAR_MinimumNumber);
    
nbrmax GetConVarInt(CVAR_MaximumNumber);
    
maxcredits GetConVarInt(CVAR_MaximumCredits);
    
mincredits GetConVarInt(CVAR_MinimumCredits);
    
minplayers GetConVarInt(CVAR_MinimumPlayers);
}

public 
Action EndQuestion(Handle timer)
{
    
SendEndQuestion(-1);
}

public 
Action CreateQuestion(Handle timer)
{
    
int players GetClientCount(true);
    if(
players minplayers)
        return;
    
int nbr1 GetRandomInt(nbrminnbrmax);
    
int nbr2 GetRandomInt(nbrminnbrmax);
    
credits GetRandomInt(mincreditsmaxcredits);
    
    
Format(opsizeof(op), operators[GetRandomInt(0,3)]);
    
    if(
StrEqual(opPLUS))
    {
        
questionResult nbr1 nbr2;
    }
    else if(
StrEqual(opMINUS))
    {
        
questionResult nbr1 nbr2;
    }
    else if(
StrEqual(opDIVISOR))
    {
        do{
            
nbr1 GetRandomInt(nbrminnbrmax);
            
nbr2 GetRandomInt(nbrminnbrmax);
        }while(
nbr1 nbr2 != 0);
        
questionResult nbr1 nbr2;
    }
    else if(
StrEqual(opMULTIPL))
    {
        
questionResult nbr1 nbr2;
    }
    
    
CPrintToChatAll("%s {green}%i {default}%s {green}%i {default}= ?? >>>{lightgreen}%i {red}credits{default}<<<"PLUGIN_TAGnbr1opnbr2credits);
    
inQuizz true;
    
    
timerQuestionEnd CreateTimer(GetConVarFloat(CVAR_TimeAnswer), EndQuestion);
}

public 
Action OnChatMessage(&authorHandle recipientschar[] namechar[] message)
{
    if(
inQuizz)
    {
        if(
ProcessSolution(authormessage))
            
SendEndQuestion(author);
    }
}

public 
bool ProcessSolution(client, const char[] answer)
{
    
char str[8];
    
IntToString(questionResultstrsizeof(str));
    
    if(
StrContains(answerstr) != -1)
    {
        
int test Store_GetClientCredits(client);
        
Store_SetClientCredits(clienttest credits);
        
        return 
true;
    }
    else
    {
        return 
false;
    }
}

public 
void SendEndQuestion(int client)
{
    if(
timerQuestionEnd != INVALID_HANDLE)
    {
        
KillTimer(timerQuestionEnd);
        
timerQuestionEnd INVALID_HANDLE;
    }
    
    
char answer[100];
    
    if(
client != -1)
        
Format(answersizeof(answer), "%s {green}%N {default}has given a correct answer and got {lightgreen}%i {red}credits !"PLUGIN_TAGclientcredits);
    else
        
Format(answersizeof(answer), "%s {default}Time end ! No answer."PLUGIN_TAG);
        
    
Handle pack CreateDataPack();
    
CreateDataTimer(0.3AnswerQuestionpack);
    
WritePackString(packanswer);
    
    
inQuizz false;
}

public 
Action AnswerQuestion(Handle timerHandle pack)
{
    
char str[100];
    
ResetPack(pack);
    
ReadPackString(packstrsizeof(str));
     
delete pack;

    
CPrintToChatAll(str);


I also closed that handle, too.

Chaosxk 03-21-2017 04:15

Re: Ignore color input
 
Quote:

Originally Posted by Headline (Post 2505317)
I was unable to compile so it might have an error I couldn't catch, but this might be what you're looking for lol.

I also closed that handle, too.

https://sm.alliedmods.net/new-api/ti...reateDataTimer

Yours will still have a memory leak, CreateDataTimer will automatically create a DataPack handle when called and is freed automatically when the timer ends.

Just need to change Handle pack = CreateDataPack(); to Handle pack;
and don't need to delete the datapack since it's automatically closes.

Quote:

Originally Posted by Timocop (Post 2505311)
Wops, better clone it :oops: I was dumb, forgot to add +1 size for the null terminator :oops: You're better off with the regex one, works better and less code.

Your regex solution is the best way, also you forgot to close the regex handle.

headline 03-21-2017 04:17

Re: Ignore color input
 
Quote:

Originally Posted by Chaosxk (Post 2505321)
https://sm.alliedmods.net/new-api/ti...reateDataTimer

Yours will still have a memory leak, CreateDataTimer will automatically create a DataPack handle when called and is freed automatically when the timer ends.

Just need to change Handle pack = CreateDataPack(); to Handle pack;
and don't need to delete the datapack since it's automatically closes.

Lol okay, I can only do so much with like 5 minutes :D


All times are GMT -4. The time now is 01:49.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.