226 lines
8.4 KiB
Java
226 lines
8.4 KiB
Java
package de.ocame.wuerfelbot.Simpleroll;
|
|
|
|
import java.util.Random;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
public class messageInterpreter {
|
|
|
|
private Integer intResult = 0;
|
|
|
|
private final language activLang;
|
|
|
|
private final StringBuilder bodyOutput;
|
|
|
|
private final StringBuilder resolveOutput;
|
|
|
|
private final StringBuilder headerOutput;
|
|
|
|
private boolean isComplex;
|
|
|
|
public Integer getIntResult() {
|
|
return intResult;
|
|
}
|
|
|
|
|
|
/**
|
|
*
|
|
* @param activLang Aktive Sprache
|
|
*/
|
|
public messageInterpreter(language activLang) {
|
|
this.activLang = activLang;
|
|
bodyOutput = new StringBuilder();
|
|
resolveOutput = new StringBuilder();
|
|
headerOutput = new StringBuilder();
|
|
isComplex = false;
|
|
}
|
|
public messageInterpreter(language activLang, String Input) {
|
|
this(activLang);
|
|
inputInterpreter(Input);
|
|
}
|
|
|
|
public void inputInterpreter(String Input) {
|
|
|
|
if(Input.trim().toLowerCase().replace(" ", "").matches("\\d+")) {
|
|
Input = Input.trim().toLowerCase().replace(" ", "") + "w6";
|
|
}
|
|
subpart result = new subpart();
|
|
result = subPartAnalyse(Input);
|
|
resolveOutput.append(result.getResolveOutput());
|
|
bodyOutput.append(result.getBodyOutput());
|
|
headerOutput.append(result.getHeaderOutput());
|
|
intResult = result.getResolve();
|
|
}
|
|
|
|
|
|
private subpart subPartAnalyse(String part) {
|
|
subpart result = new subpart();
|
|
part = part.trim().toLowerCase().replace(" ", "");
|
|
boolean first = true;
|
|
/*
|
|
if(part.contains("max(")) {
|
|
/*
|
|
1d6 + max(5d5+10,2d4+2) + 15
|
|
|
|
* /
|
|
Pattern pattern = Pattern.compile("(?:.*)max\\((.*?)\\).*");
|
|
Matcher matcher = pattern.matcher(part);
|
|
if(matcher.find()) {
|
|
//String pre = part.substring(0, (matcher.start() - 3));
|
|
//String operation = part.substring((matcher.start() + 1), (matcher.end()-1));
|
|
//String after = part.substring(matcher.end());
|
|
StringBuilder newBody = new StringBuilder("max(");
|
|
StringBuilder newHeader = new StringBuilder("max(");
|
|
String[] subParts = matcher.group(1).split(",");
|
|
subpart[] tempSubparts = new subpart[subParts.length];
|
|
int i = 0;
|
|
int highest = 0;
|
|
for(String subPart : subParts) { // Vergleichen welcher wert der höhste ist
|
|
tempSubparts[i] = subPartAnalyse(subPart);
|
|
if(i != highest && tempSubparts[i].getResolve() > tempSubparts[highest].getResolve()) {
|
|
highest = i;
|
|
}
|
|
if(i > 0) {
|
|
newBody.append("), ");
|
|
newHeader.append(", ");
|
|
}
|
|
newBody.append("(");
|
|
newBody.append(tempSubparts[i].getBodyOutput());
|
|
newHeader.append(tempSubparts[i].getHeaderOutput());
|
|
i++;
|
|
}
|
|
newBody.append(")");
|
|
newHeader.append(")");
|
|
|
|
int partmarker = new Random().nextInt(1500);
|
|
//subPartAnalyse(part.substring(0, (matcher.start() - 3)) + tempSubparts[highest].getResolve() + part.substring(matcher.end()));
|
|
subpart contAnalyser = subPartAnalyse(part.substring(0, (matcher.start(1) - 4)) + " ###b" + tempSubparts[highest].getResolve() + "##" + partmarker + "e### " + part.substring(matcher.end(1)+1));
|
|
tempSubparts[highest].getResolve();
|
|
result.appendBodyOutput(contAnalyser.getBodyOutput().replaceAll("###b" + tempSubparts[highest].getResolve() + "##" + partmarker + "e###", newBody.toString()));
|
|
result.appendResolveOutput(contAnalyser.getResolveOutput().replaceAll("###b" + tempSubparts[highest].getResolve() + "##" + partmarker + "e###", "max(" + contAnalyser.getResolve().toString() + ")"));
|
|
result.appendHeaderOutput(contAnalyser.getHeaderOutput().replaceAll("###b" + tempSubparts[highest].getResolve() + "##" + partmarker + "e###", newHeader.toString()));
|
|
result.addResolve(contAnalyser.getResolve());
|
|
|
|
}
|
|
//part.matches("")
|
|
return result;
|
|
}
|
|
if(part.contains("min(")) {
|
|
|
|
}*/
|
|
if(part.contains("+")) {
|
|
String[] subParts = part.trim().split("\\+");
|
|
subpart tempSubpart;
|
|
for(String subPart : subParts) {
|
|
tempSubpart = subPartAnalyse(subPart);
|
|
if(!first) {
|
|
result.appendBodyOutput(" + ");
|
|
result.appendResolveOutput(" + ");
|
|
result.appendHeaderOutput(" + ");
|
|
}
|
|
first = false;
|
|
result.appendBodyOutput(tempSubpart.getBodyOutput());
|
|
result.appendResolveOutput(tempSubpart.getResolveOutput());
|
|
result.appendHeaderOutput(tempSubpart.getHeaderOutput());
|
|
result.addResolve(tempSubpart.getResolve());
|
|
}
|
|
isComplex = true;
|
|
return result;
|
|
}
|
|
if(part.contains("-")) {
|
|
String[] subParts = part.trim().split("\\-");
|
|
subpart tempSubpart;
|
|
for(String subPart : subParts) {
|
|
tempSubpart = subPartAnalyse(subPart);
|
|
if(!first) {
|
|
result.appendBodyOutput(" - ");
|
|
result.appendResolveOutput(" - ");
|
|
result.appendHeaderOutput(" - ");
|
|
result.addResolve(tempSubpart.getResolve()*-1);
|
|
}
|
|
else
|
|
result.addResolve(tempSubpart.getResolve());
|
|
first = false;
|
|
result.appendBodyOutput(tempSubpart.getBodyOutput());
|
|
result.appendResolveOutput(tempSubpart.getResolveOutput());
|
|
result.appendHeaderOutput(tempSubpart.getHeaderOutput());
|
|
|
|
}
|
|
isComplex = true;
|
|
return result;
|
|
}
|
|
|
|
if(part.matches("([0-9]*)[dw]([0-9]*)")) {
|
|
String[] diceInterpreter = part.split("[wd]");
|
|
try {
|
|
int diceart = Integer.parseInt(diceInterpreter[1]);
|
|
int numberOfDice = 1;
|
|
if(!diceInterpreter[0].isEmpty())
|
|
numberOfDice = Integer.parseInt(diceInterpreter[0]);
|
|
result = roll(diceart, numberOfDice);
|
|
} catch (NumberFormatException e) {
|
|
String message = activLang.error;
|
|
message = message.replace("%command%", part.trim().toLowerCase());
|
|
throw new IllegalArgumentException( message );
|
|
}
|
|
}
|
|
|
|
else if(part.matches("\\d+")) {
|
|
result.addResolve(Integer.parseInt(part));
|
|
result.appendBodyOutput(part);
|
|
result.appendHeaderOutput(part);
|
|
result.appendResolveOutput(part);
|
|
}
|
|
else if(part.matches("###b\\d+##\\d+e###")) {
|
|
Pattern pattern = Pattern.compile("###b(\\d+)##\\d+e###");
|
|
Matcher matcher = pattern.matcher(part);
|
|
matcher.find();
|
|
result.addResolve(Integer.parseInt(matcher.group(1)));
|
|
result.appendBodyOutput(part);
|
|
result.appendHeaderOutput(part);
|
|
result.appendResolveOutput(part);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
subpart roll(Integer diceart, Integer numberOfDice) {
|
|
int dice = 0;
|
|
subpart result = new subpart();
|
|
result.appendHeaderOutput(numberOfDice.toString() + "w" + diceart.toString());
|
|
result.appendBodyOutput("(");
|
|
for(int i = 0; i < numberOfDice; i++) {
|
|
dice = new Random().nextInt(diceart);
|
|
if(i > 0) {
|
|
result.appendBodyOutput(", ");
|
|
}
|
|
result.appendBodyOutput(dice+1);
|
|
result.addResolve(dice+1);
|
|
}
|
|
result.appendBodyOutput(")");
|
|
result.appendResolveOutput("(" + result.getResolve() + ")");
|
|
return result;
|
|
}
|
|
|
|
public String getBodyOutput() {
|
|
return bodyOutput.toString();
|
|
}
|
|
|
|
public String getResolveOutput() {
|
|
return resolveOutput.toString();
|
|
}
|
|
|
|
public String getHeaderOutput() {
|
|
return headerOutput.toString();
|
|
}
|
|
|
|
public Integer getResult() {
|
|
return intResult;
|
|
}
|
|
|
|
public boolean isComplex() {
|
|
return isComplex;
|
|
}
|
|
|
|
}
|