diff --git a/pom.xml b/pom.xml index 374c01bdee0e15a90f1a7e648d031aa8e71e0fce..52b4842c2ae2234b0a3030828ec6735278e02d79 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,11 @@ <artifactId>sirocco-text-table-formatter</artifactId> <version>1.0</version> </dependency> + <dependency> + <groupId>com.codahale.metrics</groupId> + <artifactId>metrics-core</artifactId> + <version>3.0.2</version> + </dependency> </dependencies> <build> diff --git a/src/main/java/com/comandante/creeper/room/MapMaker.java b/src/main/java/com/comandante/creeper/room/MapMaker.java index b2d3747c8fc6a117f7eb95e3e3630308f0dad2d1..69ec3e0bec8fa4e09eff51be413c2a5837905f5e 100644 --- a/src/main/java/com/comandante/creeper/room/MapMaker.java +++ b/src/main/java/com/comandante/creeper/room/MapMaker.java @@ -1,6 +1,8 @@ package com.comandante.creeper.room; +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.Timer; import com.comandante.creeper.server.Color; import com.google.common.base.Function; import com.google.common.base.Optional; @@ -19,53 +21,33 @@ import java.util.Map; public class MapMaker { private final RoomManager roomManager; - List<List<Optional<Room>>> fullMatrix; + private List<List<Optional<Room>>> fullMatrix; public MapMaker(RoomManager roomManager) { this.roomManager = roomManager; } public String drawMap(Integer roomId) { + MetricRegistry metricRegistry = new MetricRegistry(); + Timer timer = metricRegistry.timer("draw-map"); + final Timer.Context context = timer.time(); fullMatrix = getBlankMatrix(); Room E4 = getRoom(roomId); - int c = 0; Iterator<Map.Entry<String, Integer>> iterator = getRoomIds(E4.getRoomId(), "4|4").entrySet().iterator(); ImmutableList<Map<String, Integer>> maps = FluentIterable.from(ImmutableList.copyOf(iterator)) .transform(getRoomProcessorFunction()) .filter(getNonEmpty()) .toList(); + // TODO : Make this less embarrassing. for (Map<String, Integer> next : maps) { - c++; - Iterator<Map.Entry<String, Integer>> iterator1 = next.entrySet().iterator(); - ImmutableList<Map<String, Integer>> maps1 = FluentIterable.from(ImmutableList.copyOf(iterator1)).transform(getRoomProcessorFunction()).filter(getNonEmpty()).toList(); - for (Map<String, Integer> next1 : maps1) { - c++; - Iterator<Map.Entry<String, Integer>> iterator2 = next1.entrySet().iterator(); - ImmutableList<Map<String, Integer>> maps2 = FluentIterable.from(ImmutableList.copyOf(iterator2)).transform(getRoomProcessorFunction()).filter(getNonEmpty()).toList(); - for (Map<String, Integer> next2 : maps2) { - c++; - Iterator<Map.Entry<String, Integer>> iterator3 = next2.entrySet().iterator(); - ImmutableList<Map<String, Integer>> maps3 = FluentIterable.from(ImmutableList.copyOf(iterator3)).transform(getRoomProcessorFunction()).filter(getNonEmpty()).toList(); - for (Map<String, Integer> next3 : maps3) { - c++; - Iterator<Map.Entry<String, Integer>> iterator4 = next3.entrySet().iterator(); - ImmutableList<Map<String, Integer>> maps4 = FluentIterable.from(ImmutableList.copyOf(iterator4)).transform(getRoomProcessorFunction()).filter(getNonEmpty()).toList(); - for (Map<String, Integer> next4 : maps4) { - c++; - Iterator<Map.Entry<String, Integer>> iterator5 = next4.entrySet().iterator(); - ImmutableList<Map<String, Integer>> maps5 = FluentIterable.from(ImmutableList.copyOf(iterator5)).transform(getRoomProcessorFunction()).filter(getNonEmpty()).toList(); - for (Map<String, Integer> next5 : maps5) { - c++; - Iterator<Map.Entry<String, Integer>> iterator6 = next5.entrySet().iterator(); - ImmutableList<Map<String, Integer>> maps6 = FluentIterable.from(ImmutableList.copyOf(iterator6)).transform(getRoomProcessorFunction()).filter(getNonEmpty()).toList(); - for (Map<String, Integer> next6 : maps6) { - c++; - Iterator<Map.Entry<String, Integer>> iterator7 = next6.entrySet().iterator(); - ImmutableList<Map<String, Integer>> maps7 = FluentIterable.from(ImmutableList.copyOf(iterator7)).transform(getRoomProcessorFunction()).filter(getNonEmpty()).toList(); - for (Map<String, Integer> next7 : maps7) { - c++; - Iterator<Map.Entry<String, Integer>> iterator8 = next7.entrySet().iterator(); - ImmutableList<Map<String, Integer>> maps8 = FluentIterable.from(ImmutableList.copyOf(iterator8)).transform(getRoomProcessorFunction()).filter(getNonEmpty()).toList(); + for (Map<String, Integer> next1 : processMapCoordinates(next)) { + for (Map<String, Integer> next2 : processMapCoordinates(next1)) { + for (Map<String, Integer> next3 : processMapCoordinates(next2)) { + for (Map<String, Integer> next4 : processMapCoordinates(next3)) { + for (Map<String, Integer> next5 : processMapCoordinates(next4)) { + for (Map<String, Integer> next6 : processMapCoordinates(next5)) { + for (Map<String, Integer> next7 : processMapCoordinates(next6)) { + processMapCoordinates(next7); } } } @@ -73,7 +55,6 @@ public class MapMaker { } } } - } StringBuilder sb = new StringBuilder(); for (List<Optional<Room>> next : fullMatrix) { @@ -84,10 +65,20 @@ public class MapMaker { } sb.append("\r\n"); } - System.out.println("Count - " + c); + context.stop(); + System.out.println("avg map generation time: " + timer.getMeanRate()); return sb.toString(); } + public List<Map<String, Integer>> processMapCoordinates(Map<String, Integer> map) { + c++; + Iterator<Map.Entry<String, Integer>> mapIterator = map.entrySet().iterator(); + return FluentIterable. + from(ImmutableList.copyOf(mapIterator)). + transform(getRoomProcessorFunction()). + filter(getNonEmpty()).toList(); + } + public Function<Optional<Room>, String> getRendering(final Integer currentroomId) { return new Function<Optional<Room>, String>() { @Override @@ -185,21 +176,13 @@ public class MapMaker { return fullMatrix.get(row); } - - public void populateRowsWithEmpty() { - for (List<Optional<Room>> roomOpts : fullMatrix) { - for (int i = 0; i <= 7; i++) { - roomOpts.add(Optional.<Room>absent()); - } - } - } - private Room getRoom(Integer roomId) { return roomManager.getRoom(roomId); } public static List<List<Optional<Room>>> getBlankMatrix() { - ArrayList<List<Optional<Room>>> lists = Lists.<List<Optional<Room>>>newArrayList(Lists.<Optional<Room>>newArrayList(), + ArrayList<List<Optional<Room>>> lists = + Lists.<List<Optional<Room>>>newArrayList(Lists.<Optional<Room>>newArrayList(), Lists.<Optional<Room>>newArrayList(), Lists.<Optional<Room>>newArrayList(), Lists.<Optional<Room>>newArrayList(), Lists.<Optional<Room>>newArrayList(), Lists.<Optional<Room>>newArrayList(), Lists.<Optional<Room>>newArrayList(),