Advertisement
Guest User

Untitled

a guest
Oct 16th, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. package com.unityps.network.mysql;
  2.  
  3. import java.sql.PreparedStatement;
  4.  
  5. import com.unityps.game.model.entity.players.Client;
  6.  
  7. public class Highscores implements Runnable {
  8.  
  9. private Client c;
  10.  
  11. public Highscores(Client c) {
  12. this.c = c;
  13. }
  14.  
  15. public Client getC() {
  16. return c;
  17. }
  18. public static boolean hascomplete = false;
  19.  
  20. @Override
  21. public void run() {
  22. try {
  23. //Database db = new Database("jdbc:mysql://runeunity.com/runeunit_highscores", "runeunity", "unityscores911", "runeunit_highscores");
  24. Database db = new Database("192.99.69.241", "runeunit_scores", "unityscores911", "runeunit_highscores");
  25. String name = formatPlayerNameForDisplay(c.username);
  26.  
  27. if (!db.init()) {
  28. System.err.println("Failing to update "+name+" highscores. Database could not connect.");
  29. return;
  30. }
  31. PreparedStatement stmt1 = db.prepare("DELETE FROM " + getTable() + " WHERE username=?");
  32. stmt1.setString(1, name);
  33. stmt1.execute();
  34.  
  35. PreparedStatement stmt2 = db.prepare(generateQuery());
  36. stmt2.setString(1, name);
  37. stmt2.setInt(2, c.getRights().ordinal());
  38. stmt2.setLong(3, getTotalXp());
  39.  
  40. for (int i = 0; i < 25; i++)
  41. stmt2.setInt(4 + i, (int) c.playerXP[i]);
  42. stmt2.execute();
  43.  
  44. db.destroyAll();
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. }
  48. }
  49.  
  50. public String generateQuery() {
  51. StringBuilder sb = new StringBuilder();
  52. sb.append("INSERT INTO "+ getTable() +" (");
  53. sb.append("username, ");
  54. sb.append("rights, ");
  55. sb.append("overall_xp, ");
  56. sb.append("attack_xp, ");
  57. sb.append("defence_xp, ");
  58. sb.append("strength_xp, ");
  59. sb.append("constitution_xp, ");
  60. sb.append("ranged_xp, ");
  61. sb.append("prayer_xp, ");
  62. sb.append("magic_xp, ");
  63. sb.append("cooking_xp, ");
  64. sb.append("woodcutting_xp, ");
  65. sb.append("fletching_xp, ");
  66. sb.append("fishing_xp, ");
  67. sb.append("firemaking_xp, ");
  68. sb.append("crafting_xp, ");
  69. sb.append("smithing_xp, ");
  70. sb.append("mining_xp, ");
  71. sb.append("herblore_xp, ");
  72. sb.append("agility_xp, ");
  73. sb.append("thieving_xp, ");
  74. sb.append("slayer_xp, ");
  75. sb.append("farming_xp, ");
  76. sb.append("runecrafting_xp, ");
  77. sb.append("dungeoneering_xp) "); //21
  78. sb.append("hunter_xp, ");//22
  79. sb.append("summoning_xp, "); //23
  80. sb.append("construction_xp, ");//24
  81. sb.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
  82. return sb.toString();
  83. }
  84.  
  85. public String getTable() {
  86. return c.isIronman() ? "hs_users_ironman" : c.isHardcoreIronman() ? "hs_users_hard_ironman" : "hs_users";
  87. }
  88.  
  89.  
  90. /*public long getTotalXp() {
  91. long totalxp = 0L;
  92. for (int i = 0; i <= 24; i++) {
  93. totalxp += (double) c.playerXP[i];
  94. //hascomplete = true;
  95. }
  96. return totalxp;
  97. }*/
  98. public long getTotalXp() {
  99. long totalxp = 0;
  100. for (double xp : c.playerXP) {
  101. totalxp += xp;
  102. }
  103. return totalxp;
  104. }
  105.  
  106.  
  107. public int[] getLevels() {
  108. int[] levels = new int[25];
  109. for (int i = 0; i < levels.length; i++)
  110. levels[i] = c.getLevelForXP(c.playerXP[i]);
  111. return levels;
  112. }
  113.  
  114.  
  115. public static String formatPlayerNameForDisplay(String name) {
  116. if (name == null)
  117. return "";
  118. name = name.replaceAll("_", " ");
  119. name = name.toLowerCase();
  120. StringBuilder newName = new StringBuilder();
  121. boolean wasSpace = true;
  122. for (int i = 0; i < name.length(); i++) {
  123. if (wasSpace) {
  124. newName.append(("" + name.charAt(i)).toUpperCase());
  125. wasSpace = false;
  126. } else {
  127. newName.append(name.charAt(i));
  128. }
  129. if (name.charAt(i) == ' ') {
  130. wasSpace = true;
  131. }
  132. }
  133. return newName.toString();
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement