-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added top 1, 2, 3 placeholders for elo
- Loading branch information
Showing
8 changed files
with
159 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package me.funky.praxi; | ||
|
||
|
||
import me.clip.placeholderapi.expansion.PlaceholderExpansion; | ||
import me.funky.praxi.leaderboards.Leaderboard; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.ChatColor; | ||
import org.bukkit.OfflinePlayer; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class Placeholder extends PlaceholderExpansion { | ||
@Override | ||
public @NotNull String getIdentifier() { | ||
return "praxi"; | ||
} | ||
|
||
@Override | ||
public @NotNull String getAuthor() { | ||
return "lrxh"; | ||
} | ||
|
||
@Override | ||
public @NotNull String getVersion() { | ||
return "1.0"; | ||
} | ||
|
||
@Override | ||
public boolean canRegister() { | ||
return Bukkit.getPluginManager().isPluginEnabled("Praxi"); | ||
} | ||
|
||
@Override | ||
public String onRequest(OfflinePlayer player, @NotNull String identifier) { | ||
if (player == null) return ""; | ||
if (!player.isOnline()) return "Offline Player"; | ||
|
||
switch (identifier) { | ||
case "top_1": | ||
return Leaderboard.getTopPositions().get(0).getPlayerElo().getPlayerName(); | ||
case "top_2": | ||
return Leaderboard.getTopPositions().get(1).getPlayerElo().getPlayerName(); | ||
case "top_3": | ||
return Leaderboard.getTopPositions().get(2).getPlayerElo().getPlayerName(); | ||
} | ||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/me/funky/praxi/leaderboards/Leaderboard.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package me.funky.praxi.leaderboards; | ||
|
||
import com.mongodb.client.FindIterable; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import me.funky.praxi.profile.Profile; | ||
import org.bson.Document; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class Leaderboard { | ||
@Getter | ||
private static final List<Positions> topPositions = init(); | ||
|
||
public static List<Positions> init() { | ||
FindIterable<Document> profileDocuments = Profile.collection.find(); | ||
List<PlayerElo> topPlayers = profileDocuments.into(new ArrayList<>()).stream() | ||
.map(Leaderboard::mapToPlayerElo) | ||
.sorted(Comparator.reverseOrder()) | ||
.limit(3) | ||
.collect(Collectors.toList()); | ||
|
||
return IntStream.range(0, topPlayers.size()) | ||
.mapToObj(i -> new Positions(i + 1, topPlayers.get(i))) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private static PlayerElo mapToPlayerElo(Document profileDocument) { | ||
String playerName = profileDocument.getString("username"); | ||
int elo = getElo(profileDocument); | ||
return new PlayerElo(playerName, elo); | ||
} | ||
|
||
private static int getElo(Document profileDocument) { | ||
Document kitStatistics = (Document) profileDocument.get("kitStatistics"); | ||
int totalQueue = kitStatistics.keySet().size(); | ||
if (totalQueue == 0) { | ||
return 0; | ||
} | ||
|
||
return kitStatistics.values().stream() | ||
.mapToInt(kit -> ((Document) kit).getInteger("elo")) | ||
.sum() / totalQueue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package me.funky.praxi.leaderboards; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class PlayerElo implements Comparable<PlayerElo> { | ||
private final String playerName; | ||
private final int elo; | ||
|
||
|
||
@Override | ||
public int compareTo(PlayerElo other) { | ||
return Integer.compare(this.elo, other.elo); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package me.funky.praxi.leaderboards; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
public class Positions { | ||
private final int rank; | ||
private final PlayerElo playerElo; | ||
|
||
@Override | ||
public String toString() { | ||
return rank + ". " + playerElo.getPlayerName() + ", ELO: " + playerElo.getElo(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
main: me.funky.praxi.Praxi | ||
name: Praxi | ||
version: '1.0.0' | ||
author: funkyranveer, joeleoli, lugami, lrxh | ||
author: funkyranveer, joeleoli, lugami, lrxh | ||
depend: | ||
- ProtocolLib | ||
softdepend: | ||
- PlaceholderAPI |