diff --git a/docker/Dockerfile b/docker/Dockerfile index 9680592..74a2661 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -4,4 +4,9 @@ COPY target/maze-server-*.jar /app/ RUN rm /app/*-sources.jar RUN mv /app/*.jar /app/app.jar -CMD java -Dfritteli.maze.server.host=0.0.0.0 -Dfritteli.maze.server.port=80 -jar /app/app.jar +CMD java \ + -Dfritteli.maze.server.host=0.0.0.0 \ + -Dfritteli.maze.server.port=80 \ + -Dfritteli.maze.maxheight=256 \ + -Dfritteli.maze.maxwidth=256 \ + -jar /app/app.jar diff --git a/pom.xml b/pom.xml index aed0ac3..6693fcf 100644 --- a/pom.xml +++ b/pom.xml @@ -56,7 +56,7 @@ - 0.2.1 + 0.2.2-SNAPSHOT 4.0.0-M8 2.3.18.Final diff --git a/src/main/java/ch/fritteli/maze/server/Algorithm.java b/src/main/java/ch/fritteli/maze/server/Algorithm.java new file mode 100644 index 0000000..3b0dcf7 --- /dev/null +++ b/src/main/java/ch/fritteli/maze/server/Algorithm.java @@ -0,0 +1,45 @@ +package ch.fritteli.maze.server; + +import ch.fritteli.maze.generator.algorithm.MazeGeneratorAlgorithm; +import ch.fritteli.maze.generator.algorithm.RandomDepthFirst; +import ch.fritteli.maze.generator.algorithm.Wilson; +import ch.fritteli.maze.generator.model.Maze; +import io.vavr.collection.List; +import io.vavr.collection.Stream; +import io.vavr.control.Option; +import lombok.Getter; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.function.Function; + +public enum Algorithm { + RANDOM_DEPTH_FIRST(RandomDepthFirst::new, "random", "random-depth-first"), + WILSON(Wilson::new, "wilson"); + + @NotNull + private final Function creator; + + @Getter + @NotNull + private final List names; + + Algorithm(@NotNull final Function creator, + @NotNull final String... names) { + this.creator = creator; + this.names = List.of(names); + } + + @NotNull + public static Option ofString(@Nullable final String name) { + return Option.of(name) + .map(String::toLowerCase) + .flatMap(nameLC -> Stream.of(values()) + .find(algorithm -> algorithm.getNames().contains(nameLC))); + } + + @NotNull + public MazeGeneratorAlgorithm createAlgorithm(@NotNull final Maze maze) { + return this.creator.apply(maze); + } +} diff --git a/src/main/java/ch/fritteli/maze/server/handler/ParametersToMazeExtractor.java b/src/main/java/ch/fritteli/maze/server/handler/ParametersToMazeExtractor.java index 6c747e5..8ff14aa 100644 --- a/src/main/java/ch/fritteli/maze/server/handler/ParametersToMazeExtractor.java +++ b/src/main/java/ch/fritteli/maze/server/handler/ParametersToMazeExtractor.java @@ -3,6 +3,7 @@ package ch.fritteli.maze.server.handler; import ch.fritteli.maze.generator.algorithm.RandomDepthFirst; import ch.fritteli.maze.generator.model.Maze; import ch.fritteli.maze.generator.model.Position; +import ch.fritteli.maze.server.Algorithm; import ch.fritteli.maze.server.InvalidRequestParameterException; import ch.fritteli.maze.server.OutputType; import io.vavr.collection.Stream; @@ -33,6 +34,7 @@ class ParametersToMazeExtractor { final Option id = getParameterValue(RequestParameter.ID); final Option start = getParameterValue(RequestParameter.START); final Option end = getParameterValue(RequestParameter.END); + final Option algorithm = getParameterValue(RequestParameter.ALGORITHM); if (output.isEmpty()) { return Try.failure(new InvalidRequestParameterException("Path parameter %s is required and must be one of: %s".formatted( @@ -77,7 +79,10 @@ class ParametersToMazeExtractor { } else { maze = new Maze(desiredWidth, desiredHeight, id.getOrElse(() -> new Random().nextLong())); } - new RandomDepthFirst(maze).run(); + + algorithm.getOrElse(Algorithm.RANDOM_DEPTH_FIRST) + .createAlgorithm(maze) + .run(); return new GeneratedMaze(maze, output.get(), RandomDepthFirst.class.getSimpleName()); }); } diff --git a/src/main/java/ch/fritteli/maze/server/handler/RequestParameter.java b/src/main/java/ch/fritteli/maze/server/handler/RequestParameter.java index 3808313..5cdd893 100644 --- a/src/main/java/ch/fritteli/maze/server/handler/RequestParameter.java +++ b/src/main/java/ch/fritteli/maze/server/handler/RequestParameter.java @@ -1,6 +1,7 @@ package ch.fritteli.maze.server.handler; import ch.fritteli.maze.generator.model.Position; +import ch.fritteli.maze.server.Algorithm; import ch.fritteli.maze.server.OutputType; import io.vavr.Tuple2; import io.vavr.collection.HashMap; @@ -44,7 +45,9 @@ enum RequestParameter { return new Position(x, y); }) .toOption() - .onEmpty(() -> log.debug("Unparseable value for parameter 'end': '{}'", p)), "e", "end"); + .onEmpty(() -> log.debug("Unparseable value for parameter 'end': '{}'", p)), "e", "end"), + ALGORITHM(p -> Algorithm.ofString(p) + .onEmpty(() -> log.debug("Unparseable value for parameter 'algorithm': '{}'", p)), "a", "algorithm"); @NotNull private final Function> extractor; @Getter