54 lines
1.5 KiB
Java
54 lines
1.5 KiB
Java
package com.github.dtschust.zork.repl;
|
|
|
|
import com.github.dtschust.zork.ZorkGame;
|
|
import com.github.dtschust.zork.repl.actions.*;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
|
|
public class ActionDispatcher {
|
|
private static final List<Action> actionList = List.of(
|
|
new AddAction(),
|
|
new AttackAction(),
|
|
new DeleteAction(),
|
|
new DropItemAction(),
|
|
new GameOverAction(),
|
|
new InventoryAction(),
|
|
new MoveAction(),
|
|
new OpenAction(),
|
|
new PutAction(),
|
|
new ReadAction(),
|
|
new StartGameAction(),
|
|
new TakeAction(),
|
|
new TurnOnAction(),
|
|
new UpdateAction()
|
|
);
|
|
|
|
private final ZorkGame game;
|
|
|
|
public ActionDispatcher(ZorkGame game) {
|
|
this.game = game;
|
|
}
|
|
|
|
private Optional<Action> findAction(final List<String> arguments) {
|
|
if (arguments.isEmpty()) return Optional.empty();
|
|
final int size = arguments.size();
|
|
return actionList.stream().filter(
|
|
u -> size >= u.getMinimumArgCount() &&
|
|
size <= u.getMaximumArgCount() &&
|
|
u.matchesInput(arguments)
|
|
).findAny();
|
|
}
|
|
|
|
public void dispatch(String input) {
|
|
final List<String> arguments = Arrays.asList(input.split(" "));
|
|
final Optional<Action> action = findAction(arguments);
|
|
|
|
if (action.isEmpty() || !action.get().run(game, arguments)) {
|
|
game.stream.println("Error");
|
|
}
|
|
}
|
|
|
|
}
|