Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using GrandTheftMultiplayer.Server.API;
- using GrandTheftMultiplayer.Server.Elements;
- namespace Gamemode
- {
- public class Login : Script
- {
- public static Login Instance;
- public Action<Client, User> OnUserLogin;
- public Login()
- {
- Instance = this;
- API.onClientEventTrigger += API_onClientEventTrigger;
- }
- private void API_onClientEventTrigger(Client sender, string eventName, params object[] arguments)
- {
- if (eventName == "CheckUser")
- {
- // This will return which button was clicked. Since above in ShowDialog(),
- // we declared 3 buttons to be displayed, then it will return either
- // 0, 1, or 2 depending on which button was clicked
- int button_clicked = Convert.ToInt32(arguments[0]);
- // This will return the string that was inputted into the input box
- string input_text = arguments[1].ToString();
- if (button_clicked == 0) // Login
- {
- API.consoleOutput(input_text);
- ShowPasswordSelection(sender, input_text);
- return;
- }
- if (button_clicked == 1) // Register
- {
- API.consoleOutput(input_text);
- ShowRegistrationPassword(sender, input_text);
- return;
- }
- return;
- }
- if (eventName == "CheckPassword")
- {
- int button_clicked = Convert.ToInt32(arguments[0]);
- string input_text = arguments[1].ToString();
- if (button_clicked == 0)
- {
- API.consoleOutput(input_text);
- CheckPassword(sender, input_text);
- return;
- }
- }
- if (eventName == "UserRegistration")
- {
- int button_clicked = Convert.ToInt32(arguments[0]);
- string input_text = arguments[1].ToString();
- if (button_clicked == 0)
- {
- API.consoleOutput(input_text);
- string username = sender.getData("username");
- StartRegistration(sender, username, input_text);
- return;
- }
- }
- }
- public static void ShowLogin(Client player)
- {
- 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");
- }
- private void ShowPasswordSelection(Client player, string username)
- {
- player.setData("username", username);
- DBXHandler.Instance.ShowDialog(player, "dbx_text_input", "CheckPassword", "Login", "Enter your Password for " + username, 1, "Login", true);
- }
- private void ShowRegistrationPassword(Client player, string username)
- {
- if (User.GetUser(username, false) != null)
- {
- player.sendChatMessage("~r~This username is already in use");
- ShowLogin(player);
- }
- player.setData("username", username);
- DBXHandler.Instance.ShowDialog(player, "dbx_text_input", "UserRegistration", "Registration", "Enter your Password for " + username, 1, "Register", true);
- }
- private void CheckPassword(Client player, string password)
- {
- var username = player.getData("username");
- var pass = password.Replace(username, "");
- API.consoleOutput(pass);
- User user = User.GetUser(username);
- if (user == null)
- {
- player.sendChatMessage("~r~Invalid username or password");
- ShowLogin(player);
- }
- if (API.verifyPasswordHashBCrypt(pass, user.Hash) == false)
- {
- player.sendChatMessage("~r~Invalid username or password");
- ShowLogin(player);
- }
- player.resetData("username");
- LoginUser(player, user);
- }
- private void StartRegistration(Client player, string username, string password)
- {
- player.resetData("username");
- var pass = password.Replace(username, "");
- API.consoleOutput(pass);
- RegisterNewUser(player, username, pass);
- }
- private void RegisterNewUser(Client player, string username, string password)
- {
- User user = User.CreateNewUser(username, password);
- player.SendInfoMessage("You've ~g~sucessfully ~w~created your account. Please login.");
- ShowLogin(player);
- }
- private void LoginUser(Client player, User user)
- {
- player.setData("USER", user);
- if (OnUserLogin != null)
- {
- player.SetLoggedIn(true);
- OnUserLogin(player, user);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement