diff --git a/src/config.yml b/src/config.yml index 2938bbb..c15892f 100644 --- a/src/config.yml +++ b/src/config.yml @@ -1,6 +1,6 @@ ######################################################################## # # -# CryptoMarket v1.0 # +# CryptoMarket v1.1.0 # # This is cryptocurrency market simulator based on real prices # # The prices come live from https://www.CoinMarketCap.com # # # @@ -13,7 +13,7 @@ price: 100 # The language of messages. # You can create Your own by editing 'messages_en.yml' in Resources dir -# and saving it with name i.e. 'messages_pl.yml' - lang: pl +# and saving it with name i.e. 'messages_pl.yml' - then set lang: pl lang: en # The Transaction Fee for transfers and exchanges as a percentage diff --git a/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/api/Player.java b/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/api/Player.java index 3fdd7e6..1f93718 100644 --- a/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/api/Player.java +++ b/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/api/Player.java @@ -1,5 +1,6 @@ package main.java.pl.csrv.divinecraft.evirth.cryptomarket.api; +import main.java.pl.csrv.divinecraft.evirth.cryptomarket.commands.models.Stats; import main.java.pl.csrv.divinecraft.evirth.cryptomarket.enums.TransactionType; import main.java.pl.csrv.divinecraft.evirth.cryptomarket.helpers.CoinHelper; import main.java.pl.csrv.divinecraft.evirth.cryptomarket.models.Coin; @@ -362,6 +363,32 @@ public void printStats() { this.player.sendMessage(this.checkStats()); } + /** + * Gets a Stats object which presents Player's statistics. + * + * @return The Player's statistics as the Stats object. + */ + public Stats getStats() { + if (this.account.getBalance().isEmpty()) { + return null; + } + + List withdrawals = this.account.getTransactions().stream().filter(f -> f.getType() == TransactionType.WITHDRAWAL).collect(Collectors.toList()); + List deposits = this.account.getTransactions().stream().filter(f -> f.getType() == TransactionType.DEPOSIT).collect(Collectors.toList()); + + int wDiamonds = 0; + int dDiamonds = 0; + for (Transaction t : deposits) { + dDiamonds += t.getAmountOfDiamonds(); + } + for (Transaction t : withdrawals) { + wDiamonds += t.getAmountOfDiamonds(); + } + + int result = wDiamonds - dDiamonds; + return new Stats(this.name, dDiamonds, wDiamonds, result); + } + /** * Gets a message which presents Player's statistics. * diff --git a/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/commands/CommandExecutorImpl.java b/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/commands/CommandExecutorImpl.java index fdea276..c252097 100644 --- a/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/commands/CommandExecutorImpl.java +++ b/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/commands/CommandExecutorImpl.java @@ -20,6 +20,7 @@ public CommandExecutorImpl() { new Command("global", CryptoMarket.resourceManager.getResource("GlobalCommandDescription"), "/cm global"), new Command("stats", CryptoMarket.resourceManager.getResource("StatsCommandDescription"), "/cm stats [player]"), new Command("history", CryptoMarket.resourceManager.getResource("HistoryCommandDescription"), "/cm history [page/player]"), + new Command("top", CryptoMarket.resourceManager.getResource("TopCommandDescription"), "/cm top"), new Command("add", CryptoMarket.resourceManager.getResource("AddCommandDescription"), "/cm add [d] "), new Command("remove", CryptoMarket.resourceManager.getResource("RemoveCommandDescription"), "/cm remove [d] ") }; diff --git a/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/commands/CommandFactory.java b/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/commands/CommandFactory.java index 29504ce..c267c98 100644 --- a/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/commands/CommandFactory.java +++ b/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/commands/CommandFactory.java @@ -40,6 +40,9 @@ public static ICommand create(String command) { case "history": c = new HistoryCommand(); break; + case "top": + c = new TopCommand(); + break; case "add": c = new AddCommand(); break; diff --git a/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/commands/models/Stats.java b/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/commands/models/Stats.java new file mode 100644 index 0000000..dcdff1f --- /dev/null +++ b/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/commands/models/Stats.java @@ -0,0 +1,31 @@ +package main.java.pl.csrv.divinecraft.evirth.cryptomarket.commands.models; + +public class Stats { + private int depositedDiamonds; + private int withdrawnDiamonds; + private int finalStats; + private String playerName; + + public Stats(String playerName, int depositedDiamonds, int withdrawnDiamonds, int finalStats) { + this.playerName = playerName; + this.depositedDiamonds = depositedDiamonds; + this.withdrawnDiamonds = withdrawnDiamonds; + this.finalStats = finalStats; + } + + public int getDepositedDiamonds() { + return this.depositedDiamonds; + } + + public int getWithdrawnDiamonds() { + return this.withdrawnDiamonds; + } + + public int getFinalStats() { + return this.finalStats; + } + + public String getPlayerName() { + return this.playerName; + } +} \ No newline at end of file diff --git a/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/commands/player/PriceCommand.java b/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/commands/player/PriceCommand.java index 0693d4c..3797643 100644 --- a/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/commands/player/PriceCommand.java +++ b/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/commands/player/PriceCommand.java @@ -25,12 +25,12 @@ public boolean execute(CommandSender commandSender, String[] strings) { List list = CoinMarketCap.ticker(10); if (list != null) { for (CoinMarket coin : list) { - sb.append(ChatColor.translateAlternateColorCodes('&', String.format("#%s. &6%s&f (&6%s&f) - &6$%.6f&f\n", coin.getRank(), coin.getName(), coin.getSymbol(), coin.getPriceUSD()))); + sb.append(ChatColor.translateAlternateColorCodes('&', String.format("#%d. &6%s&f (&6%s&f) - &6$%.6f&f\n", coin.getRank(), coin.getName(), coin.getSymbol(), coin.getPriceUSD()))); } } } else { CoinMarket coin = CoinMarketCap.ticker(strings[1]); - sb.append(ChatColor.translateAlternateColorCodes('&', String.format("#%s. &6%s&f (&6%s&f) - &6$%.6f&f", coin.getRank(), coin.getName(), coin.getSymbol(), coin.getPriceUSD()))); + sb.append(ChatColor.translateAlternateColorCodes('&', String.format("#%d. &6%s&f (&6%s&f) - &6$%.6f&f", coin.getRank(), coin.getName(), coin.getSymbol(), coin.getPriceUSD()))); } } catch (Exception e) { sb.append(CryptoMarket.resourceManager.getResource("CouldNotGetInfoAboutCrypto")); diff --git a/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/commands/player/TopCommand.java b/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/commands/player/TopCommand.java new file mode 100644 index 0000000..6e26cab --- /dev/null +++ b/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/commands/player/TopCommand.java @@ -0,0 +1,59 @@ +package main.java.pl.csrv.divinecraft.evirth.cryptomarket.commands.player; + +import main.java.pl.csrv.divinecraft.evirth.cryptomarket.CryptoMarket; +import main.java.pl.csrv.divinecraft.evirth.cryptomarket.api.Player; +import main.java.pl.csrv.divinecraft.evirth.cryptomarket.commands.ICommand; +import main.java.pl.csrv.divinecraft.evirth.cryptomarket.commands.Permissions; +import main.java.pl.csrv.divinecraft.evirth.cryptomarket.commands.models.Stats; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.OfflinePlayer; +import org.bukkit.command.CommandSender; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +public class TopCommand implements ICommand { + + @Override + public boolean execute(CommandSender commandSender, String[] strings) { + try { + if (!commandSender.hasPermission(Permissions.CRYPTOMARKET_PLAYER)) { + commandSender.sendMessage(CryptoMarket.resourceManager.getResource("MissingPermission")); + return true; + } + + List stats = new ArrayList<>(); + OfflinePlayer[] players = Bukkit.getOfflinePlayers(); + for (OfflinePlayer o : players) { + Player p = new Player(o.getName()); + Stats s = p.getStats(); + if (s != null) { + stats.add(s); + } + } + + stats.sort(Comparator.comparing(Stats::getFinalStats)); + Collections.reverse(stats); + StringBuilder sb = new StringBuilder(); + sb.append(ChatColor.translateAlternateColorCodes('&', "&6----- CryptoMarket ----- Top Players -----\n")); + for (int i = 0; i < stats.size() && i < 3; i++) { + sb.append(ChatColor.translateAlternateColorCodes('&', + String.format("#%d. &5%s&f - Stats: &%s%d&f (Withdrawn: &a%d&f : Deposited: &c%d&f)\n", + i + 1, + stats.get(i).getPlayerName(), + stats.get(i).getFinalStats() < 0 ? "c" : "a", + stats.get(i).getFinalStats(), + stats.get(i).getWithdrawnDiamonds(), + stats.get(i).getDepositedDiamonds()))); + } + sb.append(ChatColor.translateAlternateColorCodes('&', "&6---------------------------------------")); + commandSender.sendMessage(sb.toString().split("\n")); + } catch (Exception e) { + commandSender.sendMessage(CryptoMarket.resourceManager.getResource("CouldNotGetTopList")); + } + return true; + } +} diff --git a/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/resources/ResourceManager.java b/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/resources/ResourceManager.java index fdc95c7..4fe1080 100644 --- a/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/resources/ResourceManager.java +++ b/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/resources/ResourceManager.java @@ -43,6 +43,10 @@ public ResourceManager(Plugin plugin) { if (currentFileLines != resourceFileLines) { File fbak = new File(Paths.get(this.resourcePath, file + ".old").toString()); + if (fbak.exists()) { + fbak.delete(); + } + if (f.renameTo(fbak)) { this.exportResource(file); this.plugin.getLogger().info(String.format("Resource file '%s' has been updated.", file)); diff --git a/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/resources/messages_en.yml b/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/resources/messages_en.yml index b0b7c75..8c23263 100644 --- a/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/resources/messages_en.yml +++ b/src/main/java/pl/csrv/divinecraft/evirth/cryptomarket/resources/messages_en.yml @@ -10,6 +10,7 @@ CouldNotGetInfoAboutGlobals: "Could not get information about global data. Pleas CouldNotGetPlayerBalance: "Could not get &5%s&f's balance." CouldNotGetPlayerHistory: "Could not get &5%s&f's transaction history." CouldNotGetPlayerStats: "Could not get &5%s&f's stats." +CouldNotGetTopList: "Could not get Top Players list." DontHaveCoin: "You don't have any &6%s&f coin." DontHaveThatAmountOfDiamonds: "You don't have that amount of Diamonds." DontHaveThatManyCoins: "You don't have that many &6%s&f coins." @@ -43,6 +44,6 @@ PriceCommandDescription: "Shows the current price of cryptocurrency." GlobalCommandDescription: "Shows the global data." StatsCommandDescription: "Shows the statistics." HistoryCommandDescription: "Shows the transaction history." +TopCommandDescription: "Shows the Top Players list." AddCommandDescription: "Adds the balance to a specific player." -RemoveCommandDescription: "Removes the balance of a specific player." -CheckCommandDescription: "Shows Player's balance." \ No newline at end of file +RemoveCommandDescription: "Removes the balance of a specific player." \ No newline at end of file diff --git a/src/plugin.yml b/src/plugin.yml index 3ceadcc..097b253 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,5 +1,5 @@ name: CryptoMarket -version: 1.0.2 +version: 1.1.0 description: Cryptocurrency market simulator author: Evirth main: main.java.pl.csrv.divinecraft.evirth.cryptomarket.CryptoMarket