Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/xdnw/locutus
Browse files Browse the repository at this point in the history
  • Loading branch information
xdnw committed Jun 21, 2024
2 parents e628f93 + 9e18d68 commit f0c9240
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.function.Predicate;

public enum Operation {
public enum MathOperation {
LESSER_EQUAL("<="),
GREATER_EQUAL(">="),
NOT_EQUAL("!="),
Expand All @@ -12,16 +12,16 @@ public enum Operation {

public final String code;

Operation(String s) {
MathOperation(String s) {
this.code = s;
}

public Predicate<Number> getNumberPredicate(Number compareTo) {
double val2 = compareTo.doubleValue();
return input -> {
if (input == null) return Operation.this == Operation.NOT_EQUAL;
if (input == null) return MathOperation.this == MathOperation.NOT_EQUAL;
double val1 = input.doubleValue();
return switch (Operation.this) {
return switch (MathOperation.this) {
case LESSER_EQUAL -> val1 <= val2;
case GREATER_EQUAL -> val1 >= val2;
case EQUAL -> val1 == val2;
Expand All @@ -34,8 +34,8 @@ public Predicate<Number> getNumberPredicate(Number compareTo) {

public Predicate<String> getStringPredicate(String compareTo) {
return val1 -> {
if (val1 == null) return Operation.this == Operation.NOT_EQUAL;
return switch (Operation.this) {
if (val1 == null) return MathOperation.this == MathOperation.NOT_EQUAL;
return switch (MathOperation.this) {
case LESSER_EQUAL -> val1.compareTo(compareTo) <= 0;
case GREATER_EQUAL -> val1.compareTo(compareTo) >= 0;
case EQUAL -> val1.equalsIgnoreCase(compareTo) || val1.matches(compareTo);
Expand All @@ -48,8 +48,8 @@ public Predicate<String> getStringPredicate(String compareTo) {

public Predicate<Boolean> getBooleanPredicate(boolean compareTo) {
return val1 -> {
if (val1 == null) return Operation.this == Operation.NOT_EQUAL;
return switch (Operation.this) {
if (val1 == null) return MathOperation.this == MathOperation.NOT_EQUAL;
return switch (MathOperation.this) {
case LESSER_EQUAL -> !val1;
case GREATER_EQUAL -> val1;
case EQUAL -> val1 == compareTo;
Expand All @@ -60,7 +60,7 @@ public Predicate<Boolean> getBooleanPredicate(boolean compareTo) {
};
}

public Operation opposite() {
public MathOperation opposite() {
return switch (this) {
case LESSER_EQUAL -> GREATER;
case GREATER_EQUAL -> LESSER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import link.locutus.discord.Locutus;
import link.locutus.discord.commands.manager.v2.binding.BindingHelper;
import link.locutus.discord.commands.manager.v2.binding.FunctionProviderParser;
import link.locutus.discord.commands.manager.v2.binding.Key;
import link.locutus.discord.commands.manager.v2.binding.LocalValueStore;
import link.locutus.discord.commands.manager.v2.binding.MethodParser;
Expand All @@ -22,18 +21,14 @@
import link.locutus.discord.commands.manager.v2.impl.pw.refs.CM;
import link.locutus.discord.commands.manager.v2.perm.PermissionHandler;
import link.locutus.discord.db.GuildDB;
import link.locutus.discord.db.entities.Coalition;
import link.locutus.discord.db.entities.SelectionAlias;
import link.locutus.discord.db.entities.SheetTemplate;
import link.locutus.discord.db.entities.DBNation;
import link.locutus.discord.util.StringMan;
import link.locutus.discord.util.math.ArrayUtil;
import link.locutus.discord.util.math.LazyMathEntity;
import link.locutus.discord.util.math.ReflectionUtil;
import link.locutus.discord.web.WebUtil;
import link.locutus.discord.web.commands.HtmlInput;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.User;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.math.NumberUtils;
Expand All @@ -42,9 +37,7 @@

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -304,8 +297,8 @@ public String getHtml(ValueStore store, String cmd, String parentId) {
ParametricCallable callable = get(cmd);
StringBuilder html = new StringBuilder(callable.toBasicHtml(store));
html.append("<select name=\"filter-operator\" for=\"").append(parentId).append("\" class=\"form-control\">");
for (Operation value : Operation.values()) {
String selected = value == Operation.EQUAL ? "selected=\"selected\"" : "";
for (MathOperation value : MathOperation.values()) {
String selected = value == MathOperation.EQUAL ? "selected=\"selected\"" : "";
html.append("<option value=\"").append(value.code).append("\" ").append(selected).append(">").append(StringEscapeUtils.escapeHtml4(value.code)).append("</option>");
}
html.append("</select>");
Expand Down Expand Up @@ -348,8 +341,8 @@ public Set<T> deserializeSelection(ValueStore store, String input) {
return parseSet(store, input);
}

private static Triple<String, Operation, String> opSplit(String input) {
for (Operation op : Operation.values()) {
private static Triple<String, MathOperation, String> opSplit(String input) {
for (MathOperation op : MathOperation.values()) {
List<String> split = StringMan.split(input, op.code, 2);
if (split.size() == 2) {
return Triple.of(split.get(0), op, split.get(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import link.locutus.discord.commands.manager.v2.binding.BindingHelper;
import link.locutus.discord.commands.manager.v2.impl.discord.binding.annotation.GuildCoalition;
import link.locutus.discord.commands.manager.v2.impl.discord.binding.annotation.NationDepositLimit;
import link.locutus.discord.commands.manager.v2.binding.bindings.Operation;
import link.locutus.discord.commands.manager.v2.binding.bindings.MathOperation;
import link.locutus.discord.commands.manager.v2.command.ParametricCallable;
import link.locutus.discord.commands.manager.v2.impl.pw.commands.UnsortedCommands;
import link.locutus.discord.commands.manager.v2.impl.pw.filter.NationPlaceholders;
Expand Down Expand Up @@ -147,7 +147,8 @@ public static DBBounty bounty(String input) {
}

@Binding(value = "The name of a nation attribute\n" +
"See: <https://github.com/xdnw/locutus/wiki/nation_placeholders>", examples = {"color", "war_policy", "continent"})
"See: <https://github.com/xdnw/locutus/wiki/nation_placeholders>", examples = {"color", "war_policy", "continent"},
webType = "CommandCallable<DBNation>")
@NationAttributeCallable
public ParametricCallable nationAttribute(NationPlaceholders placeholders, ValueStore store, String input) {
List<ParametricCallable> options = placeholders.getParametricCallables();
Expand All @@ -158,7 +159,7 @@ public ParametricCallable nationAttribute(NationPlaceholders placeholders, Value
return metric;
}

@Binding(value = "A discord slash command reference for the bot")
@Binding(value = "A discord slash command reference for the bot", webType = "CommandCallable")
public ICommand slashCommand(String input) {
List<String> split = StringMan.split(input, ' ');
CommandCallable command = Locutus.imp().getCommandManager().getV2().getCallable(split);
Expand Down Expand Up @@ -505,7 +506,8 @@ public static DBCity cityUrl(String input) {
}

@Binding(examples = ("#grant #city=1"), value = "A DepositType optionally with a value and a city tag\n" +
"See: <https://github.com/xdnw/locutus/wiki/deposits#transfer-notes>")
"See: <https://github.com/xdnw/locutus/wiki/deposits#transfer-notes>",
webType = "DepositType")
public static DepositType.DepositTypeInfo DepositTypeInfo(String input) {
DepositType type = null;
long value = 0;
Expand Down Expand Up @@ -832,12 +834,14 @@ public static NationList nationList(ParameterData data, @Default @Me Guild guild
return new SimpleNationList(nations(data, guild, input, author, me)).setFilter(input);
}

@Binding(examples = "#position>1,#cities<=5", value = "A comma separated list of filters (can include nations and alliances)")
@Binding(examples = "#position>1,#cities<=5", value = "A comma separated list of filters (can include nations and alliances)",
webType = "Predicate<DBNation>")
public NationFilter nationFilter(@Default @Me User author, @Default @Me DBNation nation, @Default @Me Guild guild, String input) {
return new NationFilterString(input, guild, author, nation);
}

@Binding(examples = "score,soldiers", value = "A comma separated list of numeric nation attributes")
@Binding(examples = "score,soldiers", value = "A comma separated list of numeric nation attributes",
webType = "Set<TypedFunction<DBNation, Double>>")
public Set<NationAttributeDouble> nationMetricDoubles(ValueStore store, String input) {
Set<NationAttributeDouble> metrics = new LinkedHashSet<>();
for (String arg : StringMan.split(input, ',')) {
Expand Down Expand Up @@ -1411,8 +1415,8 @@ public static Continent continent(String input) {
}

@Binding(value = "Math comparison operation")
public Operation op(String input) {
return emum(Operation.class, input);
public MathOperation op(String input) {
return emum(MathOperation.class, input);
}

@Binding(value = "Spy safety level")
Expand Down Expand Up @@ -1506,7 +1510,8 @@ public NationMeta.BeigeAlertRequiredStatus BeigeAlertRequiredStatus(String input

@Binding(value = "A completed nation attribute that accepts no arguments and returns a number\n" +
"To get the attribute for an attribute with arguments, you must provide a value in brackets\n" +
"See: <https://github.com/xdnw/locutus/wiki/nation_placeholders>", examples = {"score", "ships", "land", "getCitiesSince(5d)"})
"See: <https://github.com/xdnw/locutus/wiki/nation_placeholders>", examples = {"score", "ships", "land", "getCitiesSince(5d)"},
webType = "TypedFunction<DBNation,Double>")
public NationAttributeDouble nationMetricDouble(ValueStore store, String input) {
NationPlaceholders placeholders = Locutus.imp().getCommandManager().getV2().getNationPlaceholders();
NationAttributeDouble metric = placeholders.getMetricDouble(store, input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import link.locutus.discord.commands.manager.v2.binding.annotation.NoFormat;
import link.locutus.discord.commands.manager.v2.binding.annotation.Switch;
import link.locutus.discord.commands.manager.v2.binding.annotation.TextArea;
import link.locutus.discord.commands.manager.v2.binding.bindings.Operation;
import link.locutus.discord.commands.manager.v2.binding.bindings.MathOperation;
import link.locutus.discord.commands.manager.v2.command.CommandBehavior;
import link.locutus.discord.commands.manager.v2.command.CommandRef;
import link.locutus.discord.commands.manager.v2.command.ICommand;
Expand Down Expand Up @@ -52,7 +52,6 @@
import net.dv8tion.jda.api.interactions.components.ItemComponent;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
import org.json.JSONObject;
import retrofit2.http.HEAD;

import java.io.IOException;
import java.security.GeneralSecurityException;
Expand Down Expand Up @@ -658,11 +657,11 @@ public void warGuerilla(@Me User user, @Me GuildDB db, @Me IMessageIO io, @Defau
"To find contestable range, see: strengthTierGraph")
@RolePermission(Roles.ADMIN)
public void warContestedRange(@Me User user, @Me GuildDB db, @Me IMessageIO io,
@Arg("If the cutoff is greater or less than the score") Operation greaterOrLess,
@Arg("If the cutoff is greater or less than the score") MathOperation greaterOrLess,
@Arg("The score at which the conflict is not contestable")
double score, @Default MessageChannel outputChannel, @Default CommandBehavior behavior, @Switch("d") boolean resultsInDm) {
if (behavior == null) behavior = CommandBehavior.UNPRESS;
if (greaterOrLess == Operation.EQUAL || greaterOrLess == Operation.NOT_EQUAL) {
if (greaterOrLess == MathOperation.EQUAL || greaterOrLess == MathOperation.NOT_EQUAL) {
if (db.getCoalition(Coalition.ENEMIES).isEmpty()) {
throw new IllegalArgumentException("No " + Coalition.ENEMIES.name() + " coalition found. See: " + CM.coalition.create.cmd.toSlashMention());
}
Expand Down Expand Up @@ -694,8 +693,8 @@ public void warContestedRange(@Me User user, @Me GuildDB db, @Me IMessageIO io,
body += "\n\n> Results in DM";
}

Operation opposite = greaterOrLess.opposite();
boolean greater = greaterOrLess == Operation.GREATER || greaterOrLess == Operation.GREATER_EQUAL;
MathOperation opposite = greaterOrLess.opposite();
boolean greater = greaterOrLess == MathOperation.GREATER || greaterOrLess == MathOperation.GREATER_EQUAL;
double minScore = greater ? score : 0;
double maxScore = greater ? Integer.MAX_VALUE : score;
String rangeStr = String.format("%.2f", minScore) + "," + String.format("%.2f", maxScore);
Expand All @@ -704,7 +703,7 @@ public void warContestedRange(@Me User user, @Me GuildDB db, @Me IMessageIO io,
CM.war.find.enemy easy = CM.war.find.enemy.cmd.targets(
"~enemies,#off>0").onlyEasy("true").resultsInDm(dmStr);
int scoreMax;
if (greaterOrLess == Operation.GREATER || greaterOrLess == Operation.GREATER_EQUAL) {
if (greaterOrLess == MathOperation.GREATER || greaterOrLess == MathOperation.GREATER_EQUAL) {
scoreMax = (int) Math.ceil(score / 0.75);
} else {
scoreMax = (int) Math.ceil(score * 0.75);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ public String nation_placeholder(@Me IMessageIO io, NationPlaceholders placehold
if (body.length() > 4096) {
return "#" + title + "\n" + body;
}

IMessageBuilder embed = io.create().embed(title, body);

PWGPTHandler gpt = Locutus.imp().getCommandManager().getV2().getPwgptHandler();
if (gpt != null) {
List<ParametricCallable> closest = gpt.getClosestNationAttributes(store, command, 6);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/link/locutus/discord/db/entities/DBBounty.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public DBBounty(ResultSet rs) throws SQLException {
);
}

@Command(desc = "String of the bounty type and amount")
public String toLineString() {
return "#" + id + ": " + type + " $" + MathMan.format(amount);
}

@Command(desc = "Get the date this bounty was posted (epoch milliseconds)")
public long getDate() {
return date;
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/link/locutus/discord/db/entities/Treaty.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import link.locutus.discord.apiv1.enums.TreatyType;
import link.locutus.discord.commands.manager.v2.binding.annotation.Command;
import link.locutus.discord.util.PW;
import link.locutus.discord.util.TimeUtil;
import org.jooq.meta.derby.sys.Sys;

Expand Down Expand Up @@ -123,6 +124,11 @@ public String toString() {
'}';
}

@Command
public String toLineString() {
return type + ":" + PW.getName(from, true) + "/" + PW.getName(to, true);
}

@Command(desc = "Timestamp the treaty ends")
public long getEndTime() {
return Math.max(System.currentTimeMillis(), TimeUtil.getTimeFromTurn(getTurnEnds() - 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import link.locutus.discord.commands.manager.v2.impl.discord.binding.annotation.NationDepositLimit;
import link.locutus.discord.commands.manager.v2.binding.annotation.RegisteredRole;
import link.locutus.discord.commands.manager.v2.impl.pw.binding.NationAttributeDouble;
import link.locutus.discord.commands.manager.v2.binding.bindings.Operation;
import link.locutus.discord.commands.manager.v2.binding.bindings.MathOperation;
import link.locutus.discord.commands.manager.v2.command.ArgumentStack;
import link.locutus.discord.commands.manager.v2.command.ParameterData;
import link.locutus.discord.commands.manager.v2.command.ParametricCallable;
Expand Down Expand Up @@ -1127,9 +1127,9 @@ public String units(ParameterData param, boolean multiple) {
}

@HtmlInput
@Binding(types= Operation.class)
@Binding(types= MathOperation.class)
public String operation(ParameterData param) {
return multipleSelect(param, Arrays.asList(Operation.values()), op -> new AbstractMap.SimpleEntry<>(op.name(), op.name()));
return multipleSelect(param, Arrays.asList(MathOperation.values()), op -> new AbstractMap.SimpleEntry<>(op.name(), op.name()));
}

@HtmlInput
Expand Down
Loading

0 comments on commit f0c9240

Please sign in to comment.