Raised This Month: $12 Target: $400
 3% 

Automatic Command Executor


Post New Thread Reply   
 
Thread Tools Display Modes
Plugin Info:     Modification:   ALL        Category:   Server Management       
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 07-29-2015 , 10:15   Automatic Command Executor
Reply With Quote #1


< Automatic Command Executor >

This plugin allows you to add specific commands in a .ini file. The commands will be used on the player that you choose when he joins the server. This is useful when organizing tournaments in which the winner gets a specific reward that need to be given to him when he is in the server, for example deathrun points. Not all plugins use additional files for these kind of stuff, so instead of waiting for the player to join the server and an administrator to be online, simply add the player's name, SteamID or IP address and the command in the .ini file. The plugin can also be used as a server protection against particular players.

How to add a command through the .ini file:

In order to add a command, you need to enter the following information in configs/AutoCommandExec.ini:

PHP Code:
"type=name/steam/ip" "player's name/SteamID/IP" "command" "repeat (yes/no)" "message (optional)" 
In the command field you can add %info% to use the player's ID, and in the command field you can use %name% for the player's name and %prefix% for the plugin's prefix. You can also use different colors:

Quote:
!n = default (usually yellow)
!t = team color
!g = green
Examples:

PHP Code:
"name" "OciXCrom" "deathrun_give_points %info% 100" "no" "%prefix% Player !g%name% !nreceived !t300 DRPoints !nfor winning the !gTOURNAMENT"
"steam" "STEAM_0:0:123456789" "kick %info%" "yes"
"ip" "12.34.56.78" "echo He entered the server!" "no" "Player !g%name% !nhas joined the game." 
Bear in mind that any changes to the .ini file will take effect after the map changes. If you want them to take effect right away, use the "Reload .ini file" option in the main menu.

Plugin's main menu:

You can open up the plugin's main menu by typing autocommandexec in the console (requires RCON flag by default). The menu looks like this:


  • Add new entry: with this option you can add an entry directly into the .ini file
  • View all entries: this will show all active entries in the .ini file
  • Reload .ini file: use this option if you want to reload the .ini file (in case you edited it and want the changes to take effect right away)

Cvar list:
  • autocommand_time <default: "3.0"> -- The time that is needed for the command to be executed after the player has joined the server.

Images:














Last edited by OciXCrom; 07-17-2018 at 09:03. Reason: Version 2.0: Full update.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 07-29-2015 , 10:28   Re: Automatic Command Executor [1.0]
Reply With Quote #2

This was your question for ? Lol, you were fast posting this. I can see a lot evil uses(like slowhacking).

And, code needs some fixing.
__________________

Last edited by HamletEagle; 07-29-2015 at 10:29.
HamletEagle is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 07-29-2015 , 12:26   Re: Automatic Command Executor [1.0]
Reply With Quote #3

I needed this for my server as soon as possible, so I didn't take much time adding more options to it. I don't think it can be used as slowhacking, since it makes the server execute the command, not the player. I look forward to seeing some suggestions about fixing the code. Later I will probably add some extra options.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 07-29-2015 , 13:21   Re: Automatic Command Executor [1.0]
Reply With Quote #4

Yes, missed that part. Well, some suggestions:

