Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using MySql.Data.MySqlClient;
- using UnityEngine.UI;
- using System.IO;
- public class DatabaseManager : MonoBehaviour {
- public string host;
- public string database;
- public string username;
- public string password;
- public Text TxtState;
- MySqlConnection con;
- public InputField IfLogin;
- public InputField IfPassword;
- public Text TxtLogin;
- struct _Player
- {
- public int ID;
- public string Pseudo;
- public string password;
- public int xp;
- }
- _Player Player;
- public InputField IfXp;
- // Use this for initialization
- void ConnectDataBase ()
- {
- string constr = "Server=" + host + ";DATABASE=" + database + ";User ID=" + username + ";Password=" + password + ";Pooling=true;Charset=utf8;"; //ligne de commande a envoyé a MySql
- try
- {
- con = new MySqlConnection(constr);
- con.Open();
- TxtState.text = con.State.ToString();
- }
- catch(IOException Ex)//IOExeption affiche l'erreur
- {
- TxtState.text = Ex.ToString();
- }
- }
- // Update is called once per frame
- void Update ()
- {
- }
- void OnApplicationQuit()
- {
- Debug.Log("Shutdown Connexion");
- if(con != null && con.State.ToString() != "Closed")
- {
- con.Close ();
- }
- }
- public void Register()
- {
- ConnectDataBase();//appelle la connexion
- bool Exist = false;
- MySqlCommand commandsql = new MySqlCommand("SELECT pseudo FROM users WHERE pseudo ='" + IfLogin.text + "'", con);
- MySqlDataReader MyReader = commandsql.ExecuteReader();
- while(MyReader.Read ())
- {
- if(MyReader["pseudo"].ToString() != "")
- {
- TxtLogin.text = "Pseudo Exist";
- Exist = true;
- }
- }
- MyReader.Close();
- if(!Exist)
- {
- string command = "INSERT INTO users VALUES (default,'" + IfLogin.text + "','" + IfPassword.text + "','')";//edit l'info
- MySqlCommand cmd = new MySqlCommand(command, con);//ajout de la ligne a MySql
- try
- {
- cmd.ExecuteReader();
- TxtLogin.text = "Register succesfull";
- }
- catch (IOException EX)
- {
- TxtState.text = EX.ToString();
- }
- cmd.Dispose();//libere la connexion
- con.Close();//
- }
- }
- public void Login ()
- {
- ConnectDataBase();//connexion database
- string pass = null;
- try
- {
- MySqlCommand commandsql = new MySqlCommand("SELECT * FROM users WHERE pseudo = '" + IfLogin.text + "'", con);
- MySqlDataReader MyReader = commandsql.ExecuteReader();//cree un reader et on l'execute en lui donnant la ligne command.
- while (MyReader.Read())
- {
- pass = MyReader["password"].ToString();
- if (pass == IfPassword.text)// si c'est le bon password prend les info du compte.
- {
- Player.ID = (int)MyReader["ID"];
- Player.Pseudo = MyReader["pseudo"].ToString();
- Player.password = MyReader["password"].ToString();
- Player.xp = (int)MyReader["xp"];
- TxtLogin.text = "Your ID " + Player.ID + System.Environment.NewLine + "Votre xp : " + Player.xp;//affiche les info a l'ecran.
- }
- else
- {
- TxtLogin.text = "Invalid Pseudo/password";
- }
- }
- if(pass == null)
- {
- TxtLogin.text = "No Existing Account";
- }
- MyReader.Close();
- }
- catch(IOException Ex)
- {
- TxtState.text = Ex.ToString();
- }
- con.Close();
- }
- public void SetXp()
- {
- ConnectDataBase();
- string command = "UPDATE users SET xp='" + IfXp.text + "' WHERE pseudo='" + Player.Pseudo + "';";
- MySqlCommand cmd = new MySqlCommand(command,con);
- try
- {
- cmd.ExecuteReader();
- TxtLogin.text = "UPDATE Succesfull";
- }
- catch (IOException Ex)
- {
- TxtState.text = Ex.ToString();
- }
- cmd.Dispose ();
- con.Close ();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement