Raised This Month: $ Target: $400
 0% 

How to parse string


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
monolit
Junior Member
Join Date: May 2011
Old 05-13-2011 , 11:39   How to parse string
Reply With Quote #1

Hey, I would like to parse string with 3 and 4 parameters, and just ignore quotes

My string is like

!mute player time reason
as it should work if I write something like:

Quote:
!mute monolit 10 spamming and advertising
The reason should be spamming and advertising, even if I don't use quotes.

How to get reason should be something like, get whole text, and then just replace !mute, player, time with empty string.
monolit is offline
Nyuszy
Senior Member
Join Date: Apr 2009
Old 05-13-2011 , 12:04   Re: How to parse string
Reply With Quote #2

you can use strbreak:
1. replace "!mute " with ""
2. break the string to "player", "time reason", so you have the player
3. break the 2. string ("time reason") to "time", "reason"
Nyuszy is offline
monolit
Junior Member
Join Date: May 2011
Old 05-13-2011 , 12:07   Re: How to parse string
Reply With Quote #3

Can you show me example please ? I'm very begginer in scripting.

Is it something like this?
PHP Code:
/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <amxmisc>

#define PLUGIN "mute"
#define VERSION "1.0"
#define AUTHOR "monolit"


public plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
register_clcmd("say ""CmdSay")
}

public 
CmdSay(id)
{
    static 
arg[60]
    
read_argv(1argcharsmax(arg))
    
    if(
arg[0] != '!')
        return 
PLUGIN_CONTINUE;
    
    if(
equal(arg"!mute"5))
    {
        new 
target[20], mutetime[7], reason[40]
           
        
replace(argcharsmax(arg), "!mute """)
        
        
strbreak argtargetsizeof target-1mutetimesizeof mutetime-)
        
        
replace(argcharsmax(arg), target"")
        
replace(argcharsmax(arg), mutetime"")
        
        
copy(reasoncharsmax(reason), arg)
        
        
client_print(idprint_console"^nTarget: %s"target)
        
client_print(idprint_console"Time: %d"mutetime)
        
client_print(idprint_console"Reason: %s"reason)
        
        return 
PLUGIN_HANDLED_MAIN;
    }
    return 
PLUGIN_CONTINUE;

Also what is the difference between Charsmax(smth) and sizeof smth-1

Last edited by monolit; 05-13-2011 at 12:22.
monolit is offline
drekes
Veteran Member
Join Date: Jul 2009
Location: Vault 11
Old 05-13-2011 , 12:38   Re: How to parse string
Reply With Quote #4

Quote:
Originally Posted by monolit View Post
Also what is the difference between Charsmax(smth) and sizeof smth-1
charsmax() is a macro of sizeof() - 1

string.inc:
PHP Code:
#define charsmax(%1) (sizeof(%1)-1) 
__________________

Quote:
Originally Posted by nikhilgupta345 View Post
You're retarded.
drekes is offline
Send a message via MSN to drekes
minimiller
Veteran Member
Join Date: Aug 2007
Location: United Kingdom
Old 05-13-2011 , 19:01   Re: How to parse string
Reply With Quote #5

Or you can use parse()
__________________
minimiller is offline
Send a message via MSN to minimiller
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 05-14-2011 , 00:37   Re: How to parse string
Reply With Quote #6

This is how I would 'parse' the string:

PHP Code:
    new szArg[45]
    
read_args(szArgcharsmax(szArg))
    
// Might need a remove_quotes(szArg) here.  I can't remember.
    
new szCmd[12], szTarget[12], szTime[4], szReason[20]
    
strbreak(szArgszCmdcharsmax(szCmd), szArgcharsmax(szArg))
    
strbreak(szArgszTargetcharsmax(szTarget), szArgcharsmax(szArg))
    
