Author Topic: Need String Test Scripting Help.  (Read 620 times)

Legacy__Guile

  • Hero Member
  • *****
  • Posts: 1308
  • Karma: +0/-0
Need String Test Scripting Help.
« on: April 12, 2011, 09:28:12 pm »


               I'm having a horrendous time trying to script functions for checking names on a PC from a conversation.

Essentially I want to check the players name and ensure each character is legit.

The script below list all of the character I wish to allow.

Only, I don't want players using Alt Code or Spaces at the beginning of their name & I especially don't want them using the ~  `  "  '   symbols.

I need someone to help me with this script because I've tried like 9 times to get it correct and can't seem to be able to finagle it properly.

I'm going to be using the script in a starting conditional to show a line of a conversation.

Any help would be much appreciated, thanks.
               
               

               


                     Modifié par _Guile, 12 avril 2011 - 09:25 .
                     
                  


            

Legacy__Guile

  • Hero Member
  • *****
  • Posts: 1308
  • Karma: +0/-0
Need String Test Scripting Help.
« Reply #1 on: April 12, 2011, 09:50:11 pm »


               This is one of the scripts I was working with:

//Tested, NOT WORKING!!!
int StartingConditional()
{
   int i = FALSE; //Always returns FALSE (Doesn't Show the Line)
                  //Unless told otherwise by the script below..

   object oPC = GetPCSpeaker();
   string sName = GetName(oPC);
   int nLength = GetStringLength(sName);
   int nVerified, nCount;
   string sLetter;
   int nBad = FALSE;

   for(nCount=1;nCount<nTotal;nCount++)
   {
    sLetter = GetStringLeft(sName, nCount);

    if(sLetter == "0") {nVerified = 2;}
    else if(sLetter == "1") {nVerified = 2;}
    else if(sLetter == "2") {nVerified = 2;}
    else if(sLetter == "3") {nVerified = 2;}
    else if(sLetter == "4") {nVerified = 2;}
    else if(sLetter == "5") {nVerified = 2;}
    else if(sLetter == "6") {nVerified = 2;}
    else if(sLetter == "7") {nVerified = 2;}
    else if(sLetter == "8") {nVerified = 2;}
    else if(sLetter == "9") {nVerified = 2;}
    else if(sLetter == "a") {nVerified = 2;}
    else if(sLetter == "b") {nVerified = 2;}
    else if(sLetter == "c") {nVerified = 2;}
    else if(sLetter == "d") {nVerified = 2;}
    else if(sLetter == "e") {nVerified = 2;}
    else if(sLetter == "f") {nVerified = 2;}
    else if(sLetter == "g") {nVerified = 2;}
    else if(sLetter == "h") {nVerified = 2;}
    else if(sLetter == "i") {nVerified = 2;}
    else if(sLetter == "j") {nVerified = 2;}
    else if(sLetter == "k") {nVerified = 2;}
    else if(sLetter == "l") {nVerified = 2;}
    else if(sLetter == "m") {nVerified = 2;}
    else if(sLetter == "n") {nVerified = 2;}
    else if(sLetter == "o") {nVerified = 2;}
    else if(sLetter == "p") {nVerified = 2;}
    else if(sLetter == "q") {nVerified = 2;}
    else if(sLetter == "r") {nVerified = 2;}
    else if(sLetter == "s") {nVerified = 2;}
    else if(sLetter == "t") {nVerified = 2;}
    else if(sLetter == "u") {nVerified = 2;}
    else if(sLetter == "v") {nVerified = 2;}
    else if(sLetter == "w") {nVerified = 2;}
    else if(sLetter == "x") {nVerified = 2;}
    else if(sLetter == "y") {nVerified = 2;}
    else if(sLetter == "z") {nVerified = 2;}
    else if(sLetter == "A") {nVerified = 2;}
    else if(sLetter == "B") {nVerified = 2;}
    else if(sLetter == "C") {nVerified = 2;}
    else if(sLetter == "D") {nVerified = 2;}
    else if(sLetter == "E") {nVerified = 2;}
    else if(sLetter == "F") {nVerified = 2;}
    else if(sLetter == "G") {nVerified = 2;}
    else if(sLetter == "H") {nVerified = 2;}
    else if(sLetter == "I") {nVerified = 2;}
    else if(sLetter == "J") {nVerified = 2;}
    else if(sLetter == "K") {nVerified = 2;}
    else if(sLetter == "L") {nVerified = 2;}
    else if(sLetter == "M") {nVerified = 2;}
    else if(sLetter == "N") {nVerified = 2;}
    else if(sLetter == "O") {nVerified = 2;}
    else if(sLetter == "P") {nVerified = 2;}
    else if(sLetter == "Q") {nVerified = 2;}
    else if(sLetter == "R") {nVerified = 2;}
    else if(sLetter == "S") {nVerified = 2;}
    else if(sLetter == "T") {nVerified = 2;}
    else if(sLetter == "U") {nVerified = 2;}
    else if(sLetter == "V") {nVerified = 2;}
    else if(sLetter == "W") {nVerified = 2;}
    else if(sLetter == "X") {nVerified = 2;}
    else if(sLetter == "Y") {nVerified = 2;}
    else if(sLetter == "Z") {nVerified = 2;}
    else if(sLetter == "!") {nVerified = 2;}
    else if(sLetter == "@") {nVerified = 2;}
    else if(sLetter == "#") {nVerified = 2;}
    else if(sLetter == "$") {nVerified = 2;}
    else if(sLetter == "%") {nVerified = 2;}
    else if(sLetter == "^") {nVerified = 2;}
    else if(sLetter == "&") {nVerified = 2;}
    else if(sLetter == "*") {nVerified = 2;}
    else if(sLetter == "(") {nVerified = 2;}
    else if(sLetter == ")") {nVerified = 2;}
    else if(sLetter == "-") {nVerified = 2;}
    else if(sLetter == "_") {nVerified = 2;}
    else if(sLetter == "=") {nVerified = 2;}
    else if(sLetter == "+") {nVerified = 2;}
    else if(sLetter == "[") {nVerified = 2;}
    else if(sLetter == "{") {nVerified = 2;}
    else if(sLetter == "]") {nVerified = 2;}
    else if(sLetter == "}") {nVerified = 2;}
    else if(sLetter == "|") {nVerified = 2;}
    else if(sLetter == "\\") {nVerified = 2;}
    else if(sLetter == ";") {nVerified = 2;}
    else if(sLetter == ":") {nVerified = 2;}
    else if(sLetter == "'") {nVerified = 2;}
    else if(sLetter == ",") {nVerified = 2;}
    else if(sLetter == "<") {nVerified = 2;}
    else if(sLetter == ".") {nVerified = 2;}
    else if(sLetter == ">") {nVerified = 2;}
    else if(sLetter == "/") {nVerified = 2;}
    else if(sLetter == "?") {nVerified = 2;}

    //Double Check to ensure they letter IS being verified!
    if(nVerified==2)
    {
     i = FALSE;  //Not needed.
    }
    //If it's not verified then obviously the player has a bad character.
    else
    {
     nBad = TRUE;
    }
   }

   if(nBad==TRUE)
   {
    i = TRUE;
   }

   //Tell the conversation if the PC has a legal name or not.
   return i;
}
               
               

               


                     Modifié par _Guile, 13 avril 2011 - 12:16 .
                     
                  


            

Legacy_GhostOfGod

  • Hero Member
  • *****
  • Posts: 1490
  • Karma: +0/-0
Need String Test Scripting Help.
« Reply #2 on: April 13, 2011, 12:09:03 am »


               Could do something like this perhaps:


int StartingConditional()
{
    object oPC = GetPCSpeaker();
    string sName = GetName(oPC);
    int iChars = 0;
    string sIllegalChar = GetSubString(sName, iChars, 1);

    while (sIllegalChar != "")
    {
        if (sIllegalChar == GetLocalString(GetModule(), "quote") ||
            sIllegalChar == " " ||
            sIllegalChar == "'" ||
            sIllegalChar == "`" ||
            sIllegalChar == "~") return TRUE;

        iChars++;
        sIllegalChar = GetSubString(sName, iChars, 1);
    }
    return FALSE;
}


