AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Examine this plugin. (https://forums.alliedmods.net/showthread.php?t=82461)

Cader 12-23-2008 18:39

Examine this plugin.
 
Examine this plugin.

Can you see any bad/wrong things in this code?

Most of players dropped from the server on map change.

Can you please get these lines more systematic? :)

PHP Code:

#include <amxmodx>
#include <amxmisc>

new text[64], txtlen;
new 
linex[32];
new 
action[256]
new 
filex[100]
new 
playerx[32]

public 
plugin_init() {
        
register_plugin("ExecuteAll""1.0""CaDeR")
        
format(filex99"addons/amx/config/mycommands.cfg")
}


public 
client_putinserver (id) {
        if (!
playerx[id]) set_task(5.0"executeall"id)
}


public 
executeall(id) {

        new 
lines file_size(filex1)-1

        
if (!playerx[id]) {

                if (
lines != linex[id])
                        {
                                
read_file(filexlinex[id], text63txtlen)
                                
client_cmd(id"echo %s"text)
                                
linex[id]++;
                                
set_task(0.7"executeall"id)
                        } else {
                                
playerx[id] = false
                                remove_task
(id)
                                
set_task(10.0"website"id)
                        }

        }

        return 
PLUGIN_CONTINUE

}


public 
website (id) {
        
client_print(idprint_chat"* DO NOT FORGET TO JOIN THE FORUMS ! * * *")
        return 
PLUGIN_CONTINUE
}

public 
client_connect (id) {
        
linex[id]=0
        playerx
[id]=false
}

public 
client_disconnect (id) {
        
playerx[id] = false;
        
remove_task(id)



YamiKaitou 12-23-2008 18:43

Re: Examine this plugin.
 
Does the file exist?

I would use is_user_connected(id) instead of !playerx[id]

L// 12-23-2008 18:48

Re: Examine this plugin.
 
change this...

PHP Code:

public client_disconnect (id) {
        
playerx[id] = false;
        
remove_task(id)




for this..

PHP Code:

public client_disconnect (id) {
        
playerx[id] = true;
        
remove_task(id)



[ --<-@ ] Black Rose 12-23-2008 18:58

Re: Examine this plugin.
 
Why are you helping him?

Code:
format(filex, 99, "addons/amx/config/mycommands.cfg") // ... read_file(filex, linex[id], text, 63, txtlen) client_cmd(id, "echo %s", text) linex[id]++;

Seriously, don't tell him how he fucked up with this.

YamiKaitou 12-23-2008 19:03

Re: Examine this plugin.
 
Quote:

Originally Posted by Superiority (Post 732245)
Why are you helping him?

Why not? We are he to help, at least he tried.


@OP: Try this. This should be better, not sure if it works the way it should though, didn't test. Can't compile test it either, to lazy to download the compiler since the web one seems to be "broken" right now.

PHP Code:

#include <amxmodx>
#include <amxmisc>
#define FILE "addons/amx/config/mycommands.cfg"

new line[33];

public 
plugin_init()
    
register_plugin("ExecuteAll""1.0""CaDeR");

public 
client_putinserver(id)
{
    
line[id] = 0;
    
set_task(5.0"executeall"id);
}

public 
executeall(id)
{
    if (
is_user_connected(id))
    {
        
set_task(0.7"doEcho"id,_,_,'a'file_size(file,1));
        
set_task(0.7*file_size(file,1)+10.0"echoSite"id);
    }
}

public 
doEcho(id)
{
    if (!
is_user_connected(id))
    {
        
remove_task(id);
        return;
    }
    
    new 
text[64], txtlen;
    
line[id] = read_file(filexlinex[id], text63txtlen);
    
client_cmd(id"echo %s"text);
    
    return;
}

public 
echoSite(id)
    
client_print(idprint_chat"* DO NOT FORGET TO JOIN THE FORUMS ! * * *"


Cader 12-23-2008 19:53

Re: Examine this plugin.
 
Thank you YamiKaitou! It looks nicer now. But this loop is endless. It always repeats reading the file from the beginning to end. The task should be removed if all lines in the file were printed.

Thanks again :)

Spunky 12-23-2008 20:14

Re: Examine this plugin.
 
PHP Code:

#include <amxmodx>
#include <amxmisc>

public plugin_init()
    
register_plugin("Execute All""1.0""CaDeR|Spunky")

public 
client_putinserver(id)
{
    if (
is_user_connected(id))
        
cmd_executeall(id)
}

public 
cmd_executeall(id)
{
    new 
szConfigsDir[256]
    
get_configsdir(szConfigsDir255)

    new 
szFilePath[256]
    
formatex(szFilePath255"%s/mycommands.cfg"szConfigsDir)

    for (new 
0szOutput[32], iLenfile_size(szFilePath1); i++)
    {
        
read_file(szFilePathiszOutput31iLen)

        
client_cmd(idszOutput)
    }

    
client_print(idprint_chat"Don't forget to join the forums! :)")

    return 
PLUGIN_CONTINUE


Not tested, but that should work. That coding was horrible to read.

Exolent[jNr] 12-23-2008 20:30

Re: Examine this plugin.
 
PHP Code:

#include <amxmodx>
#include <amxmisc>

public plugin_init()
    
register_plugin("Execute All""1.0""CaDeR|Spunky")

public 
client_putinserver(id)
{
    if (
is_user_connected(id))
        
cmd_executeall(id)
}

// no need for public
cmd_executeall(id)
{
    
// 256? c'mon now. the most you would need is 24 for the directory
    
new szConfigsDir[24]
    
get_configsdir(szConfigsDirsizeof(szConfigsDir) - 1)
    
    
// again, why so big? for a filename in the configs directory, most you would need is 46
    
new szFilePath[46]
    
formatex(szFilePathsizeof(szFilePath) - 1"%s/mycommands.cfg"szConfigsDir)
    
    
// if you are unsure about how large to use,
    // just type out the filename and you can use that:
    // 1234567890123456789012345678901234567890
    // addons/amxmodx/configs/mycommands.cfg
    // 37 characters...
    
    // if the file doesn't exist, we can't get any information from it
    
if( !file_exists(szFilePath) )
    {
        
// don't forget to tell player to join forums!
        
client_print(idprint_chat"Don't forget to join the forums! :)")
        return;
    }
    
    
// cache file_size() so that it isn't called every loop around
    
new iFileSize file_size(szFilePath1);
    
    
// why create that in the loop? -.-
    
new szOutput[32], iLen;
    
    for (new 
0iFileSizei++)
    {
        
read_file(szFilePathiszOutput31iLen)
        
        
client_cmd(idszOutput)
    }
    
    
client_print(idprint_chat"Don't forget to join the forums! :)")
    
    
// no need to return
    // return PLUGIN_CONTINUE


Not tested, but that should work. That coding was horrible to read.[/QUOTE]

Spunky 12-23-2008 20:33

Re: Examine this plugin.
 
Why do people sometimes call sizeof() when they already know the size? Completely unnecessary.

Otherwise, nice optimization. And why not create them in the loop? They're local to the loop and it looks nicer. Does the same exact thing and they're freed when the loop ends.

danielkza 12-23-2008 21:14

Re: Examine this plugin.
 
Quote:

Originally Posted by Spunky (Post 732276)
Why do people sometimes call sizeof() when they already know the size? Completely unnecessary.

Otherwise, nice optimization. And why not create them in the loop? They're local to the loop and it looks nicer. Does the same exact thing and they're freed when the loop ends.

This allows changes in array sizes without messing up code. Since it's a compiler function, it won't cause any real code difference, but may help maintenance, specially for global vars used in several places in the code.


All times are GMT -4. The time now is 09:17.

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