Advertisement
Guest User

Untitled

a guest
Mar 9th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.32 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Xml;
  4. using System.Collections.Generic;
  5.  
  6. namespace Server
  7. {
  8.     public class AccountDatabase
  9.     {
  10.         public string path = Environment.CurrentDirectory + "/database/accounts/";
  11.  
  12.         public List<Account> accounts = new List<Account>();
  13.  
  14.         public static void createAccount(int accountID, string username, string password) {
  15.             if (accountExists(accountID)) {
  16.                 Debug.Log(TAG.INFO, "Account already exists");
  17.                 return;
  18.             }
  19.  
  20.             Globals.ad.accounts.Add(new Account() { Username = username, AccountID = accountID, Password = password });
  21.  
  22.             using (XmlWriter w = XmlWriter.Create(path + username + ".xml")) {
  23.                 w.WriteStartDocument ();
  24.                 w.WriteStartElement("account");
  25.                 w.WriteElementString("userName", username);
  26.                 w.WriteElementString("accountID", accountID);
  27.                 w.WriteElementString("password", password);
  28.                 w.WriteEndElement();
  29.                 w.WriteEndDocument ();
  30.                 w.Flush();
  31.             }
  32.  
  33.             Debug.Log(TAG.DEBUG, "Created new account with id: " + accountID);
  34.         }
  35.        
  36.         public static bool login(int index, string userName, string password) {
  37.             if (File.Exists (path + userName + ".xml")) {
  38.                 Debug.Log (TAG.INFO, "Account not found!");
  39.                 //Send packet for action failed
  40.                 return;
  41.             }
  42.  
  43.             string u, p, id;
  44.             id = u = p = null;
  45.  
  46.             using (XmlReader r = XmlReader.Create(Path + userName + ".xml")) {
  47.                 while (r.Read()) {
  48.                     if (r.IsStartElement ()) {
  49.                         if (r.Name.ToString () == "accountID")
  50.                             id = r.ReadContentAsString ();
  51.                        
  52.                         if (r.Name.ToString () == "userName")
  53.                             u = r.ReadContentAsString ();
  54.  
  55.                         if (r.Name.ToString () == "password")
  56.                             p = r.ReadContentAsString ();
  57.                     }
  58.                 }
  59.             }
  60.  
  61.             if (u != userName) {
  62.                 //Action Failed
  63.                 return;
  64.             }
  65.             if (p != password) {
  66.                 //Action Failed
  67.                 return;
  68.             }
  69.  
  70.             Globals.ad.accounts.Add (new Account { AccountID = id, Username = u, Password = p });
  71.         }
  72.  
  73.         public static bool accountExists(int accountID) {
  74.             bool final = false;
  75.  
  76.             if (File.Exists (path + accountID + ".xml")) {
  77.                 final = true;
  78.             }
  79.  
  80.             return final;
  81.         }
  82.        
  83.         public static void deleteAccount(int username) {
  84.             if (!File.Exists (path + accountID + ".xml")) {
  85.                 Debug.Log (TAG.ERROR, "Account not found!");
  86.                 return;
  87.             }
  88.                
  89.             File.Delete(path + Account + ".xml");
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement