· 8 years ago · Apr 16, 2018, 06:46 AM
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Threading.Tasks;
9using System.Windows.Forms;
10using System.IO;
11using MySql.Data.MySqlClient;
12using Path = System.IO.Path;
13namespace WindowsFormsApplication6
14{
15 public partial class RegOffice : Form
16 {
17 private MySqlConnection conn;
18 private string server;
19 private string database;
20 private string uid;
21 private string password;
22
23 public RegOffice()
24 {
25 server = "localhost";
26 database = "mtgworkshop";
27 uid = "root";
28 password = "";
29
30 string connString;
31 connString = $"SERVER={server};DATABASE={database};UID={uid};PASSWORD={password}";
32
33 conn = new MySqlConnection(connString);
34
35 InitializeComponent();
36 }
37 //Set global variables
38 string imgPath = "";
39 string imgExt = "";
40
41 private void imgbtn_Click(object sender, EventArgs e)
42 {
43 //Create a new instance of openFileDialog
44 OpenFileDialog ofd = new OpenFileDialog();
45
46 //Filter
47 ofd.Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp;...";
48
49 //When the user select the file
50 if (ofd.ShowDialog() == DialogResult.OK)
51 {
52 //Get the file's path
53 string filePath = ofd.FileName;
54
55
56 //Get the file's extension
57 string ext = Path.GetExtension(filePath);
58
59 //Check if the extension is valid
60 if(ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".bmp")
61 {
62 //Show image in box
63 //If an error occurs and the image could not be loaded, the function is stopped
64 try
65 {
66 imgBox.Load(filePath);
67 }
68 catch
69 {
70 MessageBox.Show("I can see through your lies mortal. Now, show me a true image");
71 imgbtn_Click(sender, e);
72 return;
73 }
74
75 //Save filepath
76 imgPath = filePath;
77
78 //Save fileextension
79 imgExt = ext;
80
81 }
82 else
83 {
84 //Inform the user that he cheated the system and open the dialog again
85 MessageBox.Show("You can't fool me mortal. You can only choose .jpg, jpeg, .png or .bmp");
86 imgbtn_Click(sender, e);
87 }
88
89 }
90
91 }
92
93
94 private void button3_Click(object sender, EventArgs e)
95 {
96 this.Close();
97 }
98
99 private void regBtn_Click(object sender, EventArgs e)
100 {
101 //Gets all input data from the textboxes
102 string cardName = titleBox.Text;
103 string cardCmcString = cmcBox.Text;
104 string cardType = typeBox.Text;
105 string cardPriceString = priceBox.Text;
106 string cardTags = tagBox.Text;
107
108 int cardCmc;
109 decimal cardPrice;
110
111 //Checks if the input in cmcBox and priceBox is actually a number
112 try
113 {
114 cmcBoxTitle.BackColor = System.Drawing.Color.SteelBlue;
115 priceBoxTitle.BackColor = System.Drawing.Color.SteelBlue;
116
117 cardCmc = Convert.ToInt16(cardCmcString);
118 cardPrice = Convert.ToDecimal(cardPriceString);
119
120 MessageBox.Show(Convert.ToString(cardPrice));
121 }
122 catch
123 {
124 //Test cardCmc
125 try
126 {
127 int cardCmcValue = Convert.ToInt32(cardCmcString);
128 }
129 catch
130 {
131 MessageBox.Show("Card CMC (converted mana cost) must be an integer and contain no other characters.");
132 cmcBoxTitle.BackColor = System.Drawing.Color.DarkRed;
133 }
134 //Then test cardPrice
135 try
136 {
137 decimal cardPriceValue = Convert.ToDecimal(cardPriceString);
138 }
139 catch
140 {
141 MessageBox.Show("Card price must be a decimal and contain no other characters.");
142 priceBoxTitle.BackColor = System.Drawing.Color.DarkRed;
143 }
144 return;
145
146 }
147
148
149 //Checks for illegal characters and makes sure the inputs are of the correct length
150 if (cardName.IndexOfAny(Path.GetInvalidFileNameChars()) != -1 ||
151 cardType.IndexOfAny(Path.GetInvalidFileNameChars()) != -1 ||
152 cardPriceString.IndexOf(",") != -1 ||
153 cardTags.IndexOfAny(Path.GetInvalidFileNameChars()) != -1 ||
154 cardName.Length < 2 || cardCmcString.Length < 1 || cardType.Length < 2)
155 {
156 MessageBox.Show("Illegal input characters or length. Only alphabetical characters and numbers allowed");
157
158 //Make the boxes with an invalid input appear red.
159 if (cardName.IndexOfAny(Path.GetInvalidFileNameChars()) != -1 ||
160 cardName.Length < 2)
161 {
162 titleBoxTitle.BackColor = System.Drawing.Color.DarkRed;
163 }
164 else { titleBoxTitle.BackColor = System.Drawing.Color.SteelBlue; }
165
166 if (cardType.IndexOfAny(Path.GetInvalidFileNameChars()) != -1 ||
167 cardType.Length < 2)
168 {
169 typeBoxTitle.BackColor = System.Drawing.Color.DarkRed;
170 }
171 else { typeBoxTitle.BackColor = System.Drawing.Color.SteelBlue; }
172
173 if (cardPriceString.IndexOf(",") != -1)
174 {
175 MessageBox.Show("Please use . instead of , for decimals");
176 priceBoxTitle.BackColor = System.Drawing.Color.DarkRed;
177 }
178 else { priceBoxTitle.BackColor = System.Drawing.Color.SteelBlue; }
179
180 if (cardTags.IndexOfAny(Path.GetInvalidFileNameChars()) != -1)
181 {
182 tagBoxTitle.BackColor = System.Drawing.Color.DarkRed;
183 }
184 else { tagBoxTitle.BackColor = System.Drawing.Color.SteelBlue; }
185
186 //Stops the funktion
187 return;
188 }
189 else
190 {
191
192 //Resets the colors of all the titleboxes
193 titleBoxTitle.BackColor = System.Drawing.Color.SteelBlue;
194 cmcBoxTitle.BackColor = System.Drawing.Color.SteelBlue;
195 typeBoxTitle.BackColor = System.Drawing.Color.SteelBlue;
196 priceBoxTitle.BackColor = System.Drawing.Color.SteelBlue;
197 tagBoxTitle.BackColor = System.Drawing.Color.SteelBlue;
198
199 //Copies the chosen image to the asset directory and matches the copied file's name with the cardname
200 //If the file already exists, it is overwriten
201 //If an error occurs, stop the function
202 try
203 {
204 File.Copy(imgPath, Environment.CurrentDirectory + "\\Resources\\Assets\\Images\\" + cardName + imgExt, true);
205 }
206 catch
207 {
208 MessageBox.Show("Invalid image type or image filename. Please either change the filename or select another image. If you have not chosen an image, please do so.");
209
210 //Stops the function
211 return;
212 }
213 //Before we add anything we want to check whether or not that there is an existing card with same name in pur database
214 if (cardNameExist(cardName))
215 {
216 //If it turns out to be true, we instead want to update our card instead of inserting a new card with same name in database
217 if (updateCard(cardName, cardCmc, cardType, cardPrice, cardTags, imgExt))
218 {
219 MessageBox.Show($"{cardName} has been updated!");
220 }
221 else
222 {
223 MessageBox.Show($"Error updating {cardName}");
224 }
225 }
226 else
227 {
228 //If not, we see the card as a new one and insert it into our table.
229 if (addCard(cardName, cardCmc, cardType, cardPrice, cardTags, imgExt))
230 {
231 MessageBox.Show($"{cardName} has been created!");
232 }
233 else
234 {
235 MessageBox.Show($"Error creating {cardName}");
236 }
237 }
238
239 //Resets all inputs
240 titleBox.ResetText();
241 cmcBox.ResetText();
242 typeBox.ResetText();
243 priceBox.ResetText();
244 tagBox.ResetText();
245 imgBox.Image = null;
246 }
247 }
248 //Used to insert a new card to our database
249 public bool addCard(string cardName, int cardCmc, string cardType, decimal cardPrice, string cardTags, string imgExt)
250 {
251 string query = $"INSERT INTO cards (id, cardName, cardCmc, cardType, cardPrice, cardTags, cardImage) VALUES('', '{cardName}', '{cardCmc}', '{cardType}', '{cardPrice}', '{cardTags}','{cardName + imgExt}')";
252 MySqlCommand cmd = new MySqlCommand(query, conn);
253 conn.Open();
254 cmd.ExecuteNonQuery();
255 return true;
256
257 }
258 //Used to update an already existing card
259 public bool updateCard(string cardName, int cardCmc, string cardType, decimal cardPrice, string cardTags, string imgExt)
260 {
261 string query = $"UPDATE cards SET cardCmc = '{cardCmc}', cardType = '{cardType}', cardPrice = '{cardPrice}', cardTags = '{cardTags}', cardImage = '{cardName + imgExt}' WHERE cardname = '{cardName}'";
262 MySqlCommand cmd = new MySqlCommand(query, conn);
263 conn.Open();
264 cmd.ExecuteNonQuery();
265 return true;
266 }
267 //Bool to check whether or not a card already exist based on the cardname
268 public bool cardNameExist(string cardName)
269 {
270 string query = $"SELECT * FROM cards WHERE cardname = '{cardName}';";
271
272 try
273 {
274 if (OpenConnection())
275 {
276 MySqlCommand cmd = new MySqlCommand(query, conn);
277 MySqlDataReader reader = cmd.ExecuteReader();
278
279 if (reader.Read())
280 {
281 reader.Close();
282 conn.Close();
283 return true;
284 }
285 else
286 {
287 reader.Close();
288 conn.Close();
289 return false;
290 }
291 }
292 else
293 {
294 conn.Close();
295 return false;
296 }
297 }
298 catch (Exception ex)
299 {
300 conn.Close();
301 return false;
302 }
303 }
304 private bool OpenConnection()
305 {
306 try
307 {
308 if (conn.State != ConnectionState.Open) conn.Open();
309 return true;
310 }
311 catch (MySqlException ex)
312 {
313 switch (ex.Number)
314 {
315 case 0:
316 MessageBox.Show("Connection to server failed!");
317 break;
318 case 1045:
319 MessageBox.Show("Username and password doesnt match!");
320 break;
321 }
322 return false;
323 }
324 }
325 }
326}