#include <amxmodx>
#include <fvault>
#include <cstrike>
#include <wm_play>

#define maxranks 15
#define maxlevels 50

#define xPrefix "XP System"

#define RANKS_Noobest 0
#define RANKS_Noob 1
#define RANKS_Newbiee 2
#define RANKS_Easy 3
#define RANKS_Normal 4
#define RANKS_Hard 5
#define RANKS_Expert 6
#define RANKS_SuperExpert 7
#define RANKS_Specialist 8
#define RANKS_Leader 9
#define RANKS_Mayor 10
#define RANKS_Pro 11
#define RANKS_SuperPro 12
#define RANKS_Heroic 13
#define RANKS_God 14

native get_gungame_level(const id); // --> Nativeyi kullanmak icin tanimladik.
native add_gungame_score(const id); // --> Nativeyi kullanmak icin tanimladik.

new const VERSION[] =  "1.1"
new SzMaxPlayers, SzSayText;
new playerPrefix, rankLevelBonus, rankSaveType;

new SzGTeam[3][32] = {
    "Spectator",
    "Terrorist",
    "Counter-Terrorist"
}

new const xp_num[maxlevels+1] = 
{ 
    15, 70, 150, 250, 350, 450, 550, 650, 750, 850, 950, 1050, 1150, 1250, 1350, 1450, 1550, 1650,
    1750, 1850, 1950, 2050, 2150, 2250, 2350, 2450, 2550, 2650, 2750, 2850, 2950, 3050, 3150, 3250, 3350, 3450, 3550, 3650, 
    3750, 3850, 3950, 4050, 4150, 4250, 4350, 4450, 4550, 4650, 4750, 5000 
}

new const ranks_names[maxranks][]= 
{
    "KING OF BOT",
    "KING OF NOOB",
    "KING OF THE RECRUITS",
    "KING OF CONVENIENCE",
    "KING OF NORMALS",
    "KING OF DIFFICULT PLAYERS",
    "KING OF EXPERTS",
    "KING OF SUPER SPECIALISTS",
    "KING OF SUPER EXPERT PLAYERS",
    "KING OF LEADERS",
    "KING OF MAYORS",
    "KING OF PRO PLAYERS",
    "KING OF SUPER PRO PLAYERS",
    "KING OF GUNGAME",
    "GOD KING OF GUNGAME"
}

new xp[33], level[33], rank[33]; // Dizi tanımlamaları eksikti.

public plugin_init()
{
    register_plugin("XP + LEVEL + RANK SYSTEM", VERSION, AUTHOR)

    // system of xp+lvl+rank
    playerPrefix = register_cvar("Player Prefix", "1"); //1-Prefix by RANK, 2-Prefix by LEVEL
    rankLevelBonus = register_cvar("Level Up Bonus", "10000"); // Amount of money when passing level.
    rankSaveType = register_cvar("XP Save Type", "2"); // 1 - IP || 2 - Nick || 3 - SteamID
    
    register_cvar("XPLvlRankSystem", VERSION, FCVAR_SERVER | FCVAR_SPONLY ) //Search for variable on Gametracker.com

    register_event("StatusValue", "showStatus", "be", "1=2", "2!0")
    register_event("StatusValue", "hideStatus", "be", "1=1", "2=0")
    
    register_clcmd("say", "hook_say");
    register_clcmd("say_team", "hook_say_team");
    
    register_dictionary("cs_rank_system.txt");

    SzSayText = get_user_msgid ("SayText");
    SzMaxPlayers = get_maxplayers();
    
    register_message(SzSayText, "MsgDuplicate");
    
    g_status_sync = CreateHudSyncObj()
}

public check_gungame_level(id)
{
    if (!is_user_connected(id)) 
        return PLUGIN_HANDLED;
    
    static last_gungame_level[33];
    new current_level = get_gungame_level(id);
    
    if (current_level > last_gungame_level[id]) 
    {
        xp[id] += 5;
       
        client_print_color(id, "!g[%s] %L", xPrefix, LANG_PLAYER, "MSG_GUNGAME_XP_LEVEL_UP", level[id]);
        client_print_color(0, "!g[%s] %L", xPrefix, LANG_PLAYER, "MSG_MAXLVL_ALL", name, level[id]);
        
        check_level(id, 1); 
        save_data(id); 
    }
    
    last_gungame_level[id] = current_level;
    
    return PLUGIN_HANDLED;
}

public add_gungame_score(id)
{
    if (!is_user_connected(id)) 
        return PLUGIN_HANDLED;
    
    xp[id] += 2;
    client_print_color(id, "!g[%s] %L", xPrefix, LANG_PLAYER, "MSG_GUNGAME_SCORE_XP", 2);
    
    check_level(id, 1); // Yeni seviyeyi kontrol et
    save_data(id); // Yeni veriyi kaydet

    return PLUGIN_HANDLED;
}

