Advertisement
Guest User

Java

a guest
Jul 28th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.62 KB | None | 0 0
  1. package ab.model.players;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. public class HighscoresHandler extends Thread {
  10.     private final String HOST = "";
  11.     private final String DATABASE = "";
  12.     private final String PASSWORD = "";
  13.     private final String USERNAME = "";
  14.     private final String PORT = "";
  15.  
  16.     private Player c;
  17.     private Connection con;
  18.     private Statement stmt;
  19.     private long total_level, total_exp;
  20.  
  21.     public HighscoresHandler(Player c) {
  22.         this.c = c;
  23.         c.inProcess = true;
  24.     }
  25.  
  26.     private long getTotalLevel() {
  27.         long totallevel = 0L;
  28.         for (int i = 0; i <= 20; i++) {
  29.             if (c.getLevelForXP(c.playerXP[i]) >= 99)
  30.                 totallevel += 99;
  31.             else
  32.                 totallevel += (double) c.getLevelForXP(c.playerXP[i]);
  33.         }
  34.         return totallevel;
  35.     }
  36.  
  37.     private long getTotalXp() {
  38.         long totalxp = 0L;
  39.         for (int i = 0; i <= 20; i++) {
  40.             totalxp += (double) c.playerXP[i];
  41.         }
  42.         return totalxp;
  43.     }
  44.  
  45.     @Override
  46.     public void run() {
  47.         try {
  48.             this.makeConnection();
  49.             this.total_level = this.getTotalLevel();
  50.             this.total_exp = this.getTotalXp();
  51.             if (stmt == null) {
  52.                 destroyConnection();
  53.                 return;
  54.             }
  55.             ResultSet rs = query("SELECT * FROM `hs` WHERE `username`='" + c.playerName + "'");
  56.             int r = 1;
  57.             while (rs.next() && r == 1) {
  58.                 for (int i = 0; i < 21; i++) {
  59.                     String lvl = "lvl_" + (i + 1);
  60.                     String xp = "xp_" + (i + 1);
  61.                     int level = c.getLevelForXP(c.playerXP[i]);
  62.                     if (level > 99 && i != 20)
  63.                         level = 99;
  64.                     query("UPDATE hs SET " + lvl + "='" + level + "', " + xp + "='" + c.playerXP[i] + "' WHERE username='" + c.playerName + "'");
  65.                 }
  66.                 String lvlkc = "lvl_22";
  67.                 String lvlxp = "xp_22";
  68.                 String hunternull = "lvl_23";
  69.                 String hunter = "xp_23";
  70.                 String roguenull = "lvl_24";
  71.                 String rogue = "xp_24";
  72.                 query("UPDATE hs SET " + roguenull + "='0', " + rogue + "='" + c.getBH().getTotalRogueKills() + "' WHERE username='" + c.playerName
  73.                         + "'");
  74.                 query("UPDATE hs SET " + hunternull + "='0', " + hunter + "='" + c.getBH().getTotalHunterKills() + "' WHERE username='" + c.playerName
  75.                         + "'");
  76.                 query("UPDATE hs SET " + lvlkc + "='" + c.DC + "', " + lvlxp + "='" + c.KC + "' WHERE username='" + c.playerName + "'");
  77.                 query("UPDATE hs SET rank='" + c.getRights().getValue() + "', total_exp='" + this.total_exp + "', total_lvl='" + this.total_level
  78.                         + "' WHERE username='" + c.playerName + "'");
  79.                 System.out.println("Highscores have been updated for " + c.playerName);
  80.                 r = 0;
  81.                 c.inProcess = true;
  82.                 this.destroyConnection();
  83.                 return;
  84.             }
  85.             String name = c.playerName;
  86.             query("INSERT INTO `hs`(`username`) VALUES('" + name + "')");
  87.             HighscoresHandler hh = new HighscoresHandler(c);
  88.             hh.start();
  89.         } catch (SQLException e) {
  90.             e.printStackTrace();
  91.         }
  92.     }
  93.  
  94.     public ResultSet query(String s) throws SQLException {
  95.         try {
  96.             if (s.toLowerCase().startsWith("select")) {
  97.                 ResultSet rs = stmt.executeQuery(s);
  98.                 return rs;
  99.             }
  100.             stmt.executeUpdate(s);
  101.             return null;
  102.         } catch (Exception e) {
  103.             e.printStackTrace();
  104.         }
  105.         return null;
  106.     }
  107.  
  108.     private void destroyConnection() {
  109.         try {
  110.             this.con = null;
  111.             this.stmt = null;
  112.         } catch (Exception e) {
  113.             e.printStackTrace();
  114.         }
  115.     }
  116.  
  117.     private void makeConnection() {
  118.         try {
  119.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  120.             con = DriverManager.getConnection("jdbc:mysql://" + HOST + ":" + PORT + "/" + DATABASE, USERNAME, PASSWORD);
  121.             stmt = con.createStatement();
  122.         } catch (Exception e) {
  123.             e.printStackTrace();
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement