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

[CS:GO] Data Switch Bug.. Really tired of this.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
aexi0n
AlliedModders Donor
Join Date: Nov 2014
Location: bhop_deluxe
Old 01-18-2015 , 14:45   [CS:GO] Data Switch Bug.. Really tired of this.
Reply With Quote #1

I have no idea long I've been working on trying to fix this damn plugin, but for some reason it just wont work properly. I'm sure by now you have at least seen one of my threads regarding the different bugs that have been occurring, and I actually finally thought I had it all fixed, but I was wrong.


This time however, I have managed to figure re-create the bug every time. What is happening is players are getting assigned a different players level from the database. This is an example scenario of what is done to re-create this bug:

Two players in game:

[Lv. 10] Player 1
[Lv. 5] Player 2

This is what is happening:

[Lv. 10] Player 1 Disconnects

[Lv. 5] Player 2 Connects -> Receives [Lv. 10]

[Lv. 5] Player 1 Re-Connects -> Recieves [Lv. 5]

Now I know it has to do with the client indexes being switched, but what I don't understand how since I'm using userid in almost every instance:

Code:
public OnClientAuthorized(client)
{
	if(!IsFakeClient(client) && IsValidClient(client, false))
	{
		ClientDataLoaded[client] = false;
			
		GetPlayerData(client);
		
		decl String:name[MAX_NAME_LENGTH];
		GetClientName(client, name, sizeof(name));
		
		PrintToChatAll("Player \x01\x04%s \x01\x07has joined the server!", name);
	}
}
Code:
/* Paradise Server Database Functions
**
** Functions To Send & Retrieve Data From The MySQL Database
** -------------------------------------------------------------------------- */
//Connect To Database
CreateDB(&Handle:DbHNDL)
{
	SQL_TConnect(SQL_OnConnect, "default");	
}
//Connection Callback
public SQL_OnConnect(Handle:owner, Handle:hndl, const String:Error[], any:data) 
{ 
	if (hndl == INVALID_HANDLE) 
	{ 
		//It didn't work, so we log the error 
		PrintToServer("SQL ERROR: %s", Error);
	} 
	else 
	{ 
		db = hndl;
		PrintToServer("[SQL] Connection Successful!");
		SQL_CreateTables();
	} 
}
//Create Database Tables
SQL_CreateTables()
{
	decl String:Query[255];
	Format(Query, sizeof(Query), "CREATE TABLE IF NOT EXISTS playerdata (steamid VARCHAR(64), name TEXT, level TINYINT, XP INT);");
	SQL_TQuery(db, SQL_OnCreatedTable, Query);
}
//Create Tables Callback
public SQL_OnCreatedTable(Handle:owner, Handle:hndl, const String:Error[], any:data) 
{ 
	if (hndl == INVALID_HANDLE) 
	{ 
		PrintToServer("SQL ERROR: %s", Error); 
	}
}

//New Player - Add To Database
public CreatePlayer(client)
{
	new userid = GetClientUserId(client);
	
	decl String:steamid[32], String:name[(MAX_NAME_LENGTH + 1) * 2], String:Query[255], String:nameBuffer[MAX_NAME_LENGTH];
	
	GetClientName(client, nameBuffer, sizeof(nameBuffer));

	SQL_EscapeString(db, nameBuffer, name, sizeof(name)); 
	
	if(!ClientDataLoaded[client] && GetClientAuthString(client, steamid, sizeof(steamid)))
	{
		if(IsValidClient(client, false))
		{
			GetClientAuthString(client, steamid, sizeof(steamid));
			Format(Query, sizeof(Query), "INSERT INTO playerdata (steamid, name, level, XP) VALUES ('%s', '%s', '1', '0')", steamid, name);
			SQL_TQuery(db, SQL_ErrorCallback, Query, userid);
		}
	}
	
	GetPlayerData(client);
}

//Existing Player - Update Database
public UpdatePlayer(client)
{
	new userid = GetClientUserId(client);
	
	decl String:steamid[32], String:Query[255];
	
	if(ClientDataLoaded[client] && GetClientAuthString(client, steamid, sizeof(steamid)))
	{
		if(IsValidClient(client, false))
		{
			GetClientAuthString(client, steamid, sizeof(steamid));
			Format(Query, sizeof(Query), "UPDATE playerdata SET level='%i', XP='%i' WHERE (steamid='%s')", Level[client], XP[client], steamid);
			SQL_TQuery(db, SQL_ErrorCallback, Query, userid);
		}
	}
		
	GetPlayerData(client);
}
//General Error Callback
public SQL_ErrorCallback(Handle:owner, Handle:hndl, const String:Error[], any:data)
{
	new client = GetClientOfUserId(data);

	if(hndl == INVALID_HANDLE)
	{
		PrintToServer("SQL ERROR: %s", Error);
	}

	if (client == 0)
	{
		return;
	}
}


//Retrieve Player Data
public GetPlayerData(client)
{	
	new userid = GetClientUserId(client);
	decl String:Query[255], String:steamid[32];
	new datacheck = 0;
	
	if(GetClientAuthString(client, steamid, sizeof(steamid)))
	{
		if(IsValidClient(client, false))
		{
			GetClientAuthString(client, steamid, sizeof(steamid));
			Format(Query, sizeof(Query), "SELECT level, XP FROM playerdata WHERE (steamid='%s')", steamid);
			SQL_TQuery(db, SQL_GetPlayerData, Query, userid);
			
			datacheck = 1;
		}
	}
	
	if (datacheck != 1)
	{
		PrintToServer("SQL ERROR: Failed to retrieve player data!");
	}
}
//Retrieve Player Data Callback
public SQL_GetPlayerData(Handle:owner, Handle:hndl, const String:Error[], any:data)
{
	new client = GetClientOfUserId(data);

	if(hndl == INVALID_HANDLE)
	{
		PrintToServer("SQL ERROR: %s", Error);
	}
	
	if (client == 0)
	{
		return;
	}

	if(!SQL_FetchRow(hndl))
	{
		CreatePlayer(client);
	}
	else
	{
		Level[client] = SQL_FetchInt(hndl, 0);
		XP[client] = SQL_FetchInt(hndl, 1);
		new L = Level[client];
		XP_Needed[client] = ((L * L + L + 3) * 4);
		
		ClientDataLoaded[client] = true;
	}	
}

I'm so frustrated with this.. I really would love to finally have a stable plugin, so I can move on to developing other things but life is never easy..
aexi0n is offline
Starbish
AlliedModders Donor
Join Date: Oct 2011
Location: South Korea
Old 01-19-2015 , 09:21   Re: [CS:GO] Data Switch Bug.. Really tired of this.
Reply With Quote #2

print "SteamID" and "Loaded Data" like debugging on players connection.

and you'd be better to clear variables for clients like Level or whatever.
__________________

Last edited by Starbish; 01-19-2015 at 09:21.
Starbish is offline
aexi0n
AlliedModders Donor
Join Date: Nov 2014
Location: bhop_deluxe
Old 01-19-2015 , 11:57   Re: [CS:GO] Data Switch Bug.. Really tired of this.
Reply With Quote #3

Actually managed to solve the problem, after all the trouble I have gone through with trying to save/load data safe and securely, I think I might make a post in the snippets/tutorials section regarding all the procedures you should go through.
aexi0n is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 01-19-2015 , 15:51   Re: [CS:GO] Data Switch Bug.. Really tired of this.
Reply With Quote #4

Why are you calling GetPlayerData(client) at the end of UpdatePlayer(client) and CreatePlayer(client)?

The database queries have not finished and your asking to get data.
__________________
Neuro Toxin 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 09:33.


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