<commit_before>'use strict';

// syntax: put [item] in [container] or put [item] [container]
//TODO: Change get to auto-put or auto-hold...

// When finding item to put in container:
// - Look at held items first.
// - Then look in the room at large.

// When finding container:
// - Look at worn containers first (inventory)
// - Then nested containers
// - Finally, look in room

const util        = require('util');
const _           = require('../src/helpers');
const CommandUtil = require('../src/command_util').CommandUtil;
const Broadcast   = require('../src/broadcast').Broadcast;

exports.command = (rooms, items, players, npcs, Commands) =>
  (args, player) => {
    args = args.trim();

    if (!args) {
      return player.warn('Put which item into which container?');
    }

    if (player.isInCombat()) {
      return player.warn('You cannot do that while fighting!');
    }

    const room   = rooms.getAt(player.getLocation());
    const toRoom = Broadcast.toRoom(room, player, null, players);

    const [ itemTarget, containerTarget ] = _.getTargets(args);

    const item      = findItem(itemTarget);
    const container = findContainer(containerTarget);

    if (!item)      { return player.warn(`Could not find ${itemTarget}.`); }
    if (!container) { return player.warn(`Could not find ${containerTarget}.`); }

    putInContainer(item, container);

    // -- helpers -- //TODO: Put in CommandUtil?

    function findItem(itemTarget) {
      return CommandUtil.findItemInRoom(items, itemTarget, room, player, true) || CommandUtil.findItemInInventory(itemTarget, player, true);
    }

    function findContainer(containerTarget) {
      if (!containerTarget || containerTarget === 'away') {
        return player.getContainerWithCapacity(items, item.getAttribute('size'));
      }
      const possibleContainersInInventory = player.getContainersWithCapacity(items, item.getAttribute('size'))
        .filter(item => item.hasKeyword(containerTarget));
      return possibleContainersInInventory[0] || CommandUtil.findItemInRoom(items, containerTarget, room, player, true);
    }

    function putInContainer(item, container) {
      container.addItem(item);
      
      if (item.isEquipped()) {
        item.setEquipped(false);
        const isDropping = true; //TODO: Come up with better param name...
        player.unequip(item, items, players, isDropping);
      }
      
      const holder = container.getHolder() || null;
      item.setHolder(holder);
      player.removeItem(item);
     
      item.setRoom(null);
      if (room) { room.removeItem(item); }

      const containerDesc = container.getShortDesc();
      const itemName = item.getShortDesc();
      toRoom({
        firstPartyMessage: 'You place the ' + itemName + ' into the ' + containerDesc + '.',
        thirdPartyMessage: player.getName() + ' places the ' + itemName + ' into the ' + containerDesc + '.'
      });
      
      player.emit('action', 1, items);

    }

  };
<commit_msg>Better messaging to player if they do not have a container that would work.<commit_after>'use strict';

// syntax: put [item] in [container] or put [item] [container]
//TODO: Change get to auto-put or auto-hold...

// When finding item to put in container:
// - Look at held items first.
// - Then look in the room at large.

// When finding container:
// - Look at worn containers first (inventory)
// - Then nested containers
// - Finally, look in room

const util        = require('util');
const _           = require('../src/helpers');
const CommandUtil = require('../src/command_util').CommandUtil;
const Broadcast   = require('../src/broadcast').Broadcast;

exports.command = (rooms, items, players, npcs, Commands) =>
  (args, player) => {
    args = args.trim();

    if (!args) {
      return player.warn('Put which item into which container?');
    }

    if (player.isInCombat()) {
      return player.warn('You cannot do that while fighting!');
    }

    const room   = rooms.getAt(player.getLocation());
    const toRoom = Broadcast.toRoom(room, player, null, players);

    const [ itemTarget, containerTarget ] = _.getTargets(args);

    const item      = findItem(itemTarget);
    const container = findContainer(containerTarget);

    if (!item)      { return player.warn(`Could not find ${itemTarget}.`); }
    if (!container) { return player.warn(`Could not find ${containerTarget} that would fit ${item.getShortDesc()} in it.`); }

    putInContainer(item, container);

    // -- helpers -- //TODO: Put in CommandUtil?

    function findItem(itemTarget) {
      return CommandUtil.findItemInRoom(items, itemTarget, room, player, true) || CommandUtil.findItemInInventory(itemTarget, player, true);
    }

    function findContainer(containerTarget) {
      console.log('keywords:', containerTarget)
      if (!containerTarget || containerTarget === 'away') {
        console.log("put stuff awayyyy");
        return player.getContainerWithCapacity(items, item.getAttribute('size'));
      }
      const possibleContainersInInventory = player.getContainersWithCapacity(items, item.getAttribute('size'))
        .filter(item => item.hasKeyword(containerTarget));
      console.log('possible inventory stuff: ', possibleContainersInInventory);
      return possibleContainersInInventory[0] || CommandUtil.findItemInRoom(items, containerTarget, room, player, true);
    }

    function putInContainer(item, container) {
      container.addItem(item);
      
      if (item.isEquipped()) {
        item.setEquipped(false);
        const isDropping = true; //TODO: Come up with better param name...
        player.unequip(item, items, players, isDropping);
      }
      
      const holder = container.getHolder() || null;
      item.setHolder(holder);
      player.removeItem(item);
     
      item.setRoom(null);
      if (room) { room.removeItem(item); }

      const containerDesc = container.getShortDesc();
      const itemName = item.getShortDesc();
      toRoom({
        firstPartyMessage: 'You place the ' + itemName + ' into the ' + containerDesc + '.',
        thirdPartyMessage: player.getName() + ' places the ' + itemName + ' into the ' + containerDesc + '.'
      });
      
      player.emit('action', 1, items);

    }

  };

{'fraction_non_alphanumeric': 0.13393143223325857, 'fraction_numerical': 0.0006408202499198974, 'mean_word_length': 3.0830608240680184, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '178482', 'n_tokens_mistral': 1770, 'n_tokens_neox': 1747, 'n_words': 474}