It checks for bad characters instead of good characters. Seems like it'd be a smaller list. To check for the quotes though you have to put it as a string on the module and check for it since you can't stick the quotes between 2 quotes. And then if there are any other characters that you don't want to allow you can stick em in there.

Hope it helps. Good luck.
               
               

               


                     Modifié par GhostOfGod, 12 avril 2011 - 11:34 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Need String Test Scripting Help.
« Reply #3 on: April 13, 2011, 12:23:50 am »


               Here is my version.

int StartingConditional()
{
  string sValidCharacters = "1234567890 abcdefghijklmnopqrstuvwxyz";
  string sName= GetStringLowerCase(GetName(GetPCSpeaker()));
  if (sName == "") return FALSE;
  int nLength = GetStringLength(sName);
  int nCount;
 
for(nCount=0;nCount<nLength;nCount++)
  {
    if( FindSubString(sValidCharacters, GetSubString(sName,nCount,1))==-1) return FALSE;
  }
  return TRUE;
}

I also think you should use the script debugger on your version above.  Once you use it you will see what is going wrong right away.
               
               

               


                     Modifié par Lightfoot8, 12 avril 2011 - 11:34 .
                     
                  


            

Legacy__Guile

  • Hero Member
  • *****
  • Posts: 1308
  • Karma: +0/-0
