/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <amxmisc>
#include <nvault_util>

#define PLUGIN "COD:MW3 - Top 15"
#define VERSION "1.0"
#define AUTHOR "Play"

#define MAX_TOP 15

new	top_cmd[32],
	vault_name[32];
	
new	top_string[1512];

new 	top_name[MAX_TOP+1][64],
	top_class[MAX_TOP+1][32],
	top_poziom[MAX_TOP+1],
	top_doswiadczenie[MAX_TOP+1];

public plugin_init(){
	register_plugin(PLUGIN, VERSION, AUTHOR);
	
	RegisterCvars();
	RegisterCmds();
	LoadTop();
}

public toplvl(id){
	show_motd(id, top_string, "COD Elite | Top 15");
}


RegisterCvars(){
	register_cvar("cod_top15_cmd", "/toplvl");
	register_cvar("cod_top15_save_file", "CodElite");
	
	get_cvar_string("cod_top15_cmd", top_cmd, 31);
	get_cvar_string("cod_top15_save_file", vault_name, 31);
}

RegisterCmds() 
{
	new temp[64];
	format(temp, 63, "say %s", top_cmd);
	
	register_clcmd(temp, "toplvl");
}

LoadTop() 
{
	new key[64], data[32], timestamp, pos, vault = nvault_util_open(vault_name);
	new num = nvault_util_count(vault);
	
	new dane[6][32], name[64], class[64], trash[16], poziom, doswiadczenie, temp = get_cvar_num("cod_savetype");
	format(trash, 15, "-%i-cod", temp);
	
	for(new i = 1; i <= num; i ++){
		pos = nvault_util_read(vault, pos, key, 63, data, 31, timestamp)
		
		replace_all(data, 255, "#", " ");
		
		parse(data, dane[0], 31, dane[1], 31, dane[2], 31, dane[3], 31, dane[4], 31, dane[5], 31);
		
		split(key, name, 63, class, 63, "-");
		
		replace_all(class, 63, trash, "");  
		
		poziom = max(1, str_to_num(dane[1]));
		doswiadczenie = str_to_num(dane[0]);	
		
		for(new j = 0; j < MAX_TOP; j ++){
			if(doswiadczenie >= top_doswiadczenie[j]){
				for(new k = MAX_TOP; k > j; k --){
					copy(top_name[k], 31, top_name[k-1]);
					copy(top_class[k], 31, top_class[k-1]);
					
					top_poziom[k] = top_poziom[k-1];
					top_doswiadczenie[k] = top_doswiadczenie[k-1];
				}
				
				copy(top_name[j], 63, name);
				copy(top_class[j], 31, class);
				
				top_poziom[j] = poziom;
				top_doswiadczenie[j] = doswiadczenie;
				
				break;
			}
		}
	}	

	new len = format(top_string, 1511, "<html><body bgcolor=^"#666666^">")
	
	len += format(top_string[len], 1511 - len, "<center><b><font size=6 color=#1E90FF>COD Elite | Top 15</b><hr size=1 color=#00CD00><table style=^"color:#FFFFFF;width:750%^">");
	len += format(top_string[len], 1511 - len, "<tr style=color:#FF00FF;><td>Miejsce<td>Gracz<td>Klasa<td>Poziom<td>Doswiadczenie");
	
	for(new l = 0; l < MAX_TOP; l ++){
		if(equal(top_name[l], ""))
			break;
		
		len += format(top_string[len], 1511 - len, "<tr><td>%i.<td>%s<td>%s<td>%i<td>%i", l+1, top_name[l], top_class[l], top_poziom[l], top_doswiadczenie[l]);
	}
	
	
	return PLUGIN_CONTINUE;
} 
