<commit_before>var _ = require('lodash'),
    utils =  require('../../../utils'),
    driver = utils.getDriver(),
    C = driver.constants;

module.exports = function(object, intent, scope) {
    const {roomObjects, roomTerrain, gameTime, bulk, eventLog, roomController} = scope;

    if(roomController) {
        if(!roomController.isPowerEnabled) {
            return;
        }
        if(roomController.user != object.user && roomController.safeMode > gameTime) {
            return;
        }
    }

    const powerInfo = C.POWER_INFO[intent.power];
    if(!powerInfo) {
        return;
    }
    const creepPower = object.powers[intent.power];
    let target;

    if(!creepPower || creepPower.level == 0 || creepPower.cooldownTime > gameTime) {
        return;
    }

    var ops = powerInfo.ops || 0;
    if(_.isArray(ops)) {
        ops = ops[creepPower.level-1];
    }

    if((object.ops || 0) < ops) {
        return;
    }

    if(powerInfo.range) {
        target = roomObjects[intent.id];
        if(!target) {
            return;
        }
        if(utils.dist(object, target) > powerInfo.range) {
            return;
        }
        var currentEffect = _.find(target.effects, i => i.power == intent.power);
        if(currentEffect && currentEffect.level > creepPower.level && currentEffect.endTime > gameTime) {
            return;
        }
    }

    var applyEffectOnTarget = false;

    switch(intent.power) {

        case C.PWR_GENERATE_OPS: {
            bulk.update(object, {
                [C.RESOURCE_OPS]: (object[C.RESOURCE_OPS] || 0) + powerInfo.effect[creepPower.level-1],
            });
            let sum = utils.calcResources(object);

            if (sum > object.energyCapacity) {
                require('./drop')(object, {
                    amount: Math.min(object[C.RESOURCE_OPS], sum - object.energyCapacity),
                    resourceType: C.RESOURCE_OPS
                }, scope);
            }
            break;
        }

        case C.PWR_OPERATE_SPAWN: {
            if(target.type != 'spawn') {
                return;
            }
            applyEffectOnTarget = true;
            break;
        }

        case C.PWR_OPERATE_TOWER: {
            if(target.type != 'tower') {
                return;
            }
            applyEffectOnTarget = true;
            break;
        }

        case C.PWR_OPERATE_STORAGE: {
            if(target.type != 'storage') {
                return;
            }
            applyEffectOnTarget = true;
            break;
        }

        case C.PWR_OPERATE_LAB: {
            if(target.type != 'lab') {
                return;
            }
            applyEffectOnTarget = true;
            break;
        }

        case C.PWR_OPERATE_EXTENSION: {
            if(target.type != 'storage' && target.type != 'terminal' && target.type !== 'container') {
                return;
            }
            var effect = _.find(target.effects, {power: C.PWR_DISRUPT_TERMINAL});
            if(effect && effect.endTime > gameTime) {
                return;
            }
            var energyLimit = powerInfo.effect[creepPower.level-1];
            var extensions = _.filter(roomObjects, {type: 'extension'});
            extensions.sort(utils.comparatorDistance(target));
            extensions.every((object) => {
                var energy = Math.min(energyLimit, target.energy, object.energyCapacity - object.energy);
                bulk.update(object, {energy: object.energy + energy});
                bulk.update(target, {energy: target.energy - energy});<commit_msg>refact(processor): change OPERATE_EXTENSION power behavior<commit_after>var _ = require('lodash'),
    utils =  require('../../../utils'),
    driver = utils.getDriver(),
    C = driver.constants;

module.exports = function(object, intent, scope) {
    const {roomObjects, roomTerrain, gameTime, bulk, eventLog, roomController} = scope;

    if(roomController) {
        if(!roomController.isPowerEnabled) {
            return;
        }
        if(roomController.user != object.user && roomController.safeMode > gameTime) {
            return;
        }
    }

    const powerInfo = C.POWER_INFO[intent.power];
    if(!powerInfo) {
        return;
    }
    const creepPower = object.powers[intent.power];
    let target;

    if(!creepPower || creepPower.level == 0 || creepPower.cooldownTime > gameTime) {
        return;
    }

    var ops = powerInfo.ops || 0;
    if(_.isArray(ops)) {
        ops = ops[creepPower.level-1];
    }

    if((object.ops || 0) < ops) {
        return;
    }

    if(powerInfo.range) {
        target = roomObjects[intent.id];
        if(!target) {
            return;
        }
        if(utils.dist(object, target) > powerInfo.range) {
            return;
        }
        var currentEffect = _.find(target.effects, i => i.power == intent.power);
        if(currentEffect && currentEffect.level > creepPower.level && currentEffect.endTime > gameTime) {
            return;
        }
    }

    var applyEffectOnTarget = false;

    switch(intent.power) {

        case C.PWR_GENERATE_OPS: {
            bulk.update(object, {
                [C.RESOURCE_OPS]: (object[C.RESOURCE_OPS] || 0) + powerInfo.effect[creepPower.level-1],
            });
            let sum = utils.calcResources(object);

            if (sum > object.energyCapacity) {
                require('./drop')(object, {
                    amount: Math.min(object[C.RESOURCE_OPS], sum - object.energyCapacity),
                    resourceType: C.RESOURCE_OPS
                }, scope);
            }
            break;
        }

        case C.PWR_OPERATE_SPAWN: {
            if(target.type != 'spawn') {
                return;
            }
            applyEffectOnTarget = true;
            break;
        }

        case C.PWR_OPERATE_TOWER: {
            if(target.type != 'tower') {
                return;
            }
            applyEffectOnTarget = true;
            break;
        }

        case C.PWR_OPERATE_STORAGE: {
            if(target.type != 'storage') {
                return;
            }
            applyEffectOnTarget = true;
            break;
        }

        case C.PWR_OPERATE_LAB: {
            if(target.type != 'lab') {
                return;
            }
            applyEffectOnTarget = true;
            break;
        }

        case C.PWR_OPERATE_EXTENSION: {
            if(target.type != 'storage' && target.type != 'terminal' && target.type !== 'container') {
                return;
            }
            var effect = _.find(target.effects, {power: C.PWR_DISRUPT_TERMINAL});
            if(effect && effect.endTime > gameTime) {
                return;
            }
            var extensions = _.filter(roomObjects, i => i.type == 'extension' && i.user == target.user && !i.off);
            var energyLimit = powerInfo.effect[creepPower.level-1] * _.sum(extensions, 'energyCapacity');
            extensions.sort(utils.comparatorDistance(target));
            extensions.every((extension) => {
                var energy = Math.min(energyLimit, target.energy, extension.energyCapacity - extension.energy);
                bulk.update(extension, {energy: extension.energy + energy});
                bulk.update(target, {energy: target.energy - energy});
{'fraction_non_alphanumeric': 0.12383561643835617, 'fraction_numerical': 0.0019178082191780822, 'mean_word_length': 1.6607142857142858, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 22, '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': '2882185', 'n_tokens_mistral': 2129, 'n_tokens_neox': 2054, 'n_words': 361}