Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using MySql.Data.MySqlClient;
  5. using UnityEngine.UI;
  6. using System.IO;
  7.  
  8. public class DatabaseManager : MonoBehaviour {
  9.  
  10. public string host;
  11. public string database;
  12. public string username;
  13. public string password;
  14. public Text TxtState;
  15. MySqlConnection con;
  16. public InputField IfLogin;
  17. public InputField IfPassword;
  18. public Text TxtLogin;
  19.  
  20. struct _Player
  21. {
  22. public int ID;
  23. public string Pseudo;
  24. public string password;
  25. public int xp;
  26. }
  27. _Player Player;
  28.  
  29. public InputField IfXp;
  30.  
  31. // Use this for initialization
  32. void ConnectDataBase ()
  33. {
  34. string constr = "Server=" + host + ";DATABASE=" + database + ";User ID=" + username + ";Password=" + password + ";Pooling=true;Charset=utf8;"; //ligne de commande a envoyé a MySql
  35.  
  36. try
  37. {
  38. con = new MySqlConnection(constr);
  39. con.Open();
  40. TxtState.text = con.State.ToString();
  41. }
  42. catch(IOException Ex)//IOExeption affiche l'erreur
  43. {
  44. TxtState.text = Ex.ToString();
  45. }
  46. }
  47.  
  48. // Update is called once per frame
  49. void Update ()
  50. {
  51.  
  52. }
  53.  
  54. void OnApplicationQuit()
  55. {
  56. Debug.Log("Shutdown Connexion");
  57.  
  58. if(con != null && con.State.ToString() != "Closed")
  59. {
  60. con.Close ();
  61. }
  62. }
  63.  
  64. public void Register()
  65. {
  66. ConnectDataBase();//appelle la connexion
  67.  
  68. bool Exist = false;
  69.  
  70. MySqlCommand commandsql = new MySqlCommand("SELECT pseudo FROM users WHERE pseudo ='" + IfLogin.text + "'", con);
  71. MySqlDataReader MyReader = commandsql.ExecuteReader();
  72.  
  73. while(MyReader.Read ())
  74. {
  75. if(MyReader["pseudo"].ToString() != "")
  76. {
  77. TxtLogin.text = "Pseudo Exist";
  78. Exist = true;
  79. }
  80. }
  81. MyReader.Close();
  82.  
  83. if(!Exist)
  84. {
  85. string command = "INSERT INTO users VALUES (default,'" + IfLogin.text + "','" + IfPassword.text + "','')";//edit l'info
  86. MySqlCommand cmd = new MySqlCommand(command, con);//ajout de la ligne a MySql
  87.  
  88. try
  89. {
  90. cmd.ExecuteReader();
  91. TxtLogin.text = "Register succesfull";
  92. }
  93. catch (IOException EX)
  94. {
  95. TxtState.text = EX.ToString();
  96. }
  97. cmd.Dispose();//libere la connexion
  98. con.Close();//
  99. }
  100. }
  101.  
  102. public void Login ()
  103. {
  104. ConnectDataBase();//connexion database
  105. string pass = null;
  106. try
  107. {
  108. MySqlCommand commandsql = new MySqlCommand("SELECT * FROM users WHERE pseudo = '" + IfLogin.text + "'", con);
  109. MySqlDataReader MyReader = commandsql.ExecuteReader();//cree un reader et on l'execute en lui donnant la ligne command.
  110.  
  111. while (MyReader.Read())
  112. {
  113. pass = MyReader["password"].ToString();
  114.  
  115. if (pass == IfPassword.text)// si c'est le bon password prend les info du compte.
  116. {
  117. Player.ID = (int)MyReader["ID"];
  118. Player.Pseudo = MyReader["pseudo"].ToString();
  119. Player.password = MyReader["password"].ToString();
  120. Player.xp = (int)MyReader["xp"];
  121. TxtLogin.text = "Your ID " + Player.ID + System.Environment.NewLine + "Votre xp : " + Player.xp;//affiche les info a l'ecran.
  122. }
  123. else
  124. {
  125. TxtLogin.text = "Invalid Pseudo/password";
  126. }
  127. }
  128. if(pass == null)
  129. {
  130. TxtLogin.text = "No Existing Account";
  131. }
  132. MyReader.Close();
  133. }
  134. catch(IOException Ex)
  135. {
  136. TxtState.text = Ex.ToString();
  137. }
  138. con.Close();
  139. }
  140.  
  141. public void SetXp()
  142. {
  143. ConnectDataBase();
  144. string command = "UPDATE users SET xp='" + IfXp.text + "' WHERE pseudo='" + Player.Pseudo + "';";
  145. MySqlCommand cmd = new MySqlCommand(command,con);
  146.  
  147. try
  148. {
  149. cmd.ExecuteReader();
  150. TxtLogin.text = "UPDATE Succesfull";
  151. }
  152. catch (IOException Ex)
  153. {
  154. TxtState.text = Ex.ToString();
  155. }
  156. cmd.Dispose ();
  157. con.Close ();
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement