示例#1
0
        static void Main(string[] args)
        {
            try
            {
                string account_name = "orior3";
                string password = "******";
                Database.Instance.Connect();
                if (Database.Instance.Connection.State != System.Data.ConnectionState.Open)
                {
                    Console.WriteLine("Failed to open Database.Instance.Connection");
                    Console.ReadLine();
                    return;
                }
                Console.WriteLine("Connected to db");

                Account account;
                if (!Account.Authorize(account_name, password, out account))
                {
                    Console.WriteLine("Failed to authorize account");
                    account = new Account(account_name, 0, 1);
                    if (!account.Create(password))
                    {
                        Console.WriteLine("Failed to create account");
                        Console.ReadLine();
                        return;
                    }
                    else
                        Console.WriteLine("Created account");
                }
                Console.WriteLine("Authorized");
                Console.WriteLine(account.ToString());
                Console.WriteLine("10 gold given and saved");
                account.Gold += 10;
                account.Save();
                Console.WriteLine(account.ToString());

                Hero heroNew = new Hero("orior", 2, 1, 2, 1);
                if (!heroNew.Create(account.Id))
                    Console.WriteLine("Failed to create hero");
                else
                    Console.WriteLine("Hero created");

                List<Hero> heroList = account.GetHeroes();
                Console.WriteLine("{0} Heroes: ", heroList.Count);
                foreach (var hero in heroList)
                {
                    Console.WriteLine(hero);
                    hero.Experience += 10;
                    hero.Level += 1;
                    hero.Save();
                }

                Console.ReadLine();
            }
            finally
            {
                try { Database.Instance.Connection.Close(); } catch { }
            }
        }
示例#2
0
 public static bool Authorize(string account_name, string password, out Account account)
 {
     account = null;
     string md5Password = GetMD5Hash(password);
     SQLiteCommand command = new SQLiteCommand(string.Format("SELECT account_id FROM account WHERE account_name='{0}' AND password='******'", account_name, md5Password), Database.Instance.Connection);
     SQLiteDataReader reader = command.ExecuteReader();
     if (!reader.HasRows)
         return false;
     reader.Read();
     int account_id = reader.GetInt32(0);
     return Load(account_id, out account);
 }
示例#3
0
        static void CommandCreateAccount()
        {
            Console.Write("Name: ");
            var name = Console.ReadLine();
            Console.Write("Password: "******"Gold: ");
            var goldString = Console.ReadLine();
            Console.Write("Gender (male/female): ");
            var gender = Console.ReadLine() == "male" ? 1 : 2;
            int gold;
            int.TryParse(goldString, out gold);

            var account = new Account(name, gold, gender);
            if (account.Create(password))
                Console.WriteLine("Account {0} created", name);
            else
                Console.WriteLine("Account already exists");
        }
示例#4
0
 public static bool Load(int id, out Account account)
 {
     account = null;
     SQLiteCommand command = new SQLiteCommand(string.Format("SELECT account_id, account_name, gold, gender FROM account WHERE account_id='{0}'", id), Database.Instance.Connection);
     SQLiteDataReader reader = command.ExecuteReader();
     if (reader.HasRows)
     {
         while (reader.Read())
         {
             var account_id = reader.GetInt32(0);
             var account_name = reader.GetString(1);
             var gold = reader.GetInt32(2);
             var gender = reader.GetInt32(3);
             account = new Account(account_name, gold, gender);
             account.Id = account_id;
             return true;
         }
     }
     return false;
 }
示例#5
0
 /// <summary>
 /// Handles the "logout" command which logs out the currently logged in user.
 /// </summary>
 static void CommandLogout()
 {
     // check that a user is even logged in
     if (currentAccount != null)
     {
         currentAccount = null;
         Console.WriteLine("Logged out.");
     }
     else
     {
         Console.ForegroundColor = ConsoleColor.Red;
         Console.WriteLine("Already logged out.");
         Console.ResetColor();
     }
 }
