public static bool CreateAccount(string username, string password, string passwordSalt = null)
        {
            username = username.Replace(" ", "");
            foreach (Player p in _playerLibrary)
            {
                if (p.username == username)
                {
                    ConsoleEx.Error("Account creation error. Username taken: " + username);
                    return(false);
                }
            }
            Player player = new Player(username, password, passwordSalt);

            _playerLibrary.Add(player);
            ConsoleEx.Log("Account " + username + " created successfully.");
            if (autosaveEnabled)
            {
                DiskCore.Enqueue(DiskAction.UpdateAccountDatabase);
            }
            return(true);
        }
 public static string Login(string username, string password)
 {
     username = username.Replace(" ", "");
     for (int i = 0; i < _playerLibrary.Count; i++)
     {
         if (username == _playerLibrary[i].username)
         {
             if (_playerLibrary[i].CheckPassword(password))
             {
                 ConsoleEx.Log("Player " + _playerLibrary[i].username + " logged in.");
                 return(_playerLibrary[i].Login());
             }
             else
             {
                 ConsoleEx.Error("Login failed. Password mismatch.");
                 return("");
             }
         }
     }
     ConsoleEx.Error("Login failed. User not found.");
     return("");
 }
Example #3
0
        //===============================================================================
        // Account management
        //===============================================================================
        public static void SaveAccounts()
        {
            //StringBuilder output = new StringBuilder();
            MemoryStream output = new MemoryStream();

            bool         savedToTempFile = false;
            StreamWriter file;

            if (File.Exists(Settings.playersXmlPath))
            {
                savedToTempFile = true;
                file            = new StreamWriter(Settings.playersTempXmlPath);
            }
            else
            {
                file = new StreamWriter(Settings.playersXmlPath);
            }
            XmlWriterSettings ws = new XmlWriterSettings();

            ws.Indent      = true;
            ws.IndentChars = "\t";
            ws.Encoding    = Encoding.UTF8;
            XmlWriter writer = XmlWriter.Create(output, ws);

            // Generate an XML
            writer.WriteStartDocument();
            writer.WriteStartElement("root");
            foreach (Player p in Authorization.playerList)
            {
                writer.WriteStartElement("player");

                writer.WriteStartElement("username");
                writer.WriteValue(p.username);
                writer.WriteEndElement();

                writer.WriteStartElement("password");
                writer.WriteValue(p.password);
                writer.WriteEndElement();

                writer.WriteStartElement("passwordSalt");
                writer.WriteValue(p.passwordSalt);
                writer.WriteEndElement();

                writer.WriteStartElement("loginCounter");
                writer.WriteValue(p.loginCounter);
                writer.WriteEndElement();

                writer.WriteStartElement("startingShipName");
                writer.WriteValue(p.startingShipName);
                writer.WriteEndElement();

                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            output.Position = 0;
            //file.Write(output.Read);
            output.WriteTo(file.BaseStream);
            output.Close();
            output.Dispose();
            file.Close();
            if (savedToTempFile)
            {
                try
                {
                    File.Move(Settings.playersXmlPath, Settings.playersBackupXmlPath);
                    File.Move(Settings.playersTempXmlPath, Settings.playersXmlPath);
                    File.Delete(Settings.playersBackupXmlPath);
                }
                catch (Exception)
                {
                    ConsoleEx.Error("Account database update failed.");
                    return;
                }
            }
            ConsoleEx.Log("Account database updated successfully.");
            //ConsoleEx.Debug(output.ToString());
        }