public check_level(id, sound)
{
    if(!is_user_connected(id))
        return PLUGIN_HANDLED;
    
    new name[32]; get_user_name(id, name, 31)
    
    if(level[id] < maxlevels) 
    {        
        while(xp[id] >= xp_num[level[id]])
        {
            level[id]++;
            
            if(sound)
            {
                if(level[id] == maxlevels)
                {
                    client_print_color(id, "!g[%s] %L", xPrefix, LANG_PLAYER, "MSG_MAXLVL_ID", level[id])
                    client_print_color(0, "!g[%s] %L",xPrefix, LANG_PLAYER, "MSG_MAXLVL_ALL", name, level[id])
                    
                    client_cmd(0, "spk ambience/wolfhowl02.wav")
                    
                    return PLUGIN_HANDLED
                }
                
                client_print_color(id,"!g[%s] %L",xPrefix,LANG_PLAYER, "MSG_RAISE_LEVEL_ID", get_pcvar_num(rankLevelBonus))
                client_print_color(0, "!g[%s] %L",xPrefix,LANG_PLAYER, "MSG_RAISE_LEVEL_ALL", name, get_pcvar_num(rankLevelBonus))
                cs_set_user_money(id, cs_get_user_money(id) + get_pcvar_num(rankLevelBonus))
                
                client_cmd(0, "spk ambience/lv_fruit1.wav")
                
                set_ranks(id)
            }
        }
    } 
    
    if(level[id] == maxlevels && xp[id] > xp_num[level[id]-1])
    {
        xp[id] = xp_num[level[id]-1]
        save_data(id)
    }
    
    if(level[id] >= maxlevels) 
    {    
        level[id] = maxlevels
        xp[id] = xp_num[level[id]-1]
        save_data(id)
    }
    
    return PLUGIN_HANDLED
}

public set_ranks(id)
{
    if(level[id] <= 2) rank[id] = RANKS_Noobest
    if(level[id] >= 2) rank[id] = RANKS_Noob
    if(level[id] >= 5) rank[id] = RANKS_Newbiee
    if(level[id] >= 10) rank[id] = RANKS_Easy
    if(level[id] >= 15) rank[id] = RANKS_Normal
    if(level[id] >= 20) rank[id] = RANKS_Hard
    if(level[id] >= 25) rank[id] = RANKS_Expert
    if(level[id] >= 30) rank[id] = RANKS_SuperExpert
    if(level[id] >= 35) rank[id] = RANKS_Specialist
    if(level[id] >= 40) rank[id] = RANKS_Leader
    if(level[id] >= 45) rank[id] = RANKS_Mayor
    if(level[id] >= 50) rank[id] = RANKS_Pro
}

public save_data(id)
{
    static FileManager:fm = create_fm("");
    
    switch(get_pcvar_num(rankSaveType))
    {
        case 1:
            fm = vault_save_data(id, xp, level, rank);
            break;
        case 2:
            fm = vault_save_data(id, xp, level, rank);
            break;
    }
}

public MsgDuplicate(msgid)
{
    return PLUGIN_HANDLED;
}

public client_authorized(id)
{
    new auth[40], data[50], x1[10], x2[10]
    
    switch(get_pcvar_num(rankSaveType))
    {
        case 1: get_user_ip(id, auth, charsmax(auth), 1)
        case 2: get_user_name(id, auth, charsmax(auth))
        case 3: get_user_authid(id, auth, charsmax(auth))
    }
    
    fvault_get_data(db_save, auth, data, charsmax(data))
    parse(data, x1, charsmax(x1), x2, charsmax(x2))
    
    level[id] = str_to_num(x1)
    xp[id] = str_to_num(x2)
    
    set_task(2.0, "set_ranks", id)
    check_level(id, 0)
    
    set_task(1.1, "hud_status", id, _, _, "b")
}

public showStatus(id)
{
    if(!is_user_bot(id) && is_user_connected(id)) 
    {
        new name[32], pid = read_data(2)
        
        get_user_name(pid, name, 31)
        
        new xxx = get_user_team(id)
        new xxx2 = get_user_team(pid)
        
        static r, g, b;
        r = random_num(0, 255), g = random_num(0, 255), b = random_num(0, 255);
        
        if (xxx == xxx2)    // friend
        {
            set_hudmessage(r, g, b, -1.0, 0.60, 1, 0.01, 3.0, 0.01, 0.01, -1)
            ShowSyncHudMsg(id, g_status_sync, "Name: %s^nRank: %s^nLevel: %d^nXP: %d/%d", name, ranks_names[rank[pid]], level[pid], xp[pid], xp_num[level[pid]])
        }
    }
}

public hideStatus(id)
{
    ClearSyncHud(id, g_status_sync)
}

/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang1046\\ f0\\ fs16 \n\\ par }
*/
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang16393\\ f0\\ fs16 \n\\ par }
*/