Leaderboards/Sorting & looping through config

Discussion in 'Plugin Development' started by kameronn, Sep 5, 2016.

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

    kameronn

    I'm making a leaderboard command for my stats plugin, however the problem is sorting them, the problem is I do not know what an efficient way to sort the data.

    Code:
        String pluginName;
        String folderName;
       
        PlayerConfig config;
       
        public void getStats(Player player) {
            this.pluginName = "PotentiaPvP";
            this.folderName = "data";
           
            int[] allelo;
           
            File dir = new File(Bukkit.getPluginManager().getPlugin(pluginName).getDataFolder() + File.separator + this.folderName);
              File[] directoryListing = dir.listFiles();
              if (directoryListing != null) {
                for (File child : directoryListing) {
    
                    String name = child.getName();
                    name.replace(",yml", "");
                   
                    config = new PlayerConfig(name);
                   
                   
                    int elo = config.getPlayerConfiguration().getInt("stats.elo");
                   
    
                   
                }
              } else {
                 
                  System.out.println("Error! There are no stats.");
    
              }
        }
    
    }
    What I'm doing is getting all the player files in the data directory, and converting them into file configurations, then I was going to add them to a int arraylist list, sort them and then make a for loop to get the top 10 highest numbers, but the problem is, how will I get which player those numbers came from, and is there a better way to do this?

    What I though of is either using a hashmap, but that would be a problem since, offline players and lag, I also thought of making a arraylist string such as "uuid, score" then split them, but I dont wanna do that either, what can I do?
     
  2. Offline

    ZP18

    First of all I'd just use one file and I'd use an ArrayList and for loop to sort


    Sent from my iPhone using Tapatalk
     
  3. @kameronn
    I don't understand your argument against the HashMap. If OfflinePlayer objects really do cause that much lag (which I seriously doubt they do), why not store UUID's in the map?
     
  4. Offline

    MatsExe

    I don't like spoonfeeding but...

    Code:
    public class ScoreComparator implements Comparator {
    
        Map base; //Can be any Map, in this case your Integers and Player names/UUID's
    
        public ScoreComparator(Map base) {
            this.base = base;
        }
    
        public int compare(Object a, Object b) { //Compares every single value in the map with all the other values
            if ((Integer) base.get(a) != null) {
                if (((Integer) base.get(a)) < ((Integer) base.get(b))) { //In case the value is higher, move the value up in the map
                    return 1;
                }
                if ((Integer) base.get(a) == (Integer) base.get(b)) { //In case the value is the name, compare the name/uuid by alphabetic order
                    return a.toString().compareTo(b.toString());
                }
                return -1; //In case the value is lower, move the value down
            }
            return 0;
        }
    }
    Code:
    private Map getSortedMap(Map base) {
            Map unsorted = base;
        
            ScoreComparator compare = new ScoreComparator(unsorted);
        
            TreeMap<UUID, Integer> leaderboard = new TreeMap<UUID, Integer>(compare);
            leaderboard.putAll(unsorted); //Put the sorted data into a TreeMap
        
            return leaderboard;
        }
    This will return a TreeMap where the Integer value is sorted or the UUID or name where the Integer values equal. Just store the data from the config into a HashMap and call the method.
     
    Last edited: Sep 6, 2016
  5. Offline

    ZP18

    Don't like spoon feeding but still does it...


    Sent from my iPhone using Tapatalk
     
  6. Offline

    Zombie_Striker

    @MatsExe
    "I don't link spoonfeeding, but I'll do it" You did not even try to explain what each line does. You did not even provide documentation for the methods.
     
  7. Offline

    MatsExe

    @ZP18 @Zombie_Striker No need to be pissed, I am only trying to help and since I am not natively english I found it difficult to explain the process via words.
     
  8. Offline

    ZP18

    You could have at least commented the code so the OP knows what each part does?


    Sent from my iPhone using Tapatalk
     
Thread Status:
Not open for further replies.

Share This Page