AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   How to parse string (https://forums.alliedmods.net/showthread.php?t=156879)

monolit 05-13-2011 11:39

How to parse string
 
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.

Nyuszy 05-13-2011 12:04

Re: How to parse string
 
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"

monolit 05-13-2011 12:07

Re: How to parse string
 
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

drekes 05-13-2011 12:38

Re: How to parse string
 
Quote:

Originally Posted by monolit (Post 1468622)
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) 


minimiller 05-13-2011 19:01

Re: How to parse string
 
Or you can use parse()

fysiks 05-14-2011 00:37

Re: How to parse string
 
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 (Post 1468815)
Or you can use parse()

You can't use parse if you want to have any spaces in the reason section.

monolit 05-14-2011 04:12

Re: How to parse string
 
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.

fysiks 05-14-2011 14:29

Re: How to parse string
 
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
    



monolit 05-14-2011 15:55

Re: How to parse string
 
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:

fysiks 05-15-2011 01:50

Re: How to parse string
 
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 (Post 1469386)
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! :wink:

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"


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

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