Advertisement
DKuzin

Untitled

Apr 16th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 31.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using MySql.Data.MySqlClient;
  12.  
  13. namespace WindowsFormsApplication6
  14. {
  15.     public partial class Mainmenu : Form
  16.     {
  17.         private MySqlConnection conn;
  18.         private string server;
  19.         private string database;
  20.         private string uid;
  21.         private string password;
  22.  
  23.         //Make lists that can contain cards
  24.         public static List<string> maincards = new List<string>();
  25.         public static List<string> sidecards = new List<string>();
  26.  
  27.         //Make a list that contain the user's decks
  28.         List<string> userdecks = new List<string>();
  29.        
  30.         //Selected deck section
  31.         string decksection = "maincards";
  32.  
  33.         //Get the signed in user's username
  34.         string signedinuser = WindowsFormsApplication6.Login.currentuser;
  35.  
  36.         //Save userdirectory
  37.         string userdir = Environment.CurrentDirectory + @"\" + WindowsFormsApplication6.Login.currentuser;
  38.  
  39.         //Connection
  40.         string connString;
  41.  
  42.         public Mainmenu()
  43.         {
  44.             server = "localhost";
  45.             database = "mtgworkshop";
  46.             uid = "root";
  47.             password = "";
  48.  
  49.            
  50.             connString = $"SERVER={server};DATABASE={database};UID={uid};PASSWORD={password}";
  51.            
  52.            
  53.             conn = new MySqlConnection(connString);
  54.  
  55.             InitializeComponent();
  56.            
  57.             title.Text = "Welcome " + signedinuser;
  58.            
  59.  
  60.             //We make a folder for the user if the folder does not exist
  61.  
  62.             if (!Directory.Exists(Environment.CurrentDirectory + "\\" + WindowsFormsApplication6.Login.currentuser)) Directory.CreateDirectory(Environment.CurrentDirectory + "\\" + WindowsFormsApplication6.Login.currentuser);
  63.  
  64.  
  65.             comboCat.SelectedIndex = 0;
  66.  
  67.  
  68.             //Checks if Asset folders exists and will otherwise create the folders
  69.             if (!Directory.Exists(Environment.CurrentDirectory + "\\Resources")) Directory.CreateDirectory(Environment.CurrentDirectory + "\\Resources");
  70.             if (!Directory.Exists(Environment.CurrentDirectory + "\\Resources\\Assets")) Directory.CreateDirectory(Environment.CurrentDirectory + "\\Resources\\Assets");
  71.             if (!Directory.Exists(Environment.CurrentDirectory + "\\Resources\\Assets\\Images")) Directory.CreateDirectory(Environment.CurrentDirectory + "\\Resources\\Assets\\Images");
  72.             if (!adminpermission(signedinuser))
  73.             {
  74.                 officeBtn.Hide();
  75.             }
  76.  
  77.             string query = $"SELECT cardName FROM cards";
  78.             conn = new MySqlConnection(connString);
  79.             conn.Open();
  80.             MySqlCommand cmd = new MySqlCommand(query, conn);
  81.             MySqlDataReader reader = cmd.ExecuteReader();
  82.  
  83.             while (reader.Read())
  84.             {
  85.                 searchBar.Items.Add(reader[0]);
  86.  
  87.             }
  88.  
  89.             //Load all available decks into the userdecks list
  90.             userdecks.AddRange(Directory.GetFiles(userdir));
  91.             //Remove the filepath and fileextension from each deck
  92.             for (int i = 0; i < userdecks.Count; i++)
  93.             {
  94.                 string ideckname = userdecks.ElementAt(i);
  95.                 int namestartsat = ideckname.LastIndexOf(@"\") + 1;
  96.                 string fixeddeckname = ideckname.Substring(namestartsat, ideckname.Length - namestartsat - 4);
  97.                 userdecks[i] = fixeddeckname;
  98.             }
  99.            
  100.             //Display available decks in the deckbox
  101.             deckList.Items.Clear();
  102.             for (int i = 0; i < userdecks.Count; i++)
  103.             {
  104.                 string item = userdecks.ElementAt(i);
  105.                 deckList.Items.Add(item);
  106.             }
  107.            
  108.         }
  109.  
  110.         public static decimal cardTotal = 0; //Assigns the card total default value
  111.  
  112.         //Here we make our own custom search function for lists
  113.         public static int myListSearch(List<string> mylist, string myparam)
  114.         {
  115.             for (int i = 0; i < mylist.Count ; i++ )
  116.             {
  117.                 if (myparam == mylist.ElementAt(i))
  118.                 {
  119.                     return i;
  120.                 }
  121.             }
  122.             return 0;
  123.         }
  124.  
  125.         private void craftbtn_Click(object sender, EventArgs e)
  126.         {
  127.             craftPanel.BringToFront(); //Sends crafting panel to front
  128.             craftPanel.Visible = true; //Makes crafting panel visible
  129.             //Display deck cards in listbox
  130.             listBox1.Items.Clear();
  131.             for (int i = 0; i < maincards.Count; i++)
  132.             {
  133.                 string item = maincards.ElementAt(i + 1) + maincards.ElementAt(i);
  134.                 listBox1.Items.Add(item);
  135.                 i = i + 1; //Skip next element
  136.             }
  137.         }
  138.  
  139.         private void mainMenuBtn_Click(object sender, EventArgs e)
  140.         {
  141.             //Ask if user if they are sure, that they want to leave without saving
  142.             DialogResult dialogResult = MessageBox.Show("Are you sure you want to go back without saving?", "Your changes may be lost", MessageBoxButtons.YesNo);
  143.             if (dialogResult == DialogResult.Yes)
  144.             {
  145.                 showMain.BackColor = System.Drawing.Color.SeaGreen;
  146.                 showMain.ForeColor = System.Drawing.Color.White;
  147.                 showSide.BackColor = System.Drawing.Color.White;
  148.                 showSide.ForeColor = System.Drawing.Color.Black;
  149.                 maincards.RemoveRange(0, maincards.Count);
  150.                 listBox1.Items.Clear();
  151.                 count.Text = "000";
  152.                 textBox1.Text = "Enter deckname";
  153.                 searchBar.Text = "";
  154.                 craftPanel.SendToBack(); //Sends crafting panel to back
  155.                 craftPanel.Visible = false; //Makes crafting panel invisible
  156.             }
  157.             else if (dialogResult == DialogResult.No)
  158.             {
  159.                 return;
  160.             }
  161.         }
  162.  
  163.         private void comboCat_SelectedIndexChanged(object sender, EventArgs e)
  164.         {
  165.             switch (comboCat.Text)
  166.             {
  167.                 case "Mainboard":
  168.                     decksection = "maincards";
  169.                     break;
  170.                 case "Sideboard":
  171.                     decksection = "sidecards";
  172.                     break;
  173.                 default:
  174.                     decksection = "maincards";
  175.                     break;
  176.             }
  177.            
  178.         }
  179.  
  180.         private void addBtn_Click(object sender, EventArgs e)
  181.         {
  182.             //Update the total card amount
  183.             cardTotal = Convert.ToDecimal(count.Text);
  184.             decimal newCardTotal = cardTotal + cardCount.Value;
  185.             string countLabel = Convert.ToString(newCardTotal);
  186.             cardTotal = newCardTotal;
  187.             count.Text = countLabel;
  188.             if (decksection == "maincards")
  189.             {
  190.                 //Update the card list
  191.                 //If the card is already in the deck, update the amount
  192.                 if (maincards.Contains(searchBar.Text))
  193.                 {
  194.                     //Find where the card is located in the list
  195.                     int cardlocation = myListSearch(maincards, searchBar.Text);
  196.                     string cardstring = maincards[cardlocation];
  197.                     string cardamount = maincards[cardlocation + 1]; //The amount is always located after the card in the list
  198.  
  199.                     int endpos = cardamount.IndexOf("x"); //find x in the card amount
  200.                     string slicedcardamount = cardamount.Substring(0, endpos); //Extract the number of the card amount without x
  201.  
  202.                     //Update the card amount
  203.                     int newcardamountint = Convert.ToInt32(slicedcardamount) + Convert.ToInt32(cardCount.Value);
  204.                     string newcardamountstring = Convert.ToString(newcardamountint) + "x ";
  205.                     maincards[cardlocation + 1] = newcardamountstring;
  206.  
  207.                 }
  208.                 else //Add the card to the end of the list
  209.                 {
  210.                     maincards.Add(searchBar.Text);
  211.                     maincards.Add(cardCount.Value + "x ");
  212.  
  213.                 }
  214.  
  215.                 showMain.BackColor = System.Drawing.Color.SeaGreen;
  216.                 showMain.ForeColor = System.Drawing.Color.White;
  217.                 showSide.BackColor = System.Drawing.Color.White;
  218.                 showSide.ForeColor = System.Drawing.Color.Black;
  219.  
  220.                 //Display deck in listbox
  221.                 listBox1.Items.Clear();
  222.                 for (int i = 0; i < maincards.Count; i++)
  223.                 {
  224.                     string item = maincards.ElementAt(i + 1) + maincards.ElementAt(i);
  225.                     listBox1.Items.Add(item);
  226.                     i = i + 1; //Skip next element
  227.                 }
  228.             }
  229.             else //It is sidecards
  230.             {
  231.                 //Update the card list
  232.                 //If the card is already in the deck, update the amount
  233.                 if (sidecards.Contains(searchBar.Text))
  234.                 {
  235.                     //Find where the card is located in the list
  236.                     int cardlocation = myListSearch(sidecards, searchBar.Text);
  237.                     string cardstring = sidecards[cardlocation];
  238.                     string cardamount = sidecards[cardlocation + 1]; //The amount is always located after the card in the list
  239.  
  240.                     int endpos = cardamount.IndexOf("x"); //find x in the card amount
  241.                     string slicedcardamount = cardamount.Substring(0, endpos); //Extract the number of the card amount without x
  242.  
  243.                     //Update the card amount
  244.                     int newcardamountint = Convert.ToInt32(slicedcardamount) + Convert.ToInt32(cardCount.Value);
  245.                     string newcardamountstring = Convert.ToString(newcardamountint) + "x ";
  246.                     sidecards[cardlocation + 1] = newcardamountstring;
  247.  
  248.                 }
  249.                 else //Add the card to the end of the list
  250.                 {
  251.                     sidecards.Add(searchBar.Text);
  252.                     sidecards.Add(cardCount.Value + "x ");
  253.  
  254.                 }
  255.  
  256.                 showMain.BackColor = System.Drawing.Color.White;
  257.                 showMain.ForeColor = System.Drawing.Color.Black;
  258.                 showSide.BackColor = System.Drawing.Color.SeaGreen;
  259.                 showSide.ForeColor = System.Drawing.Color.White;
  260.  
  261.                 //Display deck in listbox
  262.                 listBox1.Items.Clear();
  263.                 for (int i = 0; i < sidecards.Count; i++)
  264.                 {
  265.                     string item = sidecards.ElementAt(i + 1) + sidecards.ElementAt(i);
  266.                     listBox1.Items.Add(item);
  267.                     i = i + 1; //Skip next element
  268.                 }
  269.             }
  270.         }
  271.  
  272.         private void removeBtn_Click(object sender, EventArgs e)
  273.         {
  274.             //If the count total is already 0, do nothing
  275.             if(Convert.ToDecimal(count.Text) == 0)
  276.             {
  277.                 return;
  278.             }
  279.            
  280.             //Update the total card amount
  281.             cardTotal = Convert.ToDecimal(count.Text);
  282.             decimal newCardTotal = cardTotal - cardCount.Value;
  283.             string countLabel = Convert.ToString(newCardTotal);
  284.             cardTotal = newCardTotal;
  285.             count.Text = countLabel;
  286.  
  287.             if (decksection == "maincards")
  288.             {
  289.                 //Update the card list
  290.                 if (maincards.Contains(searchBar.Text))
  291.                 {
  292.                     //Find where the card is located in the list
  293.                     int cardlocation = myListSearch(maincards, searchBar.Text);
  294.                     string cardstring = maincards[cardlocation];
  295.                     string cardamount = maincards[cardlocation + 1]; //The amount is always located after the card in the list
  296.  
  297.                     int endpos = cardamount.IndexOf("x"); //find x in the card amount
  298.                     string slicedcardamount = cardamount.Substring(0, endpos); //Extract the number of the card amount without x
  299.  
  300.                     //Update the card amount
  301.                     int newcardamountint = Convert.ToInt32(slicedcardamount) - Convert.ToInt32(cardCount.Value);
  302.                     string newcardamountstring = Convert.ToString(newcardamountint) + "x ";
  303.                     maincards[cardlocation + 1] = newcardamountstring;
  304.  
  305.                     //If the card amount is less than 1, remove the entire card from the list
  306.                     if (newcardamountint < 1)
  307.                     {
  308.                         maincards.RemoveRange(cardlocation, 2);
  309.                     }
  310.  
  311.                     showMain.BackColor = System.Drawing.Color.SeaGreen;
  312.                     showMain.ForeColor = System.Drawing.Color.White;
  313.                     showSide.BackColor = System.Drawing.Color.White;
  314.                     showSide.ForeColor = System.Drawing.Color.Black;
  315.  
  316.                     //Display deck in listbox
  317.                     listBox1.Items.Clear();
  318.                     for (int i = 0; i < maincards.Count; i++)
  319.                     {
  320.                         string item = maincards.ElementAt(i + 1) + maincards.ElementAt(i);
  321.                         listBox1.Items.Add(item);
  322.                         i = i + 1; //Skip next element
  323.                     }
  324.  
  325.                 } //If the card does not exist in the list, nothing happens
  326.             }
  327.             else //It is sidecards
  328.             {
  329.                 //Update the card list
  330.                 if (sidecards.Contains(searchBar.Text))
  331.                 {
  332.                     //Find where the card is located in the list
  333.                     int cardlocation = myListSearch(sidecards, searchBar.Text);
  334.                     string cardstring = sidecards[cardlocation];
  335.                     string cardamount = sidecards[cardlocation + 1]; //The amount is always located after the card in the list
  336.  
  337.                     int endpos = cardamount.IndexOf("x"); //find x in the card amount
  338.                     string slicedcardamount = cardamount.Substring(0, endpos); //Extract the number of the card amount without x
  339.  
  340.                     //Update the card amount
  341.                     int newcardamountint = Convert.ToInt32(slicedcardamount) - Convert.ToInt32(cardCount.Value);
  342.                     string newcardamountstring = Convert.ToString(newcardamountint) + "x ";
  343.                     sidecards[cardlocation + 1] = newcardamountstring;
  344.  
  345.                     //If the card amount is less than 1, remove the entire card from the list
  346.                     if (newcardamountint < 1)
  347.                     {
  348.                         sidecards.RemoveRange(cardlocation, 2);
  349.                     }
  350.  
  351.                     showMain.BackColor = System.Drawing.Color.White;
  352.                     showMain.ForeColor = System.Drawing.Color.Black;
  353.                     showSide.BackColor = System.Drawing.Color.SeaGreen;
  354.                     showSide.ForeColor = System.Drawing.Color.White;
  355.  
  356.                     //Display deck in listbox
  357.                     listBox1.Items.Clear();
  358.                     for (int i = 0; i < sidecards.Count; i++)
  359.                     {
  360.                         string item = sidecards.ElementAt(i + 1) + sidecards.ElementAt(i);
  361.                         listBox1.Items.Add(item);
  362.                         i = i + 1; //Skip next element
  363.                     }
  364.  
  365.                 } //If the card does not exist in the list, nothing happens
  366.             }
  367.         }
  368.  
  369.         private void officeBtn_Click(object sender, EventArgs e)
  370.         {
  371.             RegOffice f3 = new RegOffice();
  372.             f3.ShowDialog();
  373.            
  374.         }
  375.  
  376.         private void probTable_Click(object sender, EventArgs e)
  377.         {
  378.             MessageBox.Show("This feature is not implemented yet.");
  379.             //Probalility f2 = new Probalility();
  380.             //f2.ShowDialog();
  381.         }
  382.  
  383.         private void logOutBtn_Click(object sender, EventArgs e)
  384.         {
  385.             this.Close();
  386.         }
  387.  
  388.         private void textBox1_TextChanged(object sender, EventArgs e)
  389.         {
  390.  
  391.         }
  392.  
  393.         private void cardAdvisor_Click(object sender, EventArgs e)
  394.         {
  395.             if(selectedname != "" && selectedname != null)
  396.             {
  397.                 cAdvisor f5 = new cAdvisor();
  398.                 f5.ShowDialog();
  399.             }
  400.             else
  401.             {
  402.                 MessageBox.Show("Please select a card that I can advice you on");
  403.             }
  404.         }
  405.  
  406.         private void button1_Click(object sender, EventArgs e) //Show Visual button
  407.         {
  408.             if (Convert.ToInt16(count.Text) != 0)
  409.             {
  410.                 visual f7 = new visual();
  411.                 f7.ShowDialog();
  412.             }
  413.             else
  414.             {
  415.                 MessageBox.Show("I can not show you an empty deck..");
  416.             }
  417.            
  418.         }
  419.  
  420.         private void priceAdvisor_Click(object sender, EventArgs e)
  421.         {
  422.             MessageBox.Show("This feature is not implemented yet.");
  423.             //pAdvisor f6 = new pAdvisor();
  424.             //f6.ShowDialog();
  425.         }
  426.         public bool adminpermission(string userstring)
  427.         {
  428.             string query = $"SELECT * FROM users WHERE username = '{userstring}' AND userlevel = '1';";
  429.  
  430.             try
  431.             {
  432.                 if (OpenConnection())
  433.                 {
  434.                     MySqlCommand cmd = new MySqlCommand(query, conn);
  435.                     MySqlDataReader reader = cmd.ExecuteReader();
  436.  
  437.                     if (reader.Read())
  438.                     {
  439.                         reader.Close();
  440.                         conn.Close();
  441.                         return true;
  442.                     }
  443.                     else
  444.                     {
  445.                         reader.Close();
  446.                         conn.Close();
  447.                         return false;
  448.                     }
  449.                 }
  450.                 else
  451.                 {
  452.                     conn.Close();
  453.                     return false;
  454.                 }
  455.             }
  456.             catch (Exception ex)
  457.             {
  458.                 conn.Close();
  459.                 return false;
  460.             }
  461.         }
  462.         private bool OpenConnection()
  463.         {
  464.             try
  465.             {
  466.                 if (conn.State != ConnectionState.Open) conn.Open();
  467.                 return true;
  468.             }
  469.             catch (MySqlException ex)
  470.             {
  471.                 switch (ex.Number)
  472.                 {
  473.                     case 0:
  474.                         MessageBox.Show("Connection to server failed!");
  475.                         break;
  476.                     case 1045:
  477.                         MessageBox.Show("Username and password doesnt match!");
  478.                         break;
  479.                 }
  480.                 return false;
  481.             }
  482.         }
  483.  
  484.         public static string selectedname;
  485.  
  486.         private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
  487.         {
  488.             try
  489.             {
  490.                 //This updates the searchbar to the selected card
  491.                 string selected = listBox1.SelectedItem.ToString();
  492.  
  493.                 int indexname = selected.IndexOf("x") + 2;
  494.                 selectedname = selected.Substring(indexname);
  495.  
  496.                 searchBar.Text = selectedname;
  497.             }
  498.             catch
  499.             {
  500.                 return;
  501.             }
  502.            
  503.         }
  504.  
  505.         private void showMain_Click(object sender, EventArgs e)
  506.         {
  507.             showMain.BackColor = System.Drawing.Color.SeaGreen;
  508.             showMain.ForeColor = System.Drawing.Color.White;
  509.             showSide.BackColor = System.Drawing.Color.White;
  510.             showSide.ForeColor = System.Drawing.Color.Black;
  511.  
  512.             //Display deck in listbox
  513.             listBox1.Items.Clear();
  514.             for (int i = 0; i < maincards.Count; i++)
  515.             {
  516.                 string item = maincards.ElementAt(i + 1) + maincards.ElementAt(i);
  517.                 listBox1.Items.Add(item);
  518.                 i = i + 1; //Skip next element
  519.             }
  520.         }
  521.  
  522.         private void showSide_Click(object sender, EventArgs e)
  523.         {
  524.             showMain.BackColor = System.Drawing.Color.White;
  525.             showMain.ForeColor = System.Drawing.Color.Black;
  526.             showSide.BackColor = System.Drawing.Color.SeaGreen;
  527.             showSide.ForeColor = System.Drawing.Color.White;
  528.  
  529.             //Display deck in listbox
  530.             listBox1.Items.Clear();
  531.             for (int i = 0; i < sidecards.Count; i++)
  532.             {
  533.                 string item = sidecards.ElementAt(i + 1) + sidecards.ElementAt(i);
  534.                 listBox1.Items.Add(item);
  535.                 i = i + 1; //Skip next element
  536.             }
  537.         }
  538.  
  539.         private void saveBtn_Click(object sender, EventArgs e)
  540.         {
  541.             string mydeckname = textBox1.Text;
  542.             //Check if the deckname contains any illegal characters and if it is long enough
  543.             if (mydeckname.IndexOfAny(Path.GetInvalidFileNameChars()) != -1 || mydeckname.Length < 3)
  544.             {
  545.                 if(mydeckname.IndexOfAny(Path.GetInvalidFileNameChars()) != -1)
  546.                 {
  547.                     MessageBox.Show("Deckname invalid.");
  548.                 }
  549.                 if(mydeckname.Length < 3)
  550.                 {
  551.                     MessageBox.Show("Deckname must be at least 3 characters long");
  552.                 }
  553.                 return;
  554.             }
  555.  
  556.             //Make a .txt document in the user's folder, with filename as the written deckname
  557.             //and export the list into the document's content
  558.  
  559.             //Make a new list to contain the save data
  560.             List<string> saveData = new List<string>();
  561.             //Add the Mainboard list to the save data
  562.             saveData.Add("@Mainboard");
  563.             for (int i = 0; i < maincards.Count; i++)
  564.             {
  565.                 saveData.Add(maincards[i + 1] + maincards[i]);
  566.                 i = i + 1;
  567.             }
  568.             //Add the Sideboard list to the save data
  569.             saveData.Add("@Sideboard");
  570.             for (int i = 0; i < sidecards.Count; i++)
  571.             {
  572.                 saveData.Add(sidecards[i + 1] + sidecards[i]);
  573.                 i = i + 1;
  574.             }
  575.             //Write .txt file and save to the user's directory
  576.            
  577.             System.IO.File.WriteAllLines(userdir + @"\" + mydeckname + ".txt" , saveData);
  578.  
  579.             //Go back to main menu
  580.             showMain.BackColor = System.Drawing.Color.SeaGreen;
  581.             showMain.ForeColor = System.Drawing.Color.White;
  582.             showSide.BackColor = System.Drawing.Color.White;
  583.             showSide.ForeColor = System.Drawing.Color.Black;
  584.             listBox1.Items.Clear();
  585.             count.Text = "000";
  586.             maincards.RemoveRange(0, maincards.Count);
  587.             textBox1.Text = "Enter deckname";
  588.             searchBar.Text = "";
  589.             craftPanel.SendToBack(); //Sends crafting panel to back
  590.             craftPanel.Visible = false; //Makes crafting panel invisible
  591.  
  592.             //Load all available decks into the userdecks list
  593.             userdecks.RemoveRange(0, userdecks.Count);
  594.             userdecks.AddRange(Directory.GetFiles(userdir));
  595.             //Remove the filepath and fileextension from each deck
  596.             for (int i = 0; i < userdecks.Count; i++)
  597.             {
  598.                 string ideckname = userdecks.ElementAt(i);
  599.                 int namestartsat = ideckname.LastIndexOf(@"\") + 1;
  600.                 string fixeddeckname = ideckname.Substring(namestartsat, ideckname.Length - namestartsat - 4);
  601.                 userdecks[i] = fixeddeckname;
  602.             }
  603.             //Display available decks in the deckbox
  604.             deckList.Items.Clear();
  605.             for (int i = 0; i < userdecks.Count; i++)
  606.             {
  607.                 string item = userdecks.ElementAt(i);
  608.                 deckList.Items.Add(item);
  609.             }
  610.         }
  611.  
  612.         string selecteddeck = "";
  613.  
  614.         private void loadBtn_Click(object sender, EventArgs e)
  615.         {
  616.             if(selecteddeck == "")
  617.             {
  618.                 MessageBox.Show("No deck selected");
  619.                 return;
  620.             }
  621.             //Loads the content of the selected deck from its file into a list
  622.             string[] loadarray = System.IO.File.ReadAllLines(userdir + @"\" + selecteddeck + ".txt");
  623.             string loadingto = "main";
  624.             int loadedcards = 0;
  625.             try
  626.             {
  627.                 foreach (string s in loadarray)
  628.                 {
  629.                     switch (s)
  630.                     {
  631.                         case "@Mainboard":
  632.                             loadingto = "main";
  633.                             break;
  634.                         case "@Sideboard":
  635.                             loadingto = "side";
  636.                             break;
  637.                         default:
  638.                             if(loadingto == "main")
  639.                             {
  640.                                 maincards.Add(s.Substring(s.IndexOf("x") + 2));
  641.                                 maincards.Add(s.Substring(0, s.IndexOf("x") + 1) + " ");
  642.                                 loadedcards = loadedcards + Convert.ToInt32(s.Substring(0, s.IndexOf("x")));
  643.                             }
  644.                             if(loadingto == "side")
  645.                             {
  646.                                 sidecards.Add(s.Substring(s.IndexOf("x") + 2));
  647.                                 sidecards.Add(s.Substring(0, s.IndexOf("x") + 1) + " ");
  648.                                 loadedcards = loadedcards + Convert.ToInt32(s.Substring(0, s.IndexOf("x")));
  649.                             }
  650.                             break;
  651.                     }
  652.                 }
  653.             } catch
  654.             {
  655.                 MessageBox.Show("An error occured while loading the deck");
  656.                 return;
  657.             }
  658.  
  659.             craftPanel.BringToFront(); //Sends crafting panel to front
  660.             craftPanel.Visible = true; //Makes crafting panel visible
  661.             count.Text = Convert.ToString(loadedcards);
  662.             textBox1.Text = selecteddeck;
  663.  
  664.             //Display deck cards in listbox
  665.             for (int i = 0; i < maincards.Count; i++)
  666.             {
  667.                 string item = maincards.ElementAt(i + 1) + maincards.ElementAt(i);
  668.                 listBox1.Items.Add(item);
  669.                 i = i + 1; //Skip next element
  670.             }
  671.         }
  672.        
  673.         private void deckList_SelectedIndexChanged(object sender, EventArgs e)
  674.         {
  675.             try
  676.             {
  677.                 selecteddeck = deckList.SelectedItem.ToString();
  678.             }
  679.             catch
  680.             {
  681.                 return;
  682.             }
  683.         }
  684.  
  685.         //The code below contains the suggested card
  686.         public static string suggested = null;
  687.  
  688.         private void searchBar_SelectedIndexChanged(object sender, EventArgs e)
  689.         {
  690.             string cardname = searchBar.Text;
  691.             string query = $"SELECT cardImage FROM cards WHERE cardName = '{cardname}';";
  692.            
  693.             conn = new MySqlConnection(connString);
  694.             conn.Open();
  695.             MySqlCommand cmd = new MySqlCommand(query, conn);
  696.             MySqlDataReader reader = cmd.ExecuteReader();
  697.             reader.Read();
  698.             try
  699.             {
  700.                 pictureBox.Load(Environment.CurrentDirectory + "\\Resources\\Assets\\Images\\" + reader.GetString(0));
  701.  
  702.             }
  703.             catch
  704.             {
  705.                 pictureBox.Image = pictureBox.ErrorImage;
  706.             }
  707.            
  708.         }
  709.  
  710.         private void deckList_DoubleClick(object sender, MouseEventArgs e)
  711.         {
  712.             //If the user doubleclicks a deck, load the deck
  713.             try
  714.             {
  715.                 loadBtn.PerformClick();
  716.             }
  717.             catch
  718.             {
  719.                 return;
  720.             }
  721.            
  722.         }
  723.  
  724.         private void listBox1_DoubleClick(object sender, MouseEventArgs e)
  725.         {
  726.             //if the user doubleclicks a card, add more of that card
  727.             try
  728.             {
  729.                 addBtn.PerformClick();
  730.             }
  731.             catch
  732.             {
  733.                 return;
  734.             }
  735.         }
  736.  
  737.         private void deletebtn_Click(object sender, EventArgs e)
  738.         {
  739.             if (selecteddeck == "")
  740.             {
  741.                 MessageBox.Show("No deck selected");
  742.                 return;
  743.             }
  744.             //Confirm that the user want to delete the selected deck
  745.             try
  746.             {
  747.                 DialogResult dialogResult = MessageBox.Show("Are you sure you want to delete: " + selecteddeck + "?", "You are about to delete a deck", MessageBoxButtons.YesNo);
  748.                 if (dialogResult == DialogResult.Yes)
  749.                 {
  750.                     //Delete the deck
  751.                     try
  752.                     {
  753.                         File.Delete(userdir + @"\" + selecteddeck + ".txt");
  754.                     }
  755.                     catch
  756.                     {
  757.                         MessageBox.Show("Deletion failed.");
  758.                     }
  759.  
  760.                     //Load all available decks into the userdecks list
  761.                     userdecks.RemoveRange(0, userdecks.Count);
  762.                     userdecks.AddRange(Directory.GetFiles(userdir));
  763.                     //Remove the filepath and fileextension from each deck
  764.                     for (int i = 0; i < userdecks.Count; i++)
  765.                     {
  766.                         string ideckname = userdecks.ElementAt(i);
  767.                         int namestartsat = ideckname.LastIndexOf(@"\") + 1;
  768.                         string fixeddeckname = ideckname.Substring(namestartsat, ideckname.Length - namestartsat - 4);
  769.                         userdecks[i] = fixeddeckname;
  770.                     }
  771.                     //Display available decks in the deckbox
  772.                     deckList.Items.Clear();
  773.                     for (int i = 0; i < userdecks.Count; i++)
  774.                     {
  775.                         string item = userdecks.ElementAt(i);
  776.                         deckList.Items.Add(item);
  777.                     }
  778.                 }
  779.                 else if (dialogResult == DialogResult.No)
  780.                 {
  781.                     return;
  782.                 }
  783.             }
  784.             catch
  785.             {
  786.                 return;
  787.             }
  788.         }
  789.  
  790.         private void activated(object sender, EventArgs e)
  791.         {
  792.             //this adds a suggested card
  793.             if (suggested != null)
  794.             {
  795.                 searchBar.Text = suggested;
  796.                 addBtn.PerformClick();
  797.  
  798.                 suggested = null;
  799.             }
  800.         }
  801.         public Form RefToLogin { get; set; }
  802.  
  803.         private void Form_Closing(object sender, FormClosingEventArgs e)
  804.         {
  805.             this.RefToLogin.Show();
  806.         }
  807.  
  808.         //Just a flag
  809.         int focuson = 0;
  810.  
  811.         private void Searchbar_FocusEnter(object sender, EventArgs e)
  812.         {
  813.             picturenotthere.Load(Environment.CurrentDirectory + "\\Resources\\Assets\\Images\\littlefix2.jpg");
  814.             focuson = 1;
  815.         }
  816.  
  817.         private void Searchbar_FocusLeave(object sender, EventArgs e)
  818.         {
  819.             picturenotthere.Load(Environment.CurrentDirectory + "\\Resources\\Assets\\Images\\littlefix.jpg");
  820.             focuson = 0;
  821.         }
  822.  
  823.         private void Searchbar_MouseEnter(object sender, EventArgs e)
  824.         {
  825.             if (focuson == 0)
  826.                 picturenotthere.Load(Environment.CurrentDirectory + "\\Resources\\Assets\\Images\\littlefix3.jpg");
  827.         }
  828.  
  829.         private void Searchbar_MouseLeave(object sender, EventArgs e)
  830.         {
  831.             if (focuson == 0)
  832.                 picturenotthere.Load(Environment.CurrentDirectory + "\\Resources\\Assets\\Images\\littlefix.jpg");
  833.         }
  834.     }
  835. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement