Very simple way to specify the algorithm to use to generate mazes.
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
a613a92adb
commit
c4d707d64a
5 changed files with 62 additions and 4 deletions
|
@ -4,4 +4,9 @@ COPY target/maze-server-*.jar /app/
|
||||||
RUN rm /app/*-sources.jar
|
RUN rm /app/*-sources.jar
|
||||||
RUN mv /app/*.jar /app/app.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
|
||||||
|
|
2
pom.xml
2
pom.xml
|
@ -56,7 +56,7 @@
|
||||||
</distributionManagement>
|
</distributionManagement>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maze-generator.version>0.2.1</maze-generator.version>
|
<maze-generator.version>0.2.2-SNAPSHOT</maze-generator.version>
|
||||||
<maven-site-plugin.version>4.0.0-M8</maven-site-plugin.version>
|
<maven-site-plugin.version>4.0.0-M8</maven-site-plugin.version>
|
||||||
<undertow.version>2.3.18.Final</undertow.version>
|
<undertow.version>2.3.18.Final</undertow.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
45
src/main/java/ch/fritteli/maze/server/Algorithm.java
Normal file
45
src/main/java/ch/fritteli/maze/server/Algorithm.java
Normal file
|
@ -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<Maze, MazeGeneratorAlgorithm> creator;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@NotNull
|
||||||
|
private final List<String> names;
|
||||||
|
|
||||||
|
Algorithm(@NotNull final Function<Maze, MazeGeneratorAlgorithm> creator,
|
||||||
|
@NotNull final String... names) {
|
||||||
|
this.creator = creator;
|
||||||
|
this.names = List.of(names);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static Option<Algorithm> 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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,6 +3,7 @@ package ch.fritteli.maze.server.handler;
|
||||||
import ch.fritteli.maze.generator.algorithm.RandomDepthFirst;
|
import ch.fritteli.maze.generator.algorithm.RandomDepthFirst;
|
||||||
import ch.fritteli.maze.generator.model.Maze;
|
import ch.fritteli.maze.generator.model.Maze;
|
||||||
import ch.fritteli.maze.generator.model.Position;
|
import ch.fritteli.maze.generator.model.Position;
|
||||||
|
import ch.fritteli.maze.server.Algorithm;
|
||||||
import ch.fritteli.maze.server.InvalidRequestParameterException;
|
import ch.fritteli.maze.server.InvalidRequestParameterException;
|
||||||
import ch.fritteli.maze.server.OutputType;
|
import ch.fritteli.maze.server.OutputType;
|
||||||
import io.vavr.collection.Stream;
|
import io.vavr.collection.Stream;
|
||||||
|
@ -33,6 +34,7 @@ class ParametersToMazeExtractor {
|
||||||
final Option<Long> id = getParameterValue(RequestParameter.ID);
|
final Option<Long> id = getParameterValue(RequestParameter.ID);
|
||||||
final Option<Position> start = getParameterValue(RequestParameter.START);
|
final Option<Position> start = getParameterValue(RequestParameter.START);
|
||||||
final Option<Position> end = getParameterValue(RequestParameter.END);
|
final Option<Position> end = getParameterValue(RequestParameter.END);
|
||||||
|
final Option<Algorithm> algorithm = getParameterValue(RequestParameter.ALGORITHM);
|
||||||
|
|
||||||
if (output.isEmpty()) {
|
if (output.isEmpty()) {
|
||||||
return Try.failure(new InvalidRequestParameterException("Path parameter %s is required and must be one of: %s".formatted(
|
return Try.failure(new InvalidRequestParameterException("Path parameter %s is required and must be one of: %s".formatted(
|
||||||
|
@ -77,7 +79,10 @@ class ParametersToMazeExtractor {
|
||||||
} else {
|
} else {
|
||||||
maze = new Maze(desiredWidth, desiredHeight, id.getOrElse(() -> new Random().nextLong()));
|
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());
|
return new GeneratedMaze(maze, output.get(), RandomDepthFirst.class.getSimpleName());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package ch.fritteli.maze.server.handler;
|
package ch.fritteli.maze.server.handler;
|
||||||
|
|
||||||
import ch.fritteli.maze.generator.model.Position;
|
import ch.fritteli.maze.generator.model.Position;
|
||||||
|
import ch.fritteli.maze.server.Algorithm;
|
||||||
import ch.fritteli.maze.server.OutputType;
|
import ch.fritteli.maze.server.OutputType;
|
||||||
import io.vavr.Tuple2;
|
import io.vavr.Tuple2;
|
||||||
import io.vavr.collection.HashMap;
|
import io.vavr.collection.HashMap;
|
||||||
|
@ -44,7 +45,9 @@ enum RequestParameter {
|
||||||
return new Position(x, y);
|
return new Position(x, y);
|
||||||
})
|
})
|
||||||
.toOption()
|
.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
|
@NotNull
|
||||||
private final Function<String, Option<?>> extractor;
|
private final Function<String, Option<?>> extractor;
|
||||||
@Getter
|
@Getter
|
||||||
|
|
Loading…
Reference in a new issue