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, no card to draw exists
|
||||||
Error, can not get resources: game not started
|
Error, can not get resources: game not started
|
||||||
Error, could not build item
|
Error, could not build item
|
||||||
Error, unknown command
|
Error, invalid build command
|
||||||
Error, can not get buildings: game not started
|
Error, can not get buildings: game not started
|
||||||
Error, can not get buildable items: game not started
|
Error, can not get buildable items: game not started
|
||||||
Error, not expecting dice roll
|
Error, not expecting dice roll
|
||||||
@ -14,7 +14,7 @@ EMPTY
|
|||||||
wood
|
wood
|
||||||
wood
|
wood
|
||||||
Error, could not build item
|
Error, could not build item
|
||||||
Error, unknown command
|
Error, invalid build command
|
||||||
EMPTY
|
EMPTY
|
||||||
EMPTY
|
EMPTY
|
||||||
Error, not expecting dice roll
|
Error, not expecting dice roll
|
||||||
@ -29,7 +29,7 @@ spider
|
|||||||
Error, roll dice, please
|
Error, roll dice, please
|
||||||
wood
|
wood
|
||||||
Error, awaiting dice roll, can not build
|
Error, awaiting dice roll, can not build
|
||||||
Error, unknown command
|
Error, invalid build command
|
||||||
EMPTY
|
EMPTY
|
||||||
Error, can not get buildable items: awaiting dice roll
|
Error, can not get buildable items: awaiting dice roll
|
||||||
Error, unexpected dice size
|
Error, unexpected dice size
|
||||||
@ -39,7 +39,7 @@ snake
|
|||||||
Error, roll dice, please
|
Error, roll dice, please
|
||||||
wood
|
wood
|
||||||
Error, awaiting dice roll, can not build
|
Error, awaiting dice roll, can not build
|
||||||
Error, unknown command
|
Error, invalid build command
|
||||||
EMPTY
|
EMPTY
|
||||||
Error, can not get buildable items: awaiting dice roll
|
Error, can not get buildable items: awaiting dice roll
|
||||||
Error, unexpected dice size
|
Error, unexpected dice size
|
||||||
@ -49,7 +49,7 @@ tiger
|
|||||||
Error, roll dice, please
|
Error, roll dice, please
|
||||||
wood
|
wood
|
||||||
Error, awaiting dice roll, can not build
|
Error, awaiting dice roll, can not build
|
||||||
Error, unknown command
|
Error, invalid build command
|
||||||
EMPTY
|
EMPTY
|
||||||
Error, can not get buildable items: awaiting dice roll
|
Error, can not get buildable items: awaiting dice roll
|
||||||
Error, unexpected dice size
|
Error, unexpected dice size
|
||||||
|
@ -12,7 +12,7 @@ import java.util.regex.Pattern;
|
|||||||
import static edu.kit.informatik.cardgame.ui.command.CommandFactory.BUILD;
|
import static edu.kit.informatik.cardgame.ui.command.CommandFactory.BUILD;
|
||||||
|
|
||||||
public final class Build extends Command {
|
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;
|
private Item item;
|
||||||
|
|
||||||
@ -31,7 +31,7 @@ public final class Build extends Command {
|
|||||||
if (input == null || !input.startsWith(BUILD)) {
|
if (input == null || !input.startsWith(BUILD)) {
|
||||||
throw new InvalidInputException("unknown command");
|
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()) {
|
if (!matcher.matches()) {
|
||||||
throw new InvalidInputException("invalid build command");
|
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 edu.kit.informatik.cardgame.ui.InvalidInputException;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.TreeMap;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Factory used to parse user input into commands.
|
* Factory used to parse user input into commands.
|
||||||
*
|
*
|
||||||
* @author Arne Keller
|
* @author Arne Keller
|
||||||
* @version 1.0
|
* @version 1.1
|
||||||
*/
|
*/
|
||||||
public final class CommandFactory {
|
public final class CommandFactory {
|
||||||
public static final String START = "start";
|
public static final String START = "start";
|
||||||
public static final String DRAW = "draw";
|
public static final String DRAW = "draw";
|
||||||
public static final String LIST_RESOURCES = "list-resources";
|
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 LIST_BUILDINGS = "list-buildings";
|
||||||
public static final String BUILDABLE = "build?";
|
public static final String BUILDABLE = "build?";
|
||||||
public static final String ROLL_DICE = "rollD";
|
public static final String ROLL_DICE = "rollD";
|
||||||
public static final String RESET = "reset";
|
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 {
|
static {
|
||||||
// initialize registered commands
|
// initialize registered commands
|
||||||
@ -50,8 +50,10 @@ public final class CommandFactory {
|
|||||||
* @throws InvalidInputException if user input is invalid
|
* @throws InvalidInputException if user input is invalid
|
||||||
*/
|
*/
|
||||||
public static Command getCommand(String input) throws InvalidInputException {
|
public static Command getCommand(String input) throws InvalidInputException {
|
||||||
Supplier<Command> commandSupplier = COMMANDS.entrySet().stream()
|
Supplier<Command> commandSupplier = COMMANDS
|
||||||
.filter(entry -> input.startsWith(entry.getKey())).map(Map.Entry::getValue)
|
.descendingKeySet().stream()
|
||||||
|
.filter(input::startsWith)
|
||||||
|
.map(COMMANDS::get)
|
||||||
.findFirst().orElse(null);
|
.findFirst().orElse(null);
|
||||||
if (commandSupplier != null) {
|
if (commandSupplier != null) {
|
||||||
Command command = commandSupplier.get();
|
Command command = commandSupplier.get();
|
||||||
|
Loading…
Reference in New Issue
Block a user