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

I need a little review


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Ratonel
Member
Join Date: Jul 2012
Location: Romania
Old 09-28-2014 , 07:30   I need a little review
Reply With Quote #1

I am not submiting this plugin because i don't think that it it/will be good enough.

I would apreciate if you could point out any mistakes, bad coding or anything that could be done in a better way. I know it's a bit too much to ask but i want to improve at this, so here i am.



The plugin is suposed to cast a vote at the near end of the map acording to the number of players.

Code:
#include <amxmodx> #include <amxmisc>  #define PLUGIN "CMaps" #define VERSION "1.0" #define AUTHOR "Ratonel" new Array:maps; new gMapCount; new gVoteMenu; new gVoting; new gVotes[20]; new gWinner; new gPrefix, gTime, gMMaps; public plugin_init(){     maps = ArrayCreate(32);     register_plugin(PLUGIN, VERSION, AUTHOR);        gPrefix = register_cvar("cmaps_prefix", "CMaps");     gTime = register_cvar("cmaps_time", "3.0");     gMMaps = register_cvar("cmaps_mmap", "5");         set_task(get_pcvar_float(gTime)*60, "ReadFile", _, _, _, "d"); } public StartVote(){     arrayset( gVotes, 0, sizeof(gVotes));         gVoteMenu = menu_create( "rNextmap should be:", "MenuHandler" );         for( new i; i < gMapCount; i++){         new map[32];         ArrayGetString(maps,i,map,sizeof(map));         menu_additem( gVoteMenu, map, "", 0 );     }         new players[32], pnum, tempid;     get_players( players, pnum );         for (new i = 0 ; i < pnum; i++ ){         tempid = players[i];         menu_display( tempid, gVoteMenu, 0 );         gVoting++;     }         set_task(10.0, "EndVote" );     return PLUGIN_HANDLED; } public MenuHandler( id, menu, item ) {     if ( item == MENU_EXIT || !gVoting ){         return PLUGIN_HANDLED;     }     gVotes[ item ]++;     return PLUGIN_HANDLED; } public EndVote(){         for (new i = 0 ; i < gMapCount; i++ ){         if(gVotes[gWinner] < gVotes[i])             gWinner = i;     }     new map[32],pref[32];     ArrayGetString(maps, gWinner, map, sizeof(map));         get_pcvar_string(gPrefix, pref, 31);         ChatColor( 0, "[!g%s!y] The map %s recived most votes.", pref, map);     menu_destroy( gVoteMenu );     gVoting = 0;         set_cvar_string("amx_nextmap", map);     ArrayDestroy(maps); } public ReadFile(){     new players = get_playersnum();     new read[64];         new StringFile[64];     get_configsdir(StringFile, sizeof(StringFile) - 1);     add(StringFile, sizeof(StringFile) - 1, "/CMaps.ini");         if( !file_exists(StringFile) )     {         return;     }         new hFileHandler = fopen(StringFile, "rt")         while( !feof(hFileHandler)){                 fgets(hFileHandler, read, sizeof(read) - 1);         trim(read);                 if( !read[0] || read[0] == ';'){             continue;         }                 if (read[0] == '#' && is_str_num(read[1])){             new n = str_to_num(read[1]);             if ( n <= players){                 ArrayClear(maps);             }             else{                 break;             }         }         if (is_map_valid(read)){             ArrayPushString(maps, read);         }     }     fclose(hFileHandler);         gMapCount = ArraySize(maps);         ScrambleMaps(); }  public ScrambleMaps(){     for (new i = 0; i < 10; i++){         ArraySwap(maps, random_num(0,gMapCount), random_num(0,gMapCount));     }     if(gMapCount>get_pcvar_num(gMMaps))         gMapCount = get_pcvar_num(gMMaps);     StartVote(); } public ChatColor( id, const input[], any:... ){     new count = 1;     new players[32];     static msg[191];     vformat(msg, 190, input, 3);         replace_all(msg, 190, "!g", "^4"); // Green Color     replace_all(msg, 190, "!y", "^1"); // Default Color     replace_all(msg, 190, "!t", "^3"); // Team Color         if (id)         players[0] = id;     else         get_players(players, count, "ch");     for (new i = 0; i < count; i++){         if (is_user_connected(players[i])){             message_begin(MSG_ONE_UNRELIABLE, get_user_msgid("SayText"), _, players[i]);             write_byte(players[i]);             write_string(msg);             message_end();         }     } }

