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 { } } }
public static bool Load(int id, out Hero hero) { hero = null; SQLiteCommand command = new SQLiteCommand(string.Format("SELECT hero_id, hero_name, hero_class_id, hero_gender_id, hero_experience, hero_level FROM hero WHERE hero.hero_id='{0}'", id), Database.Instance.Connection); SQLiteDataReader reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { var hero_id = reader.GetInt32(0); var name = reader.GetString(1); var hero_class = reader.GetInt32(2); var gender = reader.GetInt32(3); var experience = reader.GetInt32(4); var level = reader.GetInt32(5); hero = new Hero(name, hero_class, gender, experience, level); hero.Id = hero_id; return true; } } return false; }
/// <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(); } }
/// <summary> /// Retrieves data for a specific hero from the database. /// </summary> /// <param name="id">The hero_id of the desired hero.</param> /// <param name="hero">Reference to an existing Hero instance where data will be stored on success.</param> /// <returns>True on success, otherwise false.</returns> public static bool Load(int id, out Hero hero) { // clear hero instance hero = null; // initialize SQL statement SQLiteCommand command = new SQLiteCommand(string.Format("SELECT hero_id, account_id, name, hero_class_id, hero_gender_id, experience, level FROM hero WHERE hero.hero_id='{0}'", id), Database.Instance.Connection); // execute SQL (retrieve hero data for the hero whose hero_id matches the input id SQLiteDataReader reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { // load data into hero instance hero = new Hero(reader.GetInt32(1), reader.GetString(2), reader.GetInt32(3), reader.GetInt32(4), reader.GetInt32(5), reader.GetInt32(6)); // set hero id (not set by constructor) hero.Id = reader.GetInt32(0); return true; } } return false; }
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"); }