PHP Code:
filename[256
256 is too much, 128 should be fine.

PHP Code:
get_configsdir(filename255)
format(filename255"%s/AutoCommandExec.ini"filename
Do not hardcode a string array size, use charsmax() to get it. Why ? Imagine that you are updating a plugin and due to this changes you need a bigger size for a string. If you don't hardcode it you only need to change in one place, if it's hardcoded you must change it everywhere, and this is a pain IMO. Also, we should keep good practices everywhere.

PHP Code:
get_configsdir(TempFilecharsmax(TempFile)) 
Well, you can avoid calling get_configsdir two times. Create another global string and retrieve inside it configs dir path.

Something like:
PHP Code:
new ConfigsDirPath[128]

public 
plugin_init()
{
     
get_configsdir(ConfigsDirPathcharsmax(ConfigsDirPath))
     
formatex(filenamecharsax(filename), "%s/AutoCommandExec.ini"ConfigsDirPath)
}

public 
file_read(id)
{
    new 
filepointer fopen(filename"rt")
    
    if(
filepointer)
    {
        new 
TempFile[128]
                new const 
FileName[] = "/tempfile.ini"
        
formatex(TempFilecharsmax(TempFile), "%s%s"ConfigsDirPathFileName)
               
//rest of code 
Hope you get what I mean.

PHP Code:
name[32], authid[32], ip[32
Having all of this seems weird, you can create only one and change a bit the way in which you parse your file.

PHP Code:
new Auth[32]
//later
if(equal(parsedtype"name"))
{
                
get_user_name(idAuthcharsmax(Auth))
        if(
equali(Authparseddata))
            
match true
}
else if(
equal(parsedtype"steam"))
{
                
get_user_authid(...)
        if(
equal(Auth parseddata))
        {
                if(
steam_valid(id))
                    
match true
        
}

I think you get it. Also, I would do a switch instead if/else if, it will make the code more readable.

Also, indent your code please:
PHP Code:
if(equal(ipparseddata))
match true 
PHP Code:
replace_all(commandcharsmax(command), "%info%""^"%replace%^"")
replace_all(commandcharsmax(command), "%replace%"userid
Why ? Directly replace %info% with userid.

On steam only server steam can't be invalid, your steam_valid stock is not needed.

The way in which you need to read the file every time a player connects seems be bad. You can look at tries, they can help you solve your problem. When you read the file, set everything into a trie(the key can be parsedtype and the string parsedcommand). Then, when a player connects, get his name/ip/steamid and check if the key exists into the trie. If so, retrieve the string and execute the command.

And, you won't need parsedtype anymore.
__________________
HamletEagle is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 07-29-2015 , 13:57   Re: Automatic Command Executor [1.0]
Reply With Quote #5

Quote:
Originally Posted by HamletEagle View Post
Yes, missed that part. Well, some suggestions:

PHP Code:
filename[256
256 is too much, 128 should be fine.

PHP Code:
get_configsdir(filename255)
format(filename255"%s/AutoCommandExec.ini"filename
Do not hardcode a string array size, use charsmax() to get it. Why ? Imagine that you are updating a plugin and due to this changes you need a bigger size for a string. If you don't hardcode it you only need to change in one place, if it's hardcoded you must change it everywhere, and this is a pain IMO. Also, we should keep good practices everywhere.
I usually use it with "charsmax", but it seems that I accidentally did it the other way this time.

Quote:
Originally Posted by HamletEagle View Post
PHP Code:
get_configsdir(TempFilecharsmax(TempFile)) 
Well, you can avoid calling get_configsdir two times. Create another global string and retrieve inside it configs dir path.

Something like:
PHP Code:
new ConfigsDirPath[128]

public 
plugin_init()
{
     
get_configsdir(ConfigsDirPathcharsmax(ConfigsDirPath))
     
formatex(filenamecharsax(filename), "%s/AutoCommandExec.ini"ConfigsDirPath)
}

public 
file_read(id)
{
    new 
filepointer fopen(filename"rt")
    
    if(
filepointer)
    {
        new 
TempFile[128]
                new const 
FileName[] = "/tempfile.ini"
        
formatex(TempFilecharsmax(TempFile), "%s%s"ConfigsDirPathFileName)
               
//rest of code 
Hope you get what I mean.
Done.

Quote:
Originally Posted by HamletEagle View Post
PHP Code:
name[32], authid[32], ip[32
Having all of this seems weird, you can create only one and change a bit the way in which you parse your file.

PHP Code:
new Auth[32]
//later
if(equal(parsedtype"name"))
{
                
get_user_name(idAuthcharsmax(Auth))
        if(
equali(Authparseddata))
            
match true
}
else if(
equal(parsedtype"steam"))
{
                
get_user_authid(...)
        if(
equal(Auth parseddata))
        {
                if(
steam_valid(id))
                    
match true
        
}

I think you get it. Also, I would do a switch instead if/else if, it will make the code more readable.
Done. I made them all into one string and I also removed the triple checking.

Quote:
Originally Posted by HamletEagle View Post
Also, indent your code please:
PHP Code:
if(equal(ipparseddata))
match true 
I must have deleted something when copying. Fixed.

Quote:
Originally Posted by HamletEagle View Post
PHP Code:
replace_all(commandcharsmax(command), "%info%""^"%replace%^"")
replace_all(commandcharsmax(command), "%replace%"userid
Why ? Directly replace %info% with userid.
At first I made it use the player's name, so I needed to add quotes because the name can have spaces. Forgot to change it when I made it with userID. Fixed.[/QUOTE]

Quote:
Originally Posted by HamletEagle View Post
On steam only server steam can't be invalid, your steam_valid stock is not needed.
I wasn't sure about "STEAM_ID_LAN", but I don't think anyone will use a command on that one. Removed.

Quote:
Originally Posted by HamletEagle View Post
The way in which you need to read the file every time a player connects seems be bad. You can look at tries, they can help you solve your problem. When you read the file, set everything into a trie(the key can be parsedtype and the string parsedcommand). Then, when a player connects, get his name/ip/steamid and check if the key exists into the trie. If so, retrieve the string and execute the command.

And, you won't need parsedtype anymore.
I will need to read some tutorials about that. I will leave it like this for now.

Thank you for all of your suggestions! The code has been updated.

Last edited by OciXCrom; 07-29-2015 at 13:57.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 07-29-2015 , 14:15   Re: Automatic Command Executor [1.0]
Reply With Quote #6

Quote:
Originally Posted by HamletEagle View Post
PHP Code:
filename[256
256 is too much, 128 should be fine.
It's actually not, Windows path can be up to 259 (256 + Drive letter, a colon and a backslash) characters long, and even AMXX uses 256 in its code to retrieve paths, like configs directory. Take a look here for an example. Nothing wrong with having 256 in there. I even myself define a PATH_MAX constant in my plugins when working with files, and set its value to 256/260.
klippy is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 10-04-2015 , 09:18   Re: Automatic Command Executor
Reply With Quote #7

The plugin is updated. Now it can be used as a server protection by enabling the repeat option on a specific command, for example:

PHP Code:
"name" "OciXCrom" "kick %info%" "yes" 

Last edited by OciXCrom; 10-04-2015 at 09:18.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 10-04-2015 , 09:38   Re: Automatic Command Executor
Reply With Quote #8

The use of tries is a requierement if you want this approved. Basically, reading the file on each join is ineficient. If you really don't know how to use tries open a topic in scripting help forum.
__________________
HamletEagle is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 10-04-2015 , 09:41   Re: Automatic Command Executor
Reply With Quote #9

I somewhat know how to use them, but I'm not sure how exactly am I going to accomplish it. Reading the file and creating the tries is probably easy, but I have no idea what to do after the command is executed, since the file is no longer open, but it still needs to be edited.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 10-04-2015 , 16:31   Re: Automatic Command Executor [1.0]
Reply With Quote #10

Quote:
Originally Posted by HamletEagle View Post
Yes, missed that part. Well, some suggestions:

Something like:
PHP Code:
new ConfigsDirPath[128]

public 
plugin_init()
{
     
get_configsdir(ConfigsDirPathcharsmax(ConfigsDirPath))
     
formatex(filenamecharsax(filename), "%s/AutoCommandExec.ini"ConfigsDirPath)
}

public 
file_read(id)
{
    new 
filepointer fopen(filename"rt")
    
    if(
filepointer)
    {
        new 
TempFile[128]
                new const 
FileName[] = "/tempfile.ini"
        
formatex(TempFilecharsmax(TempFile), "%s%s"ConfigsDirPathFileName)
               
//rest of code 
Hope you get what I mean.
Hi, I just recently noticed this, and I want to recommend it. Don't use a name like 'ConfigsDirPath' for a global variable, as the code grows, you can end confusing a global with local variable.

Hence, just add a g_ prefix to it (global) and the problem is solved. Keep in mind too, when using cvar pointes, for example, 'gp_weaponPrice' (global pointer).

And I think is good to keep the Object Oriented Programming convention, to start identifiers and functions name with lower case level and each consecutively word with Upper case level, as these days OOP is really in right now.

PHP Code:
new g_configsDirPath[128]

public 
plugin_init()
{
     
get_configsdir(ConfigsDirPathcharsmax(g_configsDirPath))
     
formatex(filenamecharsax(filename), "%s/AutoCommandExec.ini"g_configsDirPath)
}

public 
file_read(id)
{
    new 
filepointer fopen(filename"rt")
    
    if(
filepointer)
    {
        new 
TempFile[128]
                new const 
FileName[] = "/tempfile.ini"
        
formatex(TempFilecharsmax(TempFile), "%s%s"g_configsDirPathFileName)
               
//rest of code 

Last edited by addons_zz; 10-04-2015 at 16:37.
addons_zz 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 05:56.


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