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 
 
Persistent Merchant Inventory

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



Joined: 06 Jan 2015
Posts: 1

PostPosted: Tue Jan 06, 2015 4:56    Post subject: Persistent Merchant Inventory Reply with quote

I have been looking for a way to create persistent merchant inventory for my PW module, but coming up with nothing. Is such a thing even possible? I have very little idea about what I am doing, but willing to learn, unless someone just happens to have such a script lying around.

Thanks in advance!
Back to top
View user's profile Send private message
Baaleos



Joined: 02 Sep 2007
Posts: 830

PostPosted: Wed Jan 07, 2015 3:31    Post subject: Reply with quote

Code:

// Persistent World Shop Include
// By Baaleos

#include "aps_include"


void SetPersistentItemInStore(object oOwner, string sVarName, object oObject, int iExpiration =
                         0, string sTable = "pwobjshops")
{
    string sPlayer;
    string sTag;
    string sItemName = SQLEncodeSpecialChars(GetName(oObject));
    string sResRef = GetResRef(oObject);
    if (GetIsPC(oOwner))
    {
        sPlayer = SQLEncodeSpecialChars(GetPCPlayerName(oOwner));
        sTag = SQLEncodeSpecialChars(GetName(oOwner));
    }
    else
    {
        sPlayer = "~";
        sTag = GetTag(oOwner);
    }
    sVarName = SQLEncodeSpecialChars(sVarName);

    string sSQL = "SELECT player FROM " + sTable + " WHERE player='" + sPlayer +
        "' AND tag='" + sTag + "' AND name='" + sVarName + "'";
    SQLExecDirect(sSQL);

    if (SQLFetch() == SQL_SUCCESS)
    {
        // row exists
        sSQL = "UPDATE " + sTable + " SET val=%s,expire=" + IntToString(iExpiration) +
            " WHERE player='" + sPlayer + "' AND tag='" + sTag + "' AND name='" + sVarName + "'";
        SetLocalString(GetModule(), "NWNX!ODBC!SETSCORCOSQL", sSQL);
        StoreCampaignObject ("NWNX", "-", oObject);
    }
    else
    {
        // row doesn't exist
        sSQL = "INSERT INTO " + sTable + " (player,tag,name,val,expire,resref,item_name) VALUES" +
            "('" + sPlayer + "','" + sTag + "','" + sVarName + "',%s," + IntToString(iExpiration) + ",'"+sResRef+"','"+sItemName+"')";
        SetLocalString(GetModule(), "NWNX!ODBC!SETSCORCOSQL", sSQL);
        StoreCampaignObject ("NWNX", "-", oObject);
    }
}


void ClearInventoryForLoad(object oStore)
{
    object oItem = GetFirstItemInInventory(oStore);
    while (GetIsObjectValid(oItem))
    {
      if(GetPlotFlag(oItem))
      {
        SetPlotFlag(oItem,FALSE);
      }
      DestroyObject(oItem);
      oItem = GetNextItemInInventory(oStore);
    }

}


//Load Items Associated with this Store
//
void ReLoadStore(object oStore)
{
   int iNumber = 1;
   int iLoaded = GetLocalInt(OBJECT_SELF,"STORE_LOADED");
   if(!iLoaded)
   {
        ClearInventoryForLoad(oStore);
        object oItem = GetPersistentObject(oStore,"ITEM_"+IntToString(iNumber),oStore,"pwobjshops");
        while(oItem != OBJECT_INVALID)
        {
         iNumber++;
         oItem = GetPersistentObject(oStore,"ITEM_"+IntToString(iNumber),oStore,"pwobjshops");
        }
        SetLocalInt(OBJECT_SELF,"STORE_LOADED",1);
   }
}

void StoreItems(object oStore)
{

  if (GetIsObjectValid(oStore))
  {
    object oItem = GetFirstItemInInventory(oStore);
    int iNum = 1;
    while (GetIsObjectValid(oItem))
    {
      if(GetPlotFlag(oItem))
      {
        SetPlotFlag(oItem,FALSE);
      }
      SetPersistentItemInStore(oStore,"ITEM_"+IntToString(iNum),oItem,0,"pwobjshops");
      DestroyObject(oItem);
      oItem = GetNextItemInInventory(oStore);
      iNum++;
    }
    SetPersistentInt(oStore,"ITEMS_IN_STORE",iNum);
    DeleteLocalInt(OBJECT_SELF,"STORE_LOADED");

  }

}

void ClearPersistentStoreItemsFromDB(object oStore)
{
     string sTag = GetTag(oStore);
     string sSQL = "DELETE from pwobjshops where tag = '"+SQLEncodeSpecialChars(sTag)+"'";
     SQLExecDirect(sSQL);
}




void OnStoreOpen(object oStore)
{
//ActionSpeakStringByStrRef(53090, TALKVOLUME_TALK);

 //Load the Store
 ReLoadStore(oStore);
 //Clean the DB
 ClearPersistentStoreItemsFromDB(oStore);
}

void OnStoreClose(object oStore)
{
   StoreItems(oStore);
   DeleteLocalObject(oStore,"CUSTOMER");
}


Made this years ago-
Designed to remove items from a merchant but not before backing them up in database
When you open the store you can reload the inventory

Inflated inventories cause lag but only if the items are in the merchants inventory- by shoving them into DB storage you can aleveate the lag
Back to top
View user's profile Send private message
Ravine



Joined: 26 Jul 2006
Posts: 105

PostPosted: Wed Jan 07, 2015 8:41    Post subject: Reply with quote

Hi, the latest ODBC2 for linux can save stores and placeables completely as a whole, without the need to handle items in it. I'm using it for player banks (saving placeables), it's kinda flawless. I'm sure it works the same for stores as well.

The link for the prototypes in this topic:
http://www.nwnx.org/phpBB2/viewtopic.php?t=1083

If you know scripting, you can make it much more simpler to suit your needs.

Or, there is another DB plugin (incompatible with the ODBC2 as i know), but has the same function with simpler interface:
http://www.nwnx.org/phpBB2/viewtopic.php?t=1900&highlight=nwnxdb

But it's not supported as official, not sure...
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