Add f top command functionality. Resolves #71.

Gives the ability to see top Factions on the server sorted by certain criteria:
Balance: sorts by faction bank balance and all faction member balances.
Power: total power.
Land: total land claimed.
Online: members currently online.
Members: total members, online and offline.

Future criteria can be added of course.
This commit is contained in:
drtshock 2015-01-23 09:07:18 -06:00
parent ebf00ccf0d
commit 3e6bd4e88a
5 changed files with 192 additions and 1 deletions

View File

@ -0,0 +1,180 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.zcore.util.TL;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class CmdTop extends FCommand {
public CmdTop() {
super();
this.aliases.add("top");
this.aliases.add("t");
//this.requiredArgs.add("");
this.requiredArgs.add("criteria");
this.optionalArgs.put("page", "1");
this.permission = Permission.TOP.node;
this.disableOnLock = false;
senderMustBePlayer = false;
senderMustBeMember = false;
senderMustBeModerator = false;
senderMustBeAdmin = false;
}
@Override
public void perform() {
// Can sort by: money, members, online, allies, enemies, power, land.
// Get all Factions and remove non player ones.
ArrayList<Faction> factionList = Factions.getInstance().getAllFactions();
factionList.remove(Factions.getInstance().getNone());
factionList.remove(Factions.getInstance().getSafeZone());
factionList.remove(Factions.getInstance().getWarZone());
String criteria = argAsString(0);
// TODO: Better way to sort?
if (criteria.equalsIgnoreCase("members")) {
Collections.sort(factionList, new Comparator<Faction>() {
@Override
public int compare(Faction f1, Faction f2) {
int f1Size = f1.getFPlayers().size();
int f2Size = f2.getFPlayers().size();
if (f1Size < f2Size) {
return 1;
} else if (f1Size > f2Size) {
return -1;
}
return 0;
}
});
} else if (criteria.equalsIgnoreCase("power")) {
Collections.sort(factionList, new Comparator<Faction>() {
@Override
public int compare(Faction f1, Faction f2) {
int f1Size = f1.getPowerRounded();
int f2Size = f2.getPowerRounded();
if (f1Size < f2Size) {
return 1;
} else if (f1Size > f2Size) {
return -1;
}
return 0;
}
});
} else if (criteria.equalsIgnoreCase("land")) {
Collections.sort(factionList, new Comparator<Faction>() {
@Override
public int compare(Faction f1, Faction f2) {
int f1Size = f1.getLandRounded();
int f2Size = f2.getLandRounded();
if (f1Size < f2Size) {
return 1;
} else if (f1Size > f2Size) {
return -1;
}
return 0;
}
});
} else if (criteria.equalsIgnoreCase("online")) {
Collections.sort(factionList, new Comparator<Faction>() {
@Override
public int compare(Faction f1, Faction f2) {
int f1Size = f1.getFPlayersWhereOnline(true).size();
int f2Size = f2.getFPlayersWhereOnline(true).size();
if (f1Size < f2Size) {
return 1;
} else if (f1Size > f2Size) {
return -1;
}
return 0;
}
});
} else if (criteria.equalsIgnoreCase("money") || criteria.equalsIgnoreCase("balance") || criteria.equalsIgnoreCase("bal")) {
Collections.sort(factionList, new Comparator<Faction>() {
@Override
public int compare(Faction f1, Faction f2) {
double f1Size = Econ.getBalance(f1.getAccountId());
// Lets get the balance of /all/ the players in the Faction.
for(FPlayer fp : f1.getFPlayers()) {
f1Size = f1Size + Econ.getBalance(fp.getAccountId());
}
double f2Size = Econ.getBalance(f2.getAccountId());
for(FPlayer fp : f1.getFPlayers()) {
f2Size = f2Size + Econ.getBalance(fp.getAccountId());
}
if (f1Size < f2Size) {
return 1;
} else if (f1Size > f2Size) {
return -1;
}
return 0;
}
});
} else {
msg(TL.COMMAND_TOP_INVALID, criteria);
}
ArrayList<String> lines = new ArrayList<String>();
factionList.add(0, Factions.getInstance().getNone());
final int pageheight = 9;
int pagenumber = this.argAsInt(1, 1);
int pagecount = (factionList.size() / pageheight) + 1;
if (pagenumber > pagecount) {
pagenumber = pagecount;
} else if (pagenumber < 1) {
pagenumber = 1;
}
int start = (pagenumber - 1) * pageheight;
int end = start + pageheight;
if (end > factionList.size()) {
end = factionList.size();
}
lines.add(TL.COMMAND_TOP_TOP.format(criteria.toUpperCase(), pagenumber, pagecount));
int rank = 1;
for (Faction faction : factionList.subList(start, end)) {
// Get the relation color if player is executing this.
String fac = sender instanceof Player ? faction.getRelationTo(fme).getColor() + faction.getTag() : faction.getTag();
lines.add(TL.COMMAND_TOP_LINE.format(rank, fac, getValue(faction, criteria)));
rank++;
}
sendMessage(lines);
}
private String getValue(Faction faction, String criteria) {
if (criteria.equalsIgnoreCase("online")) {
return String.valueOf(faction.getFPlayersWhereOnline(true).size());
} else if (criteria.equalsIgnoreCase("members")) {
return String.valueOf(faction.getFPlayers().size());
} else if (criteria.equalsIgnoreCase("land")) {
return String.valueOf(faction.getLandRounded());
} else if (criteria.equalsIgnoreCase("power")) {
return String.valueOf(faction.getPowerRounded());
} else { // Last one is balance, and it has 3 different things it could be.
double balance = Econ.getBalance(faction.getAccountId());
for(FPlayer fp : faction.getFPlayers()) {
balance = balance + Econ.getBalance(fp.getAccountId());
}
return String.valueOf(balance);
}
}
@Override
public TL getUsageTranslation() {
return TL.COMMAND_TOP_DESCRIPTION;
}
}

