· 5 years ago · Mar 28, 2020, 01:00 PM
1using MySql.Data.MySqlClient;
2using Rocket.API;
3using Rocket.Core;
4using Rocket.Core.Permissions;
5using Rocket.Unturned.Chat;
6using Rocket.Unturned.Player;
7using SDG.Unturned;
8using Steamworks;
9using System;
10using System.Data;
11using System.Text;
12using UnityEngine;
13
14namespace Dark_Web
15{
16 public class DatabaseManager
17 {
18 private Configuration config;
19
20 string username;
21
22 string loginusername;
23
24 int page = 0;
25
26 int pagevehicle = 0;
27
28 public DatabaseManager()
29 {
30 config = DarkWebManager.Instance.Configuration.Instance;
31 new I18N.West.CP1250();
32 CheckSchema();
33 CheckSchema2();
34 }
35
36 private MySqlConnection CreateConnection()
37 {
38 MySqlConnection connection = null;
39
40 if (config.DatabasePort == 0)
41 config.DatabasePort = 3306;
42 if (config.DatabaseAddress == "localhost") config.DatabaseAddress = "127.0.0.1";
43 connection = new MySqlConnection(
44 $"SERVER={config.DatabaseAddress};DATABASE={config.DatabaseName};UID={config.DatabaseUsername}" +
45 $";PASSWORD={config.DatabasePassword};PORT={config.DatabasePort};");
46
47 return connection;
48 }
49
50 public void CheckSchema()
51 {
52 try
53 {
54 MySqlConnection connection = CreateConnection();
55 MySqlCommand command = connection.CreateCommand();
56 command.CommandText = "show tables like '" + config.DatabaseTableName + "'";
57 connection.Open();
58 object test = command.ExecuteScalar();
59
60 if (test == null)
61 {
62 command.CommandText = "CREATE TABLE `" + config.DatabaseTableName + "` (`steam64id` varchar(32) NOT NULL,`username` varchar(32) NOT NULL,`password` varchar(32) NOT NULL,`balance` varchar(32) NOT NULL, `job` varchar(32) NOT NULL, PRIMARY KEY (`steam64id`));";
63 command.ExecuteNonQuery();
64 }
65 connection.Close();
66 }
67 catch (Exception ex)
68 {
69 Rocket.Core.Logging.Logger.LogException(ex);
70 }
71 }
72 public void CheckSchema2()
73 {
74 try
75 {
76 MySqlConnection connection = CreateConnection();
77 MySqlCommand command = connection.CreateCommand();
78 command.CommandText = "show tables like '" + config.DatabaseVehicleName + "'";
79 connection.Open();
80 object test = command.ExecuteScalar();
81
82 if (test == null)
83 {
84 command.CommandText = "CREATE TABLE `" + config.DatabaseVehicleName + "` (`AdID` INT(11) NOT NULL,`VehicleID` varchar(32) NOT NULL,`name` varchar(64) NOT NULL,`price` varchar(32) NOT NULL,`seller` varchar(32) NOT NULL, `buyer` varchar(32) NOT NULL, PRIMARY KEY (`AdID`));";
85 command.ExecuteNonQuery();
86 }
87 connection.Close();
88 }
89 catch (Exception ex)
90 {
91 Rocket.Core.Logging.Logger.LogException(ex);
92 }
93 }
94
95 public void Check(UnturnedPlayer player, string name)
96 {
97 try
98 {
99 if (!EntryExist(name))
100 {
101 EffectManager.sendUIEffect(12686, 12686, player.CSteamID, true, name);
102 }
103 else
104 {
105 DarkWebManager.Instance.GenerateUserName(config.usernamelength, false, player);
106 }
107 }
108 catch (Exception ex)
109 {
110 Rocket.Core.Logging.Logger.LogException(ex);
111 }
112 }
113
114 public void RegisterUser(UnturnedPlayer player, string name, string password, int balance, string job)
115 {
116 try
117 {
118 if (!EntryExist(name) && !EntryExist2(player.CSteamID))
119 {
120 if(name != null)
121 {
122 MySqlConnection connection = CreateConnection();
123 MySqlCommand command = connection.CreateCommand();
124
125 command.CommandText = "INSERT INTO " + config.DatabaseTableName + "(steam64id, username, password, balance, job) VALUES ('" + player.CSteamID + "', '" + name + "', '" + password + "', '" + balance + "', '" + job + "')";
126
127 connection.Open();
128 command.ExecuteNonQuery();
129 connection.Close();
130 EffectManager.askEffectClearByID(12686, player.CSteamID);
131 EffectManager.sendUIEffect(12684, 12684, player.CSteamID, true, "Successfully Created account with name: " + name);
132 player.Player.gameObject.AddComponent<UIManager>().Initialize(player);
133 GetLogin(player);
134 }
135 else
136 {
137 MySqlConnection connection = CreateConnection();
138 MySqlCommand command = connection.CreateCommand();
139
140 command.CommandText = "INSERT INTO " + config.DatabaseTableName + "(steam64id, username, password, balance, job) VALUES ('" + player.CSteamID + "', '" + username + "', '" + password + "', '" + balance + "', '" + job + "')";
141
142 connection.Open();
143 command.ExecuteNonQuery();
144 connection.Close();
145 EffectManager.askEffectClearByID(12686, player.CSteamID);
146 EffectManager.sendUIEffect(12684, 12684, player.CSteamID, true, "Successfully Created account with name: " + username);
147 player.Player.gameObject.AddComponent<UIManager>().Initialize(player);
148 GetLogin(player);
149 }
150 }
151 else
152 {
153 EffectManager.sendUIEffect(12684, 12684, player.CSteamID, true, "Failed to create account");
154 player.Player.gameObject.AddComponent<UIManager>().Initialize(player);
155 }
156 }
157 catch (Exception ex)
158 {
159 Rocket.Core.Logging.Logger.LogException(ex);
160 }
161 }
162
163 public void GetUserCrypto(UnturnedPlayer player)
164 {
165 try
166 {
167 if (EntryExist2(player.CSteamID))
168 {
169 MySqlConnection connection = CreateConnection();
170 MySqlCommand command = connection.CreateCommand();
171
172 command.CommandText = "select * from `" + config.DatabaseTableName + "` where `steam64id` = '" + player.CSteamID + "'";
173 connection.Open();
174 MySqlDataReader reader = command.ExecuteReader();
175 if (reader.Read())
176 {
177 string name = reader.GetString(1);
178 string password = reader.GetString(2);
179 int balance = reader.GetInt32(3);
180 connection.Close();
181
182 EffectManager.askEffectClearByID(12688, player.CSteamID);
183 EffectManager.sendUIEffect(12685, 12685, player.CSteamID, true, "Balance: " + balance);
184 }
185 }
186 else
187 {
188 EffectManager.sendUIEffect(12684, 12684, player.CSteamID, true, "No account found!");
189 player.Player.gameObject.AddComponent<UIManager>().Initialize(player);
190 }
191 }
192 catch (Exception ex)
193 {
194 Rocket.Core.Logging.Logger.LogException(ex);
195 }
196 }
197
198 public void GetVehicles(UnturnedPlayer player, bool next, bool prev)
199 {
200 try
201 {
202 if(next == true)
203 {
204 page++;
205 if (VehicleListExist(page))
206 {
207 MySqlConnection connection = CreateConnection();
208 MySqlCommand command = connection.CreateCommand();
209
210 command.CommandText = "select * from `" + config.DatabaseVehicleName + "` where `AdID` = '" + page + "'";
211 connection.Open();
212 MySqlDataReader reader = command.ExecuteReader();
213 if (reader.Read())
214 {
215 EffectManager.sendUIEffect(12690, 12690, player.CSteamID, true, reader[2].ToString(), reader[3].ToString(), reader[4].ToString());
216 connection.Close();
217 reader.Close();
218 }
219 }
220 else if(page != DarkWebManager.Instance.Configuration.Instance.maxList +1)
221 {
222 EffectManager.sendUIEffect(12690, 12690, player.CSteamID, true);
223 GetVehicles(player, true, false);
224 }
225 else
226 {
227 EffectManager.sendUIEffect(12684, 12684, player.CSteamID, true, "No more vehicles found.");
228 player.Player.gameObject.AddComponent<UIManager>().Initialize(player);
229 page--;
230 }
231 }
232 else if(prev == true)
233 {
234 if(page - 1 == -1)
235 {
236 page = 0;
237 }
238 else
239 {
240 page--;
241 if (VehicleListExist(page))
242 {
243 MySqlConnection connection = CreateConnection();
244 MySqlCommand command = connection.CreateCommand();
245
246 command.CommandText = "select * from `" + config.DatabaseVehicleName + "` where `AdID` = '" + page + "'";
247 connection.Open();
248 MySqlDataReader reader = command.ExecuteReader();
249 if (reader.Read())
250 {
251 EffectManager.sendUIEffect(12690, 12690, player.CSteamID, true, reader[2].ToString(), reader[3].ToString(), reader[4].ToString());
252 connection.Close();
253 reader.Close();
254 }
255 }
256 else if (page != 0)
257 {
258 EffectManager.sendUIEffect(12690, 12690, player.CSteamID, true);
259 GetVehicles(player, false, true);
260 }
261 else
262 {
263 EffectManager.sendUIEffect(12684, 12684, player.CSteamID, true, "No previous vehicle found.");
264 player.Player.gameObject.AddComponent<UIManager>().Initialize(player);
265 page++;
266 }
267 }
268 }
269 else
270 {
271 if (VehicleListExist(page))
272 {
273 MySqlConnection connection = CreateConnection();
274 MySqlCommand command = connection.CreateCommand();
275
276 command.CommandText = "select * from `" + config.DatabaseVehicleName + "` where `AdID` = '" + page + "'";
277 connection.Open();
278 MySqlDataReader reader = command.ExecuteReader();
279 if (reader.Read())
280 {
281 EffectManager.sendUIEffect(12690, 12690, player.CSteamID, true, reader[2].ToString(), reader[3].ToString(), reader[4].ToString());
282 connection.Close();
283 reader.Close();
284 }
285 }
286 else
287 {
288 GetVehicles(player, true, false);
289 }
290 }
291 }
292 catch (Exception ex)
293 {
294 Rocket.Core.Logging.Logger.LogException(ex);
295 }
296 }
297
298 public void AddVehicle(UnturnedPlayer player, int vehicleid, string name, int price, CSteamID buyer)
299 {
300 try
301 {
302 if (!VehicleListExist(pagevehicle))
303 {
304 MySqlConnection connection = CreateConnection();
305 MySqlCommand command = connection.CreateCommand();
306
307 command.CommandText = "INSERT INTO " + config.DatabaseVehicleName + "(AdID, VehicleID, name, price, seller, buyer) VALUES ('" + pagevehicle + "', '" + vehicleid + "', '" + name + "', '" + price + "', 'null' , '" + buyer + "')";
308 connection.Open();
309 command.ExecuteNonQuery();
310 connection.Close();
311 EffectManager.askEffectClearByID(12680, player.CSteamID);
312 EffectManager.sendUIEffect(12681, 12681, player.CSteamID, true);
313 EffectManager.sendUIEffect(12684, 12684, player.CSteamID, true, "You successfully created a listing with the ID: " + pagevehicle);
314 player.Player.gameObject.AddComponent<UIManager>().Initialize(player);
315 }
316 else
317 {
318 pagevehicle++;
319 AddVehicle(player, vehicleid, name, price, buyer);
320 }
321 }
322 catch (Exception ex)
323 {
324 Rocket.Core.Logging.Logger.LogException(ex);
325 }
326 }
327
328 public bool VehicleListExist(int page)
329 {
330 try
331 {
332 MySqlConnection connection = CreateConnection();
333 MySqlCommand command = connection.CreateCommand();
334 int exists = 0;
335 command.CommandText = "SELECT EXISTS(SELECT 1 FROM `" + config.DatabaseVehicleName + "` WHERE `AdID` ='" + page + "' LIMIT 1);";
336 connection.Open();
337 object result = command.ExecuteScalar();
338 if (result != null) Int32.TryParse(result.ToString(), out exists);
339 connection.Close();
340
341 if (exists != 0)
342 {
343 return true;
344 }
345 }
346 catch (Exception ex)
347 {
348 Rocket.Core.Logging.Logger.LogException(ex);
349 }
350
351 return false;
352 }
353
354 public void AddBalance(UnturnedPlayer player, int amount)
355 {
356 try
357 {
358 if (EntryExist2(player.CSteamID))
359 {
360 amount = 0;
361 MySqlConnection connection = CreateConnection();
362 MySqlCommand command = connection.CreateCommand();
363
364
365 command.CommandText = "select * from `" + config.DatabaseTableName + "` where `steam64id` = '" + player.CSteamID + "'";
366 connection.Open();
367 MySqlDataReader reader = command.ExecuteReader();
368 if (reader.Read())
369 {
370 int balance = reader.GetInt32(3);
371 connection.Close();
372
373 balance += amount;
374
375 command.CommandText = string.Concat(new object[]
376 {
377 "update `",
378 config.DatabaseTableName,
379 "` set `balance`='"+balance+"' where `steam64id`='"+player.CSteamID+"' limit 1;"
380 });
381 connection.Open();
382 command.ExecuteNonQuery();
383 connection.Close();
384
385 EffectManager.askEffectClearByID(12685, player.CSteamID);
386 EffectManager.sendUIEffect(12685, 12685, player.CSteamID, true, "Balance: " + balance);
387 EffectManager.sendUIEffect(12684, 12684, player.CSteamID, true, "Successfully added: " + amount + " to your balance");
388 player.Player.gameObject.AddComponent<UIManager>().Initialize(player);
389 }
390 }
391 else
392 {
393 EffectManager.sendUIEffect(12684, 12684, player.CSteamID, true, "No account found!");
394 player.Player.gameObject.AddComponent<UIManager>().Initialize(player);
395 }
396 }
397 catch (Exception ex)
398 {
399 Rocket.Core.Logging.Logger.LogException(ex);
400 }
401 }
402
403 public void GetBalance(UnturnedPlayer player)
404 {
405 try
406 {
407 if (EntryExist2(player.CSteamID))
408 {
409 MySqlConnection connection = CreateConnection();
410 MySqlCommand command = connection.CreateCommand();
411
412 command.CommandText = "select * from `" + config.DatabaseTableName + "` where `steam64id` = '" + player.CSteamID + "'";
413 connection.Open();
414 MySqlDataReader reader = command.ExecuteReader();
415 if (reader.Read())
416 {
417 int balance = reader.GetInt32(3);
418 connection.Close();
419
420 EffectManager.askEffectClearByID(12685, player.CSteamID);
421 EffectManager.sendUIEffect(12688, 12688, player.CSteamID, true,"Balance: " + balance);
422 }
423 }
424 else
425 {
426 EffectManager.sendUIEffect(12684, 12684, player.CSteamID, true, "No account found!");
427 player.Player.gameObject.AddComponent<UIManager>().Initialize(player);
428 }
429 }
430 catch (Exception ex)
431 {
432 Rocket.Core.Logging.Logger.LogException(ex);
433 }
434 }
435 public void BuyBitcoin(UnturnedPlayer player, int amount)
436 {
437 try
438 {
439 if (EntryExist2(player.CSteamID))
440 {
441 amount = 0;
442 MySqlConnection connection = CreateConnection();
443 MySqlCommand command = connection.CreateCommand();
444
445 command.CommandText = "select * from `" + config.DatabaseTableName + "` where `steam64id` = '" + player.CSteamID + "'";
446 connection.Open();
447 MySqlDataReader reader = command.ExecuteReader();
448 if (reader.Read())
449 {
450 int balance = reader.GetInt32(3);
451 connection.Close();
452
453 if(balance >= amount)
454 {
455 balance -= amount;
456
457 command.CommandText = string.Concat(new object[]
458 {
459 "update `",
460 config.DatabaseTableName,
461 "` set `balance`='"+balance+"' where `steam64id`='"+player.CSteamID+"' limit 1;"
462 });
463 connection.Open();
464 command.ExecuteNonQuery();
465 connection.Close();
466
467 EffectManager.askEffectClearByID(12685, player.CSteamID);
468 EffectManager.sendUIEffect(12685, 12685, player.CSteamID, true, "Balance: " + balance);
469 EffectManager.sendUIEffect(12684, 12684, player.CSteamID, true, "Successfully bought a bitcoin.");
470 player.Player.gameObject.AddComponent<UIManager>().Initialize(player);
471 player.GiveItem(50308, 1);
472 }
473 else
474 {
475 EffectManager.sendUIEffect(12684, 12684, player.CSteamID, true, "You don't have enough money in your balance.");
476 player.Player.gameObject.AddComponent<UIManager>().Initialize(player);
477 }
478 }
479 }
480 else
481 {
482 EffectManager.sendUIEffect(12684, 12684, player.CSteamID, true, "No account found!");
483 player.Player.gameObject.AddComponent<UIManager>().Initialize(player);
484 }
485 }
486 catch (Exception ex)
487 {
488 Rocket.Core.Logging.Logger.LogException(ex);
489 }
490 }
491
492 public void Login(UnturnedPlayer player, string password)
493 {
494 try
495 {
496 if (EntryExist(loginusername) && EntryExist2(player.CSteamID))
497 {
498 MySqlConnection connection = CreateConnection();
499 MySqlCommand command = connection.CreateCommand();
500
501 command.CommandText = "select * from `" + config.DatabaseTableName + "` where `steam64id` = '" + player.CSteamID + "'";
502 connection.Open();
503 MySqlDataReader reader = command.ExecuteReader();
504 if (reader.Read())
505 {
506 string Name = reader.GetString(1);
507 string Password = reader.GetString(2);
508 int balance = reader.GetInt32(3);
509 connection.Close();
510
511 if (loginusername == Name && Password == password)
512 {
513 EffectManager.askEffectClearByID(12687, player.CSteamID);
514 EffectManager.sendUIEffect(12688, 12688, player.CSteamID, true, "Balance: " + balance);
515 EffectManager.sendUIEffect(12684, 12684, player.CSteamID, true, "Successfully logged into your account.");
516 player.Player.gameObject.AddComponent<UIManager>().Initialize(player);
517 }
518 else
519 {
520 EffectManager.sendUIEffect(12684, 12684, player.CSteamID, true, "Username or password is wrong!");
521 player.Player.gameObject.AddComponent<UIManager>().Initialize(player);
522 }
523 }
524 }
525 else
526 {
527 EffectManager.sendUIEffect(12684, 12684, player.CSteamID, true, "No account found!");
528 player.Player.gameObject.AddComponent<UIManager>().Initialize(player);
529 }
530 }
531 catch (Exception ex)
532 {
533 Rocket.Core.Logging.Logger.LogException(ex);
534 }
535 }
536
537 public void GetLogin(UnturnedPlayer player)
538 {
539 try
540 {
541 if (EntryExist2(player.CSteamID))
542 {
543 MySqlConnection connection = CreateConnection();
544 MySqlCommand command = connection.CreateCommand();
545
546 command.CommandText = "select * from `" + config.DatabaseTableName + "` where `steam64id` = '" + player.CSteamID + "'";
547 connection.Open();
548 MySqlDataReader reader = command.ExecuteReader();
549 if (reader.Read())
550 {
551 string Name = reader.GetString(1);
552 connection.Close();
553 loginusername = Name;
554 EffectManager.sendUIEffect(12687, 12687, player.CSteamID, true, Name);
555 }
556 }
557 else
558 {
559 EffectManager.askEffectClearByID(12689, player.CSteamID);
560 GenerateUserName(DarkWebManager.Instance.Configuration.Instance.usernamelength, false, player);
561 EffectManager.sendUIEffect(12686, 12686, player.CSteamID, true, username);
562 EffectManager.sendUIEffect(12684, 12684, player.CSteamID, true, "No account found!");
563 player.Player.gameObject.AddComponent<UIManager>().Initialize(player);
564 }
565 }
566 catch (Exception ex)
567 {
568 Rocket.Core.Logging.Logger.LogException(ex);
569 }
570 }
571
572 public void GetPassword(UnturnedPlayer player)
573 {
574 try
575 {
576 if (EntryExist2(player.CSteamID))
577 {
578 MySqlConnection connection = CreateConnection();
579 MySqlCommand command = connection.CreateCommand();
580
581 command.CommandText = "select * from `" + config.DatabaseTableName + "` where `steam64id` = '" + player.CSteamID + "'";
582 connection.Open();
583 MySqlDataReader reader = command.ExecuteReader();
584 if (reader.Read())
585 {
586 string name = reader.GetString(1);
587 string password = reader.GetString(1);
588 connection.Close();
589
590 EffectManager.sendUIEffect(12684, 12684, player.CSteamID, true, "Your password is: " + password);
591 player.Player.gameObject.AddComponent<UIManager>().Initialize(player);
592 }
593 }
594 else
595 {
596 EffectManager.sendUIEffect(12684, 12684, player.CSteamID, true, "No account found!");
597 player.Player.gameObject.AddComponent<UIManager>().Initialize(player);
598 }
599 }
600 catch (Exception ex)
601 {
602 Rocket.Core.Logging.Logger.LogException(ex);
603 }
604 }
605
606 public bool EntryExist(string name)
607 {
608 try
609 {
610 MySqlConnection connection = CreateConnection();
611 MySqlCommand command = connection.CreateCommand();
612 int exists = 0;
613 command.CommandText = "SELECT EXISTS(SELECT 1 FROM `" + config.DatabaseTableName + "` WHERE `username` ='" + name + "' LIMIT 1);";
614 connection.Open();
615 object result = command.ExecuteScalar();
616 if (result != null) Int32.TryParse(result.ToString(), out exists);
617 connection.Close();
618
619 if (exists != 0)
620 {
621 return true;
622 }
623 }
624 catch (Exception ex)
625 {
626 Rocket.Core.Logging.Logger.LogException(ex);
627 }
628
629 return false;
630 }
631
632 public bool EntryExist2(CSteamID id)
633 {
634 try
635 {
636 MySqlConnection connection = CreateConnection();
637 MySqlCommand command = connection.CreateCommand();
638 int exists = 0;
639 command.CommandText = "SELECT EXISTS(SELECT 1 FROM `" + config.DatabaseTableName + "` WHERE `steam64id` ='" + id + "' LIMIT 1);";
640 connection.Open();
641 object result = command.ExecuteScalar();
642 if (result != null) Int32.TryParse(result.ToString(), out exists);
643 connection.Close();
644
645 if (exists != 0)
646 {
647 return true;
648 }
649 }
650 catch (Exception ex)
651 {
652 Rocket.Core.Logging.Logger.LogException(ex);
653 }
654
655 return false;
656 }
657
658 public string GenerateUserName(int size, bool lowerCase, UnturnedPlayer player)
659 {
660 StringBuilder builder = new StringBuilder();
661 System.Random random = new System.Random();
662 char ch;
663 for (int i = 0; i < size; i++)
664 {
665 ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
666 builder.Append(ch);
667 }
668 if (lowerCase)
669 Check(player, builder.ToString().ToLower());
670
671 Check(player, builder.ToString());
672 username = builder.ToString();
673 return builder.ToString();
674 }
675 }
676}