Advertisement
Guest User

Untitled

a guest
Jun 28th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.89 KB | None | 0 0
  1. package fr.kaviozz.prac.utils;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.UUID;
  9.  
  10. import org.bukkit.Bukkit;
  11. import org.bukkit.entity.Player;
  12.  
  13. import fr.kaviozz.prac.match.Queue;
  14.  
  15. public class DataBase
  16. {
  17. private String url_base;
  18. private String host;
  19. private String name;
  20. private String username;
  21. private String password;
  22. private String table;
  23. private Connection connection;
  24. private int DEFAULT_ELO = 1000;
  25.  
  26. public DataBase(String url_base, String host, String name, String username, String password, String table)
  27. {
  28. this.url_base = url_base;
  29. this.host = host;
  30. this.name = name;
  31. this.username = username;
  32. this.password = password;
  33. this.table = table;
  34. }
  35.  
  36. public void connect()
  37. {
  38. if (!isConnected()) {
  39. try
  40. {
  41. this.connection = DriverManager.getConnection("jdbc:mysql://" + this.host + "/" + this.name, this.username, this.password);
  42. createTable();
  43. }
  44. catch (SQLException e)
  45. {
  46. System.out.println("Error: Connect to database SQL...");
  47. e.printStackTrace();
  48. }
  49. }
  50. }
  51.  
  52. public void createTable()
  53. {
  54. try
  55. {
  56. this.connection.prepareStatement("CREATE TABLE `players` (`uuid` VARCHAR(36) NOT NULL, `pseudo` VARCHAR(16) NOT NULL, `nodebuff` INT NOT NULL DEFAULT '1000', `debuff` INT NOT NULL DEFAULT '1000', `gapple` INT NOT NULL DEFAULT '1000', `archer` INT NOT NULL DEFAULT '1000', `axe` INT NOT NULL DEFAULT '1000', `classic` INT NOT NULL DEFAULT '1000', `soup` INT NOT NULL DEFAULT '1000', `globalelo` INT NOT NULL DEFAULT '1000', `wins` INT NOT NULL DEFAULT '0', `losses` INT NOT NULL DEFAULT '0', `coins` INT NOT NULL DEFAULT '0', `enablescoreboard` BOOL NOT NULL DEFAULT '1', `enableduel` BOOL NOT NULL DEFAULT '1', `country` VARCHAR(100) NOT NULL DEFAULT 'None', PRIMARY KEY (`uuid`), UNIQUE (`uuid`))").executeUpdate();
  57. }
  58. catch (SQLException e)
  59. {
  60. if (!e.getMessage().contains("already exists"))
  61. {
  62. System.out.println("Failed createTables");
  63. e.printStackTrace();
  64. }
  65. }
  66. }
  67.  
  68. public void close()
  69. {
  70. if (isConnected()) {
  71. try
  72. {
  73. this.connection.close();
  74. }
  75. catch (SQLException e)
  76. {
  77. e.printStackTrace();
  78. }
  79. }
  80. }
  81.  
  82. public boolean isConnected()
  83. {
  84. try
  85. {
  86. if ((this.connection == null) || (this.connection.isClosed()) || (!this.connection.isValid(5))) {
  87. return false;
  88. }
  89. return true;
  90. }
  91. catch (SQLException e)
  92. {
  93. e.printStackTrace();
  94. }
  95. return false;
  96. }
  97.  
  98. public Connection getConnection()
  99. {
  100. return this.connection;
  101. }
  102.  
  103. public void createAccount(UUID uuid)
  104. {
  105. if (isConnected()) {
  106. try
  107. {
  108. PreparedStatement sts = getConnection().prepareStatement("SELECT pseudo FROM " + this.table + " WHERE uuid = ?");
  109. sts.setString(1, uuid.toString());
  110. ResultSet rs = sts.executeQuery();
  111. if (!rs.next())
  112. {
  113. sts.close();
  114.  
  115. PreparedStatement sts2 = getConnection().prepareStatement("INSERT INTO " + this.table + " (uuid, pseudo, nodebuff, debuff, gapple, archer, axe, classic, soup, globalelo, wins, losses, coins, enablescoreboard, enableduel, country) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
  116. sts2.setString(1, uuid.toString());
  117. sts2.setString(2, Bukkit.getPlayer(uuid).getName());
  118. sts2.setInt(3, this.DEFAULT_ELO);
  119. sts2.setInt(4, this.DEFAULT_ELO);
  120. sts2.setInt(5, this.DEFAULT_ELO);
  121. sts2.setInt(6, this.DEFAULT_ELO);
  122. sts2.setInt(7, this.DEFAULT_ELO);
  123. sts2.setInt(8, this.DEFAULT_ELO);
  124. sts2.setInt(9, this.DEFAULT_ELO);
  125. sts2.setInt(10, this.DEFAULT_ELO);
  126. sts2.setInt(11, Integer.valueOf(0).intValue());
  127. sts2.setInt(12, Integer.valueOf(0).intValue());
  128. sts2.setInt(13, Integer.valueOf(0).intValue());
  129. sts2.setInt(14, Integer.valueOf(1).intValue());
  130. sts2.setInt(15, Integer.valueOf(1).intValue());
  131. sts2.setString(16, SkIP.getIPData(SkIP.getPlayerIP(Bukkit.getPlayer(uuid))).getCountryName());
  132. sts2.executeUpdate();
  133. sts2.close();
  134. }
  135. updatePlayerName(Bukkit.getPlayer(uuid));
  136. }
  137. catch (SQLException e)
  138. {
  139. e.printStackTrace();
  140. }
  141. } else {
  142. Bukkit.getPlayer(uuid).sendMessage("§cThe database is not connected! Please contact Kavioz_z!");
  143. }
  144. }
  145.  
  146. public void updatePlayerName(Player player)
  147. {
  148. try
  149. {
  150. PreparedStatement sts = getConnection().prepareStatement("SELECT pseudo FROM " + this.table + " WHERE uuid = ?");
  151. sts.setString(1, player.getUniqueId().toString());
  152. ResultSet rs = sts.executeQuery();
  153. if (!rs.next())
  154. {
  155. sts.close();
  156. return;
  157. }
  158. if (!rs.getString("pseudo").equals(player.getName()))
  159. {
  160. sts.close();
  161.  
  162. PreparedStatement sts2 = getConnection().prepareStatement("UPDATE " + this.table + " SET pseudo = ? WHERE uuid = ?");
  163. sts2.setString(1, player.getName());
  164. sts2.setString(2, player.getUniqueId().toString());
  165. sts2.executeUpdate();
  166. sts2.close();
  167. }
  168. }
  169. catch (SQLException e)
  170. {
  171. e.printStackTrace();
  172. }
  173. }
  174.  
  175. public Integer getPlayerElo(UUID uuid, String mode)
  176. {
  177. Integer elo = Integer.valueOf(0);
  178. try
  179. {
  180. PreparedStatement sts = getConnection().prepareStatement("SELECT " + mode.toLowerCase() + " FROM " + this.table + " WHERE uuid = ?");
  181. sts.setString(1, uuid.toString());
  182. ResultSet rs = sts.executeQuery();
  183. if (!rs.next())
  184. {
  185. sts.close();
  186. return Integer.valueOf(this.DEFAULT_ELO);
  187. }
  188. elo = Integer.valueOf(rs.getInt(mode));
  189. sts.close();
  190. }
  191. catch (SQLException e)
  192. {
  193. e.printStackTrace();
  194. }
  195. return elo;
  196. }
  197.  
  198. public void addPlayerElo(UUID uuid, String mode, int elo)
  199. {
  200. try
  201. {
  202. PreparedStatement sts = getConnection().prepareStatement("UPDATE " + this.table + " SET " + mode.toLowerCase() + " = " + mode.toLowerCase() + " + ? WHERE uuid = ?");
  203. sts.setInt(1, elo);
  204. sts.setString(2, uuid.toString());
  205. sts.executeUpdate();
  206. sts.close();
  207. }
  208. catch (SQLException e)
  209. {
  210. e.printStackTrace();
  211. }
  212. }
  213.  
  214. public void removePlayerElo(UUID uuid, String mode, int elo)
  215. {
  216. try
  217. {
  218. PreparedStatement sts = getConnection().prepareStatement("UPDATE " + this.table + " SET " + mode.toLowerCase() + " = " + mode.toLowerCase() + " - ? WHERE uuid = ?");
  219. sts.setInt(1, elo);
  220. sts.setString(2, uuid.toString());
  221. sts.executeUpdate();
  222. sts.close();
  223. }
  224. catch (SQLException e)
  225. {
  226. e.printStackTrace();
  227. }
  228. }
  229.  
  230. public int getPlayerGlobalElo(UUID uuid)
  231. {
  232. Integer elo = Integer.valueOf(0);
  233. try
  234. {
  235. PreparedStatement sts = getConnection().prepareStatement("SELECT globalelo FROM " + this.table + " WHERE uuid = ?");
  236. sts.setString(1, uuid.toString());
  237. ResultSet rs = sts.executeQuery();
  238. if (!rs.next())
  239. {
  240. sts.close();
  241. return Integer.valueOf(this.DEFAULT_ELO).intValue();
  242. }
  243. setPlayerGlobalElo(uuid);
  244. elo = Integer.valueOf(rs.getInt("globalelo"));
  245. sts.close();
  246. }
  247. catch (SQLException e)
  248. {
  249. e.printStackTrace();
  250. }
  251. return elo.intValue();
  252. }
  253.  
  254. public void setPlayerGlobalElo(UUID uuid)
  255. {
  256. int global = getPlayerElo(uuid, "NoDebuff").intValue() + getPlayerElo(uuid, "Debuff").intValue() + getPlayerElo(uuid, "Gapple").intValue() + getPlayerElo(uuid, "Archer").intValue() + getPlayerElo(uuid, "Axe").intValue() + getPlayerElo(uuid, "Classic").intValue() + getPlayerElo(uuid, "Soup").intValue();
  257.  
  258. global /= Queue.getInstance().kitlistR.length;
  259. try
  260. {
  261. PreparedStatement sts = getConnection().prepareStatement("UPDATE " + this.table + " SET globalelo = ? WHERE uuid = ?");
  262. sts.setInt(1, global);
  263. sts.setString(2, uuid.toString());
  264. sts.executeUpdate();
  265. sts.close();
  266. }
  267. catch (SQLException e)
  268. {
  269. e.printStackTrace();
  270. }
  271. }
  272.  
  273. public Integer getPlayerWins(UUID uuid)
  274. {
  275. Integer wins = Integer.valueOf(0);
  276. try
  277. {
  278. PreparedStatement sts = getConnection().prepareStatement("SELECT wins FROM " + this.table + " WHERE uuid = ?");
  279. sts.setString(1, uuid.toString());
  280. ResultSet rs = sts.executeQuery();
  281. if (!rs.next())
  282. {
  283. sts.close();
  284. return Integer.valueOf(0);
  285. }
  286. wins = Integer.valueOf(rs.getInt("wins"));
  287. sts.close();
  288. }
  289. catch (SQLException e)
  290. {
  291. e.printStackTrace();
  292. }
  293. return wins;
  294. }
  295.  
  296. public Integer getPlayerLosses(UUID uuid)
  297. {
  298. Integer losses = Integer.valueOf(0);
  299. try
  300. {
  301. PreparedStatement sts = getConnection().prepareStatement("SELECT losses FROM " + this.table + " WHERE uuid = ?");
  302. sts.setString(1, uuid.toString());
  303. ResultSet rs = sts.executeQuery();
  304. if (!rs.next())
  305. {
  306. sts.close();
  307. return Integer.valueOf(0);
  308. }
  309. losses = Integer.valueOf(rs.getInt("losses"));
  310. sts.close();
  311. }
  312. catch (SQLException e)
  313. {
  314. e.printStackTrace();
  315. }
  316. return losses;
  317. }
  318.  
  319. public void addPlayerWins(UUID uuid)
  320. {
  321. try
  322. {
  323. PreparedStatement sts = getConnection().prepareStatement("UPDATE " + this.table + " SET wins = wins + ? WHERE uuid = ?");
  324. sts.setInt(1, Integer.valueOf(1).intValue());
  325. sts.setString(2, uuid.toString());
  326. sts.executeUpdate();
  327. sts.close();
  328. }
  329. catch (SQLException e)
  330. {
  331. e.printStackTrace();
  332. }
  333. }
  334.  
  335. public void addPlayerLosses(UUID uuid)
  336. {
  337. try
  338. {
  339. PreparedStatement sts = getConnection().prepareStatement("UPDATE " + this.table + " SET losses = losses + ? WHERE uuid = ?");
  340. sts.setInt(1, Integer.valueOf(1).intValue());
  341. sts.setString(2, uuid.toString());
  342. sts.executeUpdate();
  343. sts.close();
  344. }
  345. catch (SQLException e)
  346. {
  347. e.printStackTrace();
  348. }
  349. }
  350.  
  351. public Integer getPlayerCoins(UUID uuid)
  352. {
  353. Integer coins = Integer.valueOf(0);
  354. try
  355. {
  356. PreparedStatement sts = getConnection().prepareStatement("SELECT coins FROM " + this.table + " WHERE uuid = ?");
  357. sts.setString(1, uuid.toString());
  358. ResultSet rs = sts.executeQuery();
  359. if (!rs.next())
  360. {
  361. sts.close();
  362. return Integer.valueOf(0);
  363. }
  364. coins = Integer.valueOf(rs.getInt("coins"));
  365. sts.close();
  366. }
  367. catch (SQLException e)
  368. {
  369. e.printStackTrace();
  370. }
  371. return coins;
  372. }
  373.  
  374. public void addPlayerCoins(UUID uuid, Integer coins)
  375. {
  376. try
  377. {
  378. PreparedStatement sts = getConnection().prepareStatement("UPDATE " + this.table + " SET coins = coins + ? WHERE uuid = ?");
  379. sts.setInt(1, Integer.valueOf(coins.intValue()).intValue());
  380. sts.setString(2, uuid.toString());
  381. sts.executeUpdate();
  382. sts.close();
  383. }
  384. catch (SQLException e)
  385. {
  386. e.printStackTrace();
  387. }
  388. }
  389.  
  390. public void removePlayerCoins(UUID uuid, Integer coins)
  391. {
  392. try
  393. {
  394. PreparedStatement sts = getConnection().prepareStatement("UPDATE " + this.table + " SET coins = coins - ? WHERE uuid = ?");
  395. sts.setInt(1, Integer.valueOf(coins.intValue()).intValue());
  396. sts.setString(2, uuid.toString());
  397. sts.executeUpdate();
  398. sts.close();
  399. }
  400. catch (SQLException e)
  401. {
  402. e.printStackTrace();
  403. }
  404. }
  405.  
  406. public boolean isEnable(UUID uuid, String code)
  407. {
  408. Boolean b = Boolean.valueOf(true);
  409. try
  410. {
  411. PreparedStatement sts = getConnection().prepareStatement("SELECT enable" + code.toLowerCase() + " FROM " + this.table + " WHERE uuid = ?");
  412. sts.setString(1, uuid.toString());
  413. ResultSet rs = sts.executeQuery();
  414. if (!rs.next())
  415. {
  416. sts.close();
  417. return Boolean.valueOf(true).booleanValue();
  418. }
  419. b = Boolean.valueOf(rs.getBoolean("enable" + code));
  420. sts.close();
  421. }
  422. catch (SQLException e)
  423. {
  424. e.printStackTrace();
  425. }
  426. return b.booleanValue();
  427. }
  428.  
  429. public void setEnable(UUID uuid, String code, boolean b)
  430. {
  431. try
  432. {
  433. PreparedStatement sts = getConnection().prepareStatement("UPDATE " + this.table + " SET enable" + code.toLowerCase() + " = ? WHERE uuid = ?");
  434. sts.setInt(1, Integer.valueOf(b ? 1 : 0).intValue());
  435. sts.setString(2, uuid.toString());
  436. sts.executeUpdate();
  437. sts.close();
  438. }
  439. catch (SQLException e)
  440. {
  441. e.printStackTrace();
  442. }
  443. }
  444. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement