Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.unityps.network.mysql;
- import java.sql.PreparedStatement;
- import com.unityps.game.model.entity.players.Client;
- public class Highscores implements Runnable {
- private Client c;
- public Highscores(Client c) {
- this.c = c;
- }
- public Client getC() {
- return c;
- }
- public static boolean hascomplete = false;
- @Override
- public void run() {
- try {
- //Database db = new Database("jdbc:mysql://runeunity.com/runeunit_highscores", "runeunity", "unityscores911", "runeunit_highscores");
- Database db = new Database("192.99.69.241", "runeunit_scores", "unityscores911", "runeunit_highscores");
- String name = formatPlayerNameForDisplay(c.username);
- if (!db.init()) {
- System.err.println("Failing to update "+name+" highscores. Database could not connect.");
- return;
- }
- PreparedStatement stmt1 = db.prepare("DELETE FROM " + getTable() + " WHERE username=?");
- stmt1.setString(1, name);
- stmt1.execute();
- PreparedStatement stmt2 = db.prepare(generateQuery());
- stmt2.setString(1, name);
- stmt2.setInt(2, c.getRights().ordinal());
- stmt2.setLong(3, getTotalXp());
- for (int i = 0; i < 25; i++)
- stmt2.setInt(4 + i, (int) c.playerXP[i]);
- stmt2.execute();
- db.destroyAll();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public String generateQuery() {
- StringBuilder sb = new StringBuilder();
- sb.append("INSERT INTO "+ getTable() +" (");
- sb.append("username, ");
- sb.append("rights, ");
- sb.append("overall_xp, ");
- sb.append("attack_xp, ");
- sb.append("defence_xp, ");
- sb.append("strength_xp, ");
- sb.append("constitution_xp, ");
- sb.append("ranged_xp, ");
- sb.append("prayer_xp, ");
- sb.append("magic_xp, ");
- sb.append("cooking_xp, ");
- sb.append("woodcutting_xp, ");
- sb.append("fletching_xp, ");
- sb.append("fishing_xp, ");
- sb.append("firemaking_xp, ");
- sb.append("crafting_xp, ");
- sb.append("smithing_xp, ");
- sb.append("mining_xp, ");
- sb.append("herblore_xp, ");
- sb.append("agility_xp, ");
- sb.append("thieving_xp, ");
- sb.append("slayer_xp, ");
- sb.append("farming_xp, ");
- sb.append("runecrafting_xp, ");
- sb.append("dungeoneering_xp) "); //21
- sb.append("hunter_xp, ");//22
- sb.append("summoning_xp, "); //23
- sb.append("construction_xp, ");//24
- sb.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
- return sb.toString();
- }
- public String getTable() {
- return c.isIronman() ? "hs_users_ironman" : c.isHardcoreIronman() ? "hs_users_hard_ironman" : "hs_users";
- }
- /*public long getTotalXp() {
- long totalxp = 0L;
- for (int i = 0; i <= 24; i++) {
- totalxp += (double) c.playerXP[i];
- //hascomplete = true;
- }
- return totalxp;
- }*/
- public long getTotalXp() {
- long totalxp = 0;
- for (double xp : c.playerXP) {
- totalxp += xp;
- }
- return totalxp;
- }
- public int[] getLevels() {
- int[] levels = new int[25];
- for (int i = 0; i < levels.length; i++)
- levels[i] = c.getLevelForXP(c.playerXP[i]);
- return levels;
- }
- public static String formatPlayerNameForDisplay(String name) {
- if (name == null)
- return "";
- name = name.replaceAll("_", " ");
- name = name.toLowerCase();
- StringBuilder newName = new StringBuilder();
- boolean wasSpace = true;
- for (int i = 0; i < name.length(); i++) {
- if (wasSpace) {
- newName.append(("" + name.charAt(i)).toUpperCase());
- wasSpace = false;
- } else {
- newName.append(name.charAt(i));
- }
- if (name.charAt(i) == ' ') {
- wasSpace = true;
- }
- }
- return newName.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement