-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
15 changed files
with
122 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/main/java/de/janno/evaluator/dice/function/ColorOn.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package de.janno.evaluator.dice.function; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import de.janno.evaluator.dice.*; | ||
import lombok.NonNull; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static de.janno.evaluator.dice.ValidatorUtil.checkContainsNoOrSingleElement; | ||
import static de.janno.evaluator.dice.ValidatorUtil.checkContainsSingleRoll; | ||
|
||
public class ColorOn extends Function { | ||
public ColorOn(int maxNumberOfElements, boolean keepChildrenRolls) { | ||
super("colorOn", 3, Integer.MAX_VALUE, maxNumberOfElements, keepChildrenRolls); | ||
} | ||
|
||
@Override | ||
public @NonNull RollBuilder evaluate(@NonNull List<RollBuilder> arguments, @NonNull ExpressionPosition expressionPosition) throws ExpressionException { | ||
return new RollBuilder() { | ||
@Override | ||
public @NonNull Optional<List<Roll>> extendRoll(@NonNull RollContext rollContext) throws ExpressionException { | ||
if (arguments.size() % 2 == 0) { | ||
throw new ExpressionException(String.format("'%s' requires an odd number of arguments but was %d", getName(), arguments.size()), expressionPosition); | ||
} | ||
Optional<List<Roll>> input = arguments.getFirst().extendRoll(rollContext); | ||
if (input.isEmpty()) { | ||
return Optional.of(List.of()); | ||
} | ||
ImmutableList.Builder<Roll> allRolls = ImmutableList.<Roll>builder().addAll(input.get()); | ||
ImmutableList<RollElement> inputRollElements = input.get().stream().flatMap(r -> r.getElements().stream()).collect(ImmutableList.toImmutableList()); | ||
for (int i = 1; i < arguments.size() - 1; i = i + 2) { | ||
|
||
Optional<List<Roll>> inRolls = arguments.get(i).extendRoll(rollContext); | ||
checkContainsSingleRoll(expressionPosition, inRolls, i + 1); | ||
final Roll inRoll = inRolls.orElseThrow().getFirst(); | ||
allRolls.add(inRoll); | ||
|
||
ImmutableList.Builder<RollElement> currentIterationElements = ImmutableList.builder(); | ||
for (RollElement rollElement : inputRollElements) { | ||
if (inRoll.isElementsContainsElementWithValueAndTag(rollElement)) { | ||
Optional<List<Roll>> colorRolls = arguments.get(i + 1).extendRoll(rollContext); | ||
checkContainsSingleRoll(expressionPosition, colorRolls, i + 2); | ||
Roll colorRoll = colorRolls.orElseThrow().getFirst(); | ||
checkContainsNoOrSingleElement(expressionPosition, colorRoll, "%d argument".formatted(i + 2)); | ||
final String color = colorRoll.asSingleValue().orElse(RollElement.NO_COLOR); | ||
allRolls.add(colorRoll); | ||
currentIterationElements.add(new RollElement(rollElement.getValue(), rollElement.getTag(), color)); | ||
} else { | ||
currentIterationElements.add(rollElement); | ||
} | ||
} | ||
inputRollElements = currentIterationElements.build(); | ||
} | ||
|
||
return Optional.of(ImmutableList.of(new Roll(toExpression(), | ||
inputRollElements, | ||
RandomElementsBuilder.fromRolls(allRolls.build(), rollContext), | ||
allRolls.build(), | ||
expressionPosition, | ||
maxNumberOfElements, keepChildrenRolls))); | ||
|
||
} | ||
|
||
@Override | ||
public @NonNull String toExpression() { | ||
return getExpression(expressionPosition, arguments); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters