Beispiel #1
0
 private void Window_Loaded(object sender, RoutedEventArgs e)
 {
     try
     {
         //Get the list of card types from a text file
         string ExecutePath = System.IO.Directory.GetCurrentDirectory();
         //assuming the file exists get its full contents
         if (File.Exists($"{ExecutePath}\\CardTypes.txt"))
         {
             string FullCardTypes = System.IO.File.ReadAllText($"{ExecutePath}\\CardTypes.txt");
             //split it up based on # deliminators into an array
             string[] CardTypes = FullCardTypes.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);
             // for each of these array elements make a combo box item for the first combo box
             foreach (string CardType in CardTypes)
             {
                 ComboBoxItem item = new ComboBoxItem();
                 item.Content = CardType.Trim();
                 CMB_CardType.Items.Add(item);
             }
         }
         if (IsCardNew == false)
         {
             //Needed for passing to the combo box updater
             string TypeComboIndex = "";
             //if the card is a pre-existing one thats been brought in from the set list not the add new button
             //query the database to get the information needed from the index required
             string           GetCard        = $"SELECT * FROM {CardsSet} WHERE card_code='{CardsCode}'";
             SQLiteCommand    GetCardCommand = new SQLiteCommand(GetCard, Globals.GlobalVars.DatabaseConnection);
             SQLiteDataReader GetCardReader  = GetCardCommand.ExecuteReader();
             //Get the card details out of the reader
             if (GetCardReader.Read())
             {
                 //First set the names in the text boxes at the top
                 TBX_CardName.Text = GetCardReader["name_primary"].ToString();
                 TBX_CardSub.Text  = GetCardReader["name_secondary"].ToString();
                 //Next get the array of keywords and split them up
                 string[] KeywordArray = GetCardReader["keywords"].ToString().Split(',');
                 // go through all of these keywords and strip out any unnecessary spaces
                 for (int i = 0; i < KeywordArray.Length; i++)
                 {
                     KeywordArray[i] = KeywordArray[i].ToString().Trim(' ');
                 }
                 //Now take the cards type from the first item in this array
                 //and compare it to the contents of the first combo box, selecting the index that matches if any
                 for (int i = 0; i < CMB_CardType.Items.Count; i++)
                 {
                     //First you need to get the item since you cant do that in one line
                     ComboBoxItem TestedItem = (ComboBoxItem)CMB_CardType.Items.GetItemAt(i);
                     //now compare the contents of that item with what you got from the database
                     if (KeywordArray[0].ToString() == TestedItem.Content.ToString())
                     {
                         //if it matches, set the index and end the loop and save the contents of the matching box
                         CMB_CardType.SelectedIndex = i;
                         TypeComboIndex             = TestedItem.Content.ToString();
                         i = CMB_CardType.Items.Count + 1;
                     }
                 }
                 //Now update the rest of the combo boxes so that the rest of this code functions correctly
                 UpdateSubComboBoxes(TypeComboIndex);
                 //Now loop through all the contents just updated and get the one used for each combo box
                 //Start with CMB_Form
                 for (int i = 0; i < CMB_Form.Items.Count; i++)
                 {
                     //Get the item out of the combobox
                     ComboBoxItem item = new ComboBoxItem();
                     item = (ComboBoxItem)CMB_Form.Items.GetItemAt(i);
                     //Get its contents and compare to the second Keyword, if it matches set the Combobox index to i and break the loop
                     if (KeywordArray[1] == item.Content.ToString())
                     {
                         CMB_Form.SelectedIndex = i;
                         break;
                     }
                 }
                 //Now CMB_Species
                 for (int i = 0; i < CMB_Species.Items.Count; i++)
                 {
                     //Get the item out of the combobox
                     ComboBoxItem item = new ComboBoxItem();
                     item = (ComboBoxItem)CMB_Species.Items.GetItemAt(i);
                     //Get its contents and compare to the second Keyword, if it matches set the Combobox index to i and break the loop
                     if (KeywordArray[2] == item.Content.ToString())
                     {
                         CMB_Species.SelectedIndex = i;
                         break;
                     }
                 }
                 //CMB_Gender
                 for (int i = 0; i < CMB_Gender.Items.Count; i++)
                 {
                     //Get the item out of the combobox
                     ComboBoxItem item = new ComboBoxItem();
                     item = (ComboBoxItem)CMB_Gender.Items.GetItemAt(i);
                     //Get its contents and compare to the second Keyword, if it matches set the Combobox index to i and break the loop
                     if (KeywordArray[3] == item.Content.ToString())
                     {
                         CMB_Gender.SelectedIndex = i;
                         break;
                     }
                 }
                 //CMB_Affiliation
                 for (int i = 0; i < CMB_Affiliation.Items.Count; i++)
                 {
                     //Get the item out of the combobox
                     ComboBoxItem item = new ComboBoxItem();
                     item = (ComboBoxItem)CMB_Affiliation.Items.GetItemAt(i);
                     //Get its contents and compare to the second Keyword, if it matches set the Combobox index to i and break the loop
                     if (KeywordArray[4] == item.Content.ToString())
                     {
                         CMB_Affiliation.SelectedIndex = i;
                         break;
                     }
                 }
                 //Now CMB_Class
                 for (int i = 0; i < CMB_Class.Items.Count; i++)
                 {
                     //Get the item out of the combobox
                     ComboBoxItem item = new ComboBoxItem();
                     item = (ComboBoxItem)CMB_Class.Items.GetItemAt(i);
                     //Get its contents and compare to the second Keyword, if it matches set the Combobox index to i and break the loop
                     if (KeywordArray[5] == item.Content.ToString())
                     {
                         CMB_Class.SelectedIndex = i;
                         break;
                     }
                 }
                 //Lastly CMB_Rules
                 for (int i = 0; i < CMB_Rules.Items.Count; i++)
                 {
                     //Get the item out of the combobox
                     ComboBoxItem item = new ComboBoxItem();
                     item = (ComboBoxItem)CMB_Rules.Items.GetItemAt(i);
                     //Get its contents and compare to the second Keyword, if it matches set the Combobox index to i and break the loop
                     if (KeywordArray[6] == item.Content.ToString())
                     {
                         CMB_Rules.SelectedIndex = i;
                         break;
                     }
                 }
                 // And for any edgelords with a custom keyword
                 TBX_CustomKeyword.Text = KeywordArray[7];
                 //Now do the same as above for the cost box
                 for (int i = 0; i < CMB_CostSelector.Items.Count; i++)
                 {
                     //as per normal get the item out of the combobox at i
                     ComboBoxItem item = new ComboBoxItem();
                     item = (ComboBoxItem)CMB_CostSelector.Items.GetItemAt(i);
                     //if it matches whats in the cost column of the grabbed data
                     if (item.Content.ToString() == GetCardReader["cost"].ToString())
                     {
                         // make the combobox match the current index
                         CMB_CostSelector.SelectedIndex = i;
                         //and break the loop since we are done here
                         break;
                     }
                 }
                 //now for the HP, ATK and DEF textboxes which are simple copies
                 TBX_CardHP.Text  = GetCardReader["hp"].ToString();
                 TBX_CardATK.Text = GetCardReader["atk"].ToString();
                 TBX_CardDEF.Text = GetCardReader["def"].ToString();
                 //More substitution for the flavour text and image path by the same means
                 TBX_FlavourText.Text = GetCardReader["flavour"].ToString();
                 TBX_ImagePath.Text   = GetCardReader["imagestring"].ToString();
                 //pay respects to the artists
                 TBX_ArtistBox.Text = GetCardReader["artist"].ToString();
                 //get the rarity from the database and set the appropriate radio button
                 string Rarity = GetCardReader["rarity"].ToString();
                 if (Rarity == "ultima")
                 {
                     RBT_Ultima.IsChecked = true;
                 }
                 else if (Rarity == "super")
                 {
                     RBT_Super.IsChecked = true;
                 }
                 else if (Rarity == "rare")
                 {
                     RBT_Rare.IsChecked = true;
                 }
                 else
                 {
                     RBT_Common.IsChecked = true;
                 }
                 //Did the Abilities Last cos i hadnt figured out how to format them yet
                 // put the full set of text into a string because its cleaner to type
                 string AbilityString = GetCardReader["ability"].ToString();
                 //Split it up based on commas as this is how I deliminated different abilities
                 string[] AbilityArray = AbilityString.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                 // then for each ability they have make a new item in the stack panel
                 foreach (string Ability in AbilityArray)
                 {
                     //Split the ability into its 3 parts of name , cost and effect
                     string[] SplitAbility = Ability.Split(new char[] { ':' });
                     //Now make a listviewitem for this ability
                     MakeAbilityBox(SplitAbility[0], SplitAbility[1], SplitAbility[2]);
                 }
             }
             GetCardReader.Close();
             GetCardCommand.Dispose();
         }
         else
         {
             //if the card is new default to setting it to being common
             RBT_Common.IsChecked = true;
         }
     }
     catch (Exception ex)
     {
         // If something goes wrong somehow show an error explaining what went wrong then kill the application
         MessageBox.Show($"An error occured {ex}");
         Application.Current.Shutdown();
     }
 }
