67 lines
2.1 KiB
Java
67 lines
2.1 KiB
Java
package com.github.dtschust.zork;
|
|
|
|
import com.github.dtschust.zork.types.HasPrintsAndActions;
|
|
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
import java.util.stream.Stream;
|
|
|
|
public class ZorkTrigger implements HasPrintsAndActions {
|
|
|
|
private final List<String> print;
|
|
private final List<String> action;
|
|
|
|
/* By default, "single" */
|
|
private final ZorkTriggerType type;
|
|
private final List<ZorkCondition> conditions;
|
|
private final List<ZorkCommand> commands;
|
|
|
|
public ZorkTrigger(final ZorkTriggerType type,
|
|
final List<ZorkCondition> conditions,
|
|
final List<ZorkCommand> commands,
|
|
final List<String> print,
|
|
final List<String> action) {
|
|
this.conditions = conditions;
|
|
this.commands = commands;
|
|
this.print = print;
|
|
this.action = action;
|
|
this.type = type;
|
|
}
|
|
|
|
public static boolean evaluateTriggersFor(final Stream<String> collection,
|
|
final ZorkGame game,
|
|
final String input) {
|
|
// non short-circuited to execute all side effects of evaluateTriggers
|
|
return collection
|
|
.map(game::getObject)
|
|
.filter(Optional::isPresent)
|
|
.map(Optional::get)
|
|
.map(a -> a.evaluateTriggers(game, input))
|
|
.reduce(false, (a, b) -> a || b);
|
|
}
|
|
|
|
public boolean hasCommand() {
|
|
return !this.commands.isEmpty();
|
|
}
|
|
|
|
public boolean isTriggered(final ZorkGame game, final String currentCommand) {
|
|
return commands.stream().allMatch(c -> c.matchesInput(currentCommand)) &&
|
|
conditions.stream().allMatch(c -> c.evaluate(game));
|
|
}
|
|
|
|
@Override
|
|
public List<String> getPrints() {
|
|
return Collections.unmodifiableList(print);
|
|
}
|
|
|
|
@Override
|
|
public List<String> getActions() {
|
|
return Collections.unmodifiableList(action);
|
|
}
|
|
|
|
public ZorkTriggerType getType() {
|
|
return type;
|
|
}
|
|
}
|