Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.99 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. using System.Collections;
  4.  
  5. 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
  6.  
  7. using System;
  8.  
  9. using System.Text;
  10.  
  11.  
  12.  
  13. public class nTrol: MonoBehaviour
  14.  
  15. {
  16.  
  17.     public String Username;
  18.  
  19.     public String Password;
  20.  
  21.     public String tword;
  22.  
  23.     public int[] Data = new int[40];
  24.  
  25.     public String tempData;
  26.  
  27.     public bool loginSuccess;
  28.  
  29.    
  30.  
  31.     bool login()
  32.  
  33.     {
  34.  
  35.         string connectionString = "server=127.0.0.1;userid=user;password=pass;database=thebro"; //Command needed for the server to connect to the mysql server
  36.  
  37.         mClient.MySqlConnection myConnection = new mClient.MySqlConnection(connectionString); //Instantiates a new mysqlconnection object that will take in the connection parameters above
  38.  
  39.         mClient.MySqlCommand command = new mClient.MySqlCommand("SELECT Password FROM account WHERE Username IN ('" + Username + "')", myConnection); //Mysql command that gets the players password to check based on the username
  40.  
  41.         //Reads the mysql data we just requested
  42.  
  43.         mClient.MySqlDataReader reader;
  44.  
  45.         myConnection.Open();
  46.  
  47.         reader = command.ExecuteReader();
  48.  
  49.            
  50.  
  51.         while (reader.Read())
  52.  
  53.         {
  54.  
  55.             for (int i = 0; i < reader.FieldCount; i++)
  56.  
  57.             {
  58.  
  59.                 tPassword = reader.GetValue(i).ToString()+ ""; //Set a temp password for the server to store the password we got back from the server
  60.  
  61.             }
  62.  
  63.         }
  64.  
  65.            
  66.  
  67.         myConnection.Close();
  68.  
  69.            
  70.  
  71.         if (tword == tempPad) //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
  72.  
  73.         {
  74.  
  75.             getClass();
  76.  
  77.             myConnection.Close();
  78.  
  79.             return true;
  80.  
  81.         }
  82.  
  83.            
  84.  
  85.         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
  86.  
  87.         {
  88.  
  89.             rname = "";
  90.  
  91.             assword = "";
  92.  
  93.             myConnection.Close();
  94.  
  95.             return false;
  96.  
  97.         }  
  98.  
  99.     }
  100.  
  101.  
  102.  
  103.     void OnPlayerDisconected(NetworkPlayer player)
  104.  
  105.     {
  106.  
  107.         Debug.Log("Clean up after player " + player);
  108.  
  109.         Network.RemoveRPCs(player);
  110.  
  111.         Network.DestroyPlayerObjects(player);
  112.  
  113.     }
  114.  
  115.  
  116.  
  117.     void CloseAllConnections()
  118.  
  119.     {
  120.  
  121.         Network.maxConnections = -1;
  122.  
  123.     }
  124.  
  125.  
  126.  
  127.     void noNewConnections()
  128.  
  129.     {
  130.  
  131.         Network.maxConnections = 0;
  132.  
  133.     }
  134.  
  135.    
  136.  
  137.     //Check to see if the username already exist
  138.  
  139.     bool ch(String name)
  140.  
  141.     {
  142.  
  143.         string tUseTest = "";
  144.  
  145.         string connectring = "server=127.0.0.1;userid=user;password=pass;database=thebro";
  146.  
  147.         mClient.MySqlConnection myConnection = new mClient.MySqlConnection(connectionString);
  148.  
  149.         mClient.MySqlCommand command = new mClient.MySqlCommand("SELECT Username FROM account WHERE Username IN ('" + n + "')", myConnection);
  150.  
  151.         mClient.MySqlDataReader reader;
  152.  
  153.         myConnection.Open();
  154.  
  155.         reader = command.ExecuteReader();
  156.  
  157.            
  158.  
  159.         while (reader.Read())
  160.  
  161.         {
  162.  
  163.             for (int i = 0; i < reader.FieldCount; i++)
  164.  
  165.             {
  166.  
  167.                 tUsernameTest = reader.GetValue(i).ToString()+ "";
  168.  
  169.             }
  170.  
  171.         }
  172.  
  173.            
  174.  
  175.         myConnection.Close();
  176.  
  177.            
  178.  
  179.         if (tUsTest == na)
  180.  
  181.         {
  182.  
  183.             myConnection.Close();
  184.  
  185.             return true;
  186.  
  187.         }
  188.  
  189.            
  190.  
  191.         else
  192.  
  193.         {
  194.  
  195.             myConnection.Close();
  196.  
  197.             return false;
  198.  
  199.         }  
  200.  
  201.     }
  202.  
  203.    
  204.  
  205.     void getClass()
  206.  
  207.     {
  208.  
  209.         string connectionString = "server=127.0.0.1;userid=user;password=pass;database=thebro";
  210.  
  211.         mClient.MySqlConnection myConnection = new mClient.MySqlConnection(connectionString);
  212.  
  213.         mClient.MySqlCommand commandStat = new mClient.MySqlCommand("SELECT Lv,Class,UserID FROM account WHERE Username IN ('" + tempUse + "')", myConnection);
  214.  
  215.         mClient.MySqlDataReader readerStat;
  216.  
  217.         myConnection.Open();
  218.  
  219.         readerStat = commandStat.ExecuteReader();
  220.  
  221.        
  222.  
  223.         while (readerStat.Read())
  224.  
  225.         {
  226.  
  227.             for (int i = 0; i < readerStat.FieldCount; i++)
  228.  
  229.             {
  230.  
  231.                 tempData = readerStat.GetValue(i).ToString()+ " ";
  232.  
  233.                 Data[i] = Convert.ToInt32(tempData);
  234.  
  235.             }
  236.  
  237.         }
  238.  
  239.         tempUame = "";
  240.  
  241.         tempPord = "";
  242.  
  243.         myConnection.Close();
  244.  
  245.     }
  246.  
  247.    
  248.  
  249.     //Creates a new account in the database
  250.  
  251.     void newAccountServer(String ne, String ps)
  252.  
  253.     {
  254. pass;database=thebro"; //String to connect to the server
  255.  
  256.         mClient.MySqlConnection myConnection = new mClient.MySqlConnection(connectionString);
  257.  
  258.         mClient.MySqlCommand commandStat = new mClient.MySqlCommand("INSERT INTO account (Username,Password) VALUES('" + e + "','" + s + "')", myConnection);
  259.  
  260.         myConnection.Open();
  261.  
  262.         commandStat.ExecuteNonQuery();
  263.  
  264.         myConnection.Close();
  265.  
  266.     }
  267.  
  268.    
  269.  
  270.     //Method to be called to request a login from the client
  271.  
  272.     void startLogin(String name, String password)
  273.  
  274.     {
  275.  
  276.         networkView.RPC("remot", RPCMode.Server, networkView.viewID, na, passd);
  277.  
  278.     }
  279.  
  280.    
  281.  
  282.     void stare(String name, String password)
  283.  
  284.     {
  285.  
  286.         networkView.RPC("newAt", RPCMode.Server, networkView.viewID, na, passw);
  287.  
  288.         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
  289.  
  290.     }
  291.  
  292.    
  293.  
  294.     [RPC]
  295.  
  296.     void remotin(NetworkViewID teD, String loame, String loginord) //The message received by the server to log the player in
  297.  
  298.     {
  299.  
  300.         tempUame = logime;
  301.  
  302.         tempPaord = logissword;
  303.  
  304.         logccess = login();
  305.  
  306.         for (int d=0;d<40;d++)
  307.  
  308.         {
  309.  
  310.             networkView.RPC("recgin", RPCMode.Others, d, Data[d],pID, logcess);
  311.  
  312.         }
  313.  
  314.     }
  315.  
  316.    
  317.  
  318.     [RPC]
  319.  
  320.     void receilogin(int d, int data, NetworkViewID tID, bool Suess) //This is what the client receives to be added to the player
  321.  
  322.     {
  323.  
  324.         if (networkView.viewID == tempID && Success == true)
  325.  
  326.         {
  327.  
  328.             Data[d] = data;
  329.  
  330.         }
  331.  
  332.        
  333.  
  334.         if (Success == false)
  335.  
  336.         {
  337.  
  338.             Network.Disconnect();
  339.  
  340.         }
  341.  
  342.     }
  343.  
  344.    
  345.  
  346.     [RPC]
  347.  
  348.     void newA (NetworkViewID teD, String tUern, String tssn)
  349.  
  350.     {
  351.  
  352.         if (checkMe(tUn) == false)
  353.  
  354.         {
  355.  
  356.             newAccountServer(trn, tn);
  357.  
  358.         }
  359.  
  360.     }
  361.  
  362. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement