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 { } } }
/// <summary> /// Handles the "create hero" command which creates a hero under the currently logged in account and saves it to the database. /// </summary> static void CommandCreateHero() { // check that user is logged in if (currentAccount == null) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Not logged in."); Console.ResetColor(); return; // exit } // prompt for hero name Console.Write("Name: "); // read input string name = Console.ReadLine(); // check that name doesn't contain numbers while (Regex.IsMatch(name, @"\d")) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Invalid name. Names cannot contain numbers."); Console.ResetColor(); Console.Write("Name: "); name = Console.ReadLine(); } while (name.Length < 3 || name.Length > 12) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Invalid name. Names must be between 3 and 12 characters inclusive."); Console.ResetColor(); Console.Write("Name: "); name = Console.ReadLine(); } // prompt for hero class Console.WriteLine("Hero Class:\n1: Wizard\n2: Witch Doctor\n3: Demon Hunter\n4: Monk\n5: Barbarian"); // read input string heroClassString = Console.ReadLine(); int heroClass; // check that input is a valid class id while (!int.TryParse(heroClassString, out heroClass) || heroClass > 5 || heroClass < 1) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Invalid class. Please choose a number."); Console.ResetColor(); Console.WriteLine("Hero Class:\n1: Wizard\n2: Witch Doctor\n3: Demon Hunter\n4: Monk\n5: Barbarian"); heroClassString = 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; } } // prompt for hero level Console.Write("Level: "); // read input string levelString = Console.ReadLine(); int level; // check that input is numeric while (!int.TryParse(levelString, out level)) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Invalid level. Please choose a number."); Console.ResetColor(); Console.Write("Level: "); levelString = Console.ReadLine(); } // prompt for hero experience Console.Write("Experience: "); // read input string experienceString = Console.ReadLine(); int experience; // check that input is numeric while (!int.TryParse(experienceString, out experience)) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Invalid experience. Please choose a number."); Console.ResetColor(); Console.Write("Experience: "); experienceString = Console.ReadLine(); } // initialize hero object Hero hero = new Hero(currentAccount.Id, name, heroClass, gender, experience, level); // create hero if (hero.Create()) { Console.Write("Hero "); Console.ForegroundColor = ConsoleColor.Green; Console.Write(name); Console.ResetColor(); Console.WriteLine(" created."); } else // hero already exists { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Hero with that name already exists"); Console.ResetColor(); } }
static void CommandCreateHero() { if (currentAccount == null) { Console.WriteLine("not logged in"); return; } Console.Write("Name: "); var name = Console.ReadLine(); // Check if the hero name contains a number if (Regex.IsMatch(name, @"\d")) { while (Regex.IsMatch(name, @"\d")) { Console.WriteLine("Invalid name - Names cannot contain a number."); Console.Write("Name: "); name = Console.ReadLine(); } } Console.WriteLine("Hero Class:\n1: Wizard\n2: Witch Doctor\n3: Demon Hunter\n4: Monk\n5: Barbarian"); var heroClassString = Console.ReadLine(); int heroClass; if (!int.TryParse(heroClassString, out heroClass)) { while (!int.TryParse(heroClassString, out heroClass)) { Console.WriteLine("Invalid class. Please choose a number."); Console.WriteLine("Hero Class:\n1: Wizard\n2: Witch Doctor\n3: Demon Hunter\n4: Monk\n5: Barbarian"); heroClassString = Console.ReadLine(); } } Console.Write("Gender (male/female): "); var gender = Console.ReadLine() == "male" ? 1 : 2; Console.Write("Level: "); var levelString = Console.ReadLine(); Console.Write("Experience: "); var experienceString = Console.ReadLine(); int level; int.TryParse(levelString, out level); int experience; int.TryParse(experienceString, out experience); var hero = new Hero(name, heroClass, gender, experience, level); if (hero.Create(currentAccount.Id)) Console.WriteLine("Hero {0} created", name); else Console.WriteLine("Hero already exists"); }
static void CommandCreateHero() { if (currentAccount == null) { Console.WriteLine("not logged in"); return; } Console.Write("Name: "); var name = Console.ReadLine(); Console.Write("Hero class 1-5: "); var heroClassString = Console.ReadLine(); Console.Write("Gender (male/female): "); var gender = Console.ReadLine() == "male" ? 1 : 2; Console.Write("Level: "); var levelString = Console.ReadLine(); Console.Write("Experience: "); var experienceString = Console.ReadLine(); int heroClass; int.TryParse(heroClassString, out heroClass); int level; int.TryParse(levelString, out level); int experience; int.TryParse(experienceString, out experience); var hero = new Hero(name, heroClass, gender, experience, level); if (hero.Create(currentAccount.Id)) Console.WriteLine("Hero {0} created", name); else Console.WriteLine("Hero already exists"); }