Hello, I wrote a simple credits plugin and sometimes , I don't know when, player's credits reset.
It doesn't show any error in the logs, I logged everytime the plugins load someone's credits from db and what it saves and sometimes it just saves 2 credits like when a player's kills someone its credits increment by 2 and then saves it, so if someone have 24 credits and kills another player the credits reset and it saves 2. I don't know when it happends. It doesn't happen to all players and not all the time.
Loading code:
PHP Code:
public void OnClientPostAdminCheck(client)
{
if(IsFakeClient(client))
return;
LoadClientCredits(client);
}
public void LoadClientCredits(int client)
{
if(!IsClientConnected(client))
return;
char query[256], steamid[32];
GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid));
FormatEx(query, sizeof(query), "SELECT * FROM store_players WHERE authid = '%s'", steamid);
LogToFile(logPath, query);
db.Query(T_LoadData, query, GetClientUserId(client));
}
public void T_LoadData(Database db1, DBResultSet results, const char[] error, any data)
{
if(db1 == null || results == null)
{
LogError("T_LoadData returned error: %s", error);
return;
}
int client = GetClientOfUserId(data);
if(!client || IsFakeClient(client) || !IsClientConnected(client))
return;
int creditsCol;
results.FieldNameToNum("credits", creditsCol);
if(results.FetchRow())
{
player_credits[client] = results.FetchInt(creditsCol);
LogToFile(logPath, "%i", player_credits[client]);
}
else
{
//not found in db, insert data:
char steamid[32], name[64], name_sanitazed[64]; GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid)); GetClientName(client, name, sizeof(name));
db.Escape(name, name_sanitazed, sizeof(name_sanitazed));
char query[256]; Format(query, sizeof(query), "INSERT INTO store_players (authid, name, credits) VALUES ('%s', '%s', 0);", steamid, name_sanitazed );
LogToFile(logPath, "NOT IN DATABASE");
db1.Query(T_Generic, query);
}
}
public void T_Generic(Database db1, DBResultSet results, const char[] error, any data)
{
if(db1 == null || results == null)
{
LogError("T_Generic returned error: %s", error);
return;
}
}
Saving code:
PHP Code:
public Action Event_PlayerDeath(Event event, const char[] chName, bool bDontBroadcast)
{
int credits = c_kill.IntValue;
int attacker = GetClientOfUserId(GetEventInt(event, "attacker"));
int victim = GetClientOfUserId(GetEventInt(event, "userid"));
if(!attacker || !victim || IsFakeClient(victim) || IsFakeClient(attacker) || !IsClientConnected(attacker))
return;
kills[attacker]++;
bool headshot = GetEventBool(event, "headshot");
bool noscope = GetEventBool(event, "noscope");
bool smoke = GetEventBool(event, "thrusmoke");
int wallbang = GetEventInt(event, "noscope");
bool blind = GetEventBool(event, "attackerblind");
if(headshot)
credits += c_headshot.IntValue;
if(noscope)
credits += c_noscope.IntValue;
if(smoke)
credits += c_smoke.IntValue;
if(wallbang)
credits += c_wall.IntValue;
if(blind)
credits += c_blind.IntValue;
if(c_multikill.IntValue)
credits += c_multikill.IntValue * kills[attacker];
player_credits[attacker] += credits;
UpdateCredits(attacker);
return;
}
public void UpdateCredits(int client)
{
char steamid[32]; GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid));
LogToFile(logPath, "%s - %i SAVED", steamid, player_credits[client]);
char query[256]; Format(query, sizeof(query), "UPDATE store_players SET credits = '%i' WHERE authid = '%s';", player_credits[client], steamid);
db.Query(T_Generic, query);
}
I also reset a player's credits when he disconnects
PHP Code:
public void OnClientDisconnect(int client)
{
player_credits[client] = 0;
}