View File

@ -64,6 +64,7 @@ public class FCmdRoot extends FCommand {
public CmdModifyPower cmdModifyPower = new CmdModifyPower();
public CmdLogins cmdLogins = new CmdLogins();
public CmdClaimLine cmdClaimLine = new CmdClaimLine();
public CmdTop cmdTop = new CmdTop();
public FCmdRoot() {
super();
@ -143,6 +144,7 @@ public class FCmdRoot extends FCommand {
this.addSubCommand(this.cmdModifyPower);
this.addSubCommand(this.cmdLogins);
this.addSubCommand(this.cmdClaimLine);
this.addSubCommand(this.cmdTop);
}
@Override

View File

@ -73,6 +73,7 @@ public enum Permission {
SCOREBOARD("scoreboard"),
SEECHUNK("seechunk"),
SETWARP("setwarp"),
TOP("top"),
WARP("warp");
public final String node;

View File

@ -453,6 +453,11 @@ public enum TL {
COMMAND_TITLE_CHANGED("%1$s<i> changed a title: %2$s"),
COMMAND_TITLE_DESCRIPTION("Set or remove a players title"),
COMMAND_TOP_DESCRIPTION("Sort Factions to see the top of some criteria."),
COMMAND_TOP_TOP("Top Factions by %s. Page %d/%d"),
COMMAND_TOP_LINE("%d. &6%s: &c%s"), // Rank. Faction: Value
COMMAND_TOP_INVALID("Could not sort by %s. Try balance, online, members, power or land."),
COMMAND_UNCLAIM_SAFEZONE_SUCCESS("<i>Safe zone was unclaimed."),
COMMAND_UNCLAIM_SAFEZONE_NOPERM("<b>This is a safe zone. You lack permissions to unclaim."),
COMMAND_UNCLAIM_WARZONE_SUCCESS("<i>War zone was unclaimed."),

View File

@ -92,6 +92,7 @@ permissions:
factions.showinvites: true
factions.seechunk: true
factions.monitorlogins: true
factions.top: true
factions.admin:
description: hand over your admin rights
factions.admin.any:
@ -246,4 +247,6 @@ permissions:
factions.monitorlogins:
description: monitor join and leaves of faction members
factions.claim.line:
description: claim in a line
description: claim in a line
factions.top:
description: sort factions