Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
Modified baltop command to include pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Flibio committed May 21, 2016
1 parent 42915fa commit 02a6032
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 32 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ sourceCompatibility = '1.8'
targetCompatibility = '1.8'

group 'io.github.flibio'
version '2.0.7'
version '2.1.0'

defaultTasks 'licenseFormat', 'clean', 'build'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ default public boolean deposit(UUID uuid, BigDecimal amount, Currency currency)
/**
* Gets the top unique accounts registered in the EconomyLite system.
*
* @param start The starting account to get.
* @param end The ending account to get.
* @return The top unique accounts registered in the EconomyLite system.
*/
public List<UniqueAccount> getTopAccounts();
public List<UniqueAccount> getTopAccounts(int start, int end);

}
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,10 @@ default public boolean deposit(String id, BigDecimal amount, Currency currency)
/**
* Gets the top virtual accounts registered in the EconomyLite system.
*
* @param start The starting account to get.
* @param end The ending account to get.
* @return The top virtual accounts registered in the EconomyLite system.
*/
public List<VirtualAccount> getTopAccounts();
public List<VirtualAccount> getTopAccounts(int start, int end);

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,51 +24,106 @@
*/
package io.github.flibio.economylite.commands.balance;

import com.google.common.collect.ImmutableMap;
import io.github.flibio.economylite.EconomyLite;
import io.github.flibio.utils.commands.AsyncCommand;
import io.github.flibio.utils.commands.BaseCommandExecutor;
import io.github.flibio.utils.commands.Command;
import io.github.flibio.utils.message.MessageStorage;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.args.GenericArguments;
import org.spongepowered.api.command.spec.CommandSpec;
import org.spongepowered.api.command.spec.CommandSpec.Builder;
import org.spongepowered.api.service.economy.Currency;
import org.spongepowered.api.service.economy.account.UniqueAccount;
import org.spongepowered.api.text.Text;

import com.google.common.collect.ImmutableMap;

import io.github.flibio.economylite.EconomyLite;
import io.github.flibio.utils.commands.AsyncCommand;
import io.github.flibio.utils.commands.BaseCommandExecutor;
import io.github.flibio.utils.commands.Command;
import org.spongepowered.api.text.action.TextActions;
import org.spongepowered.api.text.format.TextColors;

import java.math.BigDecimal;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;

@AsyncCommand
@Command(aliases = {"baltop"}, permission = "economylite.baltop")
public class BalTopCommand extends BaseCommandExecutor<CommandSource> {

MessageStorage messages = EconomyLite.getMessageStorage();

private int count;

@Override
public Builder getCommandSpecBuilder() {
return CommandSpec.builder()
.executor(this);
.executor(this)
.arguments(GenericArguments.optional(GenericArguments.integer(Text.of("page"))));
}

@Override
public void run(CommandSource src, CommandContext args) {
Currency currency = EconomyLite.getCurrencyService().getCurrentCurrency();
src.sendMessage(EconomyLite.getMessageStorage().getMessage("command.baltop.head"));
Currency current = EconomyLite.getEconomyService().getDefaultCurrency();
Currency current = EconomyLite.getCurrencyService().getCurrentCurrency();
TreeMap<String, BigDecimal> bals = new TreeMap<>();
for (UniqueAccount account : EconomyLite.getPlayerService().getTopAccounts()) {
Optional<Integer> pOpt = args.<Integer>getOne("page");
int pageNumber = 1;
if (pOpt.isPresent()) {
pageNumber = pOpt.get();
}
if (pageNumber < 1) {
src.sendMessage(messages.getMessage("command.baltop.invalidpage"));
return;
}
int start, end;
if (pageNumber == 1) {
start = 1;
end = 5;
} else {
start = ((pageNumber - 1) * 5) + 1;
end = pageNumber * 10;
}
for (UniqueAccount account : EconomyLite.getPlayerService().getTopAccounts(start, end + 1)) {
bals.put(account.getDisplayName().toPlain(), account.getBalance(current));
}
bals.entrySet().stream().sorted(Map.Entry.<String, BigDecimal>comparingByValue().reversed()).limit(3).forEachOrdered(e -> {
boolean nextPage = true;
if (bals.size() < 1) {
src.sendMessage(messages.getMessage("command.baltop.nodata"));
return;
}
if (bals.size() < 6) {
nextPage = false;
}
src.sendMessage(messages.getMessage("command.baltop.head", "page", String.valueOf(pageNumber)));
count = start;
bals.entrySet().stream().sorted(Map.Entry.<String, BigDecimal>comparingByValue().reversed()).limit(5).forEachOrdered(e -> {
Text label = current.getPluralDisplayName();
if (e.getValue().equals(BigDecimal.ONE)) {
label = current.getDisplayName();
}
src.sendMessage(EconomyLite.getMessageStorage().getMessage("command.baltop.data",
ImmutableMap.of("name", Text.of(e.getKey()), "balance", currency.format(e.getValue()), "label", label)));
src.sendMessage(messages.getMessage(
"command.baltop.data",
ImmutableMap.of("position", Text.of(count), "name", Text.of(e.getKey()), "balance",
current.format(e.getValue()), "label", label)));
count++;
});
Text toSend = Text.of();
if (pageNumber > 1) {
toSend = toSend.toBuilder().append(
messages.getMessage("command.baltop.navigation", "button", "<-").toBuilder()
.onClick(TextActions.runCommand("/baltop " + (pageNumber - 1)))
.onHover(TextActions.showText(Text.of(TextColors.GREEN, "BACK"))).build()).build();
toSend = toSend.toBuilder().append(Text.of(" ")).build();
}
toSend = toSend.toBuilder().append(messages.getMessage("command.baltop.navigation", "button", String.valueOf(pageNumber))).build();
if (nextPage) {
toSend = toSend.toBuilder().append(Text.of(" ")).build();
toSend = toSend.toBuilder().append(
messages.getMessage("command.baltop.navigation", "button", "->").toBuilder()
.onClick(TextActions.runCommand("/baltop " + (pageNumber + 1)))
.onHover(TextActions.showText(Text.of(TextColors.GREEN, "NEXT"))).build()).build();
}
if (!toSend.toPlain().isEmpty())
src.sendMessage(toSend);
count = 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,14 @@ public void clearCurrency(Currency currency) {
manager.executeUpdate("DELETE FROM economyliteplayers WHERE currency = ?", currency.getId());
}

public List<UniqueAccount> getTopAccounts() {
public List<UniqueAccount> getTopAccounts(int start, int end) {
int offset = start - 1;
int limit = end - offset;
ArrayList<UniqueAccount> accounts = new ArrayList<>();
List<String> uuids =
manager.queryTypeList("uuid", String.class, "SELECT uuid FROM economyliteplayers WHERE currency = ? ORDER BY balance DESC LIMIT 3",
EconomyLite.getEconomyService().getDefaultCurrency().getId());
manager.queryTypeList("uuid", String.class,
"SELECT uuid FROM economyliteplayers WHERE currency = ? ORDER BY balance DESC LIMIT ?, ?",
EconomyLite.getEconomyService().getDefaultCurrency().getId(), String.valueOf(offset), String.valueOf(limit));
EconomyService ecoService = EconomyLite.getEconomyService();
for (String uuid : uuids) {
Optional<UniqueAccount> uOpt = ecoService.getOrCreateAccount(UUID.fromString(uuid));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.spongepowered.api.service.economy.EconomyService;
import org.spongepowered.api.service.economy.account.Account;
import org.spongepowered.api.service.economy.account.VirtualAccount;

import io.github.flibio.economylite.EconomyLite;
import io.github.flibio.economylite.api.VirtualEconService;
import io.github.flibio.utils.sql.SqlManager;
Expand Down Expand Up @@ -83,11 +82,13 @@ public void clearCurrency(Currency currency) {
manager.executeUpdate("DELETE FROM economylitevirts WHERE currency = ?", currency.getId());
}

public List<VirtualAccount> getTopAccounts() {
public List<VirtualAccount> getTopAccounts(int start, int end) {
int offset = start - 1;
int limit = end - offset;
ArrayList<VirtualAccount> accounts = new ArrayList<>();
List<String> ids =
manager.queryTypeList("id", String.class, "SELECT id FROM economylitevirts WHERE currency = ? ORDER BY balance DESC LIMIT 3",
EconomyLite.getEconomyService().getDefaultCurrency().getId());
manager.queryTypeList("id", String.class, "SELECT id FROM economylitevirts WHERE currency = ? ORDER BY balance DESC LIMIT ?, ?",
EconomyLite.getEconomyService().getDefaultCurrency().getId(), String.valueOf(offset), String.valueOf(limit));
EconomyService ecoService = EconomyLite.getEconomyService();
for (String id : ids) {
Optional<Account> vOpt = ecoService.getOrCreateAccount(id);
Expand All @@ -97,5 +98,4 @@ public List<VirtualAccount> getTopAccounts() {
}
return accounts;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,14 @@ public class LiteUniqueAccount implements UniqueAccount {

public LiteUniqueAccount(UUID uuid) {
this.uuid = uuid;
Optional<String> nOpt = NameUtils.getName(uuid);
if (nOpt.isPresent()) {
this.name = nOpt.get();
} else {
try {
Optional<String> nOpt = NameUtils.getName(uuid);
if (nOpt.isPresent()) {
this.name = nOpt.get();
} else {
this.name = uuid.toString();
}
} catch (Exception e) {
this.name = uuid.toString();
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ command.noperm=&cYou do not have permission to run this command!
command.invalidsource=&cYou must be a {sourcetype} to use this command!
command.error=&cAn internal error has occurred!

command.baltop.head=&6Top Three Balances:
command.baltop.data=&6{name}: &a{balance} &6{label}
command.baltop.head=&6&lTop Balances
command.baltop.data=&7[&f{position}&7] &e{name}: &a{balance} &e{label}
command.baltop.invalidpage=&cInvalid page number!
command.baltop.nodata=&cNo data found for that page!
command.baltop.navigation=&7[&a{button}&7]

command.balance=&6Current Balance: &a{balance} &6{label}
command.balanceother=&6{player}'s Balance: &a{balance} &6{label}
Expand Down

0 comments on commit 02a6032

Please sign in to comment.