Skip to content

Commit

Permalink
TTL conflict data
Browse files Browse the repository at this point in the history
  • Loading branch information
xdnw committed May 19, 2024
1 parent d4eec19 commit 6adc631
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package link.locutus.discord.commands.manager.v2.command;

import com.google.gson.JsonObject;
import link.locutus.discord.commands.manager.v2.binding.ValueStore;
import link.locutus.discord.commands.manager.v2.binding.WebStore;
import link.locutus.discord.commands.manager.v2.perm.PermissionHandler;
Expand Down Expand Up @@ -35,6 +36,8 @@ public interface CommandCallable {

List<String> aliases();

JsonObject toJson();

default void validatePermissions(ValueStore store, PermissionHandler permisser) throws IllegalArgumentException {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,11 @@ public CommandGroup(CommandCallable parent, String[] aliases, ValueStore store,
this.aliases = Arrays.asList(aliases);
}

public JsonObject generateSiteMap() {
public JsonObject toJson() {
JsonObject root = new JsonObject();
getParametricCallables(f -> {
String fullPath = f.getFullPath();
String[] split = fullPath.split(" ");
JsonObject current = root;
for (int i = 0; i < split.length - 1; i++) {
String s = split[i];
if (!current.has(s)) {
current.add(s, new JsonObject());
}
current = current.getAsJsonObject(s);
}
current.addProperty(split[split.length - 1], "");
return false;
});
for (Map.Entry<String, CommandCallable> entry : subcommands.entrySet()) {
root.add(entry.getKey(), entry.getValue().toJson());
}
return root;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package link.locutus.discord.commands.manager.v2.command;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import link.locutus.discord.commands.manager.v2.binding.Key;
import link.locutus.discord.commands.manager.v2.binding.Parser;
import link.locutus.discord.commands.manager.v2.binding.annotation.Binding;
Expand All @@ -19,6 +21,11 @@ public class ParameterData {
private String desc;
private int group = -1;

public JsonElement toJson() {
JsonObject arg = new JsonObject();
return null;
}

public Type getType() {
return type;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package link.locutus.discord.commands.manager.v2.command;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import gg.jte.generated.precompiled.command.JteparametriccallableGenerated;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import link.locutus.discord.Locutus;
Expand Down Expand Up @@ -893,6 +896,71 @@ public Object apply(ParameterData param, Object o) {
return paramVals;
}

@Override
public JsonObject toJson() {
JsonObject command = new JsonObject();
//simpleHelp
//simpleDesc
//groups
//groupDescs
//valueFlags
//provideFlags
//annotations
//userParameters
command.addProperty("help", simpleHelp());
command.addProperty("desc", simpleDesc());
if (groups != null && groups.length != 0) {
JsonArray groupsList = new JsonArray();
for (String g : groups) groupsList.add(g);
command.add("groups", groupsList);
}
if (groupDescs != null && groupDescs.length != 0) {
JsonArray groupDescsList = new JsonArray();
for (String g : groupDescs) groupDescsList.add(g);
command.add("group_descs", groupDescsList);
}
// Set<String> flags = new LinkedHashSet<>();
// if (valueFlags != null && !valueFlags.isEmpty()) flags.addAll(valueFlags);
// if (provideFlags != null && !provideFlags.isEmpty()) flags.addAll(provideFlags);
// if (!flags.isEmpty()) {
// JsonArray valueFlagsList = new JsonArray();
// for (String g : flags) valueFlagsList.add(g);
// command.add("flags", valueFlagsList);
// }
JsonObject annotationsObj = new JsonObject();
for (Annotation annotation : annotations) {
if (annotation instanceof Command) continue;
JsonObject annJson = new JsonObject();
for (Method method : annotation.annotationType().getDeclaredMethods()) {
try {
Object value = method.invoke(annotation);
if (value != null) {
annJson.addProperty(method.getName(), value.toString());
}
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
String key = annotation.annotationType().getSimpleName();
if (annJson.isEmpty()) {
command.addProperty(key, true);
} else {
annotationsObj.add(key, annJson);
}
}
if (!annotationsObj.entrySet().isEmpty()) {
command.add("annotations", annotationsObj);
}
JsonObject arguments = new JsonObject();
for (ParameterData param : userParameters) {
arguments.add(param.getName(), param.toJson());
}
if (!arguments.isEmpty()) {
command.add("arguments", arguments);
}
return command;
}

@Override
public Set<ParametricCallable> getParametricCallables(Predicate<ParametricCallable> returnIf) {
return (returnIf.test(this)) ? Collections.singleton(this) : Collections.emptySet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,10 @@ public String info(ConflictManager manager, Conflict conflict, boolean showParti
}
response.append("\n");

CoalitionSide col1 = conflict.getSide(true);
CoalitionSide col2 = conflict.getSide(false);
List<CoalitionSide> sides = Arrays.asList(col1, col2);
List<CoalitionSide> sides = Arrays.asList(conflict.getSide(true), conflict.getSide(false));
if (showParticipants) {
for (CoalitionSide side : sides) {
response.append("\n**" + col1.getName() + "**\n");
response.append("\n**" + side.getName() + "** (1)\n");
for (int aaId : side.getAllianceIdsSorted()) {
long start = conflict.getStartTurn(aaId);
long end = conflict.getEndTurn(aaId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ public List<String> push(ConflictManager manager, String webIdOrNull, boolean in
if (getId() == -1) throw new IllegalArgumentException("Conflict has no id");
webIdOrNull = Integer.toString(getId());
}
long ttl = isActive() ? TimeUnit.MINUTES.toSeconds(30) : TimeUnit.MINUTES.toSeconds(1);
long ttl = isActive() ? TimeUnit.MINUTES.toSeconds(1) : TimeUnit.MINUTES.toSeconds(5);
String key = "conflicts/" + webIdOrNull + ".gzip";
byte[] value = getPsonGzip(manager);
aws.putObject(key, value, ttl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
public class ConflictManager {
private final WarDB db;
private final AwsManager aws;
private boolean conflictsLoaded = false;

private final Map<Integer, Conflict> conflictById = new Int2ObjectOpenHashMap<>();
private Conflict[] conflictArr;
Expand All @@ -85,11 +86,12 @@ private AwsManager setupAws() {
}
Locutus.imp().getCommandManager().getExecutor().scheduleWithFixedDelay(() -> {
try {
if (!conflictsLoaded) return;
pushDirtyConflicts();
} catch (Exception e) {
e.printStackTrace();
}
}, 17, 7, TimeUnit.MINUTES);
}, 8, 1, TimeUnit.MINUTES);
return null;
}

Expand Down Expand Up @@ -322,13 +324,13 @@ public void updateAttack(DBWar war, AbstractCursor attack, Predicate<Integer> al

@Subscribe
public void onTurnChange(TurnChangeEvent event) {
if (!conflictsLoaded) return;
long turn = event.getCurrent();
boolean updated = false;
List<Conflict> conflicts = getActiveConflicts();
if (!conflicts.isEmpty()) {
for (Conflict conflict : conflicts) {
conflict.getSide(true).updateTurnChange(this, turn, true);
conflict.getSide(false).updateTurnChange(this, turn, false);
conflict.getSide(false).updateTurnChange(this, turn, true);
}
for (Conflict conflict : conflicts) {
conflict.push(this, null, true, false);
Expand Down Expand Up @@ -641,6 +643,7 @@ public AttackTypeSubCategory apply(IAttack a) {
});
System.out.println("Loaded graph data in " + ((-start) + (start = System.currentTimeMillis()) + "ms"));
}
conflictsLoaded = true;
} catch (Throwable e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public void putObject(String key, byte[] data, long maxAge) {
ObjectMetadata metadata = new ObjectMetadata();

metadata.setContentLength(data.length);
System.out.println("Max age " + maxAge);
metadata.setCacheControl("max-age=" + maxAge);

ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/link/locutus/discord/web/jooby/WebRoot.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,18 +254,6 @@ public File getFileRoot() {
return fileRoot;
}

private String siteMapB64;

public String generateSiteMapB64() {
if (siteMapB64 != null) return siteMapB64;
JsonObject siteMap = pageHandler.getCommands().generateSiteMap();
siteMap.add("command", Locutus.cmd().getV2().getCommands().generateSiteMap());
WikiGenHandler gen = new WikiGenHandler("", Locutus.cmd().getV2());
JsonObject wiki = gen.generateSiteMap();
siteMap.add("wiki", wiki);
return siteMapB64 = Base64.getEncoder().encodeToString(siteMap.toString().getBytes());
}

public PageHandler getPageHandler() {
return pageHandler;
}
Expand Down

0 comments on commit 6adc631

Please sign in to comment.