The config file looks like this:
Code:
#0
aim_map
de_dust2x2
fy_snow
fy_buzzkill
awp_india
cs_deagle6

#8
awp_india
cs_deagle6
de_dust2
de_inferno
de_aztec

#16
de_dust
de_dust2
de_inferno
;de_example - is ignored
de_tuscan32
de_kabul_32

#32
If there are N players on the server, the maps that will be taken for voting will be the ones that are between #A and #B where A<N<B.

Thank you for your time!
Ratonel is offline
AvaStIn
Senior Member
Join Date: Feb 2013
Location: Algeria
Old 09-28-2014 , 07:46   Re: I need a little review
Reply With Quote #2

About prefix why do u want a cvar?
U can use:
PHP Code:
New const gPrefix[] = "CMaps"

// print would be like this :))
ChatColor0"[!g%s!y] The map %s recived most votes."gPrefixmap); 
__________________
Project : Speedrun / Fastrun / CrazySpeed & [FPS CATEGORY]

o [
||||||||||||||||||||] - 95%

Project : Upgraded Drshop To V6.0

o [||||||||||||||||||||]- 100%
AvaStIn is offline
zmd94
Veteran Member
Join Date: Nov 2013
Location: Malaysia (9w2zow).
Old 09-28-2014 , 08:00   Re: I need a little review
Reply With Quote #3

Quote:
Originally Posted by HamletEagle View Post
Cache get_user_msgid("SayText").
Don't re-index an array.
zmd94 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 09-28-2014 , 08:52   Re: I need a little review
Reply With Quote #4

Do not create a new variable in a loop. Also you can cache get_pcvar_string(gPrefix, pref, 31);
__________________

Last edited by HamletEagle; 09-28-2014 at 08:53.
HamletEagle is offline
zmd94
Veteran Member
Join Date: Nov 2013
Location: Malaysia (9w2zow).
Old 09-28-2014 , 10:33   Re: I need a little review
Reply With Quote #5

This is the correct way as I said before:
PHP Code:
new g_MsyTxt

public plugin_init()
{

    
g_MsyTxt get_user_msgid("SayText")
}

public 
ChatColorid, const input[], any:... ){
    new 
count 1;
    new 
players[32];
    static 
msg[191];
    
vformat(msg190input3);
    
    
replace_all(msg190"!g""^4"); // Green Color
    
replace_all(msg190"!y""^1"); // Default Color
    
replace_all(msg190"!t""^3"); // Team Color
    
    
if (id)
        
players[0] = id
    else
        
get_players(playerscount"ch");
        
    for (new 
0counti++){
        if (
is_user_connected(players[i])){
            
message_begin(MSG_ONE_UNRELIABLEg_MsyTxt_players[i]);
            
write_byte(players[i]);
            
write_string(msg);
            
message_end();
        }
    }

Then, just change this:
PHP Code:
get_pcvar_string(gPrefixpref31
--->
PHP Code:
get_pcvar_string(gPrefixprefcharsmax(pref)) 

Last edited by zmd94; 09-28-2014 at 10:38.
zmd94 is offline
Ratonel
Member
Join Date: Jul 2012
Location: Romania
Old 09-29-2014 , 03:57   Re: I need a little review
Reply With Quote #6

Can you please tell me what that does? I want to learn this suff so i would apreciate it if you could provide some information for your snippets, suggestions.

Also, I would like to thank you for taking time to respond to this topic.
Ratonel is offline
mottzi
Veteran Member
Join Date: May 2010
Location: Switzerland
Old 09-29-2014 , 05:18   Re: I need a little review
Reply With Quote #7

Knowing that the menu wont be shown too frequently, and in your case it's only shown once, I would suggest not to cache the menu in a global variable, you can just create the menu and store it in a local variable.
mottzi is offline
Send a message via MSN to mottzi
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 17:02.


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