Author Topic: Need Help w/ PW Script (Writing to Database)  (Read 374 times)

Legacy_Pstemarie

  • Hero Member
  • *****
  • Posts: 4368
  • Karma: +0/-0
Need Help w/ PW Script (Writing to Database)
« on: July 17, 2011, 08:38:52 pm »


               Need help with writing a unique ID for the entering PC to the database so I can do a check in another script to verify that this is the PC's first time logging in to the Server.

/*
Filename:           x3_mod_pre_enter.nss
Author:             Paul Ste. Marie (Pstemarie)
Date Created:       17 July 2011
Summary:            OnClientEnter Event Hook-in Script
                    Checks if this is the first time player has logged into the
                    Server and if yes, executes initial login setup code.
--------------------------------------------------------------------------------
Post-Release Revision History
Revision Author:
Revision Date:
Revision Summary:
*/
// Removes (and destroys) all items from the inventory of object oTarget, including equipped items.
// If nTakeGold is TRUE then all gold is removed as well.
void TakeInventoryFromCreature(object oTarget, int nTakeGold = TRUE);
// Gives a predetermined amount of gold to oTarget as defined by oTarget's base class.
void GiveStartingGold(object oTarget);

void main()
{
    object oPC = GetEnteringObject();
    //Exit if oPC is not a PC
    if (!GetIsPC(oPC))
        return;
    //If this is the player's first login, then perform the following:
    //  + Strip all inventory (including creature items)
    //  + Reset XP to 0 (first level)
    //  + Give starting gold
    //  - Give a set of clothing
    //  - Assign a unique ID and store in the database
    //  - Jump to the Initial login Area
    //if first login
    TakeInventoryFromCreature(oPC, TRUE);
    SetXP(oPC, 0);
    GiveStartingGold(oPC);
    //GiveClothes(oPC) - not yet implemented
    //Database functions - WTF do I do here - GET HELP
    DelayCommand(1.0, AssignCommand(oPC, JumpToObject(GetWaypointByTag("wp_first_login"))));
}
 
void TakeInventoryFromCreature(object oTarget, int nTakeGold = TRUE)
{
    if (nTakeGold)
        ExecuteScript("takegold", oTarget);
    object oEquip;
    int nSlot = 0;
    //Take equipped items
    while (nSlot <= 13)
    {
        nSlot ++;
        oEquip = GetItemInSlot(nSlot, oTarget);
        if (GetIsObjectValid(oEquip))
        {
            DestroyObject(oEquip);
        }
    }
    //Take non-equipped items
    oEquip = GetFirstItemInInventory(oTarget);
    while(GetIsObjectValid(oEquip))
    {
        DestroyObject(oEquip);
        oEquip = GetNextItemInInventory(oTarget);
    }
}
void GiveStartingGold(object oTarget)
{
    int nclass = GetclassByPosition(1, oTarget);
    int nGold;
    switch (nclass)
    {
        case class_TYPE_DRUID:
            nGold = d4(2);
            break;
        case class_TYPE_SORCERER:
        case class_TYPE_WIZARD:
            nGold = d4(3);
            break;
        case class_TYPE_BARBARIAN:
        case class_TYPE_BARD:
            nGold = d4(4);
            break;
        case class_TYPE_CLERIC:
        case class_TYPE_MONK:
        case class_TYPE_ROGUE:
            nGold = d4(5);
            break;
        case class_TYPE_FIGHTER:
        case class_TYPE_PALADIN:
        case class_TYPE_RANGER:
            nGold = d4(6);
            break;
    }
    nGold *=10;
    GiveGoldToCreature(oTarget, nGold);
}
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Need Help w/ PW Script (Writing to Database)
« Reply #1 on: July 17, 2011, 08:44:58 pm »


               You do not need a DB to check if it is a new PC.    If it is a brand new PC they will have 0 XP.   Do your new PC stuff then give them 1 xp.
               
               

               
            

Legacy_Xardex

  • Sr. Member
  • ****
  • Posts: 414
  • Karma: +0/-0
Need Help w/ PW Script (Writing to Database)
« Reply #2 on: July 17, 2011, 09:10:38 pm »


               What Lightfoot8 said, but you could also use the characters name as the unique ID. If two characters have same name they might mess each other up in some way, or so I have heard. Anyhow I always check for duplicate-name characters, deleting the new ones to avoid these mythical errors.
               
               

               


                     Modifié par Xardex, 17 juillet 2011 - 08:13 .
                     
                  


            

Legacy_Pstemarie

  • Hero Member
  • *****
  • Posts: 4368
  • Karma: +0/-0
Need Help w/ PW Script (Writing to Database)
« Reply #3 on: July 18, 2011, 01:06:40 am »


               

Lightfoot8 wrote...

You do not need a DB to check if it is a new PC. If it is a brand new PC they will have 0 XP. Do your new PC stuff then give them 1 xp.


The reason why I was looking at storing the PC in the database is so that when they login again they will not have to go through all the initial setup again. If I just give them 1 XP - and use that to check against - any player logging in with a character over 1st level will be able to bypass this initial setup. The PW is designed so that all characters start off at 1st level - hence the reason I strip all gear, gold, and XP from 1st time logins.

