Skip to content

Commit

Permalink
Update roll dice result text data error
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriksgata committed Nov 2, 2022
1 parent 6562c2e commit 980bb7b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 25 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.eiriksgata</groupId>
<artifactId>trpg-java-dice</artifactId>
<version>1.3-SNAPSHOT</version>
<version>1.4-SNAPSHOT</version>

<properties>
<java.version>11</java.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import indi.eiriksgata.dice.operation.RollBasics;
import indi.eiriksgata.dice.reply.CustomText;
import indi.eiriksgata.dice.utlis.RegularExpressionUtils;
import indi.eiriksgata.dice.vo.RegularExpressionResult;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.RandomUtils;

Expand Down Expand Up @@ -72,41 +73,45 @@ public String rollRandom(String text, Long id) {

@Override
public String rollRandom(String text, Long id, RollRandomCallback callback) {
text = text.toUpperCase();
text = text.toUpperCase().trim();
String inputFormula = text;
List<String> list = RegularExpressionUtils.getMatchers("[0-9]*[Dd][0-9]+|[0-9]+[Dd]|[Dd]", text);
for (String temp : list) {
if (temp.charAt(0) == 'D') {
if (temp.length() == 1) {
int offsetValue = 0;
List<RegularExpressionResult> list = RegularExpressionUtils.getMatchersResult("[0-9]*[Dd][0-9]+|[0-9]+[Dd]|[Dd]", text);
for (RegularExpressionResult temp : list) {
if (temp.getMatcherText().charAt(0) == 'D') {
if (temp.getMatcherText().length() == 1) {
if (defaultDiceFace.get(id) == null) {
String diceType = DiceConfig.diceSet.getString("dice.type");
int diceFace = Integer.parseInt(DiceConfig.diceSet.getString(diceType + ".face"));
text = text.replaceFirst(temp, String.valueOf(createRandom(1, diceFace)[0]));
inputFormula = inputFormula.replaceFirst(temp, "D" + diceFace);
text = text.replaceFirst(temp.getMatcherText(), String.valueOf(createRandom(1, diceFace)[0]));
inputFormula = inputFormula.substring(0, temp.getStartIndex() + offsetValue) + "D" + diceFace + inputFormula.substring(temp.getEndIndex() + offsetValue);
offsetValue += ("" + diceFace).length();

} else {
text = text.replaceFirst(temp, String.valueOf(createRandom(1, defaultDiceFace.get(id))[0]));
inputFormula = inputFormula.replaceFirst(temp, "D" + defaultDiceFace.get(id));

text = text.replaceFirst(temp.getMatcherText(), String.valueOf(createRandom(1, defaultDiceFace.get(id))[0]));
inputFormula = inputFormula.substring(0, temp.getStartIndex()) + "D" + defaultDiceFace.get(id) + inputFormula.substring(temp.getEndIndex());
offsetValue += ("" + defaultDiceFace.get(id)).length();
}
} else {
int[] diceRandom = createRandom(1, Integer.parseInt(temp.substring(1)));
text = text.replaceFirst(temp, String.valueOf(diceRandom[0]));
int[] diceRandom = createRandom(1, Integer.parseInt(temp.getMatcherText().substring(1)));
text = text.replaceFirst(temp.getMatcherText(), String.valueOf(diceRandom[0]));
}
} else {
String[] dataSplitArr = temp.split("[dD]");
String[] dataSplitArr = temp.getMatcherText().split("[dD]");
int diceNumber;
int diceFace;
if (dataSplitArr.length == 1) {
diceNumber = Integer.parseInt(dataSplitArr[0]);
if (defaultDiceFace.get(id) == null) {
String diceType = DiceConfig.diceSet.getString("dice.type");
diceFace = Integer.parseInt(DiceConfig.diceSet.getString(diceType + ".face"));

//diceFace = 100;
} else {
diceFace = defaultDiceFace.get(id);
}
String replaceText = diceNumber + "D" + diceFace;
inputFormula = inputFormula.substring(0, temp.getStartIndex() + offsetValue) + replaceText + inputFormula.substring(temp.getEndIndex() + offsetValue);
offsetValue += ("" + diceFace).length();

} else {
diceNumber = Integer.parseInt(dataSplitArr[0]);
diceFace = Integer.parseInt(dataSplitArr[1]);
Expand All @@ -119,11 +124,9 @@ public String rollRandom(String text, Long id, RollRandomCallback callback) {
formula.append("+").append(randomData[j]);
}
formula.append(")");
text = text.replaceFirst(temp, String.valueOf(formula));
text = text.replaceFirst(temp.getMatcherText(), String.valueOf(formula));
} else {
System.out.println(text);
text = text.replaceFirst(temp, String.valueOf(randomData[0]));
System.out.println(text);
text = text.replaceFirst(temp.getMatcherText(), String.valueOf(randomData[0]));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package indi.eiriksgata.dice.utlis;

import indi.eiriksgata.dice.vo.RegularExpressionResult;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
Expand All @@ -10,7 +12,7 @@ public class RegularExpressionUtils {
/**
* 正则表达式返回内容
*
* @param regex 正则表达式
* @param regex 正则表达式
* @param source 源文本
* @return 匹配字段
*/
Expand All @@ -24,10 +26,25 @@ public static List<String> getMatchers(String regex, String source) {
return list;
}

public static List<RegularExpressionResult> getMatchersResult(String regex, String source) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(source);
List<RegularExpressionResult> result = new ArrayList<>();
while (matcher.find()) {
RegularExpressionResult regularExpressionResult = new RegularExpressionResult();
regularExpressionResult.setStartIndex(matcher.start());
regularExpressionResult.setEndIndex(matcher.end());
regularExpressionResult.setMatcherText(matcher.group());
result.add(regularExpressionResult);
}
return result;
}


/**
* 返回单个匹配数据
* @param regex 正则表达式
*
* @param regex 正则表达式
* @param source 源文本
* @return 匹配字段
*/
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/indi/eiriksgata/dice/vo/RegularExpressionResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package indi.eiriksgata.dice.vo;

import lombok.Data;

@Data
public class RegularExpressionResult {
private String matcherText;
private int startIndex;
private int endIndex;

}
23 changes: 20 additions & 3 deletions src/test/java/DiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
import org.apache.commons.lang3.RandomUtils;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* author: create by Keith
* version: v1.0
Expand All @@ -23,7 +27,7 @@ void randomTest() {
String text = "3d";
String[] resultArr = text.split("d");
System.out.println(resultArr.length);
System.out.println(resultArr[4]);
System.out.println(Arrays.toString(resultArr));

}

Expand Down Expand Up @@ -72,7 +76,7 @@ void jrrpTest() {

@Test
void roll() {
String result = new RollBasicsImpl().rollRandom("d20+20d50+2d3+d5", 233686862L);
String result = new RollBasicsImpl().rollRandom("d+5d+3d2+1d3+d+5d6+d", 233686862L);
System.out.println(result);
}

Expand All @@ -99,7 +103,7 @@ void randomUtilsNextInt() {
System.out.println(RandomUtils.nextInt(0, 2));
}
}

@Test
void sanCheckTest() {
String result = new RollBasicsImpl().sanCheck("1d5+2d3+5/1d100", "san50", new SanCheckCallback() {
Expand All @@ -111,4 +115,17 @@ public void getResultData(String attribute, int random, int sanValue, String cal
System.out.println(result);
}

@Test
void regex() {
String regex = "[0-9]*[Dd][0-9]+|[0-9]+[Dd]|[Dd]";
String source = "d+2d+5d6+d+2d3+d";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(source);
while (matcher.find()) {
System.out.println("start:" + matcher.group());
System.out.println("start:" + matcher.start() + " end:" + matcher.end());
}


}
}

0 comments on commit 980bb7b

Please sign in to comment.