Beispiel #2
0
        // this function validates the data we get from the client.
        // it also cleans it up a bit.
        // it returns an error string if anything that's required isn't present,
        // and no error if everything is present and seeming valid.
        // the error returned should contain all errors that were detected.
        public List <string> Validate()
        {
            List <string> e = new List <string>();

            if (this.Amount == 0)
            {
                Validated = false;
                return(e);
            }
            try
            {
                // We'll start by cleaning up the data that they can key in,
                // removing extraneous whitespace.
                FirstName = FirstName.Trim();
                LastName  = LastName.Trim();
                ExpMonth  = ExpMonth.Trim();
                ExpYear   = ExpYear.Trim();
                CVVNumber = CVVNumber.Trim();
                ZipCode   = ZipCode.Trim();
                CardType  = CardType.Trim();
                // Here we make sure everything that's required actually has a value.
                if (FirstName.Length == 0 || LastName.Length == 0 || CardNumber.Length == 0 ||
                    CardType.Length == 0 || ExpMonth.Length == 0 || ExpYear.Length == 0 ||
                    CVVNumber.Length == 0 || ZipCode.Length == 0)
                {
                    e.Add("Missing a required value.\n");
                }
                // FirstName, LastName, Cardnumber, CVV, and Zipcode
                // are validated just by having a value.
                if (!CardTypes.Contains(CardType))
                {
                    e.Add("Card type is invalid.\n");
                }
                // Expiration Month Validation
                if (int.TryParse(ExpMonth, out int IExpMonth))
                {
                    if (IExpMonth > 12 || IExpMonth < 1)
                    {
                        e.Add("Expiration Month is invalid.\n");
                    }
                }
                else
                {
                    e.Add("Expiration Month is not a number.\n");
                }
                // Expiration Year validation
                if (int.TryParse(ExpYear, out int IExpYear))
                {
                    if (IExpYear < DateTime.Now.Year || IExpYear > DateTime.Now.AddYears(10).Year)
                    {
                        e.Add("Expiration Year is invalid.\n");
                    }
                }
                else
                {
                    e.Add("Expiration Year is not a number.\n");
                }
            }
            catch (Exception ex)
            {
                Constants.Log(ex);

                e.Add("Error in credit card validation, unable to continue.");
            }
            // if e has a length, it's an error.
            return(e);
        }