Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using mClient = MySql.Data.MySqlClient; //Is used on every single mysql type in the script it may be taken out, it's being used to keep track of mysql types used in the script, so it's purely optional, take out or put in is your choice
- using System;
- using System.Text;
- public class netControl : MonoBehaviour
- {
- public String tempUsername;
- public String tempPassword;
- public String tPassword;
- public int[] Data = new int[40];
- public String tempData;
- public bool loginSuccess;
- bool login()
- {
- string connectionString = "server=127.0.0.1;userid=root;password=balmung;database=theworld"; //Command needed for the server to connect to the mysql server
- mClient.MySqlConnection myConnection = new mClient.MySqlConnection(connectionString); //Instantiates a new mysqlconnection object that will take in the connection parameters above
- mClient.MySqlCommand command = new mClient.MySqlCommand("SELECT Password FROM account WHERE Username IN ('" + tempUsername + "')", myConnection); //Mysql command that gets the players password to check based on the username
- //Reads the mysql data we just requested
- mClient.MySqlDataReader reader;
- myConnection.Open();
- reader = command.ExecuteReader();
- while (reader.Read())
- {
- for (int i = 0; i < reader.FieldCount; i++)
- {
- tPassword = reader.GetValue(i).ToString()+ ""; //Set a temp password for the server to store the password we got back from the server
- }
- }
- myConnection.Close();
- if (tPassword == tempPassword) //If login is a success return true and the password the player sent to the server equals the password we got back from the server
- {
- getClass();
- myConnection.Close();
- return true;
- }
- else //If login is a failure return false which is when the password we got back from the server does not match the password the player sent
- {
- tempUsername = "";
- tempPassword = "";
- myConnection.Close();
- return false;
- }
- }
- void OnPlayerDisconected(NetworkPlayer player)
- {
- Debug.Log("Clean up after player " + player);
- Network.RemoveRPCs(player);
- Network.DestroyPlayerObjects(player);
- }
- void CloseAllConnections()
- {
- Network.maxConnections = -1;
- }
- void noNewConnections()
- {
- Network.maxConnections = 0;
- }
- //Check to see if the username already exist
- bool checkMe(String name)
- {
- string tUsernameTest = "";
- string connectionString = "server=127.0.0.1;userid=root;password=balmung;database=theworld";
- mClient.MySqlConnection myConnection = new mClient.MySqlConnection(connectionString);
- mClient.MySqlCommand command = new mClient.MySqlCommand("SELECT Username FROM account WHERE Username IN ('" + name + "')", myConnection);
- mClient.MySqlDataReader reader;
- myConnection.Open();
- reader = command.ExecuteReader();
- while (reader.Read())
- {
- for (int i = 0; i < reader.FieldCount; i++)
- {
- tUsernameTest = reader.GetValue(i).ToString()+ "";
- }
- }
- myConnection.Close();
- if (tUsernameTest == name)
- {
- myConnection.Close();
- return true;
- }
- else
- {
- myConnection.Close();
- return false;
- }
- }
- void getClass()
- {
- string connectionString = "server=127.0.0.1;userid=root;password=balmung;database=theworld";
- mClient.MySqlConnection myConnection = new mClient.MySqlConnection(connectionString);
- mClient.MySqlCommand commandStat = new mClient.MySqlCommand("SELECT Lv,Class,Exp,Item1,Item2,Item3,Item4,Item5,Item6,Item7,Item8,Item9,Item10,Item11,Item12,Item13,Item14,Item15,Item16,Item17,Item18,Item19,Item20,Item21,Item22,Item23,Item24,CustMain,CustSecond,CustThird,CustFourth,Equip1,Equip2,Equip3,Equip4,Equip5,Equip6,Guild,GuildRank,UserID FROM account WHERE Username IN ('" + tempUsername + "')", myConnection);
- mClient.MySqlDataReader readerStat;
- myConnection.Open();
- readerStat = commandStat.ExecuteReader();
- while (readerStat.Read())
- {
- for (int i = 0; i < readerStat.FieldCount; i++)
- {
- tempData = readerStat.GetValue(i).ToString()+ " ";
- Data[i] = Convert.ToInt32(tempData);
- }
- }
- tempUsername = "";
- tempPassword = "";
- myConnection.Close();
- }
- //Creates a new account in the database
- void newAccountServer(String name, String pass)
- {
- string connectionString = "server=127.0.0.1;userid=root;password=balmung;database=theworld"; //String to connect to the server
- mClient.MySqlConnection myConnection = new mClient.MySqlConnection(connectionString);
- mClient.MySqlCommand commandStat = new mClient.MySqlCommand("INSERT INTO account (Username,Password) VALUES('" + name + "','" + pass + "')", myConnection);
- myConnection.Open();
- commandStat.ExecuteNonQuery();
- myConnection.Close();
- }
- //Method to be called to request a login from the client
- void startLogin(String name, String password)
- {
- networkView.RPC("remoteLogin", RPCMode.Server, networkView.viewID, name, password);
- }
- void startCreate(String name, String password)
- {
- networkView.RPC("newAccount", RPCMode.Server, networkView.viewID, name, password);
- Network.Disconnect(); //We disconnect from the server after sending this message so that we can reconnect again when we want to login, which clears up any errors that may exist by just calling Network.Connect(); a second time while still connected
- }
- [RPC]
- void remoteLogin(NetworkViewID tempID, String loginName, String loginPassword) //The message received by the server to log the player in
- {
- tempUsername = loginName;
- tempPassword = loginPassword;
- loginSuccess = login();
- for (int d=0;d<40;d++)
- {
- networkView.RPC("receiveLogin", RPCMode.Others, d, Data[d],tempID, loginSuccess);
- }
- }
- [RPC]
- void receivelogin(int d, int data, NetworkViewID tempID, bool Success) //This is what the client receives to be added to the player
- {
- if (networkView.viewID == tempID && Success == true)
- {
- Data[d] = data;
- }
- if (Success == false)
- {
- Network.Disconnect();
- }
- }
- [RPC]
- void newAccount(NetworkViewID tempID, String tUsern, String tPassn)
- {
- if (checkMe(tUsern) == false)
- {
- newAccountServer(tUsern, tPassn);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement