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

Fix plugin


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
kramesa
Veteran Member
Join Date: Feb 2011
Location: Brazil
Old 06-06-2012 , 16:17   Fix plugin
Reply With Quote #1

Hi! I have this plugin:

PHP Code:
#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <csx>
#include <sqlx>
#include <engine>
#include <hamsandwich>

#pragma semicolon 1

const MaxSlots 32;
const 
MaxSteamIdChars 35;

enum _:DataTypes 
{
    
_Data_Kills,
    
_Data_Deaths,
    
_Data_Headshots,
    
_Data_Wins,
    
_Data_Losses,
    
_Data_Points,
    
_Data_TotalDamage,
    
_Data_BD,
    
_Data_BP,
    
_Data_Leaves
};

new 
PlayerData[MaxSlots 1][DataTypes];

new 
SteamId[MaxSlots 1][MaxSteamIdChars];

new 
MaxPlayers;

new 
Handle:SQLTuple;

new 
CvarPointsKill;
new 
CvarPointsHeadshot;
new 
CvarPointsWins;

new 
gTyping[33];
new 
ClientVotes[33];
new 
gReason[35];
new 
gVotes[2] = 0;
new 
gName[32];
new 
gTName[32];

new 
Uid;
new 
gUserID;
new 
gTempID;
new 
gVoteMenu;
new 
pCvarEnable;

public 
plugin_init() {    
    
CvarPointsKill register_cvar("points_kill""2");
    
CvarPointsHeadshot register_cvar("points_headshot""4");
    
CvarPointsWins register_cvar("points_win""1");
    
    
MaxPlayers get_maxplayers();
    
    
register_event("DeathMsg""Event_DeathMsg""a");
    
    
register_event("SendAudio""Event_SendAudio_TWin""a""2&%!MRAD_terwin");
    
register_event("SendAudio""Event_SendAudio_CTWin""a""2&%!MRAD_ctwin");
    
    
RegisterHam(Ham_TakeDamage"player""FwdPlayerDamagePost"1);
}

public 
plugin_cfg() {
    
SQLTuple SQL_MakeStdTuple(); 
    
PrepareDatabase();
}

PrepareDatabase() {
    new 
Query[512];
    
formatex(Querycharsmax(Query), "CREATE TABLE IF NOT EXISTS ranking (kills int(10), deaths int(10), headshots int(10), wins int(10), losses int(10), points int(10), total_damage int(10), bd int(10), bp int(10), leaves int(10), steamid varchar(35), name varchar(32), PRIMARY KEY(steamid))");
    
    
SQL_ThreadQuery(SQLTuple"QueryDefaultHandle"Query);
}

public 
QueryDefaultHandle(FailStateHandle:QueryError[], ErrorCodeData[], DataSizeFloat:QueueTime) {
    switch(
FailState) {
        case 
TQUERY_CONNECT_FAILED: {
            
set_fail_state("SQL: Connection failed.");
        }
        case 
TQUERY_QUERY_FAILED: {
            
log_amx("SQL: Query failed. Error: %s (%i)"ErrorErrorCode);
        }
    }
}

SaveData(const Client) {
    new 
Query[512];
    new 
ClientName[MaxSlots];
    new 
ClientNameSafe[MaxSlots];
    
get_user_name(ClientClientNamecharsmax(ClientName));
    
MakeStringSQLSafe(ClientNameClientNameSafecharsmax(ClientName));
    
    
formatex(Querycharsmax(Query), "REPLACE INTO ranking (kills, deaths, headshots, wins, losses, points, total_damage, bd, bp, leaves, steamid, name) VALUES (%i, %i, %i, %i, %i, %i, %i, %i, %i, %i, '%s', '%s')"
    
PlayerData[Client][_Data_Kills], 
    
PlayerData[Client][_Data_Deaths], 
    
PlayerData[Client][_Data_Headshots], 
    
PlayerData[Client][_Data_Wins], 
    
PlayerData[Client][_Data_Losses], 
    
PlayerData[Client][_Data_Points], 
    
PlayerData[Client][_Data_TotalDamage], 
    
PlayerData[Client][_Data_BD], 
    
PlayerData[Client][_Data_BP], 
    
PlayerData[Client][_Data_Leaves], 
    
SteamId[Client], 
    
ClientNameSafe
    
);
    
    
SQL_ThreadQuery(SQLTuple"QueryDefaultHandle"Query);
}

MakeStringSQLSafe(const Input[], Output[], Len)
{
    
copy(OutputLenInput );
    
replace_all(OutputLen"'""*");
    
replace_all(OutputLen"^"", "*");
    replace_all(Output, Len, "
`", "*");
    
    remove_quotes(Output);
}

LoadData(const Client) {
    new Query[512], Data[2];
    formatex( Query, charsmax(Query),
    "SELECT * FROM ranking WHERE steamid = '%s'",
    SteamId[Client]
    );
    
    Data[0] = Client;
    SQL_ThreadQuery(SQLTuple, "QueryLoadData", Query, Data, charsmax(Data));
}

public QueryLoadData(FailState, Handle:Query, Error[], ErrorCode, Data[], DataSize, Float:QueueTime) {
    switch(FailState) {
        case TQUERY_CONNECT_FAILED:
        {
            set_fail_state("SQL: Connection failed.");
        }
        case TQUERY_QUERY_FAILED: {
            log_amx("SQL: Query failed. Error: %s (%i)", Error, ErrorCode);
        }
        default: {
            SQL_GetResults(Query, Data);
        }
    }
}

SQL_GetResults(Handle:Query, const Data[]) {
    new Client = Data[0];
    
    if(SQL_NumResults(Query)) {
        for(new DataIndex = 0; DataIndex < DataTypes; DataIndex++) {
            PlayerData[Client][DataIndex] = SQL_ReadResult(Query, DataIndex);
        }
    }
}

public client_authorized(Client) {    
    get_user_authid(Client, SteamId[Client], charsmax(SteamId[]));
    
    DefaultVariables(Client);
    
    LoadData(Client);
    
    ClientVotes[Client] = 0;
}

public client_disconnect(Client) {
    PlayerData[Client][_Data_Leaves]++;
    
    SaveData(Client);
    
    DefaultVariables(Client);
    
    SteamId[Client][0] = 0;
}

DefaultVariables(const Client) {
    for(new DataIndex = 0; DataIndex < DataTypes; DataIndex++) {
        PlayerData[Client][DataIndex] = 0;
    }
}

public Event_DeathMsg() {
    new Attacker = read_data(1);
    new Victim = read_data(2);
    
    if((1 <= Attacker <= MaxPlayers) && (1 <= Victim <= MaxPlayers)&& (Victim != Attacker) && is_user_connected(Victim) && is_user_connected(Attacker)) {
        new Points   = get_pcvar_num(CvarPointsKill);
        new Headshot = read_data(3);
        
        if(Headshot) {
            Points += get_pcvar_num(CvarPointsHeadshot);
            
            PlayerData[Attacker][_Data_Headshots]++;
        }
        
        PlayerData[Attacker][_Data_Points] += Points;
        
        PlayerData[Attacker][_Data_Kills]++;
        PlayerData[Victim][_Data_Deaths]++;
    }
    else if(!Attacker) {
        PlayerData[Victim][_Data_Deaths]++;
    }
    SaveData(Victim);
    SaveData(Attacker);
}

public Event_SendAudio_TWin()
    TeamWin(CS_TEAM_T);

public Event_SendAudio_CTWin()
    TeamWin(CS_TEAM_CT);

TeamWin(CsTeams:winner) {
    new CsTeams:loser = CS_TEAM_SPECTATOR - winner;
    new Points = get_pcvar_num(CvarPointsWins);
    
    for( new Client = 1, CsTeams:team; Client <= MaxPlayers; Client++) {
        if(is_user_connected(Client)) {
            team = cs_get_user_team(Client);
            
            if(team == winner) {
                PlayerData[Client][_Data_Points] += Points;
                PlayerData[Client][_Data_Wins]++;
            }
            else if(team == loser) {
                PlayerData[Client][_Data_Losses]++;
            }
        }
        SaveData(Client);
    }
}

public FwdPlayerDamagePost(victim, inflictor, attacker, Float:damage, damageBits) {
    if(1 <= attacker <= MaxPlayers && victim != attacker) {
        PlayerData[attacker][_Data_TotalDamage] += floatround(floatmin(100.0, entity_get_float(victim, EV_FL_dmg_take)));
    }
}

public bomb_defused(Client) {
    PlayerData[Client][_Data_Points]++;
    PlayerData[Client][_Data_BD]++;
    SaveData(Client);
}

public bomb_planted(Client) {
    PlayerData[Client][_Data_Points]++;
    PlayerData[Client][_Data_BP]++;
    SaveData(Client);
}

public plugin_end()
    SQL_FreeHandle(SQLTuple); 
But, I can compile this! The compiler crash...

And, anyone can 'optmize' this code?

Thanks

EDIT: Sorry, wrong forum. Please, admin, move this forum to sug/req

Last edited by kramesa; 06-06-2012 at 16:37.
kramesa is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 06-06-2012 , 16:24   Re: Fix plugin
Reply With Quote #2

Don't make line too long. You have 1 line way too long. Move the vars on a new line.
__________________

Last edited by Arkshine; 06-06-2012 at 16:26.
Arkshine is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 06-06-2012 , 16:26   Re: Fix plugin
Reply With Quote #3

All you have to do is comment pieces out one at a time to figure out what's crashing the compiler.

Derp.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!

Last edited by Exolent[jNr]; 06-06-2012 at 16:27.
Exolent[jNr] is offline
kramesa
Veteran Member
Join Date: Feb 2011
Location: Brazil
Old 06-06-2012 , 16:29   Re: Fix plugin
Reply With Quote #4

Quote:
Originally Posted by Arkshine View Post
Don't make line too long. You have 1 line way too long. Move the vars on a new line.
Ok, thanks!

But, I have problem! Some players have ranking reseted... I use reconnect features, have problem?
kramesa is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 06-06-2012 , 16:36   Re: Fix plugin
Reply With Quote #5

You are threading your SQL queries. This means that this scenario is possible:
1. Player connects
2. Query threaded to load player data
3. Player kills another player
4. Query threaded to save player data (with all empty data except 1 kill)
5. Load query finishes, player has full data in variables
6. Save query finishes, player has empty data except 1 kill in database
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
kramesa
Veteran Member
Join Date: Feb 2011
Location: Brazil
Old 06-06-2012 , 16:41   Re: Fix plugin
Reply With Quote #6

Quote:
Originally Posted by Exolent[jNr] View Post
You are threading your SQL queries. This means that this scenario is possible:
1. Player connects
2. Query threaded to load player data
3. Player kills another player
4. Query threaded to save player data (with all empty data except 1 kill)
5. Load query finishes, player has full data in variables
6. Save query finishes, player has empty data except 1 kill in database
Ok, but how can I fix this?
kramesa is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 06-06-2012 , 16:45   Re: Fix plugin
Reply With Quote #7

Don't save stats until the loading is done.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
kramesa
Veteran Member
Join Date: Feb 2011
Location: Brazil
Old 06-06-2012 , 16:48   Re: Fix plugin
Reply With Quote #8

Quote:
Originally Posted by Exolent[jNr] View Post
Don't save stats until the loading is done.
And how I now when loading is done?
kramesa is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 06-06-2012 , 17:16   Re: Fix plugin
Reply With Quote #9

You already do:
PHP Code:
public QueryLoadData(FailStateHandle:QueryError[], ErrorCodeData[], DataSizeFloat:QueueTime) { 
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
kramesa
Veteran Member
Join Date: Feb 2011
Location: Brazil
Old 06-06-2012 , 17:18   Re: Fix plugin
Reply With Quote #10

Quote:
Originally Posted by Exolent[jNr] View Post
You already do:
PHP Code:
public QueryLoadData(FailStateHandle:QueryError[], ErrorCodeData[], DataSizeFloat:QueueTime) { 
But you think the problem of reset rank is this?

And I really need use #pragma semicolon 1?

Thank you

Last edited by kramesa; 06-06-2012 at 17:22.
kramesa 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 14:05.


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