示例#6
0
        /// <summary>
        /// Handles the "create account" command which attempts to create a new account in the database.
        /// </summary>
        static void CommandCreateAccount()
        {
            // prompt for account name
            Console.Write("Name: ");

            // read input
            string name = Console.ReadLine();

            // check that name is valid
            while (string.IsNullOrEmpty(name))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Invalid name.");
                Console.ResetColor();
                Console.Write("Name: ");
                name = Console.ReadLine();
            }

            string password;
            string passwordConfirm;

            // prompt for password
            Console.Write("Password: "******"Invalid password.");
                Console.ResetColor();
                Console.Write("Password: "******"Confirm Password: "******"Passwords don't match.");
                Console.ResetColor();

                // prompt for password
                Console.Write("Password: "******"Invalid password.");
                    Console.ResetColor();
                    Console.Write("Password: "******"Confirm Password: "******"Gold: ");

            // read input
            string goldString = Console.ReadLine();
            int gold;

            // check that gold is numeric
            while (int.TryParse(goldString, out gold))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Invalid gold. Please choose a number.");
                Console.ResetColor();
                Console.Write("Gold: ");
                goldString = Console.ReadLine();
            }

            // prompt for gender
            Console.Write("Gender (male/female): ");

            int gender = 0;

            // check that gender is valid (1 or 2)
            while (gender == 0)
            {
                switch (Console.ReadLine().ToLower().Trim())
                {
                    case "male":
                    case "m":
                        gender = 1;
                        break;
                    case "female":
                    case "f":
                        gender = 2;
                        break;
                    default:
                        // red for errors
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Invalid gender. Please choose male or female.");
                        // reset colour
                        Console.ResetColor();
                        Console.Write("Gender (male/female): ");
                        break;
                }
            }

            // initialize account
            Account account = new Account(name, gold, gender);

            // save account to database
            if (account.Create(password))
            {
                Console.Write("Account ");
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write(name);
                Console.ResetColor();
                Console.Write(" created.");
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("An account with that name already exists");
                Console.ResetColor();
            }
        }
示例#7
0
 static void CommandLogout()
 {
     currentAccount = null;
     Console.WriteLine("Logged out");
 }
示例#8
0
        /// <summary>
        /// Validates account credentials with those stored in the database.
        /// </summary>
        /// <param name="account_name">Account name.</param>
        /// <param name="password">Account password.</param>
        /// <param name="account">Reference to the account instance.</param>
        /// <returns>False if account name and password hash don't match anything in the database, otherwise true.</returns>
        public static bool Authorize(string account_name, string password, out Account account)
        {
            account = null;

            // MD5 hash of account password
            string md5Password = GetMD5Hash(password);

            // initialize SQL statement
            SQLiteCommand command = new SQLiteCommand(string.Format("SELECT account_id FROM account WHERE account_name='{0}' AND password='******'", account_name, md5Password), Database.Instance.Connection);
            // execute SQL (retrieve row with account name and hashed password that match input)
            SQLiteDataReader reader = command.ExecuteReader();

            // false if no rows were returned
            if (!reader.HasRows)
            {
                return false;
            }

            reader.Read();
            int account_id = reader.GetInt32(0);
            return Load(account_id, out account);
        }
示例#9
0
        /// <summary>
        /// Loads an account from the database.
        /// </summary>
        /// <param name="id">ID of the account to load.</param>
        /// <param name="account">Account data.</param>
        /// <returns>True on success, otherwise false.</returns>
        public static bool Load(int id, out Account account)
        {
            account = null;

            // initialize SQL statement
            SQLiteCommand command = new SQLiteCommand(string.Format("SELECT account_id, account_name, gold, gender FROM account WHERE account_id='{0}'", id), Database.Instance.Connection);
            // execute SQL (retrieve account data where account_id matches input)
            SQLiteDataReader reader = command.ExecuteReader();

            // continue if a row was returned
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    account = new Account(reader.GetString(1), reader.GetInt32(2), reader.GetInt32(3));
                    account.Id = reader.GetInt32(0);
                    return true;
                }
            }

            return false;
        }