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

Solved can this code produce issues ?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
lexzor
Veteran Member
Join Date: Nov 2020
Old 10-05-2021 , 17:12   can this code produce issues ?
Reply With Quote #1

can this code produce issues or it s a better way to do it ?

PHP Code:
public open_all_clans_menu(id)
{
    new 
szQuery2[512], szData[1];
    
szData[0] = id;
    
formatex(szQuery2charsmax(szQuery2), "SELECT * FROM `%s` ORDER BY (total_clan_kills) DESC"g_szTables[0]);
    
SQL_ThreadQuery(g_SqlTuple"ShowAllClans"szQuery2szDatasizeof(szData));
}

public 
ShowAllClans(FailStateHandle:QueryszError[], ErrorCodeszData[], iSize)
{  
    if(
FailState || ErrorCode)
        
log_amx("^nSQL ERROR: %s^n"szError);

    new 
id szData[0];
    if(!
is_user_connected(id))
        return 
PLUGIN_HANDLED;

    new 
szTitle[64], szClanName[32], szClanLeader[64], szPassData[64], szItem[128];
    new 
iResults SQL_NumResults(Query);
    new 
iTotalClanKills;
    new 
iClanRank 0;

    
formatex(szTitlecharsmax(szTitle), "%L"id"FRAGTOP_CLANS_MENU_TITLE");
    new 
iMenu menu_create(szTitle"show_all_clans_handler");

    
formatex(szItemcharsmax(szItem), "%L"id"FRAGTOP_CLANS_MENU_FIRST_OPTION");
    
menu_additem(iMenuszItem);

    if(
iResults 0)
    {
        for(new 
iResultsi++)
        {
            
SQL_ReadResult(Query1szClanNamecharsmax(szClanName));
            
SQL_ReadResult(Query3szClanLeadercharsmax(szClanLeader));
            
iTotalClanKills SQL_ReadResult(Query6);
            
iClanRank += 1;
            
formatex(szPassDatacharsmax(szPassData), "^"%s^" %i %i"szClanLeaderiTotalClanKillsiClanRank);
            
formatex(szItemcharsmax(szItem), "\r[\w%s\r]\d - \r[\w%s\r]\d - \r[\w%i\r]"szClanNameszClanLeaderiTotalClanKills);
            
menu_additem(iMenuszItemszPassData);

            
SQL_NextRow(Query);
        }
    }

    
menu_setprop(iMenuMPROP_EXITMEXIT_ALL);
    
menu_display(idiMenu0, -1);
    
    
SQL_FreeHandle(Query);

    return 
PLUGIN_CONTINUE;


Last edited by lexzor; 10-06-2021 at 04:54.
lexzor is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 10-05-2021 , 18:10   Re: can this code produce issues ?
Reply With Quote #2

Your code is okay but there is a better alternative. Instead of loading from the SQL database everytime you open a menu, load it one time on plugin_cfg/plugin_init and store everything in a trie or an array, the one that looks better for you. Do the same to update the db, one time but on plugin_end. You could also periodically update the db setting a repeatable task for example, if your server doesn't crash frequently you don't need that. If you need an example I may provide.
__________________








CrazY. is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-05-2021 , 18:37   Re: can this code produce issues ?
Reply With Quote #3

I'd do pretty much what CrazY. said.

@plugin_init/plugin_cfg
Load data from db table into a variable/array

@During gameplay, increment variables as needed (kills, deaths, etc.)
If you do not do this, your plugin will only have the values from map start, and not reflect current changes from gameplay

@plugin_end
Call an UPDATE statement to set variable values that have changed back into table fields
__________________
Bugsy is offline
lexzor
Veteran Member
Join Date: Nov 2020
Old 10-05-2021 , 18:58   Re: can this code produce issues ?
Reply With Quote #4

i want something like "live top". thanks you all
lexzor is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-05-2021 , 19:13   Re: can this code produce issues ?
Reply With Quote #5

What is live top
__________________
Bugsy is offline
lexzor
Veteran Member
Join Date: Nov 2020
Old 10-05-2021 , 19:52   Re: can this code produce issues ?
Reply With Quote #6

plugin calculate total clan kills for every created clan after saving online members kills then show the top

like

PHP Code:
public ReadClansTable(FailStateHandle:QueryszError[], ErrorCodeszData[], iSize)
{
    if(
FailState || ErrorCode)
        
log_amx("^nSQL ERROR: %s^n"szError);

    new 
iResults SQL_NumResults(Query);
    new 
szClanName[64];
    new 
szQuery[512];
    new 
szPassData[64];
    new 
id szData[0];

    if(
iResults 0)
    {
        for(new 
0iResultsi++)
        {
            
SQL_ReadResult(Query1szClanNamecharsmax(szClanName));
            
formatex(szPassDatacharsmax(szPassData), "%s %i"szClanNameid);
            
formatex(szQuerycharsmax(szQuery), "SELECT * FROM `%s` ORDER BY (id) ASC"szClanName);
            
SQL_ThreadQuery(g_SqlTuple"GetFrags"szQueryszPassDatacharsmax(szPassData));

            
SQL_NextRow(Query);
        }
    }
    else
    {
        
clan_menu(id);
        
client_print_color(idprint_team_default"%s %L"g_szTagid"NO_CLANS");
    }

    
SQL_FreeHandle(Query);
}

public 
GetFrags(FailStateHandle:QueryszError[], ErrorCodeszData[], iSize)
{
    if(
FailState || ErrorCode)
        
log_amx("^nSQL ERROR: %s^n"szError);

    new 
iResults SQL_NumResults(Query);
    new 
iMemberFragsiClanFrags;

    new 
szClanName[64], szUserID[2];
    
parse(szDataszClanNamecharsmax(szClanName), szUserIDcharsmax(szUserID));

    if(
iResults 0)
    {
        for(new 
0iResultsi++)
        {
            
iMemberFrags SQL_ReadResult(Query5);
            
iClanFrags += iMemberFrags;
            
SQL_NextRow(Query);
        }
    }

    new 
szQuery[512];
    
formatex(szQuerycharsmax(szQuery), "UPDATE `%s` SET `total_clan_kills` = '%i' WHERE `clan_name` = '%s'",
    
g_szTables[0], iClanFragsszClanName);
    
SQL_ThreadQuery(g_SqlTuple"FreeHandle"szQuery);

    
SQL_FreeHandle(Query);


Last edited by lexzor; 10-05-2021 at 19:53.
lexzor is offline
JocAnis
Veteran Member
Join Date: Jun 2010
Old 10-05-2021 , 19:53   Re: can this code produce issues ?
Reply With Quote #7

Hmm only possible issue could be if all 30 players execute that sql function in same second, but its really small chance.
I was thinking of your method + adding check of minimum 1 (or more) second gap between that function calls, meaning you would need also a global variable to store your inputs..but its not worth when we think better

Ps: excuse me if i totally missed your idea here)
__________________
KZ Public Autocup - PrimeKZ

My blog: http://primekz.xyz (in progress...) - not active (dec 2022)
JocAnis is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-05-2021 , 20:47   Re: can this code produce issues ?
Reply With Quote #8

I'd create 2 tables, one for clans and one for players.

At any given time you have easy access, in a variable, the total clan kills as well as total player kills. You can also print out what percentage of total clan kills an individual player did.

Let me know if you have any questions

PHP Code:

#include <amxmodx>

const MaxClans 50;

enum ClanInfo
{
    
ClanName32 ],
    
ClanKills
}
new 
g_ciClanDataMaxClans ][ ClanInfo ];

/*

Clan table (tblClans)
    ID - Auto number, ClanID
    ClanName - Name of Clan
    ClanKills - Total clan Kills

Player table (tblPlayers)
    ID - Auto number, Player ID
    ClanID - ID from Clan table
    Name - Player name
    SteamID - Player steam ID
    PlayerKills - Player kills
    
*/

enum PlayerData
{
    
ClanID,
    
PlayerName32 ],
    
AuthID34 ],
    
PlayerKills
}
new 
g_pdDataMAX_PLAYERS ][ PlayerData ];

public 
plugin_init() 
{
    
}

public 
plugin_cfg() 
{
    
//SELECT ID, ClanName, ClanKills FROM tblClans
    
    //for ( new i = 0 ; i < NumResults ; i++ )
    //{
    //    g_ciClanData[ i ][ ClanName ] = tblClans.ClanName;
    //    g_ciClanData[ i ][ ClanKills ] = tblClans.ClanKills;
    //}    
}

public 
plugin_end() 
{
    
//for ( new i = 0 ; i < MaxClans ; i++ )
    //{
    //    if ( g_ciClanData[ i ][ ClanName ][ 0 ] != EOS )
    //    {
    //        UPDATE tblClans SET ClanKills=g_ciClanData[ i ][ ClanKills ] WHERE ID=i
    //    }
    //}    
}
public 
client_authorizedid )
{
    
get_user_authidid g_pdDataid ][ AuthID ] , charsmaxg_pdData[][ AuthID ] ) );
    
//SELECT ClanID, PlayerKills FROM tblPlayers WHERE SteamID=g_pdData[ id ][ AuthID ]
    
    
g_pdDataid ][ ClanID ] = tblPlayers.ClanID;
    
g_pdDataid ][ PlayerKills ] = tblPlayers.PlayerKills;
}

public 
PlayerKillid )
{
    
g_ciClanDatag_pdDataid ][ ClanID ] ][ ClanKills ]++;
    
g_pdDataid ][ PlayerKills ]++;
}

public 
client_disconnectid )
{
    
//UPDATE tblPlayers SET PlayerKills=g_pdData[ id ][ PlayerKills ] WHERE AuthID=g_pdData[ id ][ SteamID ];

__________________
Bugsy is offline
lexzor
Veteran Member
Join Date: Nov 2020
Old 10-06-2021 , 04:53   Re: can this code produce issues ?
Reply With Quote #9

i create 2 tables:

1 with all clans,
1 with user name steamid and clan

then if a players create a clan, a table with that name is created so every clan has his own table with all members
lexzor is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-06-2021 , 12:34   Re: can this code produce issues ?
Reply With Quote #10

A separate table for each clan is overkill. I'd just do 1 table with all clans, and then link players to that table using clan ID.
__________________
Bugsy 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 05:00.


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