Go Back   EQEmulator Home > EQEmulator Forums > Development > Development::Development

Development::Development Forum for development topics and for those interested in EQEMu development. (Not a support forum)

Reply
 
Thread Tools Display Modes
  #1  
Old 10-14-2013, 06:01 PM
Furniture
Hill Giant
 
Join Date: Aug 2012
Posts: 205
Default Use database queries in source code?

Hi, I need to know how to run database queries in my source code. I am trying to remove trade between characters who have different "expansion" values in my character_vars table as I made per here: http://www.eqemulator.net/wiki/wikka...mCharacterData

Here is my code:

Code:
void Client::Handle_OP_TradeRequest(const EQApplicationPacket *app)
{
	if (app->size != sizeof(TradeRequest_Struct)) {
		LogFile->write(EQEMuLog::Error, "Wrong size: OP_TradeRequest, size=%i, expected %i", app->size, sizeof(TradeRequest_Struct));
		return;
	}

	
	// Client requesting a trade session from an npc/client
	// Trade session not started until OP_TradeRequestAck is sent

	BreakInvis();

	// Pass trade request on to recipient
	TradeRequest_Struct* msg = (TradeRequest_Struct*) app->pBuffer;
	Mob* tradee = entity_list.GetMob(msg->to_mob_id);



	if (tradee && tradee->IsClient()) { {
		char *query = 0;
    MYSQL_RES *result;
    MYSQL_ROW row;
    char errbuf[MYSQL_ERRMSG_SIZE];
	int myExpansion = -1;
	int tradeeExpansion = -2;
    
    MakeAnyLenString(&query, "SELECT %s FROM character_vars WHERE character_id = %i", expansion, AccountID());
    if (database.RunQuery(query, strlen(query), errbuf, &result))
    {
        safe_delete_array(query);
        row = mysql_fetch_row(result);
        mysql_free_result(result);
         myExpansion = (atoi(row[0]));
	}

		MakeAnyLenString(&query, "SELECT %s FROM character_vars WHERE character_id = %i", expansion, tradee->CastToClient()->AccountID());
    if (database.RunQuery(query, strlen(query), errbuf, &result))
    {
        safe_delete_array(query);
        row = mysql_fetch_row(result);
        mysql_free_result(result);
         tradeeExpansion = (atoi(row[0]));
	}

	if (myExpansion != tradeeExpansion)
	{
		return;
	}

	


    

	
	

		
		tradee->CastToClient()->QueuePacket(app);
	}
This compiles fine but crashes the zone when a trade initiates. Can anybody help me fix this or even know if there is an easier way I could do this?
Reply With Quote
  #2  
Old 10-14-2013, 06:11 PM
demonstar55
Demi-God
 
Join Date: Apr 2008
Location: MA
Posts: 1,165
Default

Run this so someone can help further. (since it is a non default table)
Code:
DESCRIBE `character_vars`;
EDIT:

Quick assumption

Code:
MakeAnyLenString(&query, "SELECT expansion FROM character_vars WHERE character_id = %i", AccountID());
Reply With Quote
  #3  
Old 10-14-2013, 06:13 PM
Furniture
Hill Giant
 
Join Date: Aug 2012
Posts: 205
Default

Code:
Field	Type	Null	Key	Default	Extra
character_id	int(11)	NO	PRI	\N	
buff_balance	int(11)	YES		0	
port_balance	int(11)	YES		0	
expansion	int(11)	YES		0
Reply With Quote
  #4  
Old 10-14-2013, 06:16 PM
Furniture
Hill Giant
 
Join Date: Aug 2012
Posts: 205
Default

I will try that and see how it goes, thanks
Reply With Quote
  #5  
Old 10-14-2013, 06:23 PM
Furniture
Hill Giant
 
Join Date: Aug 2012
Posts: 205
Default

Still no luck, zone crashes on character to character trade once you click an item or plat on somebody else. but compiled fine
Reply With Quote
  #6  
Old 10-14-2013, 06:33 PM
Furniture
Hill Giant
 
Join Date: Aug 2012
Posts: 205
Default

To make it easier here is the code that I added from the above:
Quote:
Mob* tradee = entity_list.GetMob(msg->to_mob_id);



if (tradee && tradee->IsClient()) {
char *query = 0;
MYSQL_RES *result;
MYSQL_ROW row;
char errbuf[MYSQL_ERRMSG_SIZE];
int myExpansion = -1;
int tradeeExpansion = -2;

MakeAnyLenString(&query, "SELECT expansion FROM character_vars WHERE character_id = %i", CharacterID());
if (database.RunQuery(query, strlen(query), errbuf, &result))
{
safe_delete_array(query);
row = mysql_fetch_row(result);
mysql_free_result(result);
myExpansion = (atoi(row[0]));
}

MakeAnyLenString(&query, "SELECT expansion FROM character_vars WHERE character_id = %i", CharacterID());
if (database.RunQuery(query, strlen(query), errbuf, &result))
{
safe_delete_array(query);
row = mysql_fetch_row(result);
mysql_free_result(result);
tradeeExpansion = (atoi(row[0]));
}

if (myExpansion != tradeeExpansion)
{
return;
}
Reply With Quote
  #7  
Old 10-14-2013, 07:12 PM
Furniture
Hill Giant
 
Join Date: Aug 2012
Posts: 205
Default

here is my newest code: doesnt crash zone, but trade wont initiate between players with same expansion number:

Quote:
Mob* tradee = entity_list.GetMob(msg->to_mob_id);



if (tradee && tradee->IsClient()) {
char *query = 0;
MYSQL_RES *result;
MYSQL_ROW row;
char errbuf[MYSQL_ERRMSG_SIZE];
int myExpansion = -1;
int tradeeExpansion = -2;
int myID = CharacterID();
int tID = tradee->CastToClient()->CharacterID();

if (database.RunQuery(query, MakeAnyLenString(&query, "SELECT expansion FROM character_vars WHERE character_vars.character_id=myID"), errbuf, &result)) {
safe_delete_array(query);
row = mysql_fetch_row(result);
mysql_free_result(result);

myExpansion = (atoi(row[0]));

}

if (database.RunQuery(query, MakeAnyLenString(&query, "SELECT expansion FROM character_vars WHERE character_vars.character_id=tID"), errbuf, &result)) {

safe_delete_array(query);
row = mysql_fetch_row(result);
mysql_free_result(result);
tradeeExpansion = (atoi(row[0]));
}

if (myExpansion != tradeeExpansion)
{
return;
}










tradee->CastToClient()->QueuePacket(app);
}
Reply With Quote
  #8  
Old 10-14-2013, 07:34 PM
rencro
Hill Giant
 
Join Date: Sep 2008
Location: So. California
Posts: 219
Default

Are the proper user ID's being grabbed?

could try:

Code:
if (myExpansion != tradeeExpansion)
	{
		Message(13, "The trade is not good.");
		Message(13, "The value of myID is %i", myID);
		Message(13, "The value of tID is %i", tID);
		return;
	}
Reply With Quote
  #9  
Old 10-14-2013, 07:38 PM
Furniture
Hill Giant
 
Join Date: Aug 2012
Posts: 205
Default

I will try that out and report my findings, cant do any more testing til tonight, thank you i appreciate the help
Reply With Quote
  #10  
Old 10-14-2013, 07:57 PM
Uleat's Avatar
Uleat
Developer
 
Join Date: Apr 2012
Location: North Carolina
Posts: 2,815
Default

Why don't you just use 'ClientVersion' as a comparison?

you shouldn't need to access the db for this info since it a Client class property already.

Code:
if (tradee && tradee->IsClient() && (this.ClientVersion == tradee->CastToClient()->GetClientVersion()))
{ ... }
..or something like that...
__________________
Uleat of Bertoxxulous

Compilin' Dirty
Reply With Quote
  #11  
Old 10-14-2013, 08:17 PM
Furniture
Hill Giant
 
Join Date: Aug 2012
Posts: 205
Default

This is going to be used on a progression server where there is no trade between characters in different expansions. I'm using the custom expansion variable so I can change the value per character in perl quests and get the value from the source to restrict trade(the part which i'm stuck on)
Reply With Quote
  #12  
Old 10-14-2013, 08:30 PM
Uleat's Avatar
Uleat
Developer
 
Join Date: Apr 2012
Location: North Carolina
Posts: 2,815
Default

Change myID and tID at the end of the queries to %i, then add them post-quote.


EDIT: You are probably translating the id's to zero in those...
__________________
Uleat of Bertoxxulous

Compilin' Dirty
Reply With Quote
  #13  
Old 10-14-2013, 09:21 PM
rencro
Hill Giant
 
Join Date: Sep 2008
Location: So. California
Posts: 219
Default

I already have the custom char code and table from drajor, and I compiled your code into mine and it all works..

When trade is denied, the proper IDS are echoed.. I tried first with different expansion settings for both chars, and it gave me the Denied message as expected, then i made both chars expansion values equal in the db and trade went through, no crash.. Nice work on this...

Code:
	if (database.RunQuery(query, MakeAnyLenString(&query, "SELECT expansion FROM character_vars WHERE character_vars.character_id=%i", myID), errbuf, &result)) {

AND

	if (database.RunQuery(query, MakeAnyLenString(&query, "SELECT expansion FROM character_vars WHERE character_vars.character_id=%i", tID), errbuf, &result)) {

AND for giggles

	if (myExpansion != tradeeExpansion)
	{
		Message(13, "Trade denied.");
		Message(13, "Value of myID is %i", myID);
		Message(13, "Value of tID is %i", tID);
		return;
	}
		Message(13, "Yes!! And it counts.");
		tradee->CastToClient()->QueuePacket(app);
	}
Reply With Quote
  #14  
Old 10-14-2013, 10:14 PM
Uleat's Avatar
Uleat
Developer
 
Join Date: Apr 2012
Location: North Carolina
Posts: 2,815
Default

This would be a little more involved, but you could add the expansion variable to the Client class, along with supporting code, and then access
the memory reference as needed instead of doing a db call each time a trade is initiated.

The world->Client already has access to expansions, so you would need to write code to modify it in zone->Client..that way the only db call is
when the value actually changes.
__________________
Uleat of Bertoxxulous

Compilin' Dirty
Reply With Quote
  #15  
Old 10-15-2013, 12:09 AM
Furniture
Hill Giant
 
Join Date: Aug 2012
Posts: 205
Default

I tested this out again and its still not working. Did you use Uleats changes?
Reply With Quote
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:04 AM.


 

Everquest is a registered trademark of Daybreak Game Company LLC.
EQEmulator is not associated or affiliated in any way with Daybreak Game Company LLC.
Except where otherwise noted, this site is licensed under a Creative Commons License.
       
Powered by vBulletin®, Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Template by Bluepearl Design and vBulletin Templates - Ver3.3