SQL Leaderboard

Discussion in 'Plugin Development' started by Trytan, Sep 6, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Trytan

    I am currently creating a SQL Stat System and need help making a rank system as in top kills. I have finished the SQL kills, deaths, and points part of the code and now I need to grab the rank. Could someone help me.

    Code:
    package me.trytan.iffa;
    
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class SQLStats {
    
        public static boolean playerExists(String uuid) {
            try {
                ResultSet rs = Main.mysql.query("SELECT * FROM Stats WHERE UUID= '" + uuid + "'");
    
                if(rs.next()) {
                    return rs.getString("UUID") != null;
                }
                return false;
            }catch (SQLException e) {
                e.printStackTrace();
            }
            return false;
        }
    
        public static void createPlayer(String uuid){
            if(!(playerExists(uuid))){
                Main.mysql.update("INSERT INTO Stats(UUID, KILLS, DEATHS) VALUES ('" + uuid + "', '0', '0');");
            }
        }
    
        public static Integer getKills(String uuid){
            Integer i = 0;
            if(playerExists(uuid)){
                try {
                    ResultSet rs = Main.mysql.query("SELECT * FROM Stats WHERE UUID= '" + uuid + "'");
                    if((!rs.next()) || (Integer.valueOf(rs.getInt("KILLS")) == null));   
                    i = rs.getInt("KILLS");
                }catch (SQLException e) {
                    e.printStackTrace();
                }
            } else {
                createPlayer(uuid);
                getKills(uuid);
            }   
            return i;
        }
    
        public static Integer getDeaths(String uuid){
            Integer i = 0;
            if(playerExists(uuid)){
                try {
                    ResultSet rs = Main.mysql.query("SELECT * FROM Stats WHERE UUID= '" + uuid + "'");
                    if((!rs.next()) || (Integer.valueOf(rs.getInt("DEATHS")) == null));   
                    i = rs.getInt("DEATHS");
                }catch (SQLException e) {
                    e.printStackTrace();
                }
            } else {
                createPlayer(uuid);
                getDeaths(uuid);
            }   
            return i;
        }
    
        public static Integer getPoints(String uuid){
            Integer i = 0;
            if(playerExists(uuid)){
                try {
                    ResultSet rs = Main.mysql.query("SELECT * FROM Stats WHERE UUID= '" + uuid + "'");
                    if((!rs.next()) || (Integer.valueOf(rs.getInt("POINTS")) == null));   
                    i = rs.getInt("POINTS");
                }catch (SQLException e) {
                    e.printStackTrace();
                }
            } else {
                createPlayer(uuid);
                getPoints(uuid);
            }   
            return i;
        }
    
        public static void setKills(String uuid, Integer kills){
            if(playerExists(uuid)){
                Main.mysql.update("UPDATE Stats SET KILLS= '" + kills + "' WHERE UUID= '" + uuid + "';");
            } else {
                createPlayer(uuid);
                setKills(uuid, kills);
            }
        }
    
        public static void setDeaths(String uuid, Integer deaths){
            if(playerExists(uuid)){
                Main.mysql.update("UPDATE Stats SET DEATHS= '" + deaths + "' WHERE UUID= '" + uuid + "';");
            } else {
                createPlayer(uuid);
                setKills(uuid, deaths);
            }
        }
    
        public static void setPoints(String uuid, Integer points){
            if(playerExists(uuid)){
                Main.mysql.update("UPDATE Stats SET DEATHS= '" + points + "' WHERE UUID= '" + uuid + "';");
            } else {
                createPlayer(uuid);
                setKills(uuid, points);
            }
        }
    
        public static void addKills(String uuid, Integer kills){
            if(playerExists(uuid)){
                setKills(uuid, Integer.valueOf(getKills(uuid).intValue() + kills.intValue()));
            } else {
                createPlayer(uuid);
                addKills(uuid, kills);
            }
        }
    
        public static void addDeaths(String uuid, Integer deaths){
            if(playerExists(uuid)){
                setDeaths(uuid, Integer.valueOf(getDeaths(uuid).intValue() + deaths.intValue()));
            } else {
                createPlayer(uuid);
                addDeaths(uuid, deaths);
            }
        }
    
        public static void addPoints(String uuid, Integer points){
            if(playerExists(uuid)){
                setPoints(uuid, Integer.valueOf(getPoints(uuid).intValue() + points.intValue()));
            } else {
                createPlayer(uuid);
                addPoints(uuid, points);
            }
        }
    
        public static void removeKills(String uuid, Integer kills){
            if(playerExists(uuid)){
                setKills(uuid, Integer.valueOf(getKills(uuid).intValue() - kills.intValue()));
            } else {
                createPlayer(uuid);
                removeKills(uuid, kills);
            }
        }
    
        public static void removeDeaths(String uuid, Integer deaths){
            if(playerExists(uuid)){
                setDeaths(uuid, Integer.valueOf(getDeaths(uuid).intValue() - deaths.intValue()));
            } else {
                createPlayer(uuid);
                removeDeaths(uuid, deaths);
            }
        }
    
        public static void removePoints(String uuid, Integer points){
            if(playerExists(uuid)){
                setPoints(uuid, Integer.valueOf(getPoints(uuid).intValue() - points.intValue()));
            } else {
                createPlayer(uuid);
                removePoints(uuid, points);
            }
        }
    
        public static void getRank(String uuid, Integer rank){
    
    
        }
    
    }
    
     
  2. Offline

    RainoBoy97

    Use the ORDER BY keyword in SQL :) You may also want to LIMIT the query to X rows.
     
  3. Offline

    mythbusterma

    @Trytan

    Also, stop abusing "static" and don't run SQL queries on the main thread.
     
  4. Offline

    Eos

    o God, Those methods are going to cause tons of lag. What you need to do is create a hashmap with a list inside and store the kills, deaths bla bla in there. and when the player leaves or the server crashes save it into the mysql database alot more efficient
     
  5. Problem: you can't detect if the server crashes and then all data you haven't saved is lost
     
  6. Offline

    SuperOriginal

    Save it periodically... Asynchronously.
     
    mythbusterma likes this.
Thread Status:
Not open for further replies.

Share This Page