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 
 
Can someone help me with this script

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



Joined: 22 May 2005
Posts: 32

PostPosted: Fri Oct 27, 2006 11:54    Post subject: Can someone help me with this script Reply with quote

I got this script from the vault but can't seen to get it fully working.
I would also like to set it up to drop all the info to the database at one time, this way I dont have 3 or 4 lines going down my database for one character every time the save info is fired. So if anyone could help me set this up I would be greatful. ALso need help setting up a table for this script...I had it running on the pwdata table and it would never store the player name in the name colum I just always got the ~ for name then it tried to cram all the info in one line.
Code:
////::///////////////////////////////////////////////
//:: pw_inc
//:: Written by EPOlson (eolson@cisco.com)
//:: Feb 2004   (for the pw helper scripts)
//::  updated for cfs 8/18/06 by EPOlson
//:://////////////////////////////////////////////
#include "aps_include"

// switches

const string PW_DB = "players";      // database name (set to "TEMP" if you don't want data stored on disk)

// check spells 0..MAX_SPELLS
const int MAX_SPELLS = 804;
const int MAX_FEATS = 1071;

// Local vars to store data (append player name and character name)
const string PC_INFO_HP = "HP_";      // int
const string PC_INFO_SPELLS = "Spell_"; // string
const string PC_INFO_FEATS = "Feat_";  // string
const string PC_INFO_LOC = "Loc_";    // location

// keep trying to jump to the new location
//   this waits for the PC to have a valid location before jumping to a new one
void DelayJumpToLocation(object oPC, location lDestination);

// store player info on exit (HPs, Spells, Feats)
// call upon exit (client exit or area exit)
void StorePlayerInfo(object oPC);

void StorePlayerInfo(object oPC)
{
  object oPwhData = GetModule();  // store this on the module, since oPC may not be valid
   // PC Name
    string sTag = GetName(oPC);

   // PC Player Name
    string sPlayer = GetLocalString(oPC,"PLAYERNAME");

  // Hit Points
  int nCHP=GetCurrentHitPoints(oPC);
 // SetPersistentInt(oPwhData, PC_INFO_HP+sID, nCHP, 0, PW_DB);

  // Store Location
  location lMyLoc = GetLocation(oPC);
 // SetPersistentLocation(oPwhData, PC_INFO_LOC+sID, lMyLoc, 0, PW_DB);

  // Spells - store only the spells we know (e.g.  13:2 15:1 )
  int nSpell;
  int nNumSpell;
  string sSpellList = " ";
  for(nSpell=0; nSpell<MAX_SPELLS; nSpell++) {
    if (nNumSpell = GetHasSpell(nSpell,oPC)) {
      sSpellList += IntToString(nSpell)+":"+IntToString(nNumSpell)+" ";
    }
  }
 // SetPersistentString(oPwhData, PC_INFO_SPELLS+sID, sSpellList, 0, PW_DB);

  // Feats - store counts of feats we know
  int nFeat;
  int nNumFeat;
  string sFeatList = " ";
  for(nFeat=0; nFeat<MAX_FEATS; nFeat++) {
    if (nNumFeat = GetHasFeat(nFeat,oPC)) {
      sFeatList += IntToString(nFeat)+":"+IntToString(nNumFeat)+" ";
    }
 }
 // SetPersistentString(oPwhData, PC_INFO_FEATS+sID, sFeatList, 0, PW_DB);
}


// restore player info from local vars (HPs, Spells, Feats)
// call during login
void LoadPlayerInfo(object oPC);

