diff --git a/src/main/java/de/fanta/challenges/scoreboard/ScoreManager.java b/src/main/java/de/fanta/challenges/scoreboard/ScoreManager.java index 9d59651..c0bc205 100644 --- a/src/main/java/de/fanta/challenges/scoreboard/ScoreManager.java +++ b/src/main/java/de/fanta/challenges/scoreboard/ScoreManager.java @@ -87,9 +87,11 @@ } public void join(Scorable s) { - ScoreData data = new ScoreData(s, positions.size()); - scores.put(s, data); - positions.add(data); + if (!scores.containsKey(s)) { + ScoreData data = new ScoreData(s, positions.size()); + scores.put(s, data); + positions.add(data); + } } public void leave(Scorable s) { @@ -109,12 +111,14 @@ int dir = (int) Math.signum(difference * -1); IntPredicate forCondition = (dir == -1) ? curr -> curr > 0 && positions.get(curr - 1).getScore() < data.getScore() - : curr -> curr < positions.size() - 1 && positions.get(curr + 1).getScore() > data.getScore(); + : curr -> curr < positions.size() - 2 && positions.get(curr + 1).getScore() > data.getScore(); // did we have the same score as the one before us and thus hat the same position but dont anymore because we got worse? - if (dir == 1 && current > 0 && data.getScore() == positions.get(current - 1).getScore()) { + /*if (dir == 1 && current > 0 && data.getScore() == positions.get(current - 1).getScore()) { data.setPosition(data.getPostition() + 1); - } + }*/ + + int startingIndex = data.getIndex(); // swap with neighbors as long as we are now better/worse then them for (; forCondition.test(current); current += dir) { @@ -125,23 +129,39 @@ data.setIndex(current + dir); other.setIndex(current); - int oldPosition = data.getPostition(); + /*int oldPosition = data.getPostition(); data.setPosition(other.getPostition()); - other.setPosition(oldPosition); + other.setPosition(oldPosition);*/ } // do we now have the same score as the one before us and thus get the same position? - if (dir == -1 && current > 0 && data.getScore() == positions.get(current - 1).getScore()) { + /*if (dir == -1 && current > 0 && data.getScore() == positions.get(current - 1).getScore()) { data.setPosition(positions.get(current + dir).getPostition()); + }*/ + + // update positions + int endIndex = data.getIndex(); + int i = Math.min(startingIndex, endIndex); + + ScoreData curr; + int lastPosition; + int lastScore; + if (i == 0) { + curr = positions.get(0); + curr.setPosition(1); + lastPosition = 1; + lastScore = curr.getScore(); + i++; + } else { + curr = positions.get(i-1); + lastPosition = curr.getPostition(); + lastScore = curr.getScore(); } - // do the positions of those below us change as a result of our new position? - int lastPosition = data.getPostition(); - int lastScore = data.getScore(); - for (int i = current + 1; i < positions.size(); i++) { - ScoreData curr = positions.get(i); + for (; i < positions.size(); i++) { + curr = positions.get(i); int correctPosition = (curr.getScore() == lastScore) ? lastPosition : lastPosition + 1; - if (curr.getPostition() == correctPosition) + if (curr.getPostition() == correctPosition && i > Math.max(startingIndex,endIndex)) break; curr.setPosition(correctPosition); @@ -179,6 +199,10 @@ if (current.getPostition() == position) { found = middle; break; + } else if (start >= end - 1) { + plugin.getLogger().log(Level.SEVERE, "position not found but expected to be present: " + position); + plugin.getLogger().log(Level.SEVERE, positions.toString()); + return Collections.emptySet(); } else if (current.getPostition() > position) { end = middle; } else { @@ -229,20 +253,18 @@ sb.append(" Das Event hatte keine Teilnehmer!\n"); } else { if (plugin.getConfig().getBoolean("event.teams")) { - int i = 1; for (ScoreData entry : positions) { ChallengeTeam challengeTeam = TeamUtils.getTeam(entry.getScorable().getName()); if (challengeTeam != null) { Team team = TeamUtils.getScoreboard().getTeam(challengeTeam.getName()); if (team != null) { - sb.append(i++).append(". ").append(entry.getScorable().getName()).append(" (").append(team.getEntries().toString().replace("[", "").replace("]", "")).append(")").append(" ").append(entry.getScore()).append("\n"); + sb.append(entry.getPostition()).append(". ").append(entry.getScorable().getName()).append(" (").append(team.getEntries().toString().replace("[", "").replace("]", "")).append(")").append(" ").append(entry.getScore()).append("\n"); } } } } else { - int i = 1; for (ScoreData entry : positions) { - sb.append(i++).append(". ").append(entry.getScorable().getName()).append(" ").append(entry.getScore()).append("\n"); + sb.append(entry.getPostition()).append(". ").append(entry.getScorable().getName()).append(" ").append(entry.getScore()).append("\n"); } } }