|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Development::Development Forum for development topics and for those interested in EQEMu development. (Not a support forum) |
|
|
|
10-14-2013, 06:01 PM
|
Hill Giant
|
|
Join Date: Aug 2012
Posts: 205
|
|
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?
|
|
|
|
10-14-2013, 06:11 PM
|
Demi-God
|
|
Join Date: Apr 2008
Location: MA
Posts: 1,164
|
|
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());
|
10-14-2013, 06:13 PM
|
Hill Giant
|
|
Join Date: Aug 2012
Posts: 205
|
|
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
|
10-14-2013, 06:16 PM
|
Hill Giant
|
|
Join Date: Aug 2012
Posts: 205
|
|
I will try that and see how it goes, thanks
|
10-14-2013, 06:23 PM
|
Hill Giant
|
|
Join Date: Aug 2012
Posts: 205
|
|
Still no luck, zone crashes on character to character trade once you click an item or plat on somebody else. but compiled fine
|
10-14-2013, 06:33 PM
|
Hill Giant
|
|
Join Date: Aug 2012
Posts: 205
|
|
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;
}
|
|
|
|
|
10-14-2013, 07:12 PM
|
Hill Giant
|
|
Join Date: Aug 2012
Posts: 205
|
|
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);
}
|
|
|
|
|
10-14-2013, 07:34 PM
|
Hill Giant
|
|
Join Date: Sep 2008
Location: So. California
Posts: 219
|
|
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;
}
|
10-14-2013, 07:38 PM
|
Hill Giant
|
|
Join Date: Aug 2012
Posts: 205
|
|
I will try that out and report my findings, cant do any more testing til tonight, thank you i appreciate the help
|
10-14-2013, 07:57 PM
|
|
Developer
|
|
Join Date: Apr 2012
Location: North Carolina
Posts: 2,815
|
|
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
|
10-14-2013, 08:17 PM
|
Hill Giant
|
|
Join Date: Aug 2012
Posts: 205
|
|
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)
|
10-14-2013, 08:30 PM
|
|
Developer
|
|
Join Date: Apr 2012
Location: North Carolina
Posts: 2,815
|
|
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
|
|
|
|
10-14-2013, 09:21 PM
|
Hill Giant
|
|
Join Date: Sep 2008
Location: So. California
Posts: 219
|
|
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);
}
|
|
|
|
10-14-2013, 10:14 PM
|
|
Developer
|
|
Join Date: Apr 2012
Location: North Carolina
Posts: 2,815
|
|
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
|
10-15-2013, 12:09 AM
|
Hill Giant
|
|
Join Date: Aug 2012
Posts: 205
|
|
I tested this out again and its still not working. Did you use Uleats changes?
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -4. The time now is 04:13 PM.
|
|
|
|
|
|
|
|
|
|
|
|
|