void LoadPlayerInfo(object oPC)
{
  if(!GetIsPC(oPC))return;

  object oPwhData = GetModule();  // store this on the module, since oPC may not be valid
  string sID = GetPCPlayerName(oPC)+" "+GetName(oPC);
  string sPC = GetName(oPC);

  // store sID on PC object
  SetLocalString(oPC,PERSISTENT_ID,sID);

  SendMessageToPC(oPC,"OOC: restoring previous HPs, spells, and feats");

 // HP
  int nHP = GetPersistentInt(oPwhData,PC_INFO_HP+sID,PW_DB);
  if (nHP == 0) nHP = GetCurrentHitPoints(oPC); // give them full HP the first time
  ApplyEffectToObject(DURATION_TYPE_PERMANENT,
      EffectDamage(GetCurrentHitPoints(oPC)-nHP,DAMAGE_TYPE_MAGICAL,DAMAGE_POWER_PLUS_FIVE), oPC);

  // Goto Location
  location lOldLoc = GetPersistentLocation(oPwhData,PC_INFO_LOC+sID,PW_DB);
  if (GetAreaFromLocation(lOldLoc) != OBJECT_INVALID) {
      SendMessageToPC(oPC,"OOC: returning to area: "+GetName(GetAreaFromLocation(lOldLoc)));
      DelayJumpToLocation(oPC,lOldLoc);
  }

  // spells
  string sOldSpellList = GetPersistentString(oPwhData,PC_INFO_SPELLS+sID,PW_DB);
  // spells stored in string: " nn:uu nn:uu nn:uu "
  int nSpell;
  int nNumSpell;
  for( nSpell = 0; nSpell < MAX_SPELLS; nSpell++) {
    if (nNumSpell = GetHasSpell(nSpell,oPC)) {
      string sLookfor = " "+IntToString(nSpell)+":";
      int nStart = FindSubString(sOldSpellList,sLookfor);
      if (nStart >= 0) {
         while (GetSubString(sOldSpellList,nStart,1) != ":") nStart++;
         int nEnd = nStart+1;
         while (GetSubString(sOldSpellList,nEnd,1) != " ") nEnd++;
         string sSub = GetSubString(sOldSpellList,nStart+1,nEnd-nStart);
         int nOldNumSpell= StringToInt( sSub);
         int nSpellDiff = nNumSpell - nOldNumSpell;
         int suse;
         for (suse=0;suse<nSpellDiff;suse++) {
            DecrementRemainingSpellUses(oPC,nSpell);
         }
         // check to see if it worked
         int nNewNumSpell = GetHasSpell(nSpell,oPC);
         if (nNewNumSpell != nOldNumSpell) {
           PrintString("PWH anticheat - could not restore spell #"+sLookfor+ " old:"+sSub+
               ",new:"+IntToString(nNewNumSpell));
         }

      } else {
        // wipe all uses
        int suse;
        for (suse=0;suse<nNumSpell;suse++) {
            DecrementRemainingSpellUses(oPC,nSpell);
        }
      }

    }
  }

  // Feats
  //string sOldFeatList = GetLocalString(oPwhData,PC_INFO_FEATS+sID);
  //if (sOldFeatList == "") { // try DB
  string sOldFeatList = GetPersistentString(oPwhData,PC_INFO_FEATS+sID,PW_DB);
  //}
  // feats stored in string: " nn:uu nn:uu nn:uu "
  int nFeat;
  int nNumFeat;
  for(nFeat=0; nFeat<MAX_FEATS; nFeat++) {
    if (nNumFeat = GetHasFeat(nFeat,oPC)) {
      string sLookfor = " "+IntToString(nFeat)+":";
      int nStart = FindSubString(sOldFeatList,sLookfor);
      if (nStart >= 0) {
         while (GetSubString(sOldFeatList,nStart,1) != ":") nStart++;
         int nEnd = nStart+1;
         while (GetSubString(sOldFeatList,nEnd,1) != " ") nEnd++;
         string sSub = GetSubString(sOldFeatList,nStart+1,nEnd-nStart);
         int nOldNumFeat= StringToInt( sSub);
         int nFeatDiff = nNumFeat - nOldNumFeat;
         int suse;
         for (suse=0;suse<nFeatDiff;suse++) {
            DecrementRemainingFeatUses(oPC,nFeat);
         }
         // check to see if it worked
         int nNewNumFeat = GetHasFeat(nFeat,oPC);
         if (nNewNumFeat != nOldNumFeat) {
           PrintString("PWH anticheat - could not restore feat #"+sLookfor+ " old:"+sSub+
               ",new:"+IntToString(nNewNumFeat));
           //SendMessageToPC(oPC,"Debug - can't restore feat #"+sLookfor+" old="+
           //       sSub+" new="+IntToString(nNewNumFeat));
         }

      } else {
        // wipe all uses
        int suse;
        for (suse=0;suse<nNumFeat;suse++) {
            DecrementRemainingFeatUses(oPC,nFeat);
        }
      }

    }
  }
}

// keep trying to jump to the new location
//   this waits for the PC to have a valid location before jumping to a new one
void DelayJumpToLocation(object oPC, location lDestination)
{
  // see if PC has a locaton
  object chkArea = GetAreaFromLocation(GetLocation(oPC));
  if (chkArea == OBJECT_INVALID) {
      PrintString("CHK - Waiting to jump");
      DelayCommand(1.0,DelayJumpToLocation(oPC,lDestination));
  } else {
      PrintString("CHK - jump");
      AssignCommand(oPC,JumpToLocation(lDestination));
  }
}

I know its alot of code but I have honestly been trying to get this to work on my own for 4 days straight so any help would be greatly appreciated.
Frang
Back to top
View user's profile Send private message
Frang



Joined: 22 May 2005
Posts: 32

PostPosted: Wed Nov 01, 2006 6:40    Post subject: Reply with quote

Ok I hate to be a bother but I really need to get this working. With some help from a few great people here I managed to get the saving to the database working flawlessly. Now I just really need to figure out how to apply the info I get from the database back to the player. I am getting the info from the data base my logs show it working. Heres the code for getting the info and my attempt to put it back on the player.
Code:
///////////////////////////////////////////////////////////////////////////////
// Code taken from pwh_inc Written by EPOlson
//
void LoadPlayerInfo(object oPC);

