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 { } } }
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"); }
/// <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(); } }