Advertisement
Hyluss

mySQL + Unity

Jan 5th, 2018
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.17 KB | None | 0 0
  1. using UnityEngine;
  2. using MySql.Data.MySqlClient;
  3. using System.Collections.Generic;
  4.  
  5. public class DataBaseConnection : MonoBehaviour
  6. {
  7.     //Don't know why but this script must be in main folder :-(
  8.  
  9.     [SerializeField] string localHost;
  10.     [SerializeField] string user;
  11.     [SerializeField] string password;
  12.     [SerializeField] string dateBaseName;
  13.  
  14.     MySqlConnection mySQLconnection;
  15.     MySqlCommand mySQLcommand;
  16.     MySqlDataReader mySQLreader;
  17.  
  18.     static DataBaseConnection dataBaseConnectionInstance;
  19.  
  20.     int currentLoginID;
  21.     public int CurrentLoginID { get { return currentLoginID; } }
  22.  
  23.     private void Awake()
  24.     {
  25.         DontDestroyOnLoad(this);
  26.         if (dataBaseConnectionInstance == null)
  27.         {
  28.             dataBaseConnectionInstance = this;
  29.             Mediator.Instance.Subscribe<RegisterCmd>(OnRegisterCmd);
  30.             Mediator.Instance.Subscribe<LogInCmd>(OnLogInCmd);
  31.         }
  32.         else
  33.         {
  34.             DestroyObject(gameObject);
  35.         }
  36.     }
  37.  
  38.     public static List<string> GetProfessionsName()
  39.     {
  40.         List<string> professionList = new List<string>();
  41.         if (Connect())
  42.         {
  43.             string command = "select Nazwa from Profesja";
  44.             mySQLcommand = new MySqlCommand(command, mySQLconnection);
  45.             mySQLreader = mySQLcommand.ExecuteReader();
  46.             while (mySQLreader.Read())
  47.             {
  48.                 professionList.Add(mySQLreader["Nazwa"].ToString());
  49.                 //Debug.Log(mySQLreader["Nazwa"].ToString());
  50.             }
  51.             mySQLreader.Close();
  52.             mySQLconnection.Close();
  53.         }
  54.         else
  55.         {
  56.             Debug.Log("database connection is interrupt");
  57.         }
  58.         return professionList;
  59.     }
  60.  
  61.  
  62.     void OnLogInCmd(LogInCmd cmd)
  63.     {
  64.  
  65.         string login = cmd.Login;
  66.         string password = cmd.Password;
  67.  
  68.         if (CheckExistingPlayer(login) == 1)
  69.         {
  70.             if (CheckPasswordCorrect(login, password) == 1)
  71.             {              
  72.                 int loginID = GetLoginID(login);
  73.                 if (loginID != -1)
  74.                 {                    
  75.                     ShowStatement("Loging, please wait!");
  76.                     Logged(loginID);
  77.                 }
  78.                 else
  79.                 {
  80.                     Debug.Log("database interrupt");
  81.                 }
  82.             }
  83.             else if (CheckPasswordCorrect(login, password) == 0)
  84.             {
  85.                 ShowStatement("Password is incorrect!");
  86.             }
  87.             else
  88.             {
  89.                 Debug.Log("database interrupt");
  90.             }
  91.         }
  92.         else if (CheckExistingPlayer(login) == 0)
  93.         {
  94.             ShowStatement("Player doesn't exists!");
  95.         }
  96.         else
  97.         {
  98.             Debug.Log("database interrupt");
  99.             ShowStatement("database is not connected");
  100.         }
  101.     }
  102.  
  103.     void Logged(int loginID)
  104.     {
  105.         currentLoginID = loginID;
  106.  
  107.         var loggedCmd = new LoggedCmd();
  108.         loggedCmd.LoginID = loginID;
  109.         Mediator.Instance.Publish<LoggedCmd>(loggedCmd);
  110.     }
  111.  
  112.     /// <summary>
  113.     /// <para> return -1 if database connection is interrupt </para>
  114.     /// </summary>
  115.     /// <param name="login"></param>
  116.     /// <returns></returns>
  117.     int GetLoginID(string login)
  118.     {
  119.         if (Connect())
  120.         {
  121.             string command = "SELECT ID from Uzytkownik where Login = '" + login + "'";
  122.             mySQLcommand = new MySqlCommand(command, mySQLconnection);
  123.             int getLoginID = System.Convert.ToInt32(mySQLcommand.ExecuteScalar());
  124.             mySQLconnection.Close();
  125.             return getLoginID;          
  126.         }
  127.         return -1;
  128.     }
  129.  
  130.     /// <summary>
  131.     /// <para>return -1 if database connection is interrupt</para>
  132.     /// <para>return 0 if password is incorrect</para>
  133.     /// <para>return 1 if password is correct</para>
  134.     /// </summary>
  135.     /// <param name="login"></param>
  136.     /// <param name="password"></param>
  137.     /// <returns></returns>
  138.     int CheckPasswordCorrect(string login, string password)
  139.     {
  140.         if (Connect())
  141.         {
  142.             string command = "SELECT Haslo from Uzytkownik where Login = '" + login + "'";
  143.             mySQLcommand = new MySqlCommand(command, mySQLconnection);
  144.             string getPassword = System.Convert.ToString(mySQLcommand.ExecuteScalar());
  145.             mySQLconnection.Close();
  146.             if (password == getPassword)
  147.             {
  148.                 return 1;
  149.             }
  150.             else
  151.             {
  152.                 return 0;
  153.             }
  154.         }
  155.         return -1;
  156.     }
  157.  
  158.     bool Connect()
  159.     {      
  160.         mySQLconnection = new MySqlConnection("server=" + localHost + "; user=" + user + "; database=" + dateBaseName + ";");
  161.         try
  162.         {
  163.             mySQLconnection.Open();
  164.         }
  165.         catch (System.Exception e)
  166.         {
  167.             //Debug.Log("Connection state is closed");
  168.             return false;
  169.         }
  170.         if (mySQLconnection.State == System.Data.ConnectionState.Open)
  171.         {
  172.             return true;
  173.         }
  174.         return false; //?
  175.     }
  176.  
  177.     void AddAccount(string login, string password)
  178.     {
  179.         if (CheckExistingPlayer(login) == 0)
  180.         {
  181.             if (Connect())
  182.             {
  183.                 string command = "INSERT INTO `uzytkownik` (`Login`, `Haslo`) VALUES('" + login + "', '" + password + "');";
  184.                 mySQLcommand = new MySqlCommand(command, mySQLconnection);
  185.                 mySQLcommand.ExecuteNonQuery();
  186.                 Debug.Log("New user was adding");
  187.                 ShowStatement("New user was adding");
  188.                 mySQLconnection.Close();
  189.             }
  190.             else
  191.             {
  192.                 Debug.Log("database interrupt");
  193.             }
  194.         }
  195.         else if (CheckExistingPlayer(login) == 1)
  196.         {
  197.             Debug.Log("this user can't be added");
  198.             ShowStatement("this user already exsits");
  199.         }
  200.         else
  201.         {
  202.             Debug.Log("database interrupt");
  203.             ShowStatement("database is not connected");
  204.         }      
  205.     }
  206.  
  207.     /// <summary>
  208.     /// <para> return -1 if database connection is interrupt </para>
  209.     /// <para> return 0 if login doesn't exist </para>
  210.     /// <para> return 1 if login exists </para>
  211.     /// </summary>
  212.     /// <param name="login"></param>
  213.     /// <returns></returns>
  214.     int CheckExistingPlayer(string login)
  215.     {
  216.         if (Connect())
  217.         {
  218.             string command = "SELECT Count(Login) from Uzytkownik where Login = '" + login + "'";
  219.             mySQLcommand = new MySqlCommand(command, mySQLconnection);
  220.             int number = System.Convert.ToInt32(mySQLcommand.ExecuteScalar());
  221.             mySQLconnection.Close();
  222.             if (number == 0)
  223.             {
  224.                 return 0;
  225.             }
  226.             else
  227.             {
  228.                 return 1;
  229.             }
  230.         }
  231.         else
  232.         {
  233.             return -1;
  234.         }
  235.     }
  236.  
  237.     void DeletePlayer()
  238.     {
  239.         // TODO
  240.     }
  241.  
  242.     void UpdatePlayer()
  243.     {
  244.         // TODO
  245.     }
  246.  
  247.     void OnRegisterCmd(RegisterCmd cmd)
  248.     {
  249.         AddAccount(cmd.Login, cmd.Password);
  250.     }
  251.  
  252.     void ShowStatement(string statement)
  253.     {
  254.         var cmd = new ShowStatementCmd();
  255.         cmd.Statement = statement;
  256.         Mediator.Instance.Publish<ShowStatementCmd>(cmd);
  257.     }
  258.  
  259.  
  260.     [ContextMenu("CheckPasswordCorrectTEST()")]
  261.     void CheckPasswordCorrectTEST()
  262.     {
  263.         if (CheckPasswordCorrect("Mac", "Mac") == 1)
  264.         {
  265.             Debug.Log("Password is correct");
  266.         }
  267.         if (CheckPasswordCorrect("Mac", "Mac") == 0)
  268.         {
  269.             Debug.Log("Password is incorrect");
  270.         }
  271.  
  272.         if (CheckPasswordCorrect("Mac", "Kac") == 1)
  273.         {
  274.             Debug.Log("Password is correct");
  275.         }
  276.         if (CheckPasswordCorrect("Mac", "Kac") == 0)
  277.         {
  278.             Debug.Log("Password is incorrect");
  279.         }
  280.     }
  281.  
  282.     [ContextMenu("ShowPlayers()")]
  283.     void ShowPlayers()
  284.     {
  285.         if (Connect())
  286.         {
  287.             string command = "select * from Uzytkownik";
  288.             mySQLcommand = new MySqlCommand(command, mySQLconnection);
  289.             mySQLreader = mySQLcommand.ExecuteReader();
  290.             while (mySQLreader.Read())
  291.             {
  292.                 Debug.Log(mySQLreader["ID"].ToString() + " " + mySQLreader["Login"].ToString());
  293.             }
  294.             mySQLreader.Close();
  295.             mySQLconnection.Close();
  296.         }
  297.         else
  298.         {
  299.             Debug.Log("database connection is interrupt");
  300.         }
  301.     }
  302.  
  303.     [ContextMenu("AddRandomPlayerTest()")]
  304.     void AddRandomPlayerTest()
  305.     {
  306.         string name = System.Guid.NewGuid().ToString();
  307.         name = name.Substring(0, 30); //if you want set varchar's length to 30
  308.         Debug.Log("try to add player - " + name);
  309.         AddAccount(name, "haslo");
  310.     }
  311.  
  312.     [ContextMenu("AddTheSamePlayerTest()")]
  313.     void AddTheSamePlayerTest()
  314.     {
  315.         string name = "Mac";
  316.         Debug.Log("try to add player - " + name);
  317.         AddAccount(name, "haslo");
  318.     }
  319.  
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement