<commit_before>define([
    'jquery',
    'map_model',
    'selection_model',
    'action_model',
    'map_view'
  ],
function($, MapModel, SelectionModel, ActionModel, MapView) {
  // MVC implementation based on example at <https://alexatnet.com/articles/model-view-controller-mvc-javascript>

  var MapController = function(canvas) {
    this.model = {
      map: new MapModel([]),
      selection : new SelectionModel(),
      action : new ActionModel()
    };
    this.model.selection.doors = new SelectionModel();
    this.view = new MapView(this.model, canvas);

    // When actions are completed, apply the results to the map.
    var model = this.model;
    model.action.addListener(function(event, action, data) {

      // Did we just finish doing something?
      if (event === 'finish') {

        // Was it adding a room (only thing we know how to hande now?)
        if (action === 'add_room') {

          // Width and height may be negative.
          // If so, convert them so x, y is the top-left corner.
          var x = data.x, y = data.y, width = data.width, height = data.height;

          // Bail out if the room would have no area.
          if (width == 0 || height == 0) {
            // TODO: we'd like to cancel the event, but can't, as it's already finished.
            return;
          };

          if (width < 0) {
            width = -width;
            x = x - width;
          }
          if (height < 0) {
            height = -height;
            y = y - height;
          }

          model.map.addRoom(x, y, width, height);

        } else if (action === 'add_door') {

          // TODO: do I want to validate anything here?
          // TODO: should addDoor let me pass in the parent room?
          var worked = model.map.addDoor(data.x, data.y, data.direction);

          if (!worked) {
            // TODO: should I be using exceptions for this?
            console.warn('Failed to add door ' + data.direction + ' at (' + data.x + ', ' + data.y + ')');
          };

        } else {

          // We don't support whatever this is -- bail out.
          console.warn('Tried to finish unsupported action ' + action);
        }
      }
    });
  };
<commit_msg>Properly test for failing to add doors<commit_after>define([
    'jquery',
    'map_model',
    'selection_model',
    'action_model',
    'map_view'
  ],
function($, MapModel, SelectionModel, ActionModel, MapView) {
  // MVC implementation based on example at <https://alexatnet.com/articles/model-view-controller-mvc-javascript>

  var MapController = function(canvas) {
    this.model = {
      map: new MapModel([]),
      selection : new SelectionModel(),
      action : new ActionModel()
    };
    this.model.selection.doors = new SelectionModel();
    this.view = new MapView(this.model, canvas);

    // When actions are completed, apply the results to the map.
    var model = this.model;
    model.action.addListener(function(event, action, data) {

      // Did we just finish doing something?
      if (event === 'finish') {

        // Was it adding a room (only thing we know how to hande now?)
        if (action === 'add_room') {

          // Width and height may be negative.
          // If so, convert them so x, y is the top-left corner.
          var x = data.x, y = data.y, width = data.width, height = data.height;

          // Bail out if the room would have no area.
          if (width == 0 || height == 0) {
            // TODO: we'd like to cancel the event, but can't, as it's already finished.
            return;
          };

          if (width < 0) {
            width = -width;
            x = x - width;
          }
          if (height < 0) {
            height = -height;
            y = y - height;
          }

          model.map.addRoom(x, y, width, height);

        } else if (action === 'add_door') {

          // TODO: do I want to validate anything here?
          // TODO: should addDoor let me pass in the parent room?
          var worked = model.map.addDoor(data.x, data.y, data.direction);

           if (worked === false) {
            // TODO: should I be using exceptions for this?
            console.warn('Failed to add door ' + data.direction + ' at (' + data.x + ', ' + data.y + ')');
          };

        } else {

          // We don't support whatever this is -- bail out.
          console.warn('Tried to finish unsupported action ' + action);
        }
      }
    });
  };

{'fraction_non_alphanumeric': 0.12485964518302269, 'fraction_numerical': 0.001796541657309679, 'mean_word_length': 1.8154235145385589, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '2710277', 'n_tokens_mistral': 1305, 'n_tokens_neox': 1250, 'n_words': 433}