My setup plan entails having first-time logins go to a special area where they can spend their starting gold to buy gear and modify certain aspects of their character. Then they proceed to the first area of the module. Characters that have logged in before will goto whatever area they were in when they logged off - returning in essence at the exact moment they left. This is why I want to use a database. 
               
               

               


                     Modifié par Pstemarie, 18 juillet 2011 - 12:13 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Need Help w/ PW Script (Writing to Database)
« Reply #4 on: July 18, 2011, 01:40:27 am »


               If you are using a server vault. All characters will have to log in at 0 xp there first time. unless there character already exsists on your server vault. in that case they have already loged in before.

Now if you are using a player vault the entire system is moot point. they will be able to bring in any character go through your start up. log out and modify there character and come back in.

With the server vault if they have 0 xp they are new. going through the start up gives them 1 xp and they are no longer new after that and dont have to do it again. Just chectk there xp to see if they are new or not.

As far as a unique ID goes.  To me how you do it depends on if you have NWNx installed or not.   

As far as a location to teleport to on client enter after a server reset.  I see no problem with using a BD item.  it is not really the type of data you need access to when the PC is not online, So why not just leave the data with the PC.
               
               

               


                     Modifié par Lightfoot8, 18 juillet 2011 - 12:44 .
                     
                  


            

Legacy_GhostOfGod

  • Hero Member
  • *****
  • Posts: 1490
  • Karma: +0/-0
Need Help w/ PW Script (Writing to Database)
« Reply #5 on: July 18, 2011, 04:22:35 am »


               As most have already said, just checking for 0 xp is the simplest method.

void main()
{
    object oPC = GetEnteringObject();
    //If xp = 0 then it is the first time logging in. Of course this will only
    //work if the players xp can never go back to zero.
    if (GetXP(oPC) == 0)
    {
        SetXP(oPC, 1);
        //do other stuff
    }
}


You can also do other simple things like check for an item the player will get after they log in. Make it a non drop item. Then just check if the player possesses the item. If they have the item then they have logged in before.

void main()
{
    object oPC = GetEnteringObject();
    //If the rulebook item is not valid, it is the first time logging in.
    if (GetItemPossessedBy(oPC, "Rulebook") == OBJECT_INVALID)
    {
        CreateItemOnObject("rulebook", oPC);
        //do other stuff
    }
}


Trying to create unique ID to write to to the NWN database often results in problems with strings ending up too long and then getting cut off at the end thus making it so that you can not accurately check for the entry. You have more options if you are using NWNX + database.
               
               

               


                     Modifié par GhostOfGod, 18 juillet 2011 - 03:24 .
                     
                  


            

Legacy_Failed.Bard

  • Hero Member
  • *****
  • Posts: 1409
  • Karma: +0/-0
Need Help w/ PW Script (Writing to Database)
« Reply #6 on: July 18, 2011, 06:13:23 am »


               With my mod, I check for a variable stored on the PC skin to determine if they're a new or existing character.  Existing characters are ported to their last stored in game location, new characters remain in the OOC entry area and can modify their appearances and starting gear before entering the main mod itself.
  One note, that's been covered not too long ago here, is that whether on a database item, or in an actual database, location and area (as a tag string, not an object) need to be stored seperately and recombined in order for the location to persist through a module build.
               
               

               
            

Legacy_Pstemarie

  • Hero Member
  • *****
  • Posts: 4368
  • Karma: +0/-0
Need Help w/ PW Script (Writing to Database)
« Reply #7 on: July 18, 2011, 11:22:23 am »


               Thanks guys for the quick responses. This is really my first time using any sort of persistance with my PW - up until now its just been a mash of modules played over LAN, but now we want something a little...more.

I think the Server Vault sounds like the easiest solution, so I'll give that a whirl first and see if it works like I want.
               
               

               
            

Legacy_Pstemarie

  • Hero Member
  • *****
  • Posts: 4368
  • Karma: +0/-0
Need Help w/ PW Script (Writing to Database)
« Reply #8 on: July 18, 2011, 11:56:35 am »


               Server Vault works great. Exactly what I needed, thanks Lightfoot.
               
               

               


                     Modifié par Pstemarie, 18 juillet 2011 - 10:57 .
                     
                  


            

Legacy_TSMDude

  • Hero Member
  • *****
  • Posts: 1515
  • Karma: +0/-0
Need Help w/ PW Script (Writing to Database)
« Reply #9 on: July 20, 2011, 09:22:50 pm »


               

Pstemarie wrote...

Server Vault works great. Exactly what I needed, thanks Lightfoot.


Awesome to hear you are jumping into the PW World.  If you need anything let me know on Skype at paco.petro and I will gladly help in any way.

Like on the Start area, we have one for new players then one for returning players that allows returning players a couple of options.

A/ They can use a portal door and return to the exact spot they were in as you are doing.

B/ They can go to a body tailor module to say change the fact that thier PC gained weight, got beat up...basically show some wear and tear or advance in aging thing.

C/ Go to thier guild, house, temple, or one of the starting cities.

This lets them have some options that players seem to dig.
               
               

               
            

Legacy_Pstemarie

  • Hero Member
  • *****
  • Posts: 4368
  • Karma: +0/-0
Need Help w/ PW Script (Writing to Database)
« Reply #10 on: July 20, 2011, 09:29:32 pm »


               Thanks TSM - I'm sure I'll be taking you up on that offer '<img'>