<commit_before>const _ = require('underscore');

class AttachmentValidityCheck {
    constructor(game) {
        this.game = game;
        this.beingDiscarded = [];
    }

    enforceValidity() {
        let invalidAttachments = this.filterInvalidAttachments();
        let needsDiscard = _.difference(invalidAttachments, this.beingDiscarded);

        if(needsDiscard.length === 0) {
            return;
        }

        this.beingDiscarded = this.beingDiscarded.concat(needsDiscard);

        this.game.queueSimpleStep(() => {
            for(let [owner, cards] of this.groupAttachmentsByOwner(needsDiscard)) {
                owner.discardCards(cards, false);
            }

        });
        this.game.queueSimpleStep(() => {
            this.beingDiscarded = _.difference(this.beingDiscarded, needsDiscard);
        });
    }

    filterInvalidAttachments() {
        let attachmentsInPlay = this.game.filterCardsInPlay(card => card.parent && card.getType() === 'attachment' && !card.isBeingRemoved && !card.facedown);
        return attachmentsInPlay.filter(card => !card.controller.canAttach(card, card.parent));
    }

    groupAttachmentsByOwner(cards) {
        return cards.reduce((map, card) => {
            let cardsForOwner = map.get(card.owner) || [];
            map.set(card.owner, cardsForOwner.concat([card]));
            return map;
        }, new Map());
    }
}<commit_msg>Add message when discarding invalid attachments (#2259)<commit_after>const _ = require('underscore');

class AttachmentValidityCheck {
    constructor(game) {
        this.game = game;
        this.beingDiscarded = [];
    }

    enforceValidity() {
        let invalidAttachments = this.filterInvalidAttachments();
        let needsDiscard = _.difference(invalidAttachments, this.beingDiscarded);

        if(needsDiscard.length === 0) {
            return;
        }

        this.beingDiscarded = this.beingDiscarded.concat(needsDiscard);

        this.game.queueSimpleStep(() => {
            for(let [owner, cards] of this.groupAttachmentsByOwner(needsDiscard)) {
                owner.discardCards(cards, false, discarded => {
                    this.game.addMessage('{0} is forced to discard {1} due to being invalidly attached', owner, discarded);
                });
            }

        });
        this.game.queueSimpleStep(() => {
            this.beingDiscarded = _.difference(this.beingDiscarded, needsDiscard);
        });
    }

    filterInvalidAttachments() {
        let attachmentsInPlay = this.game.filterCardsInPlay(card => card.parent && card.getType() === 'attachment' && !card.isBeingRemoved && !card.facedown);
        return attachmentsInPlay.filter(card => !card.controller.canAttach(card, card.parent));
    }

    groupAttachmentsByOwner(cards) {
        return cards.reduce((map, card) => {
            let cardsForOwner = map.get(card.owner) || [];
            map.set(card.owner, cardsForOwner.concat([card]));
            return map;
        }, new Map());
    }
}
{'fraction_non_alphanumeric': 0.12958027981345768, 'fraction_numerical': 0.002664890073284477, 'mean_word_length': 2.5246478873239435, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, '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': '1198731', 'n_tokens_mistral': 886, 'n_tokens_neox': 830, 'n_words': 138}