Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

bad words block help

Discussion in 'Scripting' started by gringofxs, Dec 30, 2015.

  1. gringofxs

    gringofxs

    Joined:
    Oct 14, 2012
    Posts:
    240
    I have a login system to register the user in mysql, is it possible to block bad name and words to register?
     
  2. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    It's easy to block the words. The simple way would be to just made a list of strings and check through them:

    Code (CSharp):
    1.     public class CheckWords : MonoBehaviour
    2.     {
    3.         [SerializeField] private string[] naughtyWords;
    4.  
    5.         public bool VerifyCleanWord(string word)
    6.         {
    7.             for (int i = 0; i < naughtyWords.Length; i++)
    8.             {
    9.                 if (word.Contains(naughtyWords[i]))
    10.                 {
    11.                     return false;
    12.                 }
    13.             }
    14.             return true;
    15.         }
    16.     }
    To really protect it in detail you'd probably need to use Regular Expressions. It might be overkill, but if you reach a state where you need more detail on the words, use Regex. Eg:

    Code (CSharp):
    1. using System.IO;
    2. using System.Text.RegularExpressions;
    3.  
    4.     public class CheckWords : MonoBehaviour
    5.     {
    6.         [SerializeField] string[] words;
    7.         private string wordChecker = String.Empty;
    8.  
    9.         private void Awake()
    10.         {
    11.             for(int i=0; i < words.Length; i++)
    12.             {
    13.                 wordChecker += words[i];
    14.  
    15.                 if (i < (words.Length - 1))
    16.                 {
    17.                     wordChecker += "|";
    18.                 }
    19.             }
    20.         }
    21.  
    22.         public bool VerifyClean(string word)
    23.         {
    24.             return !Regex.IsMatch(word, wordChecker);
    25.         }
    26.     }
     
  3. gringofxs

    gringofxs

    Joined:
    Oct 14, 2012
    Posts:
    240
    where i put this code in a new C# ?
     
  4. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    The CheckWords class would be a new C#. You'd call that script against the username they're trying to create, and create if it return true, otherwise you'd ask them to use a different username, without rude words.
     
  5. gringofxs

    gringofxs

    Joined:
    Oct 14, 2012
    Posts:
    240
    this is my register and login script. Can i use it with it.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class LoginSystem : MonoBehaviour {
    6.  
    7.     public enum lMode{login,register};
    8.     public lMode LoginMode;
    9.  
    10.     public GUISkin skin;
    11.     //login
    12.     private string user = "";
    13.     private string pass = "";
    14.     private int    boolstatus;
    15.     //register
    16.     private string name = "";
    17.     private string password = "";
    18.     private string email = "";
    19.  
    20.     private string Log;
    21.     public Texture LogTexture;
    22.  
    23.     public string sceneMap;
    24.     [System.Serializable]
    25.     public class LoginParameters{
    26.         public Text LoginString;
    27.         public Text PassString;
    28.         public Toggle Remember;
    29.         public InputField UserRemember;
    30.         public InputField PassRemember;
    31.     }
    32.     [System.Serializable]
    33.     public class RegisterParameters{
    34.         public Text UserString;
    35.         public Text PassString;
    36.         public Text EmailString;
    37.     }
    38.  
    39.     public LoginParameters LP;
    40.     public RegisterParameters RP;
    41.  
    42.     public static int score;
    43.     public static string userName;
    44.  
    45.     private string[] ListStrings;
    46.  
    47.     public GameObject LoginM;
    48.     public GameObject RegisM;
    49.  
    50.     public Text logText;
    51.         public AudioClip Toque;
    52.       public AudioSource Source;
    53.     void Start () {
    54.         boolstatus = PlayerPrefs.GetInt("bs");
    55.         if (boolstatus == 1) {
    56.             LP.Remember.isOn = true;
    57.             print(PlayerPrefs.GetString("login"));
    58.             LP.UserRemember.text = PlayerPrefs.GetString ("login");
    59.             LP.PassRemember.text = PlayerPrefs.GetString ("pass");
    60.         }else{
    61.             LP.Remember.isOn = false;
    62.         }
    63.     }
    64.  
    65.     void Update () {
    66.         logText.text = Log;
    67.         if (LoginMode == lMode.login) {
    68.             LoginM.SetActive(true);
    69.             RegisM.SetActive(false);
    70.         }else if(LoginMode == lMode.register){
    71.             LoginM.SetActive(false);
    72.             RegisM.SetActive(true);
    73.         }
    74.         if (LP.Remember.isOn == true) {
    75.             PlayerPrefs.SetString("login",LP.LoginString.text);
    76.             PlayerPrefs.SetString("pass",LP.PassString.text);
    77.             boolstatus = 1;
    78.             PlayerPrefs.SetInt("bs",boolstatus);
    79.         }else{
    80.             boolstatus = 0;
    81.             PlayerPrefs.SetInt("bs",boolstatus);
    82.         }
    83.      
    84.     }
    85.     void OnGUI(){
    86.         GUI.skin = skin;
    87.     }
    88.     void RequestRegister(){
    89.         name  = RP.UserString.text;
    90.         email = RP.EmailString.text;
    91.         password = RP.PassString.text;
    92.         if (name.Length < 1) {
    93.             Log = "Preencha o campo do Usuáio.";
    94.         }else{
    95.             if(email.Length < 1){
    96.                 Log = "Preencha o campo do E-Mail.";
    97.             }else{
    98.                 if(password.Length < 1){
    99.                     Log = "Preencha o campo da Senha.";
    100.                 }else{
    101.                     WWWForm register = new WWWForm();
    102.                     register.AddField("user",name);
    103.                     register.AddField("pass",password);
    104.                     register.AddField("email",email);
    105.                     WWW w = new WWW("http://nf.php",register);
    106.                     StartCoroutine(RequestRegisterInformations(w));
    107.                 }
    108.             }
    109.         }
    110.     }
    111.     void RequestLogin(){
    112.         user = LP.LoginString.text;
    113.         pass = LP.PassString.text;
    114.         Log = "";
    115.         if (user.Length < 1) {
    116.             Log = "Preencha o campo do usuario";
    117.         }else{
    118.             if(pass.Length < 1){
    119.                 Log = "Preencha o campo da senha";
    120.             }else{
    121.                 WWWForm login = new WWWForm();
    122.                 login.AddField("user",user);
    123.                 login.AddField("pass",pass);
    124.                 WWW w = new WWW("http://nf.com/Login.php",login);
    125.                 StartCoroutine(RequestLoginInformations(w));
    126.             }
    127.         }
    128.     }
    129.     IEnumerator RequestLoginInformations(WWW w){
    130.         yield return w;
    131.         if (w.error == null) {
    132.             Log = w.text;
    133.             ListStrings = w.text.Split(';');
    134.             if(ListStrings[0] == "Bem vindo"){
    135.                 Log = "Seja " + ListStrings[0] + " " + ListStrings[1] +" ";
    136.                 userName = ListStrings[1];
    137.             }
    138.             if(ListStrings[0] == "Bem vindo"){
    139.                Source.PlayOneShot(Toque,1.0f);
    140.                 yield return new WaitForSeconds(0);
    141.                 Application.LoadLevel(sceneMap);
    142.             }
    143.         }
    144.     }
    145.     IEnumerator RequestRegisterInformations(WWW w){
    146.         yield return w;
    147.         if (w.error == null) {
    148.             Log = w.text;
    149.             if(Log == "Conta criada com sucesso!"){
    150.      Source.PlayOneShot(Toque,1.0f);
    151.                 LP.LoginString.text = RP.UserString.text;
    152.                 LP.PassString.text = RP.PassString.text;
    153.                 RP.UserString.text = "";
    154.                 RP.EmailString.text = "";
    155.                 RP.PassString.text = "";
    156.                 LoginMode = lMode.login;
    157.             }
    158.         }
    159.     }
    160.     void RegisterMode(){
    161.         LoginMode = lMode.register;
    162.     }
    163.     void LoginMod(){
    164.         LoginMode = lMode.login;
    165.     }
    166. }
    167.  
     
  6. Kamil-Says

    Kamil-Says

    Joined:
    Jun 30, 2014
    Posts:
    154
    put it on button click reg compare typped name with the black list
     
  7. Mutur01

    Mutur01

    Joined:
    Dec 28, 2015
    Posts:
    24
    I deleted my post because it was incomplete, I forgot to check bad words into the name, instead only the name. So use the function of the other guy in your code, just as another requirement like the lenght of the name.

    I think he doesn't need to create another script, he can just add the function to that class and call it when neccesary.
     
    Nigey likes this.
  8. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    What Mutur said :)
     
  9. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,376
    I would suggest using a hashtable like 'System.Collections.Generic.HashSet<string>' for faster lookup. Then segment the incoming 'username/word' into its possible words and test those. It'd be much faster for large lists.

    Or rather, because they already have a mysql database, have a table in their of all bad words, and perform a lookup based on that.

    Or interestingly enough you could use google:
    http://thenextweb.com/google/2011/08/17/google-inadvertently-creates-a-profanity-api/#gref
    Although really, this only does whole word testing...

    Really all of these are naive profanity filters, if you google about implementing profanity filters a robust algorithm can get very complicated (avoid mentioning unity, that'll just limit your results... you can port most all implementations relatively easily. Especially if you find a C# implementation).
     
  10. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    For my own knowledge.. Would you suggest against using Regex for finding patterns in strings? Is it slow?
     
  11. AssemblyBandit

    AssemblyBandit

    Joined:
    Dec 12, 2015
    Posts:
    12
    Maybe have a table and pattern check for: Fvck a55 shi7 b1tch 0h y3ah h33 h33