use of com.demod.dcba.SlashCommandEvent in project Factorio-FBSR by demodude4u.
the class BlueprintBotDiscordService method handleBlueprintItemsCommand.
private void handleBlueprintItemsCommand(SlashCommandEvent event) throws IOException {
DataTable table;
try {
table = FactorioData.getTable();
} catch (JSONException | IOException e1) {
throw new InternalError(e1);
}
String content = event.getCommandString();
Optional<Attachment> attachment = event.optParamAttachment("file");
if (attachment.isPresent()) {
content += " " + attachment.get().getUrl();
}
List<BlueprintStringData> blueprintStringDatas = BlueprintFinder.search(content, event.getReporting());
Map<String, Double> totalItems = new LinkedHashMap<>();
for (BlueprintStringData blueprintStringData : blueprintStringDatas) {
for (Blueprint blueprint : blueprintStringData.getBlueprints()) {
Map<String, Double> items = FBSR.generateTotalItems(table, blueprint);
items.forEach((k, v) -> {
totalItems.compute(k, ($, old) -> old == null ? v : old + v);
});
}
}
if (!totalItems.isEmpty()) {
String responseContent = totalItems.entrySet().stream().sorted((e1, e2) -> e1.getKey().compareTo(e2.getKey())).map(e -> table.getWikiItemName(e.getKey()) + ": " + RenderUtils.fmtDouble2(e.getValue())).collect(Collectors.joining("\n"));
String response = "```ldif\n" + responseContent + "```";
if (response.length() < 2000) {
event.reply(response);
} else {
event.replyFile(responseContent.getBytes(), "items.txt");
}
} else if (blueprintStringDatas.stream().anyMatch(d -> !d.getBlueprints().isEmpty())) {
event.replyIfNoException("I couldn't find any items!");
} else {
event.replyIfNoException("No blueprint found!");
}
}
use of com.demod.dcba.SlashCommandEvent in project Factorio-FBSR by demodude4u.
the class BlueprintBotDiscordService method handleBlueprintBookAssembleCommand.
private void handleBlueprintBookAssembleCommand(SlashCommandEvent event) {
String content = event.getCommandString();
Optional<Attachment> attachment = event.optParamAttachment("file");
if (attachment.isPresent()) {
content += " " + attachment.get().getUrl();
}
List<BlueprintStringData> blueprintStrings = BlueprintFinder.search(content, event.getReporting());
if (!blueprintStrings.isEmpty()) {
List<Blueprint> blueprints = blueprintStrings.stream().flatMap(bs -> bs.getBlueprints().stream()).collect(Collectors.toList());
JSONObject json = new JSONObject();
Utils.terribleHackToHaveOrderedJSONObject(json);
JSONObject bookJson = new JSONObject();
Utils.terribleHackToHaveOrderedJSONObject(bookJson);
json.put("blueprint_book", bookJson);
JSONArray blueprintsJson = new JSONArray();
bookJson.put("blueprints", blueprintsJson);
bookJson.put("item", "blueprint-book");
bookJson.put("active_index", 0);
MapVersion latestVersion = new MapVersion();
int index = 0;
for (Blueprint blueprint : blueprints) {
blueprint.json().put("index", index);
latestVersion = MapVersion.max(latestVersion, blueprint.getVersion());
blueprintsJson.put(blueprint.json());
index++;
}
String bookLabel = blueprintStrings.stream().filter(BlueprintStringData::isBook).map(BlueprintStringData::getLabel).filter(Optional::isPresent).map(Optional::get).map(String::trim).distinct().collect(Collectors.joining(" & "));
if (!bookLabel.isEmpty()) {
bookJson.put("label", bookLabel);
}
if (!latestVersion.isEmpty()) {
bookJson.put("version", latestVersion.getSerialized());
}
try {
event.replyFile(BlueprintStringData.encode(json).getBytes(), "blueprintBook.txt");
} catch (Exception e) {
event.getReporting().addException(e);
}
} else {
event.replyIfNoException("No blueprint found!");
}
}
Aggregations