strbreak(szArgszTimecharsmax(szTime), szReasoncharsmax(szReason))
    
    
client_print(idprint_console"Command: %s"szCmd// In your case szCmd should be "!mute"
    
client_print(idprint_console"Target: %s"szTarget)
    
client_print(idprint_console"Time: %d"str_to_num(szTime))
    
client_print(idprint_console"Reason: %s"szReason

Quote:
Originally Posted by minimiller View Post
Or you can use parse()
You can't use parse if you want to have any spaces in the reason section.
__________________

Last edited by fysiks; 05-14-2011 at 01:10.
fysiks is offline
monolit
Junior Member
Join Date: May 2011
Old 05-14-2011 , 04:12   Re: How to parse string
Reply With Quote #7

Thanks it works, also remove_quotes needed.

One more question, how to check is there a string character in szTime ?
is it correct: ?

PHP Code:
new bantime;
        
        if(
containi(szTime"m") != -1)
        {
            
bantime str_to_num(szTime) * 60;
        }else if(
containi(szTime"h") != -1)
        {
            
bantime str_to_num(szTime) * 60 60;
        }else if(
containi(szTime"d") != -1)
        {
            
bantime str_to_num(szTime) * 60 60 24;
        } 
I would like to make that if player writes 1m in bantime, the bantime would be 60seconds

if 1h then the player would be banned for 1 hour.
if 1d then the player would be banned for 1 day.

and then check if bantime exceeds the limit (7 days is max)
PHP Code:
if(bantime 604800)
        {
            
client_print_color(idDontChange"^4[ INFO ]^1 ban_time can not exceed more than 7 days")
            return 
PLUGIN_HANDLED_MAIN;
        } 
It should auto convert to seconds then I'm going to use get_systime() + bantime.

So am I doing it correctly ?

Why this is not working ?:

I set user mute time with this:
[php]
mutetime = str_to_num(szTime) * 60 * 60;
iMute[player] = get_systime() + mutetime;

format(szTemp,charsmax(szTemp),"INSERT INTO `bans` ( `ip` , `mute_time`, `mute_reason`, `mute_admin`)VALUES ('%s','%d', '%s', '%s');", szTargetIp, get_systime() + mutetime, szReason, szAdminName)
[php]

and on say event I check this:
PHP Code:
if(get_systime() + iMute[id] > get_systime())
    {
        
client_print_color(idDontChange"^4[ INFO ]^1 You can not chat while you are mutted")
        return 
PLUGIN_HANDLED;
    } 
But user still can chat even if his mute time is higher than systime.

Last edited by monolit; 05-14-2011 at 05:00.
monolit is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 05-14-2011 , 14:29   Re: How to parse string
Reply With Quote #8

You were very close.

PHP Code:
    new iMuteTime 0
    
if(containi(szTime"m") != -1)
    {
        
replace(szTimecharsmax(szTime), "m""")
        
iMuteTime str_to_num(szTime) * 60
    
}
    else if(
containi(szTime"h") != -1)
    {
        
replace(szTimecharsmax(szTime), "h""")
        
iMuteTime str_to_num(szTime) * 60 60
    
}
    else if(
containi(szTime"d") != -1)
    {
        
replace(szTimecharsmax(szTime), "d""")
        
iMuteTime str_to_num(szTime) * 60 60 24
    
}
    
    if( 
iMuteTime 604800 )
    {
        
// Exceeds 7 days.
        
return PLUGIN_HANDLED
    
}
    else if( 
iMuteTime <= )
    {
        
// Not a valid time
        
return PLUGIN_HANDLED
    
}
    
    
iMute[player] = get_systime() + iMuteTime
    
    
// say

    
if( iMute[id] > get_systime() )
    {
        
// Player is still muted.
        
return PLUGIN_HANDLED
    

__________________
fysiks is offline
monolit
Junior Member
Join Date: May 2011
Old 05-14-2011 , 15:55   Re: How to parse string
Reply With Quote #9

OK thanks, it works perfect, But how can I display player the time when it expires or something like :

mute time left: %d,

But I would like to show him something like:

mute time left: %dMinutes %dSeconds.

if mute time is less than minute then show only seconds, and if mute time is more than 60 minutes then also show %dHours. help please

It would be cool if it would say something like:

of if there are 0 days then:
monolit is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 05-15-2011 , 01:50   Re: How to parse string
Reply With Quote #10

PHP Code:
// say 

    
static iTimeLeftiDaysiHoursiMinutesiSeconds
    iTimeLeft 
iMute[id] - get_systime()
    if( 
iTimeLeft 
    { 
        
// Player is still muted.

        
iDays iTimeLeft 86400
        iHours 
iTimeLeft 86400 3600
        iMinutes 
iTimeLeft 3600 60
        iSeconds 
iTimeLeft 60
        
        client_print
(idprint_chat"You have %d Days, %d Hours, %d Minutes, and %d Seconds remaining until unmuted"iDaysiHoursiMinutesiSeconds)
        
        return 
PLUGIN_HANDLED 
    

Quote:
Originally Posted by monolit View Post
mute time left: %d,

But I would like to show him something like:

mute time left: %dMinutes %dSeconds.

if mute time is less than minute then show only seconds, and if mute time is more than 60 minutes then also show %dHours.
I did the hard part, you can sort out how it's displayed. Or did I do the easy part? I don't know!

OR an alternative:
PHP Code:
        static szDate[32]
        
format_time(szDatecharsmax(szDate), "%a, %b %d at %H:%M:%S"iMute[id])
        
client_print(idprint_chat"Mute expires %s"szDate
Which would look like: "Mute expires Mon, Jul 11 at 13:24:59"
__________________

Last edited by fysiks; 05-15-2011 at 02:12.
fysiks is offline
Reply



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:20.


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