diff --git a/src/main/java/com/comandante/creeper/managers/GameManager.java b/src/main/java/com/comandante/creeper/managers/GameManager.java index b57e42e8200c8b1904ea4878e30783cdc4907c73..5d2ec2dc73750edf6d216b9e82a73ab6e19642b8 100644 --- a/src/main/java/com/comandante/creeper/managers/GameManager.java +++ b/src/main/java/com/comandante/creeper/managers/GameManager.java @@ -9,6 +9,7 @@ import com.comandante.creeper.npc.Npc; import com.comandante.creeper.player.Player; import com.comandante.creeper.player.PlayerManager; import com.comandante.creeper.player.PlayerMovement; +import com.comandante.creeper.room.MapMaker; import com.comandante.creeper.room.Room; import com.comandante.creeper.room.RoomManager; import com.comandante.creeper.server.ChannelUtils; @@ -24,7 +25,9 @@ import java.util.Iterator; import java.util.Map; import java.util.Set; -import static com.comandante.creeper.server.Color.*; +import static com.comandante.creeper.server.Color.BOLD_OFF; +import static com.comandante.creeper.server.Color.BOLD_ON; +import static com.comandante.creeper.server.Color.RESET; public class GameManager { @@ -43,6 +46,7 @@ public class GameManager { private final EntityManager entityManager; private final ItemDecayManager itemDecayManager; private final FightManager fightManager; + private final MapMaker mapMaker; public GameManager(RoomManager roomManager, PlayerManager playerManager, EntityManager entityManager) { this.roomManager = roomManager; @@ -53,6 +57,11 @@ public class GameManager { this.newUserRegistrationManager = new NewUserRegistrationManager(playerManager); this.channelUtils = new ChannelUtils(getPlayerManager(), getRoomManager()); this.fightManager = new FightManager(channelUtils, entityManager, playerManager); + this.mapMaker = new MapMaker(roomManager); + } + + public MapMaker getMapMaker() { + return mapMaker; } public FightManager getFightManager() { @@ -80,7 +89,6 @@ public class GameManager { } - public PlayerManager getPlayerManager() { return playerManager; } @@ -225,7 +233,7 @@ public class GameManager { sb.append(RESET); //java.lang.String wrap(java.lang.String str, int wrapLength, java.lang.String newLineStr, boolean wrapLongWords) sb.append(WordUtils.wrap(playerCurrentRoom.getRoomDescription(), 80, "\r\n", true)).append("\r\n"); - + sb.append(getMapMaker().drawMap(playerCurrentRoom.getRoomId())).append("\r\n"); sb.append(getExits(playerCurrentRoom, player)); for (String searchPlayerId : playerCurrentRoom.getPresentPlayerIds()) { if (searchPlayerId.equals(player.getPlayerId())) { @@ -248,7 +256,6 @@ public class GameManager { Npc npcEntity = entityManager.getNpcEntity(npcId); sb.append("a ").append(npcEntity.getColorName()).append(" is here.\r\n"); } - sb.append("\r\n"); channelUtils.write(player.getPlayerId(), sb.toString()); } diff --git a/src/main/java/com/comandante/creeper/room/MapMaker.java b/src/main/java/com/comandante/creeper/room/MapMaker.java new file mode 100644 index 0000000000000000000000000000000000000000..19ce3383443f8e31ba4021f506e258b5426bcb71 --- /dev/null +++ b/src/main/java/com/comandante/creeper/room/MapMaker.java @@ -0,0 +1,205 @@ +package com.comandante.creeper.room; + + +import com.comandante.creeper.server.Color; +import com.google.common.base.Function; +import com.google.common.base.Optional; +import com.google.common.base.Predicate; +import com.google.common.collect.FluentIterable; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterators; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +public class MapMaker { + + private final RoomManager roomManager; + List<List<Optional<Room>>> fullMatrix; + + public MapMaker(RoomManager roomManager) { + this.roomManager = roomManager; + } + + public String drawMap(Integer roomId) { + fullMatrix = getBlankMatrix(); + Room E4 = getRoom(roomId); + 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(); + for (Map<String, Integer> next : maps) { + 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) { + 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) { + 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) { + 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) { + 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) { + 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) { + 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) { + Iterator<Map.Entry<String, Integer>> iterator8 = next7.entrySet().iterator(); + ImmutableList<Map<String, Integer>> maps8 = FluentIterable.from(ImmutableList.copyOf(iterator8)).transform(getRoomProcessorFunction()).filter(getNonEmpty()).toList(); + } + } + } + } + } + } + } + + } + StringBuilder sb = new StringBuilder(); + for (List<Optional<Room>> next : fullMatrix) { + Iterator<String> transform = Iterators.transform(next.iterator(), getRendering(roomId)); + while (transform.hasNext()) { + String s = transform.next(); + sb.append(s); + } + sb.append("\r\n"); + } + return sb.toString(); + } + + public Function<Optional<Room>, String> getRendering(final Integer currentroomId) { + return new Function<Optional<Room>, String>() { + @Override + public String apply(Optional<Room> roomOptional) { + if (roomOptional.isPresent()) { + if (roomOptional.get().getRoomId().equals(currentroomId)) { + return "[" + Color.BOLD_ON + Color.RED + "*" + Color.RESET + "]"; + } else { + return "[ ]"; + } + } else { + return " - "; + } + } + }; + } + + public Function<Map.Entry<String, Integer>, Map<String, Integer>> getRoomProcessorFunction() { + return new Function<Map.Entry<String, Integer>, Map<String, Integer>>() { + @Override + public Map<String, Integer> apply(Map.Entry<String, Integer> stringIntegerEntry) { + Integer roomId = stringIntegerEntry.getValue(); + String coords = stringIntegerEntry.getKey(); + String[] split = coords.split("\\|"); + int row; + int columnNumber; + try { + row = Integer.parseInt(split[0]); + columnNumber = Integer.parseInt(split[1]); + } catch (Exception e) { + return Maps.newHashMap(); + } + if (columnNumber < 0 || columnNumber > 7) { + return Maps.newHashMap(); + } + if (row < 0 || row > 7) { + return Maps.newHashMap(); + } else { + setCoordinateRoom(coords, getRoom(stringIntegerEntry.getValue())); + return getRoomIds(roomId, coords); + } + } + }; + } + + public Predicate<Map<String, Integer>> getNonEmpty() { + return new Predicate<Map<String, Integer>>() { + @Override + public boolean apply(Map<String, Integer> stringIntegerMap) { + if (stringIntegerMap.size() > 0) { + return true; + } + return false; + } + }; + } + + public Map<String, Integer> getRoomIds(Integer roomId, String identifier) { + Room room = getRoom(roomId); + String[] split = identifier.split("\\|"); + int row; + int columnNumber; + + row = Integer.parseInt(split[0]); + columnNumber = Integer.parseInt(split[1]); + + Map<String, Integer> roomIds = Maps.newHashMap(); + if (room.getNorthId().isPresent()) { + int b = row - 1; + roomIds.put(b + "|" + columnNumber, room.getNorthId().get()); + } + if (room.getSouthId().isPresent()) { + int b = row + 1; + roomIds.put(b + "|" + columnNumber, room.getSouthId().get()); + } + if (room.getEastId().isPresent()) { + int b = columnNumber + 1; + roomIds.put(row + "|" + b, room.getEastId().get()); + } + if (room.getWestId().isPresent()) { + int b = columnNumber - 1; + roomIds.put(row + "|" + b, room.getWestId().get()); + } + return roomIds; + } + + public void setCoordinateRoom(String coordinate, Room room) { + String[] split = coordinate.split("\\|"); + int row = Integer.parseInt(split[0]); + int column = Integer.parseInt(split[1]); + getRow(row).set(column, Optional.of(room)); + } + + public List<Optional<Room>> getRow(int row) { + 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(), + 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()); + + for (List<Optional<Room>> roomOpts : lists) { + for (int i = 0; i <= 7; i++) { + roomOpts.add(Optional.<Room>absent()); + } + } + return lists; + } +}