Need String Test Scripting Help.
« Reply #4 on: April 13, 2011, 01:13:08 pm »


               Your script is not working Lightfoot8

I'm looking at where you put return false, so every time it comes across -1 (an error?) it will stop the script and return FALSE? (As in Don't Show The Line?)  That doesn't seem to make sense to me.

When the loop is done and script continues to be read it's going to tell the script to return True no matter what happened in the script?  You aren't using a variable to tell you what happened through the course of the loop.

I'm assuming return FALSE or return TRUE means stop the script now and tell it that it's FALSE or TRUE, am I correct?

I don't have a debugger btw.  '<img'>
               
               

               


                     Modifié par _Guile, 13 avril 2011 - 12:31 .
                     
                  


            

Legacy__Guile

  • Hero Member
  • *****
  • Posts: 1308
  • Karma: +0/-0
Need String Test Scripting Help.
« Reply #5 on: April 13, 2011, 01:33:23 pm »


               

GhostOfGod wrote...

Could do something like this perhaps:


int StartingConditional()
{
    object oPC = GetPCSpeaker();
    string sName = GetName(oPC);
    int iChars = 0;
    string sIllegalChar = GetSubString(sName, iChars, 1);

    while (sIllegalChar != "")
    {
        if (sIllegalChar == GetLocalString(GetModule(), "quote") ||
            sIllegalChar == " " ||
            sIllegalChar == "'" ||
            sIllegalChar == "`" ||
            sIllegalChar == "~") return TRUE;

        iChars++;
        sIllegalChar = GetSubString(sName, iChars, 1);
    }
    return FALSE;
}


It checks for bad characters instead of good characters. Seems like it'd be a smaller list. To check for the quotes though you have to put it as a string on the module and check for it since you can't stick the quotes between 2 quotes. And then if there are any other characters that you don't want to allow you can stick em in there.

Hope it helps. Good luck.


The problem with this approach is, you can't list Alt Code, therefore players could still use altcode and bypass your script with ficticious characters which either don't show up or show up with funny lettering etc.  This is why I took the list known good characters approach instead.
               
               

               


                     Modifié par _Guile, 13 avril 2011 - 12:33 .
                     
                  


            

Legacy__Guile

  • Hero Member
  • *****
  • Posts: 1308
  • Karma: +0/-0
Need String Test Scripting Help.
« Reply #6 on: April 13, 2011, 01:49:28 pm »


               OK, I rewrote some of your functions Lightfoot8 and tested it, this is indeed working, yay! '<img'>

 int StartingConditional()
{
 object oPC = GetPCSpeaker();
 if(!GetIsPC(oPC))
 {return FALSE;}

 int i = FALSE;
 int nBad = FALSE;
 int nCheck;
 string sLetter;

 string sValidCharacters = "1234567890 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+-=[{]};:/?\\|";
 string sName = GetName(oPC);

 if (sName == "") return FALSE;  //Stop here

 int nLength = GetStringLength(sName);
 int nCount;

 for(nCount=0;nCount<nLength;nCount++)
 {
   sLetter =  GetSubString(sName,nCount,1);
   nCheck = FindSubString(sValidCharacters, sLetter);
   if(nCheck != -1)
   {
    i = FALSE;
   }
   else
   {
    nBad = TRUE;
   }
 }

  //Now let's see if i is true or false...
  if(nBad)
  { i = TRUE; }

  return i;
}
               
               

               
            

Legacy_GhostOfGod

  • Hero Member
  • *****
  • Posts: 1490
  • Karma: +0/-0
Need String Test Scripting Help.
« Reply #7 on: April 13, 2011, 04:19:44 pm »


               

_Guile wrote...

GhostOfGod wrote...

Could do something like this perhaps:


int StartingConditional()
{
    object oPC = GetPCSpeaker();
    string sName = GetName(oPC);
    int iChars = 0;
    string sIllegalChar = GetSubString(sName, iChars, 1);

    while (sIllegalChar != "")
    {
        if (sIllegalChar == GetLocalString(GetModule(), "quote") ||
            sIllegalChar == " " ||
            sIllegalChar == "'" ||
            sIllegalChar == "`" ||
            sIllegalChar == "~") return TRUE;

        iChars++;
        sIllegalChar = GetSubString(sName, iChars, 1);
    }
    return FALSE;
}


It checks for bad characters instead of good characters. Seems like it'd be a smaller list. To check for the quotes though you have to put it as a string on the module and check for it since you can't stick the quotes between 2 quotes. And then if there are any other characters that you don't want to allow you can stick em in there.

Hope it helps. Good luck.


The problem with this approach is, you can't list Alt Code, therefore players could still use altcode and bypass your script with ficticious characters which either don't show up or show up with funny lettering etc.  This is why I took the list known good characters approach instead.



Oops...sorry about that. This part kinda through me off...

_Guile wrote...

I especially don't want them using the ~  `  "  '   symbols.

Glad you got it working. Lightfoots version, as usual, is always better.'<img'>
               
               

               


                     Modifié par GhostOfGod, 13 avril 2011 - 03:21 .
                     
                  


            

Legacy_GhostOfGod

  • Hero Member
  • *****
  • Posts: 1490
  • Karma: +0/-0
Need String Test Scripting Help.
« Reply #8 on: April 13, 2011, 05:11:03 pm »


               Actually _Guile yours may not be working quite right either. In your loop you are switching an int to TRUE or FALSE but you are not returning out of it. So really your TRUE or FALSE is just going to be based on the last character in the name. If the player has a name full of illegal characters except for the last character, then all will seem fine.
From rereading your original post I am guessing that you want a line of text in the conversation to show up If the player has any illegal characters in the name?

So really Lightfoots script should work simply by reversing the TRUE and FALSE he had there...pretty sure.
But maybe like this (i also added a bit to check if the first character is a space):

int StartingConditional()
{
    object oPC = GetPCSpeaker();
    //If the speaker is not a player, don't show line.
    if(!GetIsPC(oPC)) return FALSE;
    string sName = GetName(oPC);
    //If the character has no name, show line.
    if (sName == "") return TRUE;
    //If the first character in name is a space, show line.
    if (GetStringLeft(sName, 1) == " ") return TRUE;

    string sValidCharacters = "1234567890 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+-=[{]};:/?\\|";
    int nLength = GetStringLength(sName);
    int nCount;

    for(nCount=0;nCount<nLength;nCount++)
    {
        //If an invalid character is found in the name, show the line.
        if (FindSubString(sValidCharacters, GetSubString(sName, nCount, 1)) == -1) return TRUE;
    }
    //If all the characters in the player name are OK, then don't show the line.
    return FALSE;
}

And on a side note...I'm not sure how the "\\" character will react since it is used for things such as \\n in a string..so it might not count it as a legal character...don't quote me on that though.
               
               

               


                     Modifié par GhostOfGod, 13 avril 2011 - 04:23 .
                     
                  


            

Legacy__Guile

  • Hero Member
  • *****
  • Posts: 1308
  • Karma: +0/-0
Need String Test Scripting Help.
« Reply #9 on: April 14, 2011, 12:13:41 am »


               

GhostOfGod wrote...

Actually _Guile yours may not be working quite right either. In your loop you are switching an int to TRUE or FALSE but you are not returning out of it. So really your TRUE or FALSE is just going to be based on the last character in the name. If the player has a name full of illegal characters except for the last character, then all will seem fine.
From rereading your original post I am guessing that you want a line of text in the conversation to show up If the player has any illegal characters in the name?

So really Lightfoots script should work simply by reversing the TRUE and FALSE he had there...pretty sure.
But maybe like this (i also added a bit to check if the first character is a space):

int StartingConditional()
{
    object oPC = GetPCSpeaker();
    //If the speaker is not a player, don't show line.
    if(!GetIsPC(oPC)) return FALSE;
    string sName = GetName(oPC);
    //If the character has no name, show line.
    if (sName == "") return TRUE;
    //If the first character in name is a space, show line.
    if (GetStringLeft(sName, 1) == " ") return TRUE;

    string sValidCharacters = "1234567890 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+-=[{]};:/?|";
    int nLength = GetStringLength(sName);
    int nCount;

    for(nCount=0;nCount<nLength;nCount++)
    {
        //If an invalid character is found in the name, show the line.
        if (FindSubString(sValidCharacters, GetSubString(sName, nCount, 1)) == -1) return TRUE;
    }
    //If all the characters in the player name are OK, then don't show the line.
    return FALSE;
}

And on a side note...I'm not sure how the "" character will react since it is used for things such as n in a string..so it might not count it as a legal character...don't quote me on that though.


I don't know which characters are bad for Databases, any clue?
Thank you for your added functions I can actually use a couple of those.  =D
               
               

               


                     Modifié par _Guile, 13 avril 2011 - 11:14 .
                     
                  


            

Legacy_leo_x

  • Sr. Member
  • ****
  • Posts: 403
  • Karma: +0/-0
Need String Test Scripting Help.
« Reply #10 on: April 14, 2011, 04:26:54 pm »


               Just passin' through and saw this... '<img'>  what i did was store the characters that i didn't want in player names in a string variable on the module via the toolset...  grab the string use it with findsubstring to make sure there aren't illegal characters in the name you're validating.  the other suggestions would disallow all the ascii codes, etc, which i recall you being a fan of.

what is bad for the DB is probably DB specific... you don't want ' or " in name that's going into mysql.  the normal NWNX stuff converts ' to ~, which is why you need to disallow tilde.  Logins & Names with / are a problem for character deleters.

something like this:
int StartingConditional()
{
  string sInvalid= GetLocalString(GetModule(), "ILLEGAL_CHARACTERS");
  sName = GetName(GetPCSpeaker());
  if (sName == "") return FALSE;
  int nLength = GetStringLength(sInvalid);
  int nCount;
 
for(nCount=0;nCount<nLength;nCount++)
  {
    if( FindSubString(sName, GetSubString(sInvalid,nCount,1)) !=-1) return FALSE;
  }
  return TRUE;
}

HTH
               
               

               


                     Modifié par pope_leo, 14 avril 2011 - 03:49 .
                     
                  


            

Legacy__Guile

  • Hero Member
  • *****
  • Posts: 1308
  • Karma: +0/-0
Need String Test Scripting Help.
« Reply #11 on: April 17, 2011, 10:46:01 pm »


               Great idea leo. =D

Brilliant!