Raised This Month: $32 Target: $400
 8% 

Solved Ignore color input


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
sneaK
SourceMod Moderator
Join Date: Feb 2015
Location: USA
Old 03-20-2017 , 19:14   Ignore color input
Reply With Quote #1

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!
Attached Files
File Type: sp Get Plugin or Get Source (zeph_mathcredits.sp - 246 views - 4.8 KB)
__________________

Last edited by sneaK; 03-22-2017 at 01:35. Reason: solved
sneaK is offline
Chaosxk
Veteran Member
Join Date: Aug 2010
Location: Westeros
Old 03-21-2017 , 00:25   Re: Ignore color input
Reply With Quote #2

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);
	}
}
__________________
Chaosxk is offline
sneaK
SourceMod Moderator
Join Date: Feb 2015
Location: USA
Old 03-21-2017 , 01:13   Re: Ignore color input
Reply With Quote #3

That works with no chat colors, but when I have chat colors enabled, still no dice.
__________________
sneaK is offline
Chaosxk
Veteran Member
Join Date: Aug 2010
Location: Westeros
Old 03-21-2017 , 01:25   Re: Ignore color input
Reply With Quote #4

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?
__________________

Last edited by Chaosxk; 03-21-2017 at 01:43.
Chaosxk is offline
Timocop
AlliedModders Donor
Join Date: Mar 2013
Location: Germany
Old 03-21-2017 , 02:39   Re: Ignore color input
Reply With Quote #5

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
Spoiler
Timocop is offline
sneaK
SourceMod Moderator
Join Date: Feb 2015
Location: USA
Old 03-21-2017 , 03:03   Re: Ignore color input
Reply With Quote #6

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
__________________

Last edited by sneaK; 03-21-2017 at 03:03.
sneaK is offline
Timocop
AlliedModders Donor
Join Date: Mar 2013
Location: Germany
Old 03-21-2017 , 03:14   Re: Ignore color input
Reply With Quote #7

Wops, better clone it I was dumb, forgot to add +1 size for the null terminator 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

Last edited by Timocop; 03-21-2017 at 04:31. Reason: Null terminator + Closing handles
Timocop is offline
headline
SourceMod Moderator
Join Date: Mar 2015
Old 03-21-2017 , 04:02   Re: Ignore color input
Reply With Quote #8

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.
headline is offline
Chaosxk
Veteran Member
Join Date: Aug 2010
Location: Westeros
Old 03-21-2017 , 04:15   Re: Ignore color input
Reply With Quote #9

Quote:
Originally Posted by Headline View Post
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 View Post
Wops, better clone it I was dumb, forgot to add +1 size for the null terminator 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.
__________________

Last edited by Chaosxk; 03-21-2017 at 04:18.
Chaosxk is offline
headline
SourceMod Moderator
Join Date: Mar 2015
Old 03-21-2017 , 04:17   Re: Ignore color input
Reply With Quote #10

Quote:
Originally Posted by Chaosxk View Post
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
headline is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 04:55.


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