Top Stats

Discussion in 'Plugin Development' started by The Fancy Whale, Jan 25, 2014.

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

    The Fancy Whale

    If I have a config that looks like:
    Code:
    (Playername):
      Wins: 1
    (Playername):
      Wins: 2
    ETC.
    How do I get the top scorers in order to make a scoreboard/command? (I know how to make scoreboard and command just not sure how to get individual names and scores of the highest)
     
  2. Offline

    MexMaster

    you could read from your config (to refresh) all two secs
    and create 5 (or however players you wan't in your scoreboard) variables. the first variable will be the first player loaded from config, then check the next and place player in 1 place in second etc. load nextplayer check if bis wims are higher than the fith... if check if higher than
    4 etc etc.
     
  3. Offline

    The Fancy Whale

    MexMaster But how do I check if their wins are higher?
     
  4. Offline

    MexMaster

    wait a sec. i will start my pc and code a snippet

    here:
    with '//set the players as numbers...' i mean it would look like this in config:

    Code:
    players:
      '0':
        name: TrollFreak01
        wins: 5
      '1':
        name: Baboooo
        wins: 11
      '2':
        name: Whaaaaaat
        wins: 2
      '3':
        name: xxgamerxx
        wins: 27
    
    you would need to set the every player in config when he joins for the first time (check in playerjoinevent if the player is set in config or so...)

    Code:java
    1. //set the players as numbers...
    2.  
    3. int placeOne = -1;
    4. int placeTwo = -1;
    5. int placeThree = -1;
    6. int placeFour = -1;
    7. int placeFive = -1;
    8.  
    9. boolean nextPlayer = true;
    10. int currPlayer = 0;
    11.  
    12. while(nextPlayer){
    13.  
    14. if(config.isSet("players." + currPlayer + ".name")){
    15.  
    16. int currPlayerWins = config.getInt("players." + currPlayer + ".wins");
    17.  
    18. if(currPlayerWins > config.getInt("players." + placeOne + ".wins")){
    19.  
    20. placeFive = placeFour;
    21. placeFour = placeThree;
    22. placeThree = placeTwo;
    23. placeTwo = placeOne;
    24. placeOne = currPlayer;
    25.  
    26. }else if(currPlayerWins > config.getInt("players." + placeTwo + ".wins")){
    27.  
    28. placeFive = placeFour;
    29. placeFour = placeThree;
    30. placeThree = placeTwo;
    31. placeTwo = currPlayer;
    32.  
    33. }else if(currPlayerWins > config.getInt("players." + placeThree + ".wins")){
    34.  
    35. placeFive = placeFour;
    36. placeFour = placeThree;
    37. placeThree = currPlayer;
    38.  
    39. }else if(currPlayerWins > config.getInt("players." + placeFour + ".wins")){
    40.  
    41. placeFive = placeFour;
    42. placeFour = currPlayer;
    43.  
    44. }else if(currPlayerWins > config.getInt("players." + placeTwo + ".wins")){
    45.  
    46. placeFive = currPlayer;
    47.  
    48. }
    49.  
    50. //Set scoreboard (you need to get the name of players like config.getString("players." + currPlayer + ".name"))
    51.  
    52. }else{
    53.  
    54. nextPlayer = false;
    55.  
    56. }


    try this, haven't tested it

    btw: if nobody has killed anybody the first player loaded will be place one :D
    so you can improve this alot i think

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  5. Offline

    The Fancy Whale

  6. Offline

    MexMaster

    working?
     
  7. Offline

    The Fancy Whale

    I reread the code and saw that I would have to redo the entire stats. If I redo the stats Id prefer to do SQL but since I do not want to redo the stats file I'd like to avoid redoing it at all costs
     
  8. Offline

    The Fancy Whale

    Anyone have a solution?
     
  9. Offline

    MexMaster

    wait what do you wan't now? SQL?
     
  10. Offline

    The Fancy Whale

    MexMaster How to use my current stats configuration and still get the top stats.
     
  11. Offline

    MexMaster

    loop through all players (Bukkit.getOfflinePlayers() and Bukkit.getPlayers()[if offline players don't return the online players]) and check their stats with config.getString(p.get name() + "Wins")

    and then set it like I said above but with strings and set their names than set their number under which they are saved in config
     
  12. Offline

    The Fancy Whale

    MexMaster Do I have t log every time a player joins so I can get offline players?
     
  13. Offline

    ColaCraft


    Hello The_Fancy_Whale

    (I'm PorterK from bzmc.us :rolleyes:)

    Yeah it's easy, just use

    Code:java
    1. @EventHandler
    2. public void onPlayerJoinEvent(PlayerJoinEvent e){
    3.  
    4. }
     
  14. Offline

    The Fancy Whale

    ColaCraft Yes I know how to but would i have to make a separate players.yml file to keep track of offline players?
     
  15. Offline

    ColaCraft


    Code:java
    1. File p = new File("/plugins/MyPlugin/data/" + p.getName() + ".yml");
    2.  
    3.  


    Something like that?
     
  16. Offline

    The Fancy Whale

    No I am not looking for code. I am curious if in order to use Bukkit.getOfflinePlayers() do I need to log all of the players that join or does bukkit do that automatically?
     
  17. Offline

    Maurdekye

    You could implement a sorting algorithm to sort HashMaps by their values, and return an ArrayList ordered by the highest scores;
    Code:java
    1. public ArrayList<String> sortMap(Map<String, Integer> toSort) {
    2. ArrayList<String> fin = new ArrayList<String>();
    3. for (String player : toSort.keySet()) {
    4. for (int i=0;i<fin.size();i++) {
    5. String check = fin.get(i);
    6. if (toSort.get(check) > toSort.get(player)) {
    7. fin.add(i, check);
    8. break;
    9. }
    10. fin.add(check);
    11. }
    12. }
    13. return fin;
    14. }


    And then just loop through them, and add them one by one.
     
  18. Offline

    The Fancy Whale

    Maurdekye ColaCraft MexMaster But I take it there is no easy way to simply say. Find the highest number in this file. Now tell me the name its under?
     
  19. Offline

    Maurdekye

    The Fancy Whale
    Code:java
    1. List<String> names = getConfig().getStringList("players");
    2. HashMap<String, Integer> scores = new HashMap<String, Integer>();
    3. for (String name : names)
    4. scores.put(name, getConfig().getInteger(name));
    5. sortMap(scores).get(0);


    That is assuming of course, you have your config formatted like so;
    Code:
    players:
      - TheFancyWhale
      - DYNAbeast
      - PorterK
     
    scores:
      TheFancyWhale: 5
      DYNAbeast: 2
      PorterK: 56
     
  20. Offline

    The Fancy Whale

    Format is:
    Code:
    (playername)
      stats: (number)
     
  21. Offline

    Maurdekye

    The Fancy Whale If you want a list of players from the config, that has to be under a separate option. As far as I know there's no way to get every entry in a certain level as a list of strings.
     
  22. Offline

    The Fancy Whale

    Maurdekye Ill try formatting the stats file like that and ill get back to you. Thanks!
     
  23. Offline

    The Fancy Whale

    I am trying to implement this code but I am having trouble
     
  24. Offline

    MexMaster

    i would them directly into a arraylist (player names) and then sort with the stats in config

    algorithms: sorting-algorithms.com there are many you only need to port to java
     
  25. Offline

    Stoux

    I might be confused as well, as I see the answers fly all over the place, all assuming something else.

    But I figure you have a .yml file with all the player that managed to set a score, correct? So for example: I joined the server, set a score, I left the server. The file would then be:
    Code:
    Stoux2:
      stats: (number)
    If that's the case you can easily get all of the playernames out of the config using 'getKeys(false)'. Now you can do:
    config.getInt(playername + ".stats"); to get the score. You now have the ability to sort them, or do what ever you want with those scores. Here's an example (Recommend running this in A-Sync btw):

    Code:java
    1. //Class for the Entries
    2. private class StatEntry implements Comparable<StatEntry> { //Comparable is an interface that you can implement. It allows you to the Collections.sort() this item.
    3.  
    4. private String player;
    5. private int stat;
    6.  
    7. public StatEntry(String player, int stat) {
    8. this.player = player;
    9. this.stat = stat;
    10. }
    11.  
    12. @Override
    13. public int compareTo(StatEntry o) {
    14. return o.stat - stat; //Might be the other way around, always confused by that lol.
    15. }
    16.  
    17. }
    18.  
    19.  
    20.  
    21. //In some method:
    22. ArrayList<StatEntry> entries = new ArrayList<>();
    23.  
    24. Set<String> list = config.getKeys(false); //Get all of the playernames in the Config
    25. for (String playername : list) {
    26. int stat = config.getInt(playername + ".stats"); //Get the score
    27. entries.add( //Add a new StatEntry to the list
    28. new StatEntry(playername, stat)
    29. );
    30. }
    31.  
    32. Collections.sort(entries); //Sort the list
     
  26. Offline

    MexMaster

    wait, ok his last answer to this thread was, that he has trouble with implementing this...
    The Fancy Whale could you please give us more information about your trouble?
     
  27. Offline

    The Fancy Whale

    MexMaster Stoux I guess I am just confused once I have read all of the names. How do I get the highest scoring names. Also I am not sure which stats file to use because you all gave me different responses.
    I can use either
    Code:
    TheFancyWhale
      Stats: (Number)
    MexMaster
      Stats: (Number)
    Stoux
      Stats: (Number)
    Or I could use:
    Code:
    Players:
      - TheFancyWhale
      - MexMaster
      - Stoux
    Stats:
      TheFancyWhale: (number)
      MexMaster: (number)
      Stoux: (number)
     
  28. Offline

    MexMaster

Thread Status:
Not open for further replies.

Share This Page