Skip to content

Commit

Permalink
Top command
Browse files Browse the repository at this point in the history
  • Loading branch information
Evirth committed Apr 1, 2018
1 parent e4770e7 commit a66c7ed
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/config.yml
Original file line number Diff line number Diff line change
@@ -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 #
# #
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Transaction> withdrawals = this.account.getTransactions().stream().filter(f -> f.getType() == TransactionType.WITHDRAWAL).collect(Collectors.toList());
List<Transaction> 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <player> <amount>[d] <crypto>"),
new Command("remove", CryptoMarket.resourceManager.getResource("RemoveCommandDescription"), "/cm remove <player> <amount>[d] <crypto>")
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public boolean execute(CommandSender commandSender, String[] strings) {
List<CoinMarket> 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"));
Expand Down
Original file line number Diff line number Diff line change
@@ -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> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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."
RemoveCommandDescription: "Removes the balance of a specific player."
2 changes: 1 addition & 1 deletion src/plugin.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit a66c7ed

Please sign in to comment.