mirror of
https://gitlab.com/arnekeller/kit-programmieren-ws1920-final2.git
synced 2024-11-24 01:15:03 +00:00
Correctly handle common prefixes in command names
This commit is contained in:
parent
e7c3a40ac0
commit
f39fdee4c9
@ -1,7 +1,7 @@
|
||||
Error, no card to draw exists
|
||||
Error, can not get resources: game not started
|
||||
Error, could not build item
|
||||
Error, unknown command
|
||||
Error, invalid build command
|
||||
Error, can not get buildings: game not started
|
||||
Error, can not get buildable items: game not started
|
||||
Error, not expecting dice roll
|
||||
@ -14,7 +14,7 @@ EMPTY
|
||||
wood
|
||||
wood
|
||||
Error, could not build item
|
||||
Error, unknown command
|
||||
Error, invalid build command
|
||||
EMPTY
|
||||
EMPTY
|
||||
Error, not expecting dice roll
|
||||
@ -29,7 +29,7 @@ spider
|
||||
Error, roll dice, please
|
||||
wood
|
||||
Error, awaiting dice roll, can not build
|
||||
Error, unknown command
|
||||
Error, invalid build command
|
||||
EMPTY
|
||||
Error, can not get buildable items: awaiting dice roll
|
||||
Error, unexpected dice size
|
||||
@ -39,7 +39,7 @@ snake
|
||||
Error, roll dice, please
|
||||
wood
|
||||
Error, awaiting dice roll, can not build
|
||||
Error, unknown command
|
||||
Error, invalid build command
|
||||
EMPTY
|
||||
Error, can not get buildable items: awaiting dice roll
|
||||
Error, unexpected dice size
|
||||
@ -49,7 +49,7 @@ tiger
|
||||
Error, roll dice, please
|
||||
wood
|
||||
Error, awaiting dice roll, can not build
|
||||
Error, unknown command
|
||||
Error, invalid build command
|
||||
EMPTY
|
||||
Error, can not get buildable items: awaiting dice roll
|
||||
Error, unexpected dice size
|
||||
|
@ -12,7 +12,7 @@ import java.util.regex.Pattern;
|
||||
import static edu.kit.informatik.cardgame.ui.command.CommandFactory.BUILD;
|
||||
|
||||
public final class Build extends Command {
|
||||
private static final Pattern BUILD_ARGUMENT = Pattern.compile(BUILD + "(\\w+)");
|
||||
private static final Pattern BUILD_ARGUMENT = Pattern.compile(" (\\w+)");
|
||||
|
||||
private Item item;
|
||||
|
||||
@ -31,7 +31,7 @@ public final class Build extends Command {
|
||||
if (input == null || !input.startsWith(BUILD)) {
|
||||
throw new InvalidInputException("unknown command");
|
||||
}
|
||||
final Matcher matcher = Build.BUILD_ARGUMENT.matcher(input);
|
||||
final Matcher matcher = Build.BUILD_ARGUMENT.matcher(input.substring(BUILD.length()));
|
||||
if (!matcher.matches()) {
|
||||
throw new InvalidInputException("invalid build command");
|
||||
}
|
||||
|
@ -2,27 +2,27 @@ package edu.kit.informatik.cardgame.ui.command;
|
||||
|
||||
import edu.kit.informatik.cardgame.ui.InvalidInputException;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Factory used to parse user input into commands.
|
||||
*
|
||||
* @author Arne Keller
|
||||
* @version 1.0
|
||||
* @version 1.1
|
||||
*/
|
||||
public final class CommandFactory {
|
||||
public static final String START = "start";
|
||||
public static final String DRAW = "draw";
|
||||
public static final String LIST_RESOURCES = "list-resources";
|
||||
public static final String BUILD = "build ";
|
||||
public static final String BUILD = "build";
|
||||
public static final String LIST_BUILDINGS = "list-buildings";
|
||||
public static final String BUILDABLE = "build?";
|
||||
public static final String ROLL_DICE = "rollD";
|
||||
public static final String RESET = "reset";
|
||||
|
||||
private static final Map<String, Supplier<Command>> COMMANDS = new HashMap<>();
|
||||
private static final TreeMap<String, Supplier<Command>> COMMANDS = new TreeMap<>();
|
||||
|
||||
static {
|
||||
// initialize registered commands
|
||||
@ -50,8 +50,10 @@ public final class CommandFactory {
|
||||
* @throws InvalidInputException if user input is invalid
|
||||
*/
|
||||
public static Command getCommand(String input) throws InvalidInputException {
|
||||
Supplier<Command> commandSupplier = COMMANDS.entrySet().stream()
|
||||
.filter(entry -> input.startsWith(entry.getKey())).map(Map.Entry::getValue)
|
||||
Supplier<Command> commandSupplier = COMMANDS
|
||||
.descendingKeySet().stream()
|
||||
.filter(input::startsWith)
|
||||
.map(COMMANDS::get)
|
||||
.findFirst().orElse(null);
|
||||
if (commandSupplier != null) {
|
||||
Command command = commandSupplier.get();
|
||||
|
Loading…
Reference in New Issue
Block a user