Raised This Month: $51 Target: $400
 12% 

question abut cpw usage


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
twix_p
Member
Join Date: Jul 2011
Old 03-02-2014 , 18:02   question abut cpw usage
Reply With Quote #1

Helo,
I found this rank plugin and I was thinking to continue to develop it but to keep the base, the saving and loading method.

PHP Code:
#include <amxmodx>
#include <amxmisc>

//#define HTML_IN_MOTD
#define SAVE_RANKS_AFTER_SORT
#define SORT_INTERVAL 15.0

enum RankData
{
    
Data_SteamID[32],
    
Data_Name[32],
    
Data_Kills,
    
Data_Deaths
};

new 
g_File[64];
new Array:
g_SteamID;
new 
Trie:g_NameTrie:g_KillsTrie:g_Deaths;
new 
g_Top15[2048];
new 
bool:g_Sort true;
new 
g_Data[33][RankData], bool:g_Authorized[33];

public 
plugin_init()
{
    
register_plugin("Rank""3.0""hleV");

    
get_datadir(g_File63);
    
add(g_File63"/ranks.ini");

    
g_SteamID ArrayCreate(321);
    
g_Name TrieCreate();
    
g_Kills TrieCreate();
    
g_Deaths TrieCreate();

    
LoadRanks();
    
ArraySort(g_SteamID"SortRanks");
    
WriteTop15();

    
set_task(SORT_INTERVAL"SortTask"___"b");

    
register_clcmd("say /rank""SayRank");
    
register_clcmd("say /top15""SayTop15");

    
register_event("DeathMsg""DeathMsg""a");
}

public 
plugin_end()
{
    
ArraySort(g_SteamID"SortRanks");
    
SaveRanks();

    
ArrayDestroy(g_SteamID);
}

public 
client_putinserver(Client)
{
    
get_user_authid(Clientg_Data[Client][Data_SteamID], 31);

    if (!
str_to_num(g_Data[Client][Data_SteamID][10]))
        return;

    
get_user_name(Clientg_Data[Client][Data_Name], 31);

    if (!
TrieKeyExists(g_Nameg_Data[Client][Data_SteamID]))
        
AddRank(Client);
    else
        
LoadData(Client);

    
UpdateRank(Clienttrue);

    
g_Authorized[Client] = true;
}

public 
client_disconnect(Client)
    
g_Authorized[Client] = false;

public 
client_infochanged(Client)
{
    if (!
g_Authorized[Client])
        return;

    static 
Name[32];
    
get_user_info(Client"name"Name31);

    if (
equal(Nameg_Data[Client][Data_Name]))
        return;

    
copy(g_Data[Client][Data_Name], 31Name);
    
UpdateRank(Clienttrue);
}

public 
SayRank(Client)
{
    new 
Position GetPosition(Client);

    if (!
g_Authorized[Client] || !Position)
    {
        
client_print(Clientprint_chat"* You are not ranked.");

        return;
    }

    
client_print
    
(
        
Client,
        
print_chat,
        
"* Your rank is %d of %d with %d kills and %d deaths.",
        
Position,
        
ArraySize(g_SteamID),
        
g_Data[Client][Data_Kills],
        
g_Data[Client][Data_Deaths]
    );
}

public 
SayTop15(Client)
    
show_motd(Clientg_Top15"Top 15");

public 
DeathMsg()
{
    new 
Killer read_data(1);
    new 
Victim read_data(2);

    if (
g_Authorized[Victim])
    {
        
g_Data[Victim][Data_Deaths]++;
        
g_Sort true;

        
UpdateRank(Victimfalse);
    }

    if (
g_Authorized[Killer] && Killer != Victim)
    {
        
g_Data[Killer][Data_Kills]++;
        
g_Sort true;

        
UpdateRank(Killerfalse);
    }
}

public 
SortTask()
{
    if (!
g_Sort)
        return;

    
ArraySort(g_SteamID"SortRanks");
    
WriteTop15();

#if defined SAVE_RANKS_AFTER_SORT
    
SaveRanks();
#endif
}

public 
SortRanks(Array:SteamIDPosition1Position2)
{
    static 
SteamID1[32];
    
ArrayGetString(SteamIDPosition1SteamID131);

    static 
SteamID2[32];
    
ArrayGetString(SteamIDPosition2SteamID231);

    static 
Kills1;
    
TrieGetCell(g_KillsSteamID1Kills1);

    static 
Kills2;
    
TrieGetCell(g_KillsSteamID2Kills2);

    static 
Deaths1;
    
TrieGetCell(g_DeathsSteamID1Deaths1);

    static 
Deaths2;
    
TrieGetCell(g_DeathsSteamID2Deaths2);

    if (
Kills1 Deaths1 Kills2 Deaths2)
        return 
1;
    else if (
Kills1 Deaths1 Kills2 Deaths2)
        return -
1;

    return 
0;
}

LoadRanks()
{
    new 
File fopen(g_File"r");

    if (!
File)
        return;

    new 
Data[96], SteamID[32], Name[32], Kills[16], Deaths[16];

    while (!
feof(File))
    {
        
fgets(FileData96);

        if (!
strlen(Data))
            continue;

        
parse(DataSteamID31Name31Kills15Deaths15);

        
ArrayPushString(g_SteamIDSteamID);
        
TrieSetString(g_NameSteamIDName);
        
TrieSetCell(g_KillsSteamIDstr_to_num(Kills));
        
TrieSetCell(g_DeathsSteamIDstr_to_num(Deaths));
    }

    
fclose(File);
}

SaveRanks()
{
    new 
File fopen(g_File"w+");

    if (!
File)
        return;

    for (new 
PositionSize ArraySize(g_SteamID), SteamID[32], Name[32], KillsDeathsPosition SizePosition++)
    {
        
ArrayGetString(g_SteamIDPositionSteamID31);
        
TrieGetString(g_NameSteamIDName31);
        
TrieGetCell(g_KillsSteamIDKills);
        
TrieGetCell(g_DeathsSteamIDDeaths);

        
fprintf(File"%s ^"%s^" %d %d^n"SteamIDNameKillsDeaths);
    }

    
fclose(File);
}

AddRank(Client)
{
    
g_Data[Client][Data_Kills] = 0;
    
g_Data[Client][Data_Deaths] = 0;

    
ArrayPushString(g_SteamIDg_Data[Client][Data_SteamID]);
    
TrieSetString(g_Nameg_Data[Client][Data_SteamID], g_Data[Client][Data_Name]);
}

LoadData(Client)
{
    
TrieGetCell(g_Killsg_Data[Client][Data_SteamID], g_Data[Client][Data_Kills]);
    
TrieGetCell(g_Deathsg_Data[Client][Data_SteamID], g_Data[Client][Data_Deaths]);
}

UpdateRank(Clientbool:Name)
{
    if (
Name)
        
TrieSetString(g_Nameg_Data[Client][Data_SteamID], g_Data[Client][Data_Name]);

    
TrieSetCell(g_Killsg_Data[Client][Data_SteamID], g_Data[Client][Data_Kills]);
    
TrieSetCell(g_Deathsg_Data[Client][Data_SteamID], g_Data[Client][Data_Deaths]);
}

GetPosition(Client)
{
    static 
PositionSizeSteamID[32];

    for (
Position 0Size ArraySize(g_SteamID); Position SizePosition++)
    {
        
ArrayGetString(g_SteamIDPositionSteamID31);

        if (
equal(SteamIDg_Data[Client][Data_SteamID]))
            return 
Position 1;
    }

    return 
0;
}    

WriteTop15()
{
#if defined HTML_IN_MOTD
    
static const Header[] = "<body bgcolor=#000000><font color=#FFB000><pre>%5s  %22s  %5s  %5s^n^n";
    static const 
Buffer[] = "%4d   %22s  %5d  %6d^n";
#else
    
static const Header[] = "%5s  %22s  %5s  %5s^n^n";
    static const 
Buffer[] = "%5d   %22s  %5d  %5d^n";
#endif

    
static LengthPositionSizeSteamID[32], Name[32], KillsDeaths;
    
Length formatex(g_Top15[Length], 2047 LengthHeader"Rank""Name""Kills""Deaths");

    for (
Position 0Size min(ArraySize(g_SteamID), 15); Position SizePosition++)
    {
        
ArrayGetString(g_SteamIDPositionSteamID31);
        
TrieGetString(g_NameSteamIDName31);
        
TrieGetCell(g_KillsSteamIDKills);
        
TrieGetCell(g_DeathsSteamIDDeaths);

        
Length += formatex(g_Top15[Length], 2047 LengthBufferPosition 1NameKillsDeaths);
    }

The question its about LoadRanks( ) function witch can store in Tries, even 5000 lines with saved results. In comparation with nvault in my opinion it sems to be very dumb and inneficient way to load results. But at same time its very easy to understand and manipulate the code.

In the official thread poeple are sayng fine words about it and the plugin sems to be approved.
http://forums.alliedmods.net/showthr...=119167&page=7

I'm wrong or it really affecting the lag and cpw usage very much becouse the results are permanently loaded in server memory?

Last edited by twix_p; 03-03-2014 at 07:05.
twix_p is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 03-03-2014 , 01:09   Re: question abut cpw usage
Reply With Quote #2

Even nvault is not appropriated for such storage, use rather sql so you don't need to sort data from plugin.
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
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 21:37.


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