Advertisement
Guest User

Untitled

a guest
Sep 19th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 58.92 KB | None | 0 0
  1. using MySql.Data.MySqlClient;
  2. using Rocket.API;
  3. using Rocket.Core.Logging;
  4. using Rocket.Unturned.Chat;
  5. using System;
  6. using System.Collections.Generic;
  7. using Steamworks;
  8. using Rocket.Unturned.Player;
  9.  
  10. namespace rawrfuls.ThePunisher
  11. {
  12. public class DatabaseManager
  13. {
  14. #region general funcs
  15. public int getTotalRows(string table)
  16. {
  17. MySqlConnection connection = createConnection();
  18. using (MySqlCommand cmd = new MySqlCommand("SELECT COUNT(*) FROM `" + table + "`", connection))
  19. {
  20. connection.Open();
  21. object totalrows = cmd.ExecuteScalar();
  22. connection.Close();
  23. return Convert.ToInt32(totalrows);
  24. }
  25. }
  26.  
  27. private MySqlConnection createConnection()
  28. {
  29. MySqlConnection connection = null;
  30. try
  31. {
  32. if (ThePunisher.Instance.Configuration.Instance.DatabasePort == 0) ThePunisher.Instance.Configuration.Instance.DatabasePort = 3306;
  33. connection = new MySqlConnection(String.Format("SERVER={0};DATABASE={1};UID={2};PASSWORD={3};PORT={4};", ThePunisher.Instance.Configuration.Instance.DatabaseAddress, ThePunisher.Instance.Configuration.Instance.DatabaseName, ThePunisher.Instance.Configuration.Instance.DatabaseUsername, ThePunisher.Instance.Configuration.Instance.DatabasePassword, ThePunisher.Instance.Configuration.Instance.DatabasePort));
  34. }
  35. catch (Exception ex)
  36. {
  37. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  38. Logger.LogException(ex);
  39. }
  40. return connection;
  41. }
  42.  
  43. public DatabaseManager()
  44. {
  45. new I18N.West.CP1250();
  46. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  47. {
  48. Logger.Log("Boot Sequence Initialized...");
  49. Logger.Log("Checking tables");
  50. }
  51. CheckBanSchema();
  52. CheckChatBanSchema();
  53. CheckWarnSchema();
  54. CheckReportSchema();
  55. CheckRewardlogSchema();
  56. CheckWhitelistSchema();
  57. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  58. EndSchemaChecks();
  59. }
  60.  
  61.  
  62. public int convertTimeToSeconds(string time)
  63. {
  64. int ret = 0;
  65. int tm = 0;
  66. if (time == null)
  67. return 0;
  68.  
  69. if (time.Contains("d"))
  70. {
  71. if (int.TryParse(time.Replace("d", ""), out tm))
  72. ret = (tm * 86400);
  73. }
  74. else if (time.Contains("h"))
  75. {
  76. if (int.TryParse(time.Replace("h", ""), out tm))
  77. ret = (tm * 3600);
  78. }
  79. else if (time.Contains("m"))
  80. {
  81. if (int.TryParse(time.Replace("m", ""), out tm))
  82. ret = (tm * 60);
  83. }
  84. else if (time.Contains("s"))
  85. {
  86. if (int.TryParse(time.Replace("s", ""), out tm))
  87. ret = tm;
  88. }
  89. else
  90. {
  91. if (int.TryParse(time, out tm))
  92. {
  93. //assume seconds, do nothing
  94. ret = tm;
  95. }
  96. else
  97. {
  98. //they fked it up, return 0;
  99. ret = 0;
  100. }
  101. }
  102. return ret;
  103. }
  104.  
  105. public void logItem(string item)
  106. {
  107. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  108. Logger.Log(item);
  109. }
  110. #endregion
  111.  
  112. #region general table manipulation
  113. public bool columnExists(string table_name, string table_schema, string column_name)
  114. {
  115. bool ret = true;
  116. MySqlConnection connection = createConnection();
  117. MySqlCommand command = connection.CreateCommand();
  118. command.CommandText = "SELECT NULL FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '" + table_name + "' AND table_schema = '" + table_schema + "' AND column_name = '" + column_name + "'";
  119. connection.Open();
  120. object test = command.ExecuteScalar();
  121.  
  122. if (test == null)
  123. {
  124. ret = false;
  125. }
  126.  
  127. connection.Close();
  128. return ret;
  129. }
  130. public bool alterTable(string tablename, string column, string columntype, string mode, string placement)
  131. {
  132. bool ret = true;
  133. try
  134. {
  135. MySqlConnection connection = createConnection();
  136. MySqlCommand command = connection.CreateCommand();
  137. string cmdtxt = "";
  138. if (mode == "ADD")
  139. {
  140. cmdtxt = "ALTER TABLE `" + tablename + "` ADD " + column + " " + columntype + " " + placement + ";";
  141. }
  142. else if (mode == "DROP")
  143. {
  144. cmdtxt = "ALTER TABLE `" + tablename + "` DROP " + column + ";";
  145. }
  146. else if (mode == "ADDPRIMARYKEY")
  147. {
  148. cmdtxt = "ALTER TABLE `" + tablename + "` ADD PRIMARY KEY(" + column + ");";
  149. }
  150. else if (mode == "ADDUNIQUE")
  151. {
  152. cmdtxt = "ALTER TABLE `" + tablename + "` ADD UNIQUE(" + column + ");";
  153. }
  154. else if (mode == "CLEARINDEXES")
  155. {
  156. cmdtxt = "ALTER TABLE `" + tablename + "` DROP INDEX " + column + ";";
  157. }
  158. else if (mode == "CLEARPRIMARYKEY")
  159. {
  160. cmdtxt = "ALTER TABLE `" + tablename + "` DROP PRIMARY KEY;";
  161. }
  162. command.CommandText = cmdtxt;
  163. connection.Open();
  164. command.ExecuteNonQuery();
  165. connection.Close();
  166. }
  167. catch
  168. {
  169. ret = false;
  170. }
  171. return ret;
  172. }
  173. #endregion
  174.  
  175. #region Schema Checks
  176. private bool BanLoaded = false;
  177. private bool ChatBanLoaded = false;
  178. private bool WarnLoaded = false;
  179. private bool ReportLoaded = false;
  180. private bool RewardLogLoaded = false;
  181. private bool WhitelistLoaded = false;
  182. #region check ban schema
  183.  
  184. public void CheckBanSchema()
  185. {
  186. try
  187. {
  188. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  189. {
  190. Logger.Log("Checking Ban Table...");
  191. }
  192. MySqlConnection connection = createConnection();
  193. MySqlCommand command = connection.CreateCommand();
  194. command.CommandText = "show tables like '" + ThePunisher.Instance.Configuration.Instance.DatabaseBanTableName + "'";
  195. connection.Open();
  196. object test = command.ExecuteScalar();
  197.  
  198. if (test == null)
  199. {
  200. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  201. {
  202. Logger.Log("Creating Ban Table...");
  203. }
  204. command.CommandText = "CREATE TABLE `" + ThePunisher.Instance.Configuration.Instance.DatabaseBanTableName + "` (`id` int(11) NOT NULL AUTO_INCREMENT,`steamId` varchar(32) NOT NULL,`admin` varchar(32) NOT NULL,`adminname` varchar(255) DEFAULT NULL,`banMessage` varchar(512) DEFAULT NULL,`charactername` varchar(255) DEFAULT NULL,`banDuration` int NULL,`banTime` timestamp NULL ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY (`id`));";
  205. command.ExecuteNonQuery();
  206. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  207. {
  208. Logger.Log("Ban Table Successfully created...");
  209. }
  210. }
  211. else
  212. {
  213. //check if table needs to be upgraded
  214. //first check - add "adminname" field"
  215. logItem("Checking existing table structure");
  216. if (!columnExists(ThePunisher.Instance.Configuration.Instance.DatabaseBanTableName, ThePunisher.Instance.Configuration.Instance.DatabaseName, "adminname"))
  217. {
  218. logItem("adminName column not found, creating!");
  219. //adminname column doesnt exist, alter the table.
  220. if (alterTable(ThePunisher.Instance.Configuration.Instance.DatabaseBanTableName, "adminname", "varchar(255)", "ADD", "AFTER admin"))
  221. logItem("adminName column added successfully!");
  222. }
  223. if (columnExists(ThePunisher.Instance.Configuration.Instance.DatabaseBanTableName, ThePunisher.Instance.Configuration.Instance.DatabaseName, "chatbanned"))
  224. {
  225. logItem("Found chatbanned column, removing.");
  226. if (alterTable(ThePunisher.Instance.Configuration.Instance.DatabaseBanTableName, "chatbanned", "", "DROP", ""))
  227. logItem("Successfully removed chatbanned column.");
  228. }
  229. }
  230. connection.Close();
  231. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  232. {
  233. Logger.Log("Ban Table Schema successfully validated...");
  234. }
  235. BanLoaded = true;
  236. }
  237. catch (Exception ex)
  238. {
  239. Logger.LogException(ex);
  240. }
  241. }
  242. #endregion
  243. #region check mute schema
  244. public void CheckChatBanSchema()
  245. {
  246. try
  247. {
  248. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  249. {
  250. Logger.Log("Checking chat ban table...");
  251. }
  252. MySqlConnection connection = createConnection();
  253. MySqlCommand command = connection.CreateCommand();
  254. command.CommandText = "show tables like '" + ThePunisher.Instance.Configuration.Instance.DatabaseChatbanTableName + "'";
  255. connection.Open();
  256. object test = command.ExecuteScalar();
  257.  
  258. if (test == null)
  259. {
  260. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  261. {
  262. Logger.Log("Creating Chat Ban Table...");
  263. }
  264. command.CommandText = "CREATE TABLE `" + ThePunisher.Instance.Configuration.Instance.DatabaseChatbanTableName + "` (`steamId` varchar(32) NOT NULL UNIQUE,`admin` varchar(32) NOT NULL,`banMessage` varchar(512) DEFAULT NULL,`charactername` varchar(255) DEFAULT NULL,`banDuration` int NULL,`banTime` timestamp NULL ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY (`steamId`));";
  265. command.ExecuteNonQuery();
  266. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  267. {
  268. Logger.Log("Chat Ban Table Successfully created...");
  269. }
  270. }
  271. else
  272. {
  273. //check if table needs to be upgraded
  274. //first check - add "adminname" field"
  275. logItem("Checking existing table structure");
  276. if (columnExists(ThePunisher.Instance.Configuration.Instance.DatabaseChatbanTableName, ThePunisher.Instance.Configuration.Instance.DatabaseName, "id"))
  277. {
  278. logItem("Found outdated table structure. Upgrading.");
  279. command.CommandText = "DROP TABLE " + ThePunisher.Instance.Configuration.Instance.DatabaseChatbanTableName + ";";
  280. command.ExecuteNonQuery();
  281. command.CommandText = "CREATE TABLE `" + ThePunisher.Instance.Configuration.Instance.DatabaseChatbanTableName + "` (`steamId` varchar(32) NOT NULL UNIQUE,`admin` varchar(32) NOT NULL,`banMessage` varchar(512) DEFAULT NULL,`charactername` varchar(255) DEFAULT NULL,`banDuration` int NULL,`banTime` timestamp NULL ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY (`steamId`));";
  282. command.ExecuteNonQuery();
  283. }
  284.  
  285. }
  286. connection.Close();
  287. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  288. {
  289. Logger.Log("Chat Ban Table Schema successfully validated...");
  290. }
  291. ChatBanLoaded = true;
  292. }
  293. catch (Exception ex)
  294. {
  295. Logger.LogException(ex);
  296. }
  297. }
  298. #endregion
  299. #region check warning schema
  300. public void CheckWarnSchema()
  301. {
  302. try
  303. {
  304. logItem("Checking Warning Table...");
  305. MySqlConnection connection = createConnection();
  306. MySqlCommand command = connection.CreateCommand();
  307. command.CommandText = "show tables like '" + ThePunisher.Instance.Configuration.Instance.DatabaseWarningTableName + "'";
  308. connection.Open();
  309. object test = command.ExecuteScalar();
  310.  
  311. if (test == null)
  312. {
  313. logItem("Creating Warning Table...");
  314. command.CommandText = string.Concat("CREATE TABLE `", ThePunisher.Instance.Configuration.Instance.DatabaseWarningTableName, "` (`steamId` varchar(32) NOT NULL,`warninglevel` tinyint unsigned NOT NULL,`lastwarningdate` TIMESTAMP NOT NULL DEFAULT current_timestamp, PRIMARY KEY (`steamId`)) ");
  315. command.ExecuteNonQuery();
  316. logItem("Warning Table Successfully created...");
  317. }
  318. connection.Close();
  319. logItem("Warning Table Schema successfully validated...");
  320. WarnLoaded = true;
  321. }
  322. catch (Exception ex)
  323. {
  324. logItem(ex.Message);
  325. //Logger.LogException(ex);
  326. }
  327. }
  328. #endregion
  329. #region check report schema
  330. public void CheckReportSchema()
  331. {
  332. try
  333. {
  334. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  335. {
  336. Logger.Log("Checking Report table...");
  337. }
  338. MySqlConnection connection = createConnection();
  339. MySqlCommand command = connection.CreateCommand();
  340. command.CommandText = "show tables like '" + ThePunisher.Instance.Configuration.Instance.DatabaseReportTableName + "'";
  341. connection.Open();
  342. object test = command.ExecuteScalar();
  343.  
  344. if (test == null)
  345. {
  346. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  347. {
  348. Logger.Log("Creating Report Table...");
  349. }
  350. command.CommandText = "CREATE TABLE `" + ThePunisher.Instance.Configuration.Instance.DatabaseReportTableName + "` (`id` int(11) NOT NULL AUTO_INCREMENT,`steamId` varchar(32) NOT NULL,`reportee` varchar(32) NOT NULL,`reportMessage` varchar(512) DEFAULT NULL,`charactername` varchar(255) DEFAULT NULL,`reportTime` timestamp NULL ON UPDATE CURRENT_TIMESTAMP,`rewarded` TEXT NULL DEFAULT NULL,PRIMARY KEY (`id`));";
  351. command.ExecuteNonQuery();
  352. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  353. {
  354. Logger.Log("Report table Successfully created...");
  355. }
  356. }
  357. connection.Close();
  358. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  359. {
  360. Logger.Log("Report Table Schema successfully validated...");
  361. }
  362. ReportLoaded = true;
  363. }
  364. catch (Exception ex)
  365. {
  366. Logger.LogException(ex);
  367. }
  368. }
  369. #endregion
  370. #region check reward log schema
  371. public void CheckRewardlogSchema()
  372. {
  373. try
  374. {
  375. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  376. {
  377. Logger.Log("Checking Reward Log table...");
  378. }
  379. MySqlConnection connection = createConnection();
  380. MySqlCommand command = connection.CreateCommand();
  381. command.CommandText = "show tables like '" + ThePunisher.Instance.Configuration.Instance.DatabaseRewardlogTableName + "'";
  382. connection.Open();
  383. object test = command.ExecuteScalar();
  384.  
  385. if (test == null)
  386. {
  387. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  388. {
  389. Logger.Log("Creating Reward Log Table...");
  390. }
  391. command.CommandText = "CREATE TABLE `" + ThePunisher.Instance.Configuration.Instance.DatabaseRewardlogTableName + "` (`id` int(11) NOT NULL AUTO_INCREMENT,`steamId` varchar(32) NOT NULL,`reportee` varchar(32) NOT NULL,`reportMessage` varchar(512) DEFAULT NULL,`charactername` varchar(255) DEFAULT NULL,`reportTime` timestamp NULL ON UPDATE CURRENT_TIMESTAMP,`rewarded` TEXT NULL DEFAULT NULL,PRIMARY KEY (`id`));";
  392. command.ExecuteNonQuery();
  393. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  394. {
  395. Logger.Log("Reward log table Successfully created...");
  396. }
  397. }
  398. connection.Close();
  399. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  400. {
  401. Logger.Log("Reward log Table Schema successfully validated...");
  402. }
  403. RewardLogLoaded = true;
  404. }
  405. catch (Exception ex)
  406. {
  407. Logger.LogException(ex);
  408. }
  409. }
  410. #endregion
  411. #region check whitelist schema
  412. public void CheckWhitelistSchema()
  413. {
  414. if (!ThePunisher.Instance.Configuration.Instance.WhiteListEnabled)
  415. return;
  416.  
  417. try
  418. {
  419. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  420. {
  421. Logger.Log("Checking Whitelist Table...");
  422. }
  423. MySqlConnection connection = createConnection();
  424. MySqlCommand command = connection.CreateCommand();
  425. command.CommandText = "show tables like '" + ThePunisher.Instance.Configuration.Instance.DatabaseWhiteListTableName + "'";
  426. connection.Open();
  427. object test = command.ExecuteScalar();
  428.  
  429. if (test == null)
  430. {
  431. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  432. {
  433. Logger.Log("Creating Whitelist Table...");
  434. }
  435. command.CommandText = "CREATE TABLE `" + ThePunisher.Instance.Configuration.Instance.DatabaseWhiteListTableName + "` (`id` int(11) NOT NULL AUTO_INCREMENT,`steamId` varchar(32) NOT NULL,`admin` varchar(32) NOT NULL,`charactername` varchar(255) DEFAULT NULL,`added_on` timestamp NULL ON UPDATE CURRENT_TIMESTAMP,`chatbanned` TEXT NULL,PRIMARY KEY (`id`));";
  436. command.ExecuteNonQuery();
  437. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  438. {
  439. Logger.Log("Whitelist Table Successfully created...");
  440. }
  441. }
  442. connection.Close();
  443. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  444. {
  445. Logger.Log("Whitelist Table Schema successfully validated...");
  446. }
  447. WhitelistLoaded = true;
  448. }
  449. catch (Exception ex)
  450. {
  451. Logger.LogException(ex);
  452. }
  453. }
  454. #endregion
  455. #region debug messages
  456. public void EndSchemaChecks()
  457. {
  458. if (!BanLoaded)
  459. {
  460. Logger.LogError("Ban table failed to initialize! Unloading now!");
  461. return;
  462. }
  463. if (!ChatBanLoaded)
  464. {
  465. Logger.LogError("Chat Ban table failed to initialize! Unloading now!");
  466. return;
  467. }
  468. if (!WarnLoaded)
  469. {
  470. Logger.LogError("Warning table failed to initialize! Unloading now!");
  471. return;
  472. }
  473. if (!ReportLoaded)
  474. {
  475. Logger.LogError("Reports table failed to initialize! Unloading now!");
  476. return;
  477. }
  478. if (!RewardLogLoaded)
  479. {
  480. Logger.LogError("Reward Log table failed to initialize! Unloading now!");
  481. return;
  482. }
  483. if (!WhitelistLoaded && ThePunisher.Instance.Configuration.Instance.WhiteListEnabled)
  484. {
  485. Logger.LogError("Whitelist table failed to initialize! Unloading now!");
  486. return;
  487. }
  488. Logger.Log("All tables successfully validated and loaded!");
  489. }
  490. #endregion
  491. #endregion
  492.  
  493. #region warnings
  494. public byte GetWarnings(CSteamID id)
  495. {
  496. byte num = 0;
  497. try
  498. {
  499. MySqlConnection mySqlConnection = this.createConnection();
  500. MySqlCommand mySqlCommand = mySqlConnection.CreateCommand();
  501. mySqlCommand.CommandText = string.Concat(new string[] {
  502. "select `warninglevel` from `",
  503. ThePunisher.Instance.Configuration.Instance.DatabaseWarningTableName,
  504. "` where `steamId` = '",
  505. id.ToString(),
  506. "';"
  507. });
  508. mySqlConnection.Open();
  509. object obj = mySqlCommand.ExecuteScalar();
  510. if (obj != null)
  511. {
  512. byte.TryParse(obj.ToString(), out num);
  513. }
  514. mySqlConnection.Close();
  515. }
  516. catch (Exception exception)
  517. {
  518. Logger.LogException(exception);
  519. }
  520. return num;
  521. }
  522.  
  523. public ushort GetWarningsTime(CSteamID id)
  524. {
  525. ushort num = 0;
  526. try
  527. {
  528. MySqlConnection mySqlConnection = this.createConnection();
  529. MySqlCommand mySqlCommand = mySqlConnection.CreateCommand();
  530. mySqlCommand.CommandText = string.Concat(new string[] {
  531. "select timestampdiff(day, now(), 'select `lastwarningdate` from `",
  532. ThePunisher.Instance.Configuration.Instance.DatabaseWarningTableName,
  533. "` where `steamId` = '",
  534. id.ToString(),
  535. "' ');"
  536. });
  537. mySqlConnection.Open();
  538. object obj = mySqlCommand.ExecuteScalar();
  539. if (obj != null)
  540. {
  541. ushort.TryParse(obj.ToString(), out num);
  542. }
  543. mySqlConnection.Close();
  544. }
  545. catch (Exception exception)
  546. {
  547. Logger.LogException(exception);
  548. }
  549. return num;
  550. }
  551.  
  552. public bool EditWarning(CSteamID id, short amt = 1)
  553. {
  554. bool success = false;
  555. try
  556. {
  557. MySqlConnection mySqlConnection = this.createConnection();
  558. MySqlCommand mySqlCommand = mySqlConnection.CreateCommand();
  559. mySqlCommand.CommandText = string.Concat(new string[] {
  560. "insert into `" +
  561. ThePunisher.Instance.Configuration.Instance.DatabaseWarningTableName +
  562. "` (steamId, warninglevel) VALUES ('" + id.ToString() + "', 1) on duplicate key update `warninglevel`=`warninglevel`+ " +
  563. amt.ToString()
  564. });
  565. mySqlConnection.Open();
  566. int affected = mySqlCommand.ExecuteNonQuery();
  567. mySqlConnection.Close();
  568. if (affected > 0) success = true;
  569. }
  570. catch (Exception exception)
  571. {
  572. Logger.LogException(exception);
  573. }
  574. return success;
  575. }
  576.  
  577. public bool DeleteWarnings()
  578. {
  579. bool success = false;
  580. try
  581. {
  582. MySqlConnection mySqlConnection = this.createConnection();
  583. MySqlCommand mySqlCommand = mySqlConnection.CreateCommand();
  584. mySqlCommand.CommandText = string.Concat(new string[] {
  585. "delete from `",
  586. ThePunisher.Instance.Configuration.Instance.DatabaseWarningTableName,
  587. "` where `lastwarningdate` < date_sub(now(), interval ",
  588. ThePunisher.Instance.Configuration.Instance.KeepWarningsLengthDays.ToString(),
  589. " day)"
  590. });
  591. mySqlConnection.Open();
  592. int affected = mySqlCommand.ExecuteNonQuery();
  593. mySqlConnection.Close();
  594. if (affected > 0) success = true;
  595. }
  596. catch (Exception exception)
  597. {
  598. Logger.LogException(exception);
  599. }
  600. return success;
  601. }
  602. #endregion
  603.  
  604. #region reports
  605. private int totalRows(string table)
  606. {
  607. MySqlConnection connection = createConnection();
  608. using (MySqlCommand cmd = new MySqlCommand("select * from `" + table + "`", connection))
  609. {
  610. connection.Open();
  611. int cnt = Convert.ToInt32(cmd.ExecuteScalar());
  612. connection.Close();
  613. return cnt;
  614. }
  615. }
  616. public void GetReports(IRocketPlayer caller, int page)
  617. {
  618. try
  619. {
  620. #region pagination
  621. int count = totalRows(ThePunisher.Instance.Configuration.Instance.DatabaseReportTableName);
  622. int limit = 2;
  623. int currentpage = page;
  624. if (page == 0)
  625. currentpage = 1;
  626. else
  627. currentpage = page * 1;
  628.  
  629. #endregion
  630.  
  631.  
  632.  
  633.  
  634. MySqlConnection connection = createConnection();
  635. MySqlCommand command = connection.CreateCommand();
  636. string query = "select `reportee`,`reportMessage`,`charactername`,`reportTime` from `" + ThePunisher.Instance.Configuration.Instance.DatabaseReportTableName + "`";
  637. query = query + " LIMIT " + currentpage.ToString() + "," + limit.ToString();
  638. command.CommandText = query;
  639. connection.Open();
  640. MySqlDataReader reader = command.ExecuteReader();
  641. logItem("Executed MySql Query");
  642. if (reader.HasRows)
  643. {
  644. logItem("Reader object opened");
  645. while (reader.Read())
  646. {
  647. string report = genReport(reader.GetString(0), reader.GetString(1), reader.GetString(2), reader.GetString(3));
  648. UnturnedChat.Say(caller, report);
  649. UnturnedChat.Say(caller, "Showing Page " + currentpage.ToString() + " of 1.");
  650. }
  651. /*command = connection.CreateCommand();
  652. command.Parameters.AddWithValue("@steamId", steamId);
  653. command.Parameters.AddWithValue("@reportee", reportee);
  654. command.Parameters.AddWithValue("@reportMessage", reportMessage);
  655. command.Parameters.AddWithValue("@characterName", characterName);
  656. command.Parameters.AddWithValue("@reportTime", reportTime);
  657. command.Parameters.AddWithValue("@rewarded", rewarded);
  658. command.CommandText = "INSERT INTO `" + ThePunisher.Instance.Configuration.Instance.DatabaseRewardlogTableName + "` (`steamId`, `reportee`, `reportMessage`, `charactername`, `reportTime`, `rewarded`) VALUES (@steamId, @reportee, @reportMessage, @characterName, @reportTime, @rewarded);";
  659. connection.Open();
  660. command.ExecuteNonQuery();
  661. connection.Close();
  662. command = connection.CreateCommand();
  663. command.Parameters.AddWithValue("@steamId", steamId);
  664. command.Parameters.AddWithValue("@reportee", reportee);
  665. command.CommandText = "delete from `" + ThePunisher.Instance.Configuration.Instance.DatabaseReportTableName + "` where `steamId` = @steamId AND `reportee`= @reportee;";
  666. connection.Open();
  667. command.ExecuteNonQuery();
  668. connection.Close();*/
  669. }
  670. else
  671. {
  672. logItem("No rows found.");
  673. UnturnedChat.Say(caller, "No reports found!");
  674. }
  675. connection.Close();
  676. }
  677. catch (Exception ex)
  678. {
  679. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  680. Logger.LogException(ex);
  681. }
  682. }
  683.  
  684. private string genReport(string reportee, string reportMessage, string reportedBy, string reportedOn)
  685. {
  686. string report = "";
  687. string date = "-Unknown Date-";
  688. DateTime dateTime;
  689. if (DateTime.TryParse(reportedOn, out dateTime))
  690. {
  691. date = dateTime.ToString("M/dd/yyyy");
  692. }
  693.  
  694. report = "Player " + reportee + " was reported by " + reportedBy + " on " + date + ". Reason: " + reportMessage;
  695.  
  696. return report;
  697. }
  698.  
  699. public string HasReported(string steamId)
  700. {
  701. string output = null;
  702. try
  703. {
  704. MySqlConnection connection = createConnection();
  705. MySqlCommand command = connection.CreateCommand();
  706. command.Parameters.AddWithValue("@reportee", steamId);
  707. command.CommandText = "select `reportMessage` from `" + ThePunisher.Instance.Configuration.Instance.DatabaseReportTableName + "` where `reportee` = @reportee and `rewarded` IS NULL;";
  708. connection.Open();
  709. object result = command.ExecuteScalar();
  710. if (result != null) output = result.ToString();
  711. connection.Close();
  712. }
  713. catch (Exception ex)
  714. {
  715. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  716. Logger.LogException(ex);
  717. }
  718. return output;
  719. }
  720.  
  721. public string HasReported(string steamId, string reportee)
  722. {
  723. string output = null;
  724. try
  725. {
  726. MySqlConnection connection = createConnection();
  727. MySqlCommand command = connection.CreateCommand();
  728. command.CommandText = "select `reportMessage` from `" + ThePunisher.Instance.Configuration.Instance.DatabaseReportTableName + "` where `reportee` = '" + reportee + "' and `steamId` = '" + steamId + "';";
  729. connection.Open();
  730. object result = command.ExecuteScalar();
  731. if (result != null) output = result.ToString();
  732. connection.Close();
  733. }
  734. catch (Exception ex)
  735. {
  736. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  737. Logger.LogException(ex);
  738. }
  739. return output;
  740. }
  741.  
  742. public string HasBeenRewarded(string steamId)
  743. {
  744. string output = null;
  745. try
  746. {
  747. MySqlConnection connection = createConnection();
  748. MySqlCommand command = connection.CreateCommand();
  749. command.CommandText = "select `reportMessage` from `" + ThePunisher.Instance.Configuration.Instance.DatabaseReportTableName + "` where `reportee` = '" + steamId + "' AND `rewarded` IS NOT NULL;";
  750. connection.Open();
  751. object result = command.ExecuteScalar();
  752. connection.Close();
  753. }
  754. catch (Exception ex)
  755. {
  756. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  757. Logger.LogException(ex);
  758. }
  759. return output;
  760. }
  761.  
  762. public void LogReward(string steamid)
  763. {
  764. try
  765. {
  766. ulong steamId = 0;
  767. string reportee = "";
  768. string reportMessage = "";
  769. string characterName = "";
  770. string reportTime = "";
  771. string rewarded = "";
  772. MySqlConnection connection = createConnection();
  773. MySqlCommand command = connection.CreateCommand();
  774. command.Parameters.AddWithValue("@reportee", "%" + steamid + "%");
  775. command.CommandText = "select * from `" + ThePunisher.Instance.Configuration.Instance.DatabaseReportTableName + "` where `reportee` like @reportee AND `rewarded` IS NOT NULL;";
  776. connection.Open();
  777. MySqlDataReader reader = command.ExecuteReader();
  778. if (reader.Read())
  779. {
  780. steamId = reader.GetUInt64(1);
  781. reportee = reader.GetString(2);
  782. reportMessage = reader.GetString(3);
  783. characterName = reader.GetString(4);
  784. reportTime = reader.GetString(5);
  785. rewarded = reader.GetString(6);
  786. connection.Close();
  787. command = connection.CreateCommand();
  788. command.Parameters.AddWithValue("@steamId", steamId);
  789. command.Parameters.AddWithValue("@reportee", reportee);
  790. command.Parameters.AddWithValue("@reportMessage", reportMessage);
  791. command.Parameters.AddWithValue("@characterName", characterName);
  792. command.Parameters.AddWithValue("@reportTime", reportTime);
  793. command.Parameters.AddWithValue("@rewarded", rewarded);
  794. command.CommandText = "INSERT INTO `" + ThePunisher.Instance.Configuration.Instance.DatabaseRewardlogTableName + "` (`steamId`, `reportee`, `reportMessage`, `charactername`, `reportTime`, `rewarded`) VALUES (@steamId, @reportee, @reportMessage, @characterName, @reportTime, @rewarded);";
  795. connection.Open();
  796. command.ExecuteNonQuery();
  797. connection.Close();
  798. command = connection.CreateCommand();
  799. command.Parameters.AddWithValue("@steamId", steamId);
  800. command.Parameters.AddWithValue("@reportee", reportee);
  801. command.CommandText = "delete from `" + ThePunisher.Instance.Configuration.Instance.DatabaseReportTableName + "` where `steamId` = @steamId AND `reportee`= @reportee;";
  802. connection.Open();
  803. command.ExecuteNonQuery();
  804. connection.Close();
  805. }
  806. }
  807. catch (Exception ex)
  808. {
  809. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  810. Logger.LogException(ex);
  811. }
  812. }
  813.  
  814. public void RewardForReport(string reportee)
  815. {
  816. try
  817. {
  818.  
  819. MySqlConnection connection = createConnection();
  820. MySqlCommand command = connection.CreateCommand();
  821. command.Parameters.AddWithValue("@reportee", reportee);
  822. command.CommandText = "UPDATE " + ThePunisher.Instance.Configuration.Instance.DatabaseReportTableName + " SET `rewarded` = 'Yes' WHERE `reportee` = @reportee;";
  823. connection.Open();
  824. command.ExecuteNonQuery();
  825. connection.Close();
  826. LogReward(reportee);
  827. }
  828. catch (Exception ex)
  829. {
  830. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  831. Logger.LogException(ex);
  832. }
  833. }
  834.  
  835. public void ReportPlayer(string characterName, string steamid, string reportee, string reportMessage)
  836. {
  837. try
  838. {
  839.  
  840. MySqlConnection connection = createConnection();
  841. MySqlCommand command = connection.CreateCommand();
  842. if (reportMessage == null) reportMessage = "";
  843. command.Parameters.AddWithValue("@csteamid", steamid);
  844. command.Parameters.AddWithValue("@reportee", reportee);
  845. command.Parameters.AddWithValue("@charactername", characterName);
  846. command.Parameters.AddWithValue("@reportMessage", reportMessage);
  847. command.CommandText = "insert into `" + ThePunisher.Instance.Configuration.Instance.DatabaseReportTableName + "` (`steamId`,`reportee`,`reportMessage`,`charactername`,`reportTime`) values(@csteamid,@reportee,@reportMessage,@charactername,now());";
  848. connection.Open();
  849. command.ExecuteNonQuery();
  850. connection.Close();
  851. }
  852. catch (Exception ex)
  853. {
  854. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  855. Logger.LogException(ex);
  856. }
  857. }
  858. public class ReportDeleteResult
  859. {
  860. public ulong Id;
  861. public string Reporter;
  862. public string Reportee;
  863. }
  864.  
  865. public ReportDeleteResult RemoveReport(string id = null, string player = null, string reporter = null)
  866. {
  867. if (id == null && player == null && reporter == null)
  868. {
  869. return null;
  870. }
  871. try
  872. {
  873. MySqlConnection connection = createConnection();
  874. MySqlCommand command = connection.CreateCommand();
  875. if (String.IsNullOrEmpty(player))
  876. { }
  877. command.Parameters.AddWithValue("@id", "%" + id + "%");
  878. command.Parameters.AddWithValue("@player", "%" + player + "%");
  879. command.Parameters.AddWithValue("@reportee", "%" + reporter + "%");
  880. string cmd = "select steamId,charactername,reportee from `" + ThePunisher.Instance.Configuration.Instance.DatabaseReportTableName + "` where ";
  881. if (id != null)
  882. {
  883. cmd = cmd + "`id` like @id ";
  884. }
  885. if (player != null)
  886. {
  887. if (id != null)
  888. {
  889. cmd = cmd + " OR";
  890. }
  891. cmd = cmd + " ";
  892. }
  893. if (reporter != null)
  894. {
  895. if (player != null)
  896. {
  897. cmd = cmd + " OR";
  898. }
  899. cmd = cmd + " `reportee` like @reporter";
  900. }
  901. cmd = cmd + " limit 1;";
  902. command.CommandText = cmd;
  903. connection.Open();
  904. MySqlDataReader reader = command.ExecuteReader();
  905. if (reader.Read())
  906. {
  907. ulong steamId = reader.GetUInt64(0);
  908. string charactername = reader.GetString(1);
  909. string reportee = reader.GetString(2);
  910. connection.Close();
  911. command = connection.CreateCommand();
  912. command.Parameters.AddWithValue("@steamId", steamId);
  913. command.Parameters.AddWithValue("@reportee", reportee);
  914. command.CommandText = "delete from `" + ThePunisher.Instance.Configuration.Instance.DatabaseReportTableName + "` where `steamId` = @steamId AND `reportee` = @reportee;";
  915. connection.Open();
  916. command.ExecuteNonQuery();
  917. connection.Close();
  918. return new ReportDeleteResult() { Id = steamId, Reporter = charactername, Reportee = reportee };
  919. }
  920. }
  921. catch (Exception ex)
  922. {
  923. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  924. Logger.LogException(ex);
  925. }
  926. return null;
  927. }
  928. #endregion
  929.  
  930. #region bans
  931.  
  932. public string BannedPlayers()
  933. {
  934. string output = null;
  935. try
  936. {
  937. MySqlConnection connection = createConnection();
  938. MySqlCommand command = connection.CreateCommand();
  939. command.CommandText = "select `charactername` from `" + ThePunisher.Instance.Configuration.Instance.DatabaseBanTableName + "` where (banDuration is null or ((banDuration + UNIX_TIMESTAMP(banTime)) > UNIX_TIMESTAMP()));";
  940. connection.Open();
  941. MySqlDataReader reader = command.ExecuteReader();
  942. if (reader.HasRows)
  943. {
  944. while (reader.Read())
  945. {
  946. if (output == null)
  947. output = reader.GetString(0);
  948. else
  949. output = output + "," + reader.GetString(0);
  950. }
  951. }
  952. connection.Close();
  953. }
  954. catch (Exception ex)
  955. {
  956. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  957. Logger.LogException(ex);
  958. }
  959. return output;
  960. }
  961.  
  962. public string IsBanned(string steamId)
  963. {
  964. string output = null;
  965. try
  966. {
  967. MySqlConnection connection = createConnection();
  968. MySqlCommand command = connection.CreateCommand();
  969. command.CommandText = "select `banMessage` from `" + ThePunisher.Instance.Configuration.Instance.DatabaseBanTableName + "` where `steamId` = '" + steamId + "' and (banDuration is null or ((banDuration + UNIX_TIMESTAMP(banTime)) > UNIX_TIMESTAMP()));";
  970. connection.Open();
  971. object result = command.ExecuteScalar();
  972. if (result != null) output = result.ToString();
  973. connection.Close();
  974. }
  975. catch (Exception ex)
  976. {
  977. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  978. Logger.LogException(ex);
  979. }
  980. return output;
  981. }
  982.  
  983. public void BanOfflinePlayer(string steamid, string admin, string adminName, string banMessage, int duration)
  984. {
  985. try
  986. {
  987.  
  988. MySqlConnection connection = createConnection();
  989. MySqlCommand command = connection.CreateCommand();
  990. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo) { Logger.Log("Connection to Ban table created!"); }
  991. if (banMessage == null) banMessage = "";
  992. command.Parameters.AddWithValue("@csteamid", steamid);
  993. command.Parameters.AddWithValue("@admin", admin);
  994. command.Parameters.AddWithValue("@adminname", adminName);
  995. command.Parameters.AddWithValue("@banMessage", banMessage);
  996. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo) { Logger.Log("Command paramaters added!"); }
  997. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo) { Logger.Log("Banning: " + steamid + " from the server."); }
  998. if (duration == 0)
  999. {
  1000. command.Parameters.AddWithValue("@banDuration", DBNull.Value);
  1001. }
  1002. else
  1003. {
  1004. command.Parameters.AddWithValue("@banDuration", duration);
  1005. }
  1006. string query = "insert into `" + ThePunisher.Instance.Configuration.Instance.DatabaseBanTableName + "` (`steamId`,`admin`,`adminName`,`banMessage`,`charactername`,`banTime`,`banDuration`) values(@csteamid,@admin,@adminname,@banMessage,@csteamid,now(),@banDuration);";
  1007. command.CommandText = query;
  1008. connection.Open();
  1009. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo) { Logger.Log("Connection to database opened"); }
  1010. command.ExecuteNonQuery();
  1011. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo) { Logger.Log("Command executed Successfully. Player has been banned."); }
  1012. connection.Close();
  1013. }
  1014. catch (Exception ex)
  1015. {
  1016. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  1017. Logger.LogException(ex);
  1018. }
  1019. }
  1020.  
  1021. public void BanPlayer(string characterName, string steamid, string admin, string adminName, string banMessage, int duration)
  1022. {
  1023. try
  1024. {
  1025.  
  1026. MySqlConnection connection = createConnection();
  1027. MySqlCommand command = connection.CreateCommand();
  1028. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo) { Logger.Log("Connection to Ban table created!"); }
  1029. if (banMessage == null) banMessage = "";
  1030. command.Parameters.AddWithValue("@csteamid", steamid);
  1031. command.Parameters.AddWithValue("@admin", admin);
  1032. command.Parameters.AddWithValue("@adminname", adminName);
  1033. command.Parameters.AddWithValue("@charactername", characterName);
  1034. command.Parameters.AddWithValue("@banMessage", banMessage);
  1035. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo) { Logger.Log("Command paramaters added!"); }
  1036. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo) { Logger.Log("Banning: " + steamid + " - " + characterName + " from the server."); }
  1037. if (duration == 0)
  1038. {
  1039. command.Parameters.AddWithValue("@banDuration", DBNull.Value);
  1040. }
  1041. else
  1042. {
  1043. command.Parameters.AddWithValue("@banDuration", duration);
  1044. }
  1045. string query = "insert into `" + ThePunisher.Instance.Configuration.Instance.DatabaseBanTableName + "` (`steamId`,`admin`,`adminName`,`banMessage`,`charactername`,`banTime`,`banDuration`) values(@csteamid,@admin,@adminname,@banMessage,@charactername,now(),@banDuration);";
  1046. command.CommandText = query;
  1047. connection.Open();
  1048. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo) { Logger.Log("Connection to database opened"); }
  1049. command.ExecuteNonQuery();
  1050. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo) { Logger.Log("Command executed Successfully. Player has been banned."); }
  1051. connection.Close();
  1052. }
  1053. catch (Exception ex)
  1054. {
  1055. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  1056. Logger.LogException(ex);
  1057. }
  1058. }
  1059.  
  1060. public class UnbanResult
  1061. {
  1062. public ulong Id;
  1063. public string Name;
  1064. }
  1065.  
  1066.  
  1067. public UnbanResult UnbanPlayer(string player)
  1068. {
  1069. try
  1070. {
  1071. MySqlConnection connection = createConnection();
  1072.  
  1073. MySqlCommand command = connection.CreateCommand();
  1074. command.Parameters.AddWithValue("@player", "%" + player + "%");
  1075. command.CommandText = "select steamId,charactername from `" + ThePunisher.Instance.Configuration.Instance.DatabaseBanTableName + "` where `steamId` like @player or `charactername` like @player limit 1;";
  1076. connection.Open();
  1077. MySqlDataReader reader = command.ExecuteReader();
  1078. if (reader.Read())
  1079. {
  1080. ulong steamId = reader.GetUInt64(0);
  1081. string charactername = reader.GetString(1);
  1082. connection.Close();
  1083. command = connection.CreateCommand();
  1084. command.Parameters.AddWithValue("@steamId", steamId);
  1085. command.CommandText = "delete from `" + ThePunisher.Instance.Configuration.Instance.DatabaseBanTableName + "` where `steamId` = @steamId;";
  1086. connection.Open();
  1087. command.ExecuteNonQuery();
  1088. connection.Close();
  1089. return new UnbanResult() { Id = steamId, Name = charactername };
  1090. }
  1091. }
  1092. catch (Exception ex)
  1093. {
  1094. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  1095. Logger.LogException(ex);
  1096. }
  1097. return null;
  1098. }
  1099.  
  1100. #endregion
  1101.  
  1102. #region whitelist
  1103. public string IsWhitelisted(string steamId)
  1104. {
  1105. string output = null;
  1106. try
  1107. {
  1108. MySqlConnection connection = createConnection();
  1109. MySqlCommand command = connection.CreateCommand();
  1110. command.CommandText = "select `characterName` from `" + ThePunisher.Instance.Configuration.Instance.DatabaseWhiteListTableName + "` where `steamId` = '" + steamId + "';";
  1111. connection.Open();
  1112. object result = command.ExecuteScalar();
  1113. if (result != null) output = result.ToString();
  1114. connection.Close();
  1115. }
  1116. catch (Exception ex)
  1117. {
  1118. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  1119. Logger.LogException(ex);
  1120. }
  1121. return output;
  1122. }
  1123.  
  1124. public void WhiteListPlayer(string characterName, string steamid, string admin)
  1125. {
  1126. try
  1127. {
  1128. MySqlConnection connection = createConnection();
  1129. MySqlCommand command = connection.CreateCommand();
  1130. command.Parameters.AddWithValue("@csteamid", steamid);
  1131. command.Parameters.AddWithValue("@admin", admin);
  1132. command.Parameters.AddWithValue("@charactername", characterName);
  1133. command.CommandText = "insert into `" + ThePunisher.Instance.Configuration.Instance.DatabaseWhiteListTableName + "` (`steamId`,`admin`,`charactername`,added_on) values(@csteamid,@admin,@charactername,now());";
  1134. connection.Open();
  1135. command.ExecuteNonQuery();
  1136. connection.Close();
  1137. }
  1138. catch (Exception ex)
  1139. {
  1140. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  1141. Logger.LogException(ex);
  1142. }
  1143. }
  1144.  
  1145.  
  1146. public class BlacklistResult
  1147. {
  1148. public ulong Id;
  1149. public string Name;
  1150. }
  1151.  
  1152. public BlacklistResult BlacklistPlayer(string player)
  1153. {
  1154. try
  1155. {
  1156. MySqlConnection connection = createConnection();
  1157.  
  1158. MySqlCommand command = connection.CreateCommand();
  1159. command.Parameters.AddWithValue("@player", "%" + player + "%");
  1160. command.CommandText = "select steamId,charactername from `" + ThePunisher.Instance.Configuration.Instance.DatabaseWhiteListTableName + "` where `steamId` like @player or `charactername` like @player limit 1;";
  1161. connection.Open();
  1162. MySqlDataReader reader = command.ExecuteReader();
  1163. if (reader.Read())
  1164. {
  1165. ulong steamId = reader.GetUInt64(0);
  1166. string charactername = reader.GetString(1);
  1167. connection.Close();
  1168. command = connection.CreateCommand();
  1169. command.Parameters.AddWithValue("@steamId", steamId);
  1170. command.CommandText = "delete from `" + ThePunisher.Instance.Configuration.Instance.DatabaseWhiteListTableName + "` where `steamId` = @steamId;";
  1171. connection.Open();
  1172. command.ExecuteNonQuery();
  1173. connection.Close();
  1174. return new BlacklistResult() { Id = steamId, Name = charactername };
  1175. }
  1176. }
  1177. catch (Exception ex)
  1178. {
  1179. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  1180. Logger.LogException(ex);
  1181. }
  1182. return null;
  1183. }
  1184. #endregion
  1185.  
  1186. #region mutes/chatbans
  1187. public string IsChatBanned(string steamId)
  1188. {
  1189. string output = null;
  1190. try
  1191. {
  1192. MySqlConnection connection = createConnection();
  1193. MySqlCommand command = connection.CreateCommand();
  1194. command.CommandText = "select `banMessage` from `" + ThePunisher.Instance.Configuration.Instance.DatabaseChatbanTableName + "` where `steamId` = '" + steamId + "' and (banDuration is null or ((banDuration + UNIX_TIMESTAMP(banTime)) > UNIX_TIMESTAMP()));";
  1195. connection.Open();
  1196. object result = command.ExecuteScalar();
  1197. if (result != null) output = result.ToString();
  1198. connection.Close();
  1199. }
  1200. catch (Exception ex)
  1201. {
  1202. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  1203. Logger.LogException(ex);
  1204. }
  1205. return output;
  1206. }
  1207.  
  1208. public void ChatBanPlayer(string characterName, string steamid, string admin, string banMessage, int duration)
  1209. {
  1210. try
  1211. {
  1212. MySqlConnection connection = createConnection();
  1213. MySqlCommand command = connection.CreateCommand();
  1214. if (banMessage == null) banMessage = "";
  1215. command.Parameters.AddWithValue("@csteamid", steamid);
  1216. command.Parameters.AddWithValue("@admin", admin);
  1217. command.Parameters.AddWithValue("@charactername", characterName);
  1218. command.Parameters.AddWithValue("@banMessage", banMessage);
  1219. if (duration == 0)
  1220. {
  1221. command.Parameters.AddWithValue("@banDuration", DBNull.Value);
  1222. }
  1223. else
  1224. {
  1225. command.Parameters.AddWithValue("@banDuration", duration);
  1226. }
  1227. command.CommandText = "insert into `" + ThePunisher.Instance.Configuration.Instance.DatabaseChatbanTableName + "` (`steamId`,`admin`,`banMessage`,`charactername`,`banTime`,`banDuration`) values(@csteamid,@admin,@banMessage,@charactername,now(),@banDuration) on duplicate key update `banDuration` = @banDuration, `banTime` = now();";
  1228. connection.Open();
  1229. command.ExecuteNonQuery();
  1230. connection.Close();
  1231. }
  1232. catch (Exception ex)
  1233. {
  1234. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  1235. Logger.LogException(ex);
  1236. }
  1237. }
  1238. public UnbanResult UnChatbanPlayer(string player)
  1239. {
  1240. try
  1241. {
  1242. MySqlConnection connection = createConnection();
  1243.  
  1244. MySqlCommand command = connection.CreateCommand();
  1245. command.Parameters.AddWithValue("@player", "%" + player + "%");
  1246. command.CommandText = "select steamId,charactername from `" + ThePunisher.Instance.Configuration.Instance.DatabaseChatbanTableName + "` where `steamId` like @player or `charactername` like @player limit 1;";
  1247. connection.Open();
  1248. MySqlDataReader reader = command.ExecuteReader();
  1249. if (reader.Read())
  1250. {
  1251. ulong steamId = reader.GetUInt64(0);
  1252. string charactername = reader.GetString(1);
  1253. connection.Close();
  1254. command = connection.CreateCommand();
  1255. command.Parameters.AddWithValue("@steamId", steamId);
  1256. command.CommandText = "delete from `" + ThePunisher.Instance.Configuration.Instance.DatabaseChatbanTableName + "` where `steamId` = @steamId;";
  1257. connection.Open();
  1258. command.ExecuteNonQuery();
  1259. connection.Close();
  1260. return new UnbanResult() { Id = steamId, Name = charactername };
  1261. }
  1262. }
  1263. catch (Exception ex)
  1264. {
  1265. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  1266. Logger.LogException(ex);
  1267. }
  1268. return null;
  1269. }
  1270.  
  1271. #endregion
  1272.  
  1273. #region old code
  1274. /*public void WarnPlayer(string characterName, string steamid, string admin, string warnMessage)
  1275. {
  1276. try
  1277. {
  1278.  
  1279. MySqlConnection connection = createConnection();
  1280. MySqlCommand command = connection.CreateCommand();
  1281. if (warnMessage == null) warnMessage = "";
  1282. command.Parameters.AddWithValue("@csteamid", steamid);
  1283. command.Parameters.AddWithValue("@admin", admin);
  1284. command.Parameters.AddWithValue("@charactername", characterName);
  1285. command.Parameters.AddWithValue("@warnMessage", warnMessage);
  1286. command.CommandText = "insert into `" + ThePunisher.Instance.Configuration.Instance.DatabaseWarningTableName + "` (`steamId`,`admin`,`warnMessage`,`charactername`,`warnTime`) values(@csteamid,@admin,@warnMessage,@charactername,now());";
  1287. connection.Open();
  1288. command.ExecuteNonQuery();
  1289. connection.Close();
  1290. }
  1291. catch (Exception ex)
  1292. {
  1293. if (ThePunisher.Instance.Configuration.Instance.ShowDebugInfo)
  1294. Logger.LogException(ex);
  1295. }
  1296. }*/
  1297. #endregion
  1298. }
  1299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement