Skip to content

Commit

Permalink
Switch away from remark (outdated)
Browse files Browse the repository at this point in the history
  • Loading branch information
xdnw committed Aug 18, 2024
1 parent c7b69b7 commit ecd79fa
Show file tree
Hide file tree
Showing 17 changed files with 584 additions and 149 deletions.
2 changes: 0 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,6 @@ dependencies {

// https://mvnrepository.com/artifact/gnu.trove/trove
implementation group: 'gnu.trove', name: 'trove', version: '3.0.3'
// https://mvnrepository.com/artifact/com.overzealous/remark
implementation group: 'com.overzealous', name: 'remark', version: '1.0.0'
// https://mvnrepository.com/artifact/it.unimi.dsi/fastutil
implementation group: 'it.unimi.dsi', name: 'fastutil', version: '8.2.1'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,10 @@ public static Predicate<Project> optimize(Predicate<Project> projects) {
return p -> hasProject[p.ordinal()];
}

public static Project[] values() {
return values;
}


// Recycling Initiative

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public Placeholders<T> init() {
return this;
}

public abstract Set<String> getSheetColumns();

public abstract Set<SelectorInfo> getSelectorInfo();

public Class<T> getType() {
return instanceType;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package link.locutus.discord.commands.manager.v2.binding.bindings;

import java.util.Objects;

public record SelectorInfo(String format, String example, String desc) {
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
SelectorInfo that = (SelectorInfo) obj;
return format.equals(that.format);
}

@Override
public int hashCode() {
return format.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.function.Function;
import java.util.function.Predicate;

public class SimplePlaceholders<T> extends Placeholders<T> {
public abstract class SimplePlaceholders<T> extends Placeholders<T> {

private final String help;
private final TriFunction<Placeholders<T>, ValueStore, String, Set<T>> parse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@

import link.locutus.discord.commands.manager.v2.binding.ValueStore;
import link.locutus.discord.commands.manager.v2.binding.validator.ValidatorStore;
import link.locutus.discord.commands.manager.v2.impl.pw.filter.PlaceholdersMap;
import link.locutus.discord.commands.manager.v2.perm.PermissionHandler;
import link.locutus.discord.util.StringMan;
import link.locutus.discord.util.scheduler.TriFunction;

import java.util.Arrays;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class StaticPlaceholders<T> extends SimplePlaceholders<T> {
public StaticPlaceholders(Class<T> type, ValueStore store, ValidatorStore validators, PermissionHandler permisser, String help, TriFunction<Placeholders<T>, ValueStore, String, Set<T>> parse) {
public abstract class StaticPlaceholders<T> extends SimplePlaceholders<T> {
private final Supplier<T[]> enumValues;

public StaticPlaceholders(Class<T> type, Supplier<T[]> values, ValueStore store, ValidatorStore validators, PermissionHandler permisser, String help, TriFunction<Placeholders<T>, ValueStore, String, Set<T>> parse) {
super(type, store, validators, permisser, help, parse, (inst, valueStore, s) -> {
Set<T> parsed = parse.apply(inst, valueStore, s);
return parsed::contains;
Expand All @@ -21,5 +28,15 @@ public StaticPlaceholders(Class<T> type, ValueStore store, ValidatorStore valida
}
return t.toString();
});
this.enumValues = values;
}

@Override
public final Set<SelectorInfo> getSelectorInfo() {
T[] values = enumValues.get();
String joined = Arrays.stream(values).map(StringMan::getString).collect(Collectors.joining("`, `", "`", "`"));
return Set.of(
new SelectorInfo(PlaceholdersMap.getClassName(getType().getSimpleName()).toUpperCase(), joined, "One of the entity values")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import ai.djl.MalformedModelException;
import ai.djl.repository.zoo.ModelNotFoundException;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import link.locutus.discord.Locutus;
import link.locutus.discord.commands.manager.v2.binding.*;
import link.locutus.discord.commands.manager.v2.binding.annotation.*;
import link.locutus.discord.commands.manager.v2.binding.bindings.Placeholders;
import link.locutus.discord.commands.manager.v2.binding.bindings.PrimitiveBindings;
import link.locutus.discord.commands.manager.v2.binding.bindings.PrimitiveValidators;
import link.locutus.discord.commands.manager.v2.binding.bindings.SelectorInfo;
import link.locutus.discord.commands.manager.v2.binding.validator.ValidatorStore;
import link.locutus.discord.commands.manager.v2.command.*;
import link.locutus.discord.commands.manager.v2.impl.discord.DiscordChannelIO;
Expand All @@ -35,6 +37,7 @@
import link.locutus.discord.gpt.pw.PWGPTHandler;
import link.locutus.discord.util.StringMan;
import link.locutus.discord.util.discord.DiscordUtil;
import link.locutus.discord.web.jooby.JteUtil;
import link.locutus.discord.web.test.TestCommands;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
Expand Down Expand Up @@ -117,16 +120,27 @@ public JsonObject toJson(ValueStore htmlOptionsStore, PermissionHandler permHand
}
}

JsonObject phJson = new JsonObject();
for (Class t : placeholders.getTypes()) {
Placeholders ph = placeholders.get(t);
phJson.add(t.getSimpleName(), ph.getCommands().toJson(permHandler, true));
}
JsonObject phRoot = new JsonObject();
for (Class<?> t : placeholders.getTypes()) {
Placeholders<?> ph = placeholders.get(t);
JsonObject json = new JsonObject();

JsonObject bindings = ph.getCommands().toJson(permHandler, true);
json.add("commands", bindings);

Set<SelectorInfo> selectors = ph.getSelectorInfo();
JsonArray arr = JteUtil.createArrayObj(selectors.stream().map(f -> JteUtil.createArrayObj(f.format(), f.example(), f.desc())));
json.add("selectors", arr);
Set<String> columns = ph.getSheetColumns();
if (!columns.isEmpty()) {
json.add("columns", JteUtil.createArrayCol(columns));
}
phRoot.add(t.getSimpleName(), json);
}
Gson gson = new Gson();
JsonObject result = new JsonObject();
result.add("commands", cmdJson);
result.add("placeholders", phJson);
result.add("placeholders", phRoot);
result.add("keys", gson.toJsonTree(keysData));
result.add("options", gson.toJsonTree(optionsData));
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,8 @@ public String importAllianceNames(ConflictManager manager) throws IOException, P
@RolePermission(Roles.MILCOM)
@CoalitionPermission(Coalition.MANAGE_CONFLICTS)
public String importWikiPage(@Me GuildDB db, ConflictManager manager,
String name,
@Default String url,
@Arg("The name of the wiki page") String name,
@Arg("The url of the wiki page") @Default String url,
@Default("true") boolean useCache,
@Switch("p") boolean skipPushToSite) throws IOException, ParseException {
if (name.contains("http")) return "Please specify the name of the wiki page, not the URL for `name`";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2160,7 +2160,7 @@ private double total(boolean consumption, boolean infra, boolean loot, boolean u
Each bar is segmented into four sections, from bottom to top: (soldiers, tanks, planes, ships)
Each alliance is grouped by sphere and color coded.""")
public static String militaryRanking(@Me GuildDB db, @Me IMessageIO channel,
@Default NationList nations2,
@Default NationList nations,
@Switch("n") Integer top_n_alliances,
@Switch("s") SpreadSheet sheet,
@Switch("t") boolean removeUntaxable,
Expand All @@ -2172,14 +2172,14 @@ public static String militaryRanking(@Me GuildDB db, @Me IMessageIO channel,
}
if (top_n_alliances == null) top_n_alliances = 80;
Map<Integer, List<DBNation>> byAA;
if (nations2 == null) {
if (nations == null) {
byAA = Locutus.imp().getNationDB().getNationsByAlliance(removeUntaxable, removeInactive, !includeApplicants, true, true);
} else {
Set<DBNation> tmp;
if (snapshotDate != null) {
tmp = PW.getNationsSnapshot(nations2.getNations(), nations2.getFilter(), snapshotDate, db.getGuild(), false);
tmp = PW.getNationsSnapshot(nations.getNations(), nations.getFilter(), snapshotDate, db.getGuild(), false);
} else {
tmp = nations2.getNations();
tmp = nations.getNations();
}
tmp.removeIf(f -> f.getVm_turns() > 0);
byAA = Locutus.imp().getNationDB().getNationsByAlliance(tmp, removeUntaxable, removeInactive, !includeApplicants, true);
Expand All @@ -2204,14 +2204,14 @@ public static String militaryRanking(@Me GuildDB db, @Me IMessageIO channel,
}

if (!sphereScore.containsKey(sphereId)) {
List<DBNation> nations = new ArrayList<>();
List<DBNation> tmp = new ArrayList<>();
for (DBAlliance other : sphere) {
List<DBNation> otherNations = byAA.get(other.getAlliance_id());
if (otherNations != null) {
nations.addAll(otherNations);
tmp.addAll(otherNations);
}
}
SimpleNationList nationList = new SimpleNationList(nations);
SimpleNationList nationList = new SimpleNationList(tmp);

sphereScore.put(sphereId, nationList.getScore());
if (sphere.size() > 1) {
Expand Down Expand Up @@ -2259,9 +2259,9 @@ public static String militaryRanking(@Me GuildDB db, @Me IMessageIO channel,
sphereAAs.sort((o1, o2) -> Double.compare(o2.getValue().getScore(), o1.getValue().getScore()));
for (Map.Entry<Integer, NationList> aaEntry : sphereAAs) {
int aaId = aaEntry.getKey();
NationList nations = aaEntry.getValue();
NationList tmp = aaEntry.getValue();

DBNation total = nations.getTotal();
DBNation total = tmp.getTotal();

ArrayList<Object> row = new ArrayList<>();
if (aaId != 0) {
Expand All @@ -2271,7 +2271,7 @@ public static String militaryRanking(@Me GuildDB db, @Me IMessageIO channel,
row.add("");
}
row.add(colorStr);
row.add(nations.getScore());
row.add(tmp.getScore());
row.add(total.getCities());

row.add(total.getSoldiers());
Expand All @@ -2289,13 +2289,13 @@ public static String militaryRanking(@Me GuildDB db, @Me IMessageIO channel,
row.add(airPct);
row.add(navyPct);

double[] mmr = nations.getAverageMMR(false);
double[] mmr = tmp.getAverageMMR(false);
row.add(mmr[0] * 100 / Buildings.BARRACKS.cap(total::hasProject));
row.add(mmr[1] * 100 / Buildings.FACTORY.cap(total::hasProject));
row.add(mmr[2] * 100 / Buildings.HANGAR.cap(total::hasProject));
row.add(mmr[3] * 100 / Buildings.DRYDOCK.cap(total::hasProject));

double[] buy = nations.getMilitaryBuyPct(false);
double[] buy = tmp.getMilitaryBuyPct(false);
row.add(buy[0]);
row.add(buy[1]);
row.add(buy[2]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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.bindings.Placeholders;
import link.locutus.discord.commands.manager.v2.binding.bindings.SelectorInfo;
import link.locutus.discord.commands.manager.v2.binding.bindings.TypedFunction;
import link.locutus.discord.commands.manager.v2.binding.validator.ValidatorStore;
import link.locutus.discord.commands.manager.v2.command.CommandCallable;
Expand All @@ -35,13 +36,7 @@
import java.io.IOException;
import java.lang.reflect.Type;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -71,6 +66,24 @@ public List<AllianceInstanceAttribute> getMetrics(ValueStore store) {
return result;
}

@Override
public Set<SelectorInfo> getSelectorInfo() {
return new LinkedHashSet<>(List.of(
new SelectorInfo("aa:ALLIANCE_NAME", "aa:Rose", "A qualified alliance name"),
new SelectorInfo("alliance:ALLIANCE_NAME", "alliance:Eclipse", "A qualified alliance name"),
new SelectorInfo("alliance/id=ALLIANCE_ID", "alliance/id=790", "An alliance url"),
new SelectorInfo("ALLIANCE_ID", "790", "An alliance id"),
new SelectorInfo("coalition:COALITION", "coalition:allies", "A qualified coalition name"),
new SelectorInfo("~COALITION", "~enemies", "A coalition name"),
new SelectorInfo("*", null, "All alliances")
));
}

@Override
public Set<String> getSheetColumns() {
return Set.of("alliance");
}

public AllianceInstanceAttributeDouble getMetricDouble(ValueStore store, String id) {
return getMetricDouble(store, id, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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.bindings.Placeholders;
import link.locutus.discord.commands.manager.v2.binding.bindings.SelectorInfo;
import link.locutus.discord.commands.manager.v2.binding.bindings.TypedFunction;
import link.locutus.discord.commands.manager.v2.binding.validator.ValidatorStore;
import link.locutus.discord.commands.manager.v2.command.CommandCallable;
Expand Down Expand Up @@ -63,6 +64,36 @@ public NationPlaceholders(ValueStore store, ValidatorStore validators, Permissio
super(DBNation.class, store, validators, permisser);
}

@Override
public Set<SelectorInfo> getSelectorInfo() {
return new LinkedHashSet<>(List.of(
new SelectorInfo("nation:NATION_NAME", "nation:Borg", "A qualified nation name"),
new SelectorInfo("leader:NATION_NAME", "leader:Danzek", "A qualified leader name"),
new SelectorInfo("aa:ALLIANCE_NAME", "aa:Rose", "A qualified alliance name"),
new SelectorInfo("alliance:ALLIANCE_NAME", "alliance:Eclipse", "A qualified alliance name"),
new SelectorInfo("nation/id=NATION_ID", "nation/id=6", "A nation url"),
new SelectorInfo("alliance/id=ALLIANCE_ID", "alliance/id=790", "An alliance url"),
new SelectorInfo("coalition:COALITION", "coalition:allies", "A qualified coalition name"),
new SelectorInfo("~COALITION", "~enemies", "A coalition name"),
new SelectorInfo("NATION_NAME", "Borg", "An unqualified nation name"),
new SelectorInfo("LEADER_NAME", "Danzek", "An unqualified leader name"),
new SelectorInfo("NATION_ID", "189573", "A nation id"),
new SelectorInfo("ALLIANCE_ID", "790", "An alliance id"),
new SelectorInfo("@ROLE_MENTION", "@Member", "A discord role mention or name"),
new SelectorInfo("ROLE_ID", "123456789012345678", "A discord role id"),
new SelectorInfo("@USER_MENTION", "@xdnw", "A discord user mention or name"),
new SelectorInfo("USER_ID", "123456789012345678", "A discord user id"),
new SelectorInfo("https://politicsandwar.com/index.php?id=15&tax_id=TAX_ID", "https://politicsandwar.com/index.php?id=15&tax_id=1234", "A full tax url"),
new SelectorInfo("TAX_ID", "tax_id=1234", "A tax bracket id or url"),
new SelectorInfo("*", null, "All nations")
));
}

@Override
public Set<String> getSheetColumns() {
return new LinkedHashSet<>(List.of("nation", "leader"));
}

@Override
public String getName(DBNation o) {
return o.getName();
Expand Down
Loading

0 comments on commit ecd79fa

Please sign in to comment.