grundlegente Umstellung + Earthdawn Implemntation
This commit is contained in:
@ -0,0 +1,173 @@
|
||||
package de.ocame.wuerfelbot.Earthdawn;
|
||||
|
||||
|
||||
import de.ocame.wuerfelbot.commands.ICommand;
|
||||
import de.ocame.wuerfelbot.core.LoggerUtil;
|
||||
import de.ocame.wuerfelbot.output.DiceOutputFormatter;
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.interactions.commands.OptionType;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.Commands;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class EarthdawnCommand implements ICommand {
|
||||
|
||||
private final EarthdawnLogic earthdawnLogic;
|
||||
private final DiceOutputFormatter outputFormatter;
|
||||
|
||||
public EarthdawnCommand() {
|
||||
this.earthdawnLogic = new EarthdawnLogic();
|
||||
this.outputFormatter = new DiceOutputFormatter();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "ed";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Führt eine Earthdawn-Probe durch (Stufe + optional Karma oder Weihewürfel).";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void executeSlash(SlashCommandInteractionEvent event) {
|
||||
Integer stufe = event.getOption("stufe").getAsInt(); // "stufe" ist der Name der Option
|
||||
Integer karma = event.getOption("karma") != null ? event.getOption("karma").getAsInt() : 0; // "karma" ist optional
|
||||
Integer meisterkarma = event.getOption("meisterkarma") != null ? event.getOption("meisterkarma").getAsInt() : 0;
|
||||
Integer weiheJ = event.getOption("weihe-j") != null ? event.getOption("weihe-j").getAsInt() : 0;
|
||||
Integer weiheA = event.getOption("weihe-a") != null ? event.getOption("weihe-a").getAsInt() : 0;
|
||||
Integer weiheV = event.getOption("weihe-v") != null ? event.getOption("weihe-v").getAsInt() : 0;
|
||||
|
||||
|
||||
if (stufe < 1) {
|
||||
event.reply(outputFormatter.formatError("Die Stufe muss mindestens 1 sein.")).setEphemeral(true).queue();
|
||||
return;
|
||||
}
|
||||
if (karma < 0 || meisterkarma < 0 || weiheJ < 0 || weiheA < 0 || weiheV < 0) {
|
||||
event.reply(outputFormatter.formatError("Zusatzwürfel dürfen nicht negativ sein.")).setEphemeral(true).queue();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
EarthdawnLogic.EarthdawnResult result = earthdawnLogic.performEarthdawnRoll(stufe, karma, meisterkarma, weiheJ, weiheA, weiheV);
|
||||
EmbedBuilder response = outputFormatter.DCDefaultOutput(getEffectiveName(event) + result.getTitle(), result.getDescription(), result.isGlitch());
|
||||
event.deferReply().queue();
|
||||
//event.reply(response).queue();
|
||||
//event.getChannel().sendMessageEmbeds(response.build()).queue();
|
||||
event.getHook().editOriginalEmbeds(response.build()).queue();
|
||||
// event.replyEmbeds(response.build()).queue();
|
||||
} catch (Exception e) {
|
||||
LoggerUtil.log("error", "EDCommand", "Fehler bei Earthdawn-Probe: " + e.getMessage());
|
||||
event.reply(outputFormatter.formatError("Ein Fehler ist bei der Earthdawn-Probe aufgetreten.")).setEphemeral(true).queue();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void executeText(MessageReceivedEvent event, String[] args) {
|
||||
if (args.length < 1) {
|
||||
event.getChannel().sendMessage(outputFormatter.formatError("Verwendung: `!ed <stufe> [karma]`")).queue();
|
||||
return;
|
||||
}
|
||||
|
||||
Integer stufe = 0;
|
||||
Integer karma = 0;
|
||||
Integer meisterkarma = 0;
|
||||
Integer weiheJ = 0;
|
||||
Integer weiheA = 0;
|
||||
Integer weiheV = 0;
|
||||
|
||||
boolean stufeParsed = false;
|
||||
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
String currentElement = args[i].trim().toLowerCase(); // Leerzeichen in Elementen entfernen
|
||||
|
||||
if (currentElement.isEmpty()) {
|
||||
continue; // Leere Elemente überspringen
|
||||
}
|
||||
|
||||
// 1. Stufe parsen (nur einmal ganz am Anfang)
|
||||
if (!stufeParsed && currentElement.matches("^\\d+$")) {
|
||||
stufe = Integer.parseInt(currentElement);
|
||||
stufeParsed = true;
|
||||
continue; // Stufe wurde geparst, gehe zum nächsten Element
|
||||
} else if (!stufeParsed && currentElement.matches("^\\d+.*")) {
|
||||
Pattern pattern = Pattern.compile("^(\\d+)");
|
||||
Matcher matcher = pattern.matcher(currentElement);
|
||||
matcher.find();
|
||||
stufe = Integer.parseInt(matcher.group(1));
|
||||
stufeParsed = true;
|
||||
|
||||
currentElement = currentElement.replaceAll("^\\d+", "");
|
||||
|
||||
}
|
||||
karma += parseCharacterStat(currentElement, 'k');
|
||||
meisterkarma += parseCharacterStat(currentElement, 'm');
|
||||
weiheJ += parseCharacterStat(currentElement, 'j');
|
||||
weiheA += parseCharacterStat(currentElement, 'a');
|
||||
weiheV += parseCharacterStat(currentElement, 'v');
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (stufe < 1) {
|
||||
event.getChannel().sendMessage(outputFormatter.formatError("Die Stufe muss mindestens 1 sein.")).queue();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
EarthdawnLogic.EarthdawnResult result = earthdawnLogic.performEarthdawnRoll(stufe, karma, meisterkarma, weiheJ, weiheA, weiheV);
|
||||
EmbedBuilder response = outputFormatter.DCDefaultOutput(getEffectiveName(event) + result.getTitle(), result.getDescription(), result.isGlitch());
|
||||
event.getChannel().sendMessageEmbeds(response.build()).queue();
|
||||
} catch (Exception e) {
|
||||
LoggerUtil.log("error", "EDCommand", "Fehler bei Earthdawn-Probe: " + e.getMessage());
|
||||
event.getChannel().sendMessage(outputFormatter.formatError("Ein Fehler ist bei der Earthdawn-Probe aufgetreten.")).queue();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandData getSlashCommandData() {
|
||||
return Commands.slash(getName(), getDescription())
|
||||
.addOption(OptionType.INTEGER, "stufe", "Stufe der Probe", true)
|
||||
.addOption(OptionType.INTEGER, "karma", "Optionale Anzahl der Karma (W6-Würfel)", false)
|
||||
.addOption(OptionType.INTEGER, "meisterkarma", "Optionale Anzahl der Meister-Karma (W8-Würfel)", false)
|
||||
.addOption(OptionType.INTEGER, "weihe-j", "Optionale Anzahl der Jünger-Weihewürfe (W4-Würfel)", false)
|
||||
.addOption(OptionType.INTEGER, "weihe-a", "Optionale Anzahl der Anhänger-Weihewürfel (W6-Würfel)", false)
|
||||
.addOption(OptionType.INTEGER, "weihe-v", "Optionale Anzahl der Vorbild-Weihewürfel (W8-Würfel)", false);
|
||||
}
|
||||
|
||||
private String getEffectiveName(MessageReceivedEvent event) {
|
||||
if (event.getMember() == null) {
|
||||
return "";
|
||||
}
|
||||
return event.getMember().getEffectiveName() + " ";
|
||||
}
|
||||
|
||||
private String getEffectiveName(SlashCommandInteractionEvent event) {
|
||||
if (event.getMember() == null) {
|
||||
return "";
|
||||
}
|
||||
return event.getMember().getEffectiveName() + " ";
|
||||
}
|
||||
|
||||
private static int parseCharacterStat(String currentElement, char targetChar) {
|
||||
int result = 0;
|
||||
Pattern pattern = Pattern.compile("((" + targetChar + "+) *(\\d*))");
|
||||
Matcher matcher = pattern.matcher(currentElement);
|
||||
if (matcher.find()) {
|
||||
if (matcher.group(2).length() == 1 && !matcher.group(3).isEmpty()) { // nur die K
|
||||
result += Integer.parseInt(matcher.group(3));
|
||||
}
|
||||
else {
|
||||
result += matcher.group(2).length();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user