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();
}
}
}
players on the server, the maps that will be taken for voting will be the ones that are between