Guest User

Untitled

a guest
Oct 2nd, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. package server.util;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.util.Properties;
  7.  
  8. import server.model.players.Client;
  9.  
  10. /**
  11. *
  12. * @author Joshua F
  13. * @author PJNoMore
  14. *
  15. */
  16.  
  17. public class HiscoresHandler {
  18. private static boolean HiScores = true;
  19.  
  20. private static final String DB = "revis100_hs";
  21. private static final String URL = "revisionx.org";
  22. private static final String USER = " revis100_hs";
  23. private static final String PASS = "revis100_hs";
  24. private static final Properties prop;
  25. static {
  26. prop = new Properties();
  27. prop.put("user", USER);
  28. prop.put("password", PASS);
  29. //prop.put("autoReconnect", "true");
  30. //prop.put("maxReconnects", "4");
  31. }
  32.  
  33. public static Connection conn = null;
  34.  
  35. /**
  36. * Connects to the database
  37. */
  38. public static synchronized void connect() {
  39. try {
  40. Class.forName("com.mysql.jdbc.Driver");
  41. conn = DriverManager.getConnection("jdbc:mysql://" + URL + "/" + DB, prop);
  42. System.out.println("Hiscores Handler: Success");
  43. } catch (Exception e) {
  44. System.out.println("Hiscores Handler Error: "+ e);
  45. System.out.println("Setting hiscores to false to help not cause anymore errors.");
  46. HiScores = false;
  47. }
  48. }
  49.  
  50. public static synchronized Connection getConnection() {
  51. try {
  52. if (conn == null || conn.isClosed()) {
  53. conn = DriverManager.getConnection("jdbc:mysql://" + URL + "/"
  54. + DB, prop);
  55. }
  56. } catch (SQLException e) {
  57. System.out.println(e);
  58. e.printStackTrace();
  59. }
  60. return conn;
  61. }
  62.  
  63. /**
  64. * The main method that is called upon logout
  65. */
  66. public static void hiscoresHandler(Client c) {
  67. if (HiScores == true) {
  68. deleteHiscores(c);
  69. saveHiscores(c);
  70. }
  71. }
  72.  
  73. /**
  74. * Part of the main method to save the hiscores
  75. */
  76. private static synchronized void saveHiscores(Client c) {
  77. try {
  78. int overallLVL = 0;
  79. double overallXP = 0;
  80. for (int i = 0; i < 21; i++) {
  81. overallLVL += c.getLevelForXP(c.playerXP[i]);
  82. overallXP += c.playerXP[i];
  83. }
  84. getConnection().createStatement().execute(
  85. "INSERT INTO `hiscores` VALUES ('" + c.playerName + "', '"
  86. + c.playerRights + "', '" + c.isDonator + "', '"
  87. + overallLVL + "', '" + overallXP + "', '"
  88. + c.playerXP[0] + "', '" + c.playerXP[1] + "', '"
  89. + c.playerXP[2] + "', '" + c.playerXP[3] + "', '"
  90. + c.playerXP[4] + "', '" + c.playerXP[5] + "', '"
  91. + c.playerXP[6] + "', '" + c.playerXP[7] + "', '"
  92. + c.playerXP[8] + "', '" + c.playerXP[9] + "', '"
  93. + c.playerXP[10] + "', '" + c.playerXP[11] + "', '"
  94. + c.playerXP[12] + "', '" + c.playerXP[13] + "', '"
  95. + c.playerXP[14] + "', '" + c.playerXP[15] + "', '"
  96. + c.playerXP[16] + "', '" + c.playerXP[17] + "', '"
  97. + c.playerXP[18] + "', '" + c.playerXP[19] + "', '"
  98. + c.playerXP[20] + "')");
  99. } catch (Exception e) {
  100. e.printStackTrace();
  101. }
  102. }
  103.  
  104. /**
  105. * Part of the main method to save the hiscores
  106. */
  107. private static synchronized void deleteHiscores(Client c) {
  108. try {
  109. getConnection().createStatement().execute(
  110. "DELETE FROM `hiscores` WHERE `playerName` = '"
  111. + c.playerName + "'");
  112. } catch (Exception e) {
  113. e.printStackTrace();
  114. }
  115. }
  116.  
  117. /**
  118. * Will wipe out the hiscores table, therefor cleaning them
  119. */
  120. public static synchronized void clearHiscores() {
  121. try {
  122. getConnection().createStatement().execute(
  123. "TRUNCATE TABLE `hiscores`");
  124. } catch (Exception e) {
  125. e.printStackTrace();
  126. }
  127. }
  128. }
Add Comment
Please, Sign In to add comment