<commit_before>package com.comandante.creeper.command.admin;

import com.comandante.creeper.command.Command;
import com.comandante.creeper.managers.GameManager;
import com.comandante.creeper.player.PlayerMovement;
import com.comandante.creeper.player.PlayerRole;
import com.comandante.creeper.world.*;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class BuildCommand extends Command {

    final static List<String> validTriggers = Arrays.asList("build", "b");
    final static String description = "Build new rooms in the world.";
    final static String correctUsage = "build [n|s|e|w|enter <name>|notable <name>]";
    final static Set<PlayerRole> roles = Sets.newHashSet(PlayerRole.ADMIN);

    public BuildCommand(GameManager gameManager) {
        super(gameManager, validTriggers, description, correctUsage, roles);
    }

    @Override
    public synchronized void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
        configure(e);
        try {
            if (originalMessageParts.size() > 1) {
                String desiredBuildDirection = originalMessageParts.get(1);
                if (desiredBuildDirection.equalsIgnoreCase("notable")) {
                    if (originalMessageParts.size() > 2) {
                        String notableName = originalMessageParts.get(2);
                        currentRoom.addNotable(notableName, "Set a description with the description command. (description <notableName> <description>");
                        channelUtils.write(playerId, "Notable added.\r\n");
                        return;
                    } else {
                        channelUtils.write(playerId, "Need to specify a valid notable name.\r\n");
                        return;
                    }
                }
                if (desiredBuildDirection.equalsIgnoreCase("n") | desiredBuildDirection.equalsIgnoreCase("north")) {
                    if (!currentRoom.getNorthId().isPresent() && mapMatrix.isNorthernMapSpaceEmpty(currentRoom.getRoomId())) {
                        buildBasicRoomAndProcessAllExits(mapMatrix.getNorthCords(currentRoomCoords));
                        return;
                    }
                } else if (desiredBuildDirection.equalsIgnoreCase("s") | desiredBuildDirection.equalsIgnoreCase("south")) {
                    if (!currentRoom.getSouthId().isPresent() && mapMatrix.isSouthernMapSpaceEmpty(currentRoom.getRoomId())) {
                        buildBasicRoomAndProcessAllExits(mapMatrix.getSouthCoords(currentRoomCoords));
                        return;
                    }
                } else if (desiredBuildDirection.equalsIgnoreCase("e") | desiredBuildDirection.equalsIgnoreCase("east")) {
                    if (!currentRoom.getEastId().isPresent() && mapMatrix.isEasternMapSpaceEmpty(currentRoom.getRoomId())) {
                        buildBasicRoomAndProcessAllExits(mapMatrix.getEastCoords(currentRoomCoords));
                        return;
                    }
                } else if (desiredBuildDirection.equalsIgnoreCase("w") | desiredBuildDirection.equalsIgnoreCase("west")) {
                    if (!currentRoom.getWestId().isPresent() && mapMatrix.isWesternMapSpaceEmpty(currentRoom.getRoomId())) {
                        buildBasicRoomAndProcessAllExits(mapMatrix.getWestCoords(currentRoomCoords));
                        return;
                    }
                } else if (desiredBuildDirection.equalsIgnoreCase("u") | desiredBuildDirection.equalsIgnoreCase("up")) {
                    if (!currentRoom.getUpId().isPresent()) {
                        Integer newRoomId = findUnusedRoomId();
                        Integer newFloorId = findUnusedFloorId();
                        RemoteExit remoteExit = new RemoteExit(RemoteExit.Direction.UP, newRoomId, "");
                        RemoteExit returnRemoteExit = new RemoteExit(RemoteExit.Direction.DOWN, currentRoom.getRoomId(), "");
                        mapMatrix.addRemote(currentRoom.getRoomId(), remoteExit);
                        FloorModel newFloorModel = newFloorModel(newFloorId, newRoomId, currentRoom.getRoomId(), returnRemoteExit);
                        BasicRoom basicRoom = newBasic()
                                .setRoomId(newRoomId)
                                .setFloorId(newFloorId)
                                .setDownId(Optional.of(currentRoom.getRoomId()))
                                .createBasicRoom();
                        currentRoom.setUpId(Optional.of(newRoomId));
                        addNewRoomAndFloorAndMovePlayer(basicRoom, newFloorModel, Optional.empty());
                        return;
                    }
                } else if (desiredBuildDirection.equalsIgnoreCase("d") | desiredBuildDirection.equalsIgnoreCase("down")) {
                    if (!currentRoom.getDownId().isPresent()) {
                        Integer newRoomId = findUnusedRoomId();
                        Integer newFloorId = findUnusedFloorId();
                        RemoteExit remoteExit = new RemoteExit(RemoteExit.Direction.DOWN, newRoomId, "");
                        RemoteExit returnRemoteExit = new RemoteExit(RemoteExit.Direction.UP, currentRoom.getRoomId(), "");
                        mapMatrix.addRemote(currentRoom.getRoomId(), remoteExit);
                        FloorModel newFloorModel = newFloorModel(newFloorId, newRoomId, currentRoom.getRoomId(), returnRemoteExit);
                        BasicRoom basicRoom = newBasic()
                                .setRoomId(newRoomId)
                                .setFloorId(newFloorId)
                                .setUpId(Optional.of(currentRoom.getRoomId()))
                                .createBasicRoom();
                        currentRoom.setDownId(Optional.of(newRoomId));
                        addNewRoomAndFloorAndMovePlayer(basicRoom, newFloorModel, Optional.empty());
                        return;
                    }
                } else if (desiredBuildDirection.equalsIgnoreCase("enter")) {
                    String enterName = originalMessageParts.get(2);
                    Integer newRoomId = findUnusedRoomId();
                    Integer newFloorId = findUnusedFloorId();
                    RemoteExit remoteExit = new RemoteExit(RemoteExit.Direction.ENTER, newRoomId, enterName);
                    RemoteExit returnRemoteExit = new RemoteExit(RemoteExit.Direction.ENTER, currentRoom.getRoomId(), "Leave");
                    mapMatrix.addRemote(currentRoom.getRoomId(), remoteExit);
                    FloorModel newFloorModel = newFloorModel(newFloorId, newRoomId, currentRoom.getRoomId(), returnRemoteExit);
                    BasicRoom basicRoom = newBasic()
                            .setRoomId(newRoomId)
                            .setFloorId(newFloorId)
                            .addEnterExit(returnRemoteExit)
                            .createBasicRoom();
                    currentRoom.addEnterExit(remoteExit);
                    addNewRoomAndFloorAndMovePlayer(basicRoom, newFloorModel, Optional.of(returnRemoteExit));
                    return;
                }
                channelUtils.write(playerId, "Room already exists at that location.");
            }
        } finally

        {
            super.messageReceived(ctx, e);
        }

    }

    private FloorModel newFloorModel(Integer floorId, Integer newRoomId, Integer currentRoomId, RemoteExit remoteExit) {
        FloorModel newFloorModel = new FloorModel();
        RemoteExit.Direction returnDirection = remoteExit.getDirection();
        newFloorModel.setId(floorId);
        if (returnDirection.equals(RemoteExit.Direction.DOWN)) {
            newFloorModel.setRawMatrixCsv(Integer.toString(newRoomId) + "d|" + currentRoomId);
        } else if (returnDirection.equals(RemoteExit.Direction.UP)) {
            newFloorModel.setRawMatrixCsv(Integer.toString(newRoomId) + "u|" + currentRoomId);
        } else if (returnDirection.equals(RemoteExit.Direction.ENTER)) {
            newFloorModel.setRawMatrixCsv(Integer.toString(newRoomId));
        }
        newFloorModel.setName(UUID.randomUUID().toString());<commit_msg>bounds check<commit_after>package com.comandante.creeper.command.admin;

import com.comandante.creeper.command.Command;
import com.comandante.creeper.managers.GameManager;
import com.comandante.creeper.player.PlayerMovement;
import com.comandante.creeper.player.PlayerRole;
import com.comandante.creeper.world.*;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class BuildCommand extends Command {

    final static List<String> validTriggers = Arrays.asList("build", "b");
    final static String description = "Build new rooms in the world.";
    final static String correctUsage = "build [n|s|e|w|enter <name>|notable <name>]";
    final static Set<PlayerRole> roles = Sets.newHashSet(PlayerRole.ADMIN);

    public BuildCommand(GameManager gameManager) {
        super(gameManager, validTriggers, description, correctUsage, roles);
    }

    @Override
    public synchronized void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
        configure(e);
        try {
            if (originalMessageParts.size() > 1) {
                String desiredBuildDirection = originalMessageParts.get(1);
                if (desiredBuildDirection.equalsIgnoreCase("notable")) {
                    if (originalMessageParts.size() > 2) {
                        String notableName = originalMessageParts.get(2);
                        currentRoom.addNotable(notableName, "Set a description with the description command. (description <notableName> <description>");
                        channelUtils.write(playerId, "Notable added.\r\n");
                        return;
                    } else {
                        channelUtils.write(playerId, "Need to specify a valid notable name.\r\n");
                        return;
                    }
                }
                if (desiredBuildDirection.equalsIgnoreCase("n") | desiredBuildDirection.equalsIgnoreCase("north")) {
                    if (!currentRoom.getNorthId().isPresent() && mapMatrix.isNorthernMapSpaceEmpty(currentRoom.getRoomId())) {
                        buildBasicRoomAndProcessAllExits(mapMatrix.getNorthCords(currentRoomCoords));
                        return;
                    }
                } else if (desiredBuildDirection.equalsIgnoreCase("s") | desiredBuildDirection.equalsIgnoreCase("south")) {
                    if (!currentRoom.getSouthId().isPresent() && mapMatrix.isSouthernMapSpaceEmpty(currentRoom.getRoomId())) {
                        buildBasicRoomAndProcessAllExits(mapMatrix.getSouthCoords(currentRoomCoords));
                        return;
                    }
                } else if (desiredBuildDirection.equalsIgnoreCase("e") | desiredBuildDirection.equalsIgnoreCase("east")) {
                    if (!currentRoom.getEastId().isPresent() && mapMatrix.isEasternMapSpaceEmpty(currentRoom.getRoomId())) {
                        buildBasicRoomAndProcessAllExits(mapMatrix.getEastCoords(currentRoomCoords));
                        return;
                    }
                } else if (desiredBuildDirection.equalsIgnoreCase("w") | desiredBuildDirection.equalsIgnoreCase("west")) {
                    if (!currentRoom.getWestId().isPresent() && mapMatrix.isWesternMapSpaceEmpty(currentRoom.getRoomId())) {
                        buildBasicRoomAndProcessAllExits(mapMatrix.getWestCoords(currentRoomCoords));
                        return;
                    }
                } else if (desiredBuildDirection.equalsIgnoreCase("u") | desiredBuildDirection.equalsIgnoreCase("up")) {
                    if (!currentRoom.getUpId().isPresent()) {
                        Integer newRoomId = findUnusedRoomId();
                        Integer newFloorId = findUnusedFloorId();
                        RemoteExit remoteExit = new RemoteExit(RemoteExit.Direction.UP, newRoomId, "");
                        RemoteExit returnRemoteExit = new RemoteExit(RemoteExit.Direction.DOWN, currentRoom.getRoomId(), "");
                        mapMatrix.addRemote(currentRoom.getRoomId(), remoteExit);
                        FloorModel newFloorModel = newFloorModel(newFloorId, newRoomId, currentRoom.getRoomId(), returnRemoteExit);
                        BasicRoom basicRoom = newBasic()
                                .setRoomId(newRoomId)
                                .setFloorId(newFloorId)
                                .setDownId(Optional.of(currentRoom.getRoomId()))
                                .createBasicRoom();
                        currentRoom.setUpId(Optional.of(newRoomId));
                        addNewRoomAndFloorAndMovePlayer(basicRoom, newFloorModel, Optional.empty());
                        return;
                    }
                } else if (desiredBuildDirection.equalsIgnoreCase("d") | desiredBuildDirection.equalsIgnoreCase("down")) {
                    if (!currentRoom.getDownId().isPresent()) {
                        Integer newRoomId = findUnusedRoomId();
                        Integer newFloorId = findUnusedFloorId();
                        RemoteExit remoteExit = new RemoteExit(RemoteExit.Direction.DOWN, newRoomId, "");
                        RemoteExit returnRemoteExit = new RemoteExit(RemoteExit.Direction.UP, currentRoom.getRoomId(), "");
                        mapMatrix.addRemote(currentRoom.getRoomId(), remoteExit);
                        FloorModel newFloorModel = newFloorModel(newFloorId, newRoomId, currentRoom.getRoomId(), returnRemoteExit);
                        BasicRoom basicRoom = newBasic()
                                .setRoomId(newRoomId)
                                .setFloorId(newFloorId)
                                .setUpId(Optional.of(currentRoom.getRoomId()))
                                .createBasicRoom();
                        currentRoom.setDownId(Optional.of(newRoomId));
                        addNewRoomAndFloorAndMovePlayer(basicRoom, newFloorModel, Optional.empty());
                        return;
                    }
                } else if (desiredBuildDirection.equalsIgnoreCase("enter")) {
                    if (originalMessageParts.size() != 3) {
                        channelUtils.write(playerId, "Must specify a name for new \"enter\"");
                        return;
                    }
                    String enterName = originalMessageParts.get(2);
                    Integer newRoomId = findUnusedRoomId();
                    Integer newFloorId = findUnusedFloorId();
                    RemoteExit remoteExit = new RemoteExit(RemoteExit.Direction.ENTER, newRoomId, enterName);
                    RemoteExit returnRemoteExit = new RemoteExit(RemoteExit.Direction.ENTER, currentRoom.getRoomId(), "Leave");
                    mapMatrix.addRemote(currentRoom.getRoomId(), remoteExit);
                    FloorModel newFloorModel = newFloorModel(newFloorId, newRoomId, currentRoom.getRoomId(), returnRemoteExit);
                    BasicRoom basicRoom = newBasic()
                            .setRoomId(newRoomId)
                            .setFloorId(newFloorId)
                            .addEnterExit(returnRemoteExit)
                            .createBasicRoom();
                    currentRoom.addEnterExit(remoteExit);
                    addNewRoomAndFloorAndMovePlayer(basicRoom, newFloorModel, Optional.of(returnRemoteExit));
                    return;
                }
                channelUtils.write(playerId, "Room already exists at that location.");
            }
        } finally {
            super.messageReceived(ctx, e);
        }

    }

    private FloorModel newFloorModel(Integer floorId, Integer newRoomId, Integer currentRoomId, RemoteExit remoteExit) {
        FloorModel newFloorModel = new FloorModel();
        RemoteExit.Direction returnDirection = remoteExit.getDirection();
        newFloorModel.setId(floorId);
        if (returnDirection.equals(RemoteExit.Direction.DOWN)) {
            newFloorModel.setRawMatrixCsv(Integer.toString(newRoomId) + "d|" + currentRoomId);
        } else if (returnDirection.equals(RemoteExit.Direction.UP)) {
            newFloorModel.setRawMatrixCsv(Integer.toString(newRoomId) + "u|" + currentRoomId);
        } else if (returnDirection.equals(RemoteExit.Direction.ENTER)) {
            newFloorModel.setRawMatrixCsv(Integer.toString(newRoomId));
        }
        newFloorModel.setName(UUID.randomUUID().toString());
{'fraction_non_alphanumeric': 0.09347050914830186, 'fraction_numerical': 0.0006430116326649909, 'mean_word_length': 2.0725574712643677, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1464193', 'n_tokens_mistral': 4288, 'n_tokens_neox': 4097, 'n_words': 668}