void LoadPlayerInfo(object oPC)
{
  if(!GetIsPC(oPC))return;

oPC = GetEnteringObject();
string sPlayer = GetPCPlayerName(oPC);
string sName = GetName(oPC);
string sSQL;
int nLevel;
int nHP;
string sFeats;
string sSpells;
location lLoc;
string sLoc = SQLEncodeSpecialChars(APSLocationToString(lLoc));

     // Looks for Player name in the data base
       sSQL = "SELECT player FROM   players   WHERE Player='" + sPlayer +
        "' AND Name='" + sName + "'";
        SQLExecDirect(sSQL);

if(SQLFetch() == SQL_SUCCESS) // if you retrieved an entry
{
    lLoc = APSStringToLocation(SQLDecodeSpecialChars(SQLGetData(1))); // location
    nHP = StringToInt(SQLGetData(2)); // hitpoints
    // need to write a function to seperate these
    sFeats = SQLGetData(3);
    sSpells = SQLGetData(4);

}

  SendMessageToPC(oPC,"Restoring previous HPs, spells, feats and location");


  // HP

  if (nHP == 0) nHP = GetCurrentHitPoints(oPC); // give them full HP the first time
  ApplyEffectToObject(DURATION_TYPE_PERMANENT,
      EffectDamage(GetCurrentHitPoints(oPC)-nHP,DAMAGE_TYPE_MAGICAL,DAMAGE_POWER_PLUS_FIVE), oPC);

  // Goto Location
  if (GetAreaFromLocation(lLoc) != OBJECT_INVALID) {
      SendMessageToPC(oPC,"Invalid area returning to current area: "+GetName(GetAreaFromLocation(lLoc)));
      DelayJumpToLocation(oPC,lLoc);
  }

 string sOldSpells = SQLGetData(4);
  // spells stored in string: " nn:uu nn:uu nn:uu "
  int nNumSpell;
  int nSpell;
  for( nSpell = 0; nSpell < MAX_SPELLS; nSpell++) {
    if (nNumSpell = GetHasSpell(nSpell,oPC)) {
      string sLookfor = " "+IntToString(nSpell)+":";
      int nStart = FindSubString(sOldSpells,sLookfor);
      if (nStart >= 0) {
         while (GetSubString(sOldSpells,nStart,1) != ":") nStart++;
         int nEnd = nStart+1;
         while (GetSubString(sOldSpells,nEnd,1) != " ") nEnd++;
         string sSub = GetSubString(sOldSpells,nStart+1,nEnd-nStart);
         int nOldNumSpell= StringToInt( sSub);
         int nSpellDiff = nNumSpell - nOldNumSpell;
         int suse;
         for (suse=0;suse<nSpellDiff;suse++) {
            DecrementRemainingSpellUses(oPC,nSpell);
         }
         // check to see if it worked
         int nNewNumSpell = GetHasSpell(nSpell,oPC);
         if (nNewNumSpell != nOldNumSpell) {
           PrintString("Could not restore spells #"+sLookfor+ " old:"+sSub+
               ",new:"+IntToString(nNewNumSpell));
         }

      } else {
        // wipe all uses
        int suse;
        for (suse=0;suse<nNumSpell;suse++) {
            DecrementRemainingSpellUses(oPC,nSpell);
        }
      }

    }
  }

  // Feats
  string sOldFeats = SQLGetData(3);

  // feats stored in string: " nn:uu nn:uu nn:uu "
  int nFeat;
  int nNumFeat;
  for(nFeat=0; nFeat<MAX_FEATS; nFeat++) {
    if (nNumFeat = GetHasFeat(nFeat,oPC)) {
      string sLookfor = " "+IntToString(nFeat)+":";
      int nStart = FindSubString(sOldFeats,sLookfor);
      if (nStart >= 0) {
         while (GetSubString(sOldFeats,nStart,1) != ":") nStart++;
         int nEnd = nStart+1;
         while (GetSubString(sOldFeats,nEnd,1) != " ") nEnd++;
         string sSub = GetSubString(sOldFeats,nStart+1,nEnd-nStart);
         int nOldNumFeat= StringToInt( sSub);
         int nFeatDiff = nNumFeat - nOldNumFeat;
         int suse;
         for (suse=0;suse<nFeatDiff;suse++) {
            DecrementRemainingFeatUses(oPC,nFeat);
         }
         // check to see if it worked
         int nNewNumFeat = GetHasFeat(nFeat,oPC);
         if (nNewNumFeat != nOldNumFeat) {
           PrintString("Could not restore feats #"+sLookfor+ " old:"+sSub+
               ",new:"+IntToString(nNewNumFeat));

         }

      } else {
        // wipe all uses
        int suse;
        for (suse=0;suse<nNumFeat;suse++) {
            DecrementRemainingFeatUses(oPC,nFeat);
        }
      }

    }
  }
}
// keep trying to jump to the new location
//   this waits for the PC to have a valid location before jumping to a new one
void DelayJumpToLocation(object oPC, location lLoc)
{
  // see if PC has a locaton
  object chkArea = GetAreaFromLocation(GetLocation(oPC));
  if (chkArea == OBJECT_INVALID) {
      PrintString("Waiting to jump");
      DelayCommand(8.0,DelayJumpToLocation(oPC,lLoc));
  } else {
      PrintString("Jumping to last saved Location");
      AssignCommand(oPC,JumpToLocation(lLoc));
  }
}


Thanks
Frang
Back to top
View user's profile Send private message
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