Advertisement
Guest User

Untitled

a guest
Nov 4th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using GrandTheftMultiplayer.Server.API;
  7. using GrandTheftMultiplayer.Server.Elements;
  8.  
  9. namespace Gamemode
  10. {
  11.     public class Login : Script
  12.     {
  13.         public static Login Instance;
  14.         public Action<Client, User> OnUserLogin;
  15.  
  16.         public Login()
  17.         {
  18.             Instance = this;
  19.             API.onClientEventTrigger += API_onClientEventTrigger;
  20.         }
  21.  
  22.         private void API_onClientEventTrigger(Client sender, string eventName, params object[] arguments)
  23.         {
  24.             if (eventName == "CheckUser")
  25.             {
  26.                 // This will return which button was clicked. Since above in ShowDialog(),
  27.                 // we declared 3 buttons to be displayed, then it will return either
  28.                 // 0, 1, or 2 depending on which button was clicked
  29.                 int button_clicked = Convert.ToInt32(arguments[0]);
  30.  
  31.                 // This will return the string that was inputted into the input box
  32.                 string input_text = arguments[1].ToString();
  33.  
  34.                 if (button_clicked == 0) // Login
  35.                 {
  36.                     API.consoleOutput(input_text);
  37.                     ShowPasswordSelection(sender, input_text);
  38.                     return;
  39.                 }
  40.                 if (button_clicked == 1) // Register
  41.                 {
  42.                     API.consoleOutput(input_text);
  43.                     ShowRegistrationPassword(sender, input_text);
  44.                     return;
  45.                 }
  46.                 return;
  47.             }
  48.             if (eventName == "CheckPassword")
  49.             {
  50.                 int button_clicked = Convert.ToInt32(arguments[0]);
  51.                 string input_text = arguments[1].ToString();
  52.  
  53.                 if (button_clicked == 0)
  54.                 {
  55.                     API.consoleOutput(input_text);
  56.                     CheckPassword(sender, input_text);
  57.                     return;
  58.                 }
  59.             }
  60.  
  61.             if (eventName == "UserRegistration")
  62.             {
  63.                 int button_clicked = Convert.ToInt32(arguments[0]);
  64.                 string input_text = arguments[1].ToString();
  65.  
  66.                 if (button_clicked == 0)
  67.                 {
  68.                     API.consoleOutput(input_text);
  69.                     string username = sender.getData("username");
  70.                     StartRegistration(sender, username, input_text);
  71.                     return;
  72.                 }
  73.             }
  74.         }
  75.  
  76.         public static void ShowLogin(Client player)
  77.         {
  78.             DBXHandler.Instance.ShowDialog(player, "dbx_text_input", "CheckUser", "Login", "Welcome to Blaine County Roleplay. \nPlease enter your Username into the box below.\nIf you have an account already choose Login, otherwise choose Register.", 2, "Login", "Register");
  79.            
  80.         }
  81.  
  82.         private void ShowPasswordSelection(Client player, string username)
  83.         {
  84.             player.setData("username", username);
  85.             DBXHandler.Instance.ShowDialog(player, "dbx_text_input", "CheckPassword", "Login", "Enter your Password for " + username, 1, "Login", true);
  86.         }
  87.  
  88.         private void ShowRegistrationPassword(Client player, string username)
  89.         {
  90.             if (User.GetUser(username, false) != null)
  91.             {
  92.                 player.sendChatMessage("~r~This username is already in use");
  93.                 ShowLogin(player);
  94.             }
  95.             player.setData("username", username);
  96.             DBXHandler.Instance.ShowDialog(player, "dbx_text_input", "UserRegistration", "Registration", "Enter your Password for " + username, 1, "Register", true);
  97.         }
  98.  
  99.         private void CheckPassword(Client player, string password)
  100.         {
  101.             var username = player.getData("username");
  102.             var pass = password.Replace(username, "");
  103.             API.consoleOutput(pass);
  104.  
  105.             User user = User.GetUser(username);
  106.             if (user == null)
  107.             {
  108.                 player.sendChatMessage("~r~Invalid username or password");
  109.                 ShowLogin(player);
  110.             }
  111.  
  112.             if (API.verifyPasswordHashBCrypt(pass, user.Hash) == false)
  113.             {
  114.                 player.sendChatMessage("~r~Invalid username or password");
  115.                 ShowLogin(player);
  116.             }
  117.             player.resetData("username");
  118.             LoginUser(player, user);
  119.         }
  120.  
  121.         private void StartRegistration(Client player, string username, string password)
  122.         {
  123.             player.resetData("username");
  124.             var pass = password.Replace(username, "");
  125.             API.consoleOutput(pass);
  126.             RegisterNewUser(player, username, pass);
  127.         }
  128.  
  129.         private void RegisterNewUser(Client player, string username, string password)
  130.         {
  131.             User user = User.CreateNewUser(username, password);
  132.  
  133.             player.SendInfoMessage("You've ~g~sucessfully ~w~created your account. Please login.");
  134.             ShowLogin(player);
  135.         }
  136.  
  137.         private void LoginUser(Client player, User user)
  138.         {
  139.             player.setData("USER", user);
  140.             if (OnUserLogin != null)
  141.             {
  142.                 player.SetLoggedIn(true);
  143.                 OnUserLogin(player, user);
  144.             }
  145.  
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement