Skip to content

Commit

Permalink
fix color in compare operators/functions (#80)
Browse files Browse the repository at this point in the history
fix color in compare operators/functions
  • Loading branch information
twonirwana authored Jan 4, 2024
1 parent 842ba1a commit f5f3f5c
Show file tree
Hide file tree
Showing 48 changed files with 185 additions and 152 deletions.
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ All functions are case insensitiv.
|max |`max(<expression1>, <expression2> ...)` |`max(4d6)` |returns the largest elements (multiple if the largest is not unique) of one or more inner expressions. Text is compared alphabetically
|sort asc |`asc(<expression1>, <expression2> ...)` |`asc(4d6)` |sorts all elements ascending of one or more inner expressions. Text is compared alphabetically
|sort desc |`desc(<expression1>, <expression2> ...)` |`desc(4d6)` |sorts all elements descending of one or more inner expressions. Text is compared alphabetically
|chancel |`chancel(<expression>, <listA>, <listB>)` |`chancel(8d10, 10, 1)` |the elements of listA and listB (can also be single elements) chancel each other and remove each other from the result.
|cancel |`cancel(<expression>, <listA>, <listB>)` |`cancel(8d10, 10, 1)` |the elements of listA and listB (can also be single elements) cancel each other and remove each other from the result.
|replace |`replace(<expression>, <find>, <replace> ...)` |`replace(8d10, [9/10], 'bonus')` | each element in `<expression>` that matches on of the elements in `<find>` will be replaced with the elements in `<replace>`. `<replace>` can be an empty list `[]` or literal `''` and thereby removing the found elements. It is possible to add multiple <find>/<replace> pairs to replace different elements in one replace.
|if |`if(<boolean>,<true>,<false>)` |`if(1d6=?6,'six','not six')` or `if(1d6=?6,'six')` or `val('$r',1d6), if('$r'=?1,'one','$r'=?2,'two','else') |if `<boolean>` equal true then return the `<true>` expression or else the `<false>` expression. The `<false>` expression is optional, if it is missing and `<boolean>` is `false` then the result empty. It is possible to add more than `<boolean>,<true>` pair in the function, the result will be the `<true>` of the first true `<boolean>`, coming from left. All <boolean> must be non-empty and contain only on element (therefor can't contain only `val`). `val` are will only set in the first <true>. Use the following structure to use `if` to set different value in a `val`: `if(1d6>?4, val('$a',10), val('$a',-10))`, this will set '$a' to 10 if the 1d6 roll is bigger than 4 and to -10 otherwise.
|group count |`groupC(<expression1>, <expression2> ...)` |`groupC(20d6)` | counts all elements of with the same value and provides the results as list in the format of `<count>x<value>`
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/janno/evaluator/dice/RandomElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.google.common.collect.ImmutableList;
import lombok.NonNull;
import lombok.Value;
import org.checkerframework.checker.nullness.qual.Nullable;

import javax.annotation.Nullable;

@Value
public class RandomElement {
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/de/janno/evaluator/dice/Roll.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ public class Roll {

public Optional<Integer> asInteger() {
if (elements.size() == 1) {
return elements.get(0).asInteger();
return elements.getFirst().asInteger();
}
return Optional.empty();
}

public Optional<BigDecimal> asDecimal() {
if (elements.size() == 1) {
return elements.get(0).asDecimal();
return elements.getFirst().asDecimal();
}
return Optional.empty();
}

public Optional<Boolean> asBoolean() {
if (elements.size() == 1) {
return elements.get(0).asBoolean();
return elements.getFirst().asBoolean();
}
return Optional.empty();
}
Expand Down Expand Up @@ -67,4 +67,8 @@ public boolean equalForValueAndTag(Roll other) {
}
return true;
}

public boolean isElementsContainsElementWithValueAndTag(RollElement rollElement) {
return elements.stream().anyMatch(e -> e.isEqualValueAndTag(rollElement));
}
}
4 changes: 2 additions & 2 deletions src/main/java/de/janno/evaluator/dice/Tokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.google.common.collect.ImmutableList;
import lombok.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -191,7 +191,7 @@ private Optional<Match> getBestMatch(String input) throws ExpressionException {
throw new IllegalStateException("More then one operator matched the input %s: %s".formatted(input, maxLengthMatches.stream().map(Match::token).map(Token::toString).toList()));
}

return Optional.of(maxLengthMatches.get(0));
return Optional.of(maxLengthMatches.getFirst());
}

private List<Match> getAllMatches(String input) throws ExpressionException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public AbstractIf(@NonNull String name) {
return variables -> {
List<Roll> rolls = extendAllBuilder(arguments, variables);
checkRollSize(inputValue, rolls, getMinArgumentCount(), getMaxArgumentCount());
Roll input = rolls.get(0);
Roll input = rolls.getFirst();

int counter = 1;
UniqueRandomElements.Builder randomElements = UniqueRandomElements.builder();
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/de/janno/evaluator/dice/function/Cancel.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ public Cancel() {
return variables -> {
List<Roll> rolls = extendAllBuilder(arguments, variables);
checkRollSize(inputValue, rolls, getMinArgumentCount(), getMaxArgumentCount());
Roll input = rolls.get(0);
Roll input = rolls.getFirst();
Roll typeA = rolls.get(1);
Roll typeB = rolls.get(2);

List<RollElement> noMatch = input.getElements().stream()
.filter(r -> !typeA.getElements().contains(r) && !typeB.getElements().contains(r))
.filter(r -> !typeA.isElementsContainsElementWithValueAndTag(r) && !typeB.isElementsContainsElementWithValueAndTag(r))
.collect(ImmutableList.toImmutableList());
List<RollElement> typeAMatch = input.getElements().stream()
.filter(r -> typeA.getElements().contains(r))
.filter(typeA::isElementsContainsElementWithValueAndTag)
.collect(ImmutableList.toImmutableList());
List<RollElement> typeBMatch = input.getElements().stream()
.filter(r -> typeB.getElements().contains(r))
.filter(typeB::isElementsContainsElementWithValueAndTag)
.collect(ImmutableList.toImmutableList());

ImmutableList.Builder<RollElement> resultBuilder = ImmutableList.<RollElement>builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public ColorFunction() {
return variables -> {
List<Roll> rolls = extendAllBuilder(arguments, variables);
checkRollSize(inputValue, rolls, getMinArgumentCount(), getMaxArgumentCount());
Roll p1 = rolls.get(0);
Roll p1 = rolls.getFirst();
Roll p2 = rolls.get(1);
checkContainsSingleElement(inputValue, p2, "second argument");
String color = p2.getElements().get(0).getValue();
String color = p2.getElements().getFirst().getValue();
UniqueRandomElements.Builder builder = new UniqueRandomElements.Builder();
rolls.forEach(r -> builder.addWithColor(r.getRandomElementsInRoll(), color));
return Optional.of(ImmutableList.of(new Roll(getExpression(inputValue, rolls),
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/janno/evaluator/dice/function/Double.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public Double() {
return variables -> {
List<Roll> rolls = extendAllBuilder(arguments, variables);
checkRollSize(inputValue, rolls, getMinArgumentCount(), getMaxArgumentCount());
Roll input = rolls.get(0);
Roll input = rolls.getFirst();
Roll toDuplicate = rolls.get(1);

ImmutableList<RollElement> rollElements = input.getElements().stream()
.flatMap(r -> {
if (toDuplicate.getElements().contains(r)) {
if (toDuplicate.isElementsContainsElementWithValueAndTag(r)) {
return ImmutableList.of(r, r).stream();
} else {
return ImmutableList.of(r).stream();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/janno/evaluator/dice/function/If.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public If() {
throw new ExpressionException(String.format("'%s' requires as 2 inputs but was '%s'", getName(), arguments.size()));
}
ImmutableList.Builder<Roll> allRolls = ImmutableList.builder();
Optional<List<Roll>> checkIfTrue = arguments.get(0).extendRoll(variables);
Optional<List<Roll>> checkIfTrue = arguments.getFirst().extendRoll(variables);
if (checkIfTrue.isEmpty()) {
throw new ExpressionException(String.format("'%s' requires a non-empty input as first argument", inputValue));
}
Expand All @@ -36,7 +36,7 @@ public If() {
int checkIfTrueIndex = 1;
while (checkIfTrueIndex < arguments.size()) {
checkRollSize(inputValue, checkIfTrue.orElseThrow(), 1, 1);
Roll booleanExpression = checkIfTrue.get().get(0);
Roll booleanExpression = checkIfTrue.get().getFirst();
allRolls.addAll(checkIfTrue.get());
allRolls.addAll(returnIfTrue.orElse(Collections.emptyList()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public IfGreater() {
protected boolean compare(Roll input, int inputPosition, Roll compareTo, int compareToPosition) throws ExpressionException {
checkContainsSingleElement(getName(), input, "%d argument".formatted(inputPosition));
checkContainsSingleElement(getName(), compareTo, "%d argument".formatted(compareToPosition));
return input.getElements().get(0).compareTo(compareTo.getElements().get(0)) > 0;
return input.getElements().getFirst().compareTo(compareTo.getElements().getFirst()) > 0;
}

}
2 changes: 1 addition & 1 deletion src/main/java/de/janno/evaluator/dice/function/IfIn.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public IfIn() {
@Override
protected boolean compare(Roll input, int inputPosition, Roll compareTo, int compareToPosition) throws ExpressionException {
checkContainsSingleElement(getName(), input, "%d argument".formatted(inputPosition));
return compareTo.getElements().contains(input.getElements().get(0));
return compareTo.isElementsContainsElementWithValueAndTag(input.getElements().getFirst());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public IfLesser() {
protected boolean compare(Roll input, int inputPosition, Roll compareTo, int compareToPosition) throws ExpressionException {
checkContainsSingleElement(getName(), input, "%d argument".formatted(inputPosition));
checkContainsSingleElement(getName(), compareTo, "%d argument".formatted(compareToPosition));
return input.getElements().get(0).compareTo(compareTo.getElements().get(0)) < 0;
return input.getElements().getFirst().compareTo(compareTo.getElements().getFirst()) < 0;
}
}
4 changes: 2 additions & 2 deletions src/main/java/de/janno/evaluator/dice/function/Replace.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public Replace() {
if (rolls.size() % 2 == 0) {
throw new ExpressionException(String.format("'%s' an odd number of arguments but was %d", getName(), arguments.size()));
}
Roll input = rolls.get(0);
Roll input = rolls.getFirst();
ImmutableList<RollElement> rollElements = input.getElements();
ImmutableList.Builder<Roll> childrenRollBuilder = ImmutableList.<Roll>builder()
.addAll(input.getChildrenRolls());
Expand All @@ -35,7 +35,7 @@ public Replace() {
.addAll(replace.getChildrenRolls());
rollElements = rollElements.stream()
.flatMap(r -> {
if (find.getElements().contains(r)) {
if (find.isElementsContainsElementWithValueAndTag(r)) {
return replace.getElements().stream();
}
return Stream.of(r);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/janno/evaluator/dice/function/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public Value() {
throw new ExpressionException(String.format("'%s' requires as 2 inputs but was '%s'", getName(), arguments.size()));
}
Map<String, Roll> variableNameMap = new ConcurrentHashMap<>(); //don't replace literals in the first argument of the function, but it can use new variables
Optional<List<Roll>> valNameRoll = arguments.get(0).extendRoll(variableNameMap);
Optional<List<Roll>> valNameRoll = arguments.getFirst().extendRoll(variableNameMap);
if (valNameRoll.isEmpty()) {
throw new ExpressionException(String.format("'%s' requires a non-empty input as first argument", inputValue));
}
Expand All @@ -35,7 +35,7 @@ public Value() {

checkRollSize(inputValue, rolls, getMinArgumentCount(), getMaxArgumentCount());

String valName = rolls.get(0).getElements().get(0).getValue();
String valName = rolls.getFirst().getElements().getFirst().getValue();

String expression = getExpression(inputValue, rolls);
variables.put(valName, new Roll(expression,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public AndBool() {
List<Roll> rolls = extendAllBuilder(operands, variables);
checkRollSize(inputValue, rolls, 2,2);

Roll left = rolls.get(0);
Roll left = rolls.getFirst();
Roll right = rolls.get(1);
final boolean leftBoolValue = left.asBoolean().orElseThrow(() -> throwNotBoolean(inputValue, left, "left"));
final boolean rightBoolValue = right.asBoolean().orElseThrow(() -> throwNotBoolean(inputValue, right, "right"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public EqualBool() {
List<Roll> rolls = extendAllBuilder(operands, variables);
checkRollSize(inputValue, rolls, 2, 2);

Roll left = rolls.get(0);
Roll left = rolls.getFirst();
Roll right = rolls.get(1);

final boolean isEqual = left.equalForValueAndTag(right);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public GreaterBool() {
List<Roll> rolls = extendAllBuilder(operands, variables);
checkRollSize(inputValue, rolls, 2,2);

Roll left = rolls.get(0);
Roll left = rolls.getFirst();
Roll right = rolls.get(1);
final BigDecimal leftNumber = left.asDecimal().orElseThrow(() -> throwNotDecimalExpression(inputValue, right, "left"));
final BigDecimal rightNumber = right.asDecimal().orElseThrow(() -> throwNotDecimalExpression(inputValue, right, "right"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public GreaterEqualBool() {
List<Roll> rolls = extendAllBuilder(operands, variables);
checkRollSize(inputValue, rolls, 2,2);

Roll left = rolls.get(0);
Roll left = rolls.getFirst();
Roll right = rolls.get(1);
final BigDecimal leftNumber = left.asDecimal().orElseThrow(() -> throwNotDecimalExpression(inputValue, left, "left"));
final BigDecimal rightNumber = right.asDecimal().orElseThrow(() -> throwNotDecimalExpression(inputValue, right, "right"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ public InBool() {
public @NonNull RollBuilder evaluate(@NonNull List<RollBuilder> operands, @NonNull String inputValue) throws ExpressionException {
return variables -> {
List<Roll> rolls = extendAllBuilder(operands, variables);
checkRollSize(inputValue, rolls, 2,2);
checkRollSize(inputValue, rolls, 2, 2);

Roll left = rolls.get(0);
Roll left = rolls.getFirst();
Roll right = rolls.get(1);

ImmutableList<RollElement> diceResult = ImmutableList.of(new RollElement(String.valueOf(right.getElements().containsAll(left.getElements())), RollElement.NO_TAG, RollElement.NO_COLOR));
boolean isTrue = left.getElements().stream().allMatch(right::isElementsContainsElementWithValueAndTag);

ImmutableList<RollElement> diceResult = ImmutableList.of(new RollElement(String.valueOf(isTrue), RollElement.NO_TAG, RollElement.NO_COLOR));
return Optional.of(ImmutableList.of(new Roll(getBinaryOperatorExpression(inputValue, rolls),
diceResult,
UniqueRandomElements.from(rolls),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public LesserBool() {
List<Roll> rolls = extendAllBuilder(operands, variables);
checkRollSize(inputValue, rolls, 2,2);

Roll left = rolls.get(0);
Roll left = rolls.getFirst();
Roll right = rolls.get(1);
final BigDecimal leftNumber = left.asDecimal().orElseThrow(() -> throwNotDecimalExpression(inputValue, right, "left"));
final BigDecimal rightNumber = right.asDecimal().orElseThrow(() -> throwNotDecimalExpression(inputValue, right, "right"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public LesserEqualBool() {
List<Roll> rolls = extendAllBuilder(operands, variables);
checkRollSize(inputValue, rolls, 2,2);

Roll left = rolls.get(0);
Roll left = rolls.getFirst();
Roll right = rolls.get(1);
final BigDecimal leftNumber = left.asDecimal().orElseThrow(() -> throwNotDecimalExpression(inputValue, right, "left"));
final BigDecimal rightNumber = right.asDecimal().orElseThrow(() -> throwNotDecimalExpression(inputValue, right, "right"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public NegateBool() {
List<Roll> rolls = extendAllBuilder(operands, variables);
checkRollSize(inputValue, rolls, 1, 1);

Roll value = rolls.get(0);
Roll value = rolls.getFirst();

final boolean boolValue = value.asBoolean().orElseThrow(() -> throwNotBoolean(inputValue, value, "right"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public OrBool() {
List<Roll> rolls = extendAllBuilder(operands, variables);
checkRollSize(inputValue, rolls, 2,2);

Roll left = rolls.get(0);
Roll left = rolls.getFirst();
Roll right = rolls.get(1);
final boolean leftBoolValue = left.asBoolean().orElseThrow(() -> throwNotBoolean(inputValue, left, "left"));
final boolean rightBoolValue = right.asBoolean().orElseThrow(() -> throwNotBoolean(inputValue, right, "right"));
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/janno/evaluator/dice/operator/die/Color.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public Color() {
List<Roll> rolls = extendAllBuilder(operands, variables);
checkRollSize(inputValue, rolls, 2, 2);

Roll left = rolls.get(0);
Roll left = rolls.getFirst();
Roll right = rolls.get(1);
checkAllElementsAreSameTag(inputValue, left, right);
checkContainsSingleElement(inputValue, right, "second argument");
String color = right.getElements().get(0).getValue();
String color = right.getElements().getFirst().getValue();
//colors are applied to the random elements, so they can be used for dice images
UniqueRandomElements.Builder builder = new UniqueRandomElements.Builder();
rolls.forEach(r -> builder.addWithColor(r.getRandomElementsInRoll(), color));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ExplodingAddDice(NumberSupplier numberSupplier, int maxNumberOfDice) {

UniqueRandomElements.Builder randomElements = UniqueRandomElements.builder();
if (rolls.size() == 1) {
final Roll right = rolls.get(0);
final Roll right = rolls.getFirst();
final int sidesOfDie = right.asInteger().orElseThrow(() -> throwNotIntegerExpression(inputValue, right, "right"));
if (sidesOfDie < 2) {
throw new ExpressionException(String.format("The number of sides of a die must be greater then 1 but was %d", sidesOfDie));
Expand All @@ -47,7 +47,7 @@ public ExplodingAddDice(NumberSupplier numberSupplier, int maxNumberOfDice) {
ImmutableList.of(right))));
}

final Roll left = rolls.get(0);
final Roll left = rolls.getFirst();
final Roll right = rolls.get(1);
randomElements.add(left.getRandomElementsInRoll());
randomElements.add(right.getRandomElementsInRoll());
Expand Down
Loading

0 comments on commit f5f3f5c

Please sign in to comment.