logo logo

 Back to main page

The NWNX Community Forum

 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
Need help with code

 
Post new topic   Reply to topic    nwnx.org Forum Index -> Database related
View previous topic :: View next topic  
Author Message
nachopala



Joined: 20 May 2007
Posts: 6

PostPosted: Mon May 21, 2007 4:27    Post subject: Need help with code Reply with quote

OKa, this is my first script to put in the onClientEnter event of the module.
Need help,
if anyone could help me, I would be thankfull

Code:
#include "aps_include"

void main()
{
SQLInit();
object oPlayer = GetEnteringObject();

string sPlayer;

    if (GetIsPC(oPlayer))
        {
            sPlayer = SQLEncodeSpecialChars(GetPCPlayerName(oPlayer));
        }

string sSQL = "SELECT player FROM players WHERE player='" + sPlayer + "'";
SQLExecDirect(sSQL);

    if (SQLFirstRow() == SQL_SUCCESS)
    {
        // *What should I put here if i Wanted to get from the table the part
        // *of hitpoints and location, and apply them to the PC?
    }
    else
    {
    string sSQL = "INSERT INTO players (player) VALUES" +
               "('" + sPlayer + "',0)";
        SQLExecDirect(sSQL);
    }
}


And here is my OnExit, the problem is that it dosent gets de PC name.

OnExit
Code:
#include "aps_include"

void main()
{
SQLInit();
object oPlayer = GetExitingObject();

string sPlayer;
string sPCname;
location lLocation = (GetLocation(oPlayer));
string sLocation = APSLocationToString(lLocation);

    if (GetIsPC(oPlayer))
        {
            sPlayer = SQLEncodeSpecialChars(GetPCPlayerName(oPlayer));
            sPCname = SQLEncodeSpecialChars(GetName(oPlayer));
            sLocation = SQLEncodeSpecialChars(sLocation);
        }

string sSQL = "SELECT player FROM players WHERE player='" + sPlayer + "' AND pcname='" + sPCname + "'";
SQLExecDirect(sSQL);

    if (SQLFirstRow() == SQL_SUCCESS)
    {
    string sSQL = "INSERT INTO players (location)values='" + sLocation + "'";
    SQLExecDirect(sSQL);

    }

}
Back to top
View user's profile Send private message
TroveLord



Joined: 22 Nov 2006
Posts: 136
Location: Italy

PostPosted: Mon May 21, 2007 10:45    Post subject: Reply with quote

Assuming that this is for nwn1 SQLInit(); must called once in the OnModuleLoad event
As for the location you don't really need to use the database, you can create a waypoint with the player's data and jump to it when the player relogs.
I've not tested these scripts, so let me know if they work or not.

OnEnter
Code:
#include "aps_include"
void main()
{
   object oPlayer = GetEnteringObject();
   string sPlayer = SQLEncodeSpecialChars(GetPCPlayerName(oPlayer));
   string sPCname = SQLEncodeSpecialChars(GetName(oPlayer));

   if(GetIsPC(oPlayer))
   {
      SQLExecDirect("SELECT hitpoints FROM players WHERE player = '"+sPlayer+"' AND pcname = '"+sPCName+"'"); //get hp
      int nHP = StringToInt(SQLGetData(1));
      ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectDamage(GetMaxHitPoints(oPlayer) - nHP), oPlayer); //reapply damage
      SQLExecDirect("DELETE hitpoints FROM players WHERE player = '"+sPlayer+"' AND pcname = '"+sPCName+"'"); //reset hp for next usage

      AssignCommand(oPlayer, ClearAllActions(TRUE));
      AssignCommand(oPlayer, JumpToLocation(GetWayPointByTag("wp"+sPlayer+"#"+sPCname))); //get the waypoint and jump to it
      DestroyObject(GetObjectByTag("wp"+sPlayer+"#"+sPCname)); //destroy the waypoint
   }
}


OnExit
Code:
#include "aps_include"
void main()
{
   object oPlayer = GetExitingObject();
   string sPlayer = SQLEncodeSpecialChars(GetPCPlayerName(oPlayer));
   string sPCname = SQLEncodeSpecialChars(GetName(oPlayer));
   location lLocation = SQLEncodeSpecialChars(GetLocation(oPlayer));
   string sLocation = APSLocationToString(lLocation);
   
   SQLExecDirect("INSERT INTO players WHERE player = '"+sPlayer+"' AND pcname = '"+sPCName+"' ('hitpoints') VALUES ('"+IntToString(GetCurrentHitPoints(oPlayer))+"')"; //store hp

   CreateObject(OBJECT_TYPE_WAYPOINT, "nw_waypoint", GetLocation(oPlayer), FALSE, "wp_"+sPlayer+"#"+sPCname); //create a waypoint with player data
}


You need a row called hitpoints in the players table to store the Hitpoints.
Also using the player's account and the pc name for these things might not be enough, I suggest you to add the CDKey to the players table too.
Back to top
View user's profile Send private message
nachopala



Joined: 20 May 2007
Posts: 6

PostPosted: Mon May 21, 2007 20:15    Post subject: Reply with quote

This is what i get from the log, I dont know, why the damage is not applying, it seems that is getting the right number, wich is 6, but maybe for some reason it doesnt apply the effect

Code:
NWNX ODBC2 plugin V.0.9.2.4
(c) 2005 by Ingmar Stieger (Papillon) and Jeroen Broekhuizen
visit us at http://www.nwnx.org

o Logfile maximum size limit is: 524288 bytes
o Log level: Everything will be logged.
o Using MySQL connection.
o Hooking SCO....hooked at 5d3560
o Hooking RCO....hooked at 5d3440
o Connect successful.
o Got request: SELECT player FROM players WHERE player='nachopala' AND pcname='Simir Jard'
o Sent response (9 bytes): nachopala
o Got request: SELECT hitpoints FROM players WHERE player = 'nachopala' AND pcname = 'Simir Jard'
o Sent response (1 bytes): 6
o Got request: DELETE hitpoints FROM players WHERE player = 'nachopala' AND pcname = 'Simir Jard'
! SQL Error: Unknown table 'hitpoints' in MULTI DELETE


Code:
int nHP = StringToInt(SQLGetData(1));
      ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectDamage(GetMaxHitPoints(oPlayer)-nHP), oPlayer); //reapply damage
Back to top
View user's profile Send private message
nachopala



Joined: 20 May 2007
Posts: 6

PostPosted: Mon May 21, 2007 20:30    Post subject: Reply with quote

Excelent! I got it running, i commented out the line where i was deleting the database for hitpoints. And it worked. I will put into the onexit part to Update instead of insert in the table for hitpoints, and ill see how it works.
Back to top
View user's profile Send private message
TroveLord



Joined: 22 Nov 2006
Posts: 136
Location: Italy

PostPosted: Mon May 21, 2007 21:23    Post subject: Reply with quote

nachopala wrote:
I will put into the onexit part to Update instead of insert in the table for hitpoints, and ill see how it works.

That's an even better solution but I was too lazy for that Razz

How about the location thing?
Back to top
View user's profile Send private message
nachopala



Joined: 20 May 2007
Posts: 6

PostPosted: Mon May 21, 2007 21:57    Post subject: Reply with quote

The location thing worked perfectly fine Very Happy .
Sorry to keep bothering, but... there is something else...

When the user is leaving the module i get this on the log
(it should be SELECTING the PC and storing the hitpoints, but instead...

Code:
o Got request: SELECT player FROM players WHERE player='' AND pcname=''


It seems that it is delivering player and pcname empty!, any solution?... reading somewhere in the forum i saw something about the pc leaving and the GetExitingObject starting later... maybe its that?

Edit:

Im adding an autoincrement to the database, im getting that number and planning to use it to identify a character during the Module OnHeartBeat and saving his location,hp,spells,feats, in case of crash or exploit.

Sounds good? any idea of what to put on the OnHeartBeat?.
I got now, a LocalInt setted on the PC called ID.
Back to top
View user's profile Send private message
TroveLord



Joined: 22 Nov 2006
Posts: 136
Location: Italy

PostPosted: Tue May 22, 2007 0:19    Post subject: Reply with quote

nachopala wrote:
When the user is leaving the module i get this on the log
(it should be SELECTING the PC and storing the hitpoints, but instead...

Code:
o Got request: SELECT player FROM players WHERE player='' AND pcname=''


It seems that it is delivering player and pcname empty!, any solution?... reading somewhere in the forum i saw something about the pc leaving and the GetExitingObject starting later... maybe its that?

I was unsure until you confirmed that, but when a player leaves the server doesn't make it in time to get some of his data (rough explaination but should be easy to understand) and that's explains why you get blank values, try with just the character name, that should do.

nachopala wrote:
Im adding an autoincrement to the database, im getting that number and planning to use it to identify a character during the Module OnHeartBeat and saving his location,hp,spells,feats, in case of crash or exploit.

Sounds good? any idea of what to put on the OnHeartBeat?.
I got now, a LocalInt setted on the PC called ID.

I wouldn't do that, OnHeartbeat could turn into a lag factory, so avoid all the unnecessary functions.
Back to top
View user's profile Send private message
Papillon
x-man


Joined: 28 Dec 2004
Posts: 1060
Location: Germany

PostPosted: Tue May 22, 2007 7:48    Post subject: Reply with quote

I'd rather save player data at a fixed intervals (e.g. every 3 minutes). You might also want to consider events like OnAreaEnter for saving character location.
_________________
Papillon
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    nwnx.org Forum Index -> Database related All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group