db.Bookmark.find({parent: id}, function (err, data) { if (err) { callback({error: true, message: Constants.Error.Bookmark.DELETE_FAILED}); } else if (data.length > 0) { var childFolders = _.where(data, {isFolder: true}); var childBookmarks = _.where(data, {isFolder: false}); _.each(childBookmarks, function (bookmark) { removeBookmark(bookmark._id); }); _.each(childFolders, function (childFolder) { deleteFolder(childFolder._id); removeBookmark(childFolder._id); }); removeBookmark(id); callback(Constants.Success.Bookmark.DELETED); } else { db.Bookmark.remove({_id: id}, function (err, data) { if (err) { callback({error: true, message: Constants.Error.Bookmark.DELETE_FAILED}); } else { callback(Constants.Success.Bookmark.DELETED); } }); } });
getAttackActions: function() {//(turnTime, battle) 'use strict'; var actions = [], self = this, enemiesInRange, enemyToAttack; if (!this.onCooldown && !this.moving && !this.dizzy) {//attack ready enemiesInRange = _.filter(this.ship.units, function(u) { return u.isAlive() && self.isEnemy(u) && self.isInRange(u); }); if (this.targetID !== null && this.targetID !== undefined) { //if targetID is set, it has attack priority enemyToAttack = _.where(enemiesInRange, {id: this.targetID})[0] || enemiesInRange[0]; } else { enemyToAttack = enemiesInRange[0]; } if (enemyToAttack) { actions.push(new act.Attack({ attackerID: self.id, receiverID: enemyToAttack.id, damage: self.meleeDamage, duration: self.attackCooldown })); } } return actions; },
scope.saveChanges = function () { var params = { condition: { _id: scope.form._id }, reference: { name: scope.form.name, form: scope.form.inputs, acceptance_cycle: __.where( scope.tags, {tag: scope.nameTagSelected})[0].acceptance_cycle, uploader: Auth.user.username, description: scope.form.description, associated_documents: scope.form.documents } }; params.reference.acceptance_cycle.tag = scope.nameTagSelected; Forms.updateForm( params ) .success(function (data) { scope.alerts.push({ type: 'success', message: 'Formulario actualizado correctamente' }); }) .error(function (data) { console.log(data); }); };
getStaticShipData: function(ship) { var data = {}; data.weaponConsoles = _.filter(ship.built, function(item) { return item.type === 'Console' && item.getControlled().type === 'Weapon'; }); data.teleporters = _.where(ship.built, {type: 'Teleporter'}); data.teleporterTiles = {}; _.each(data.teleporters, function(tel) { var outerTiles = [ {x: tel.x - 1, y: tel.y - 1}, {x: tel.x, y: tel.y - 1}, {x: tel.x + 1, y: tel.y - 1}, {x: tel.x + 2, y: tel.y - 1}, {x: tel.x - 1, y: tel.y}, {x: tel.x + 2, y: tel.y}, {x: tel.x - 1, y: tel.y + 1}, {x: tel.x + 2, y: tel.y + 1}, {x: tel.x, y: tel.y + 2}, {x: tel.x - 1, y: tel.y + 2}, {x: tel.x + 1, y: tel.y + 2}, {x: tel.x + 2, y: tel.y + 2} ]; data.teleporterTiles[tel.id] = _.filter(outerTiles, function(tile) { return ship.isInside(tile.x, tile.y); }); }); return data; },
cli.on("cli:go", function () { if(!fs.existsSync(configFile)) { logger.warn('SimpleSim configuration file missing, re-run simplesim.'); return; } simplesim = require(configFile); if(cli && cli.globalContext && cli.globalContext.argv) { if(cli.globalContext.argv.C || cli.globalContext.argv['device-id']) { deviceId = cli.globalContext.argv.C || cli.globalContext.argv['device-id']; } if(cli.globalContext.argv.p || cli.globalContext.argv.platform) { buildPlatform = cli.globalContext.argv.p || cli.globalContext.argv.platform; } else { //try to detect the platform from the simplesim config file _.each(platforms, function(p, key) { if (_.find(simplesim[p],{ alias: deviceId})) { cli.argv.$_.push('-p'); cli.argv.$_.push(key); cli.globalContext.argv.p = key; buildPlatform = key; } }); } // grab the right sim/emu sim = _.where(simplesim[platforms[buildPlatform]], { alias: deviceId}); if(!sim || sim.length === 0) { return; } if(cli.argv.$_.indexOf(deviceId) === -1) { return; } cli.argv.$_[cli.argv.$_.indexOf(deviceId)] = sim[0].udid; cli.globalContext.argv.C = sim[0].udid; } });
async.map(os, (o, cb)=> { if (o.date === _date) { let orderTimes = [], ids = _.pluck(_.pluck(_.where(as, {_orderId: o._id}), 'arrTime'), '_id'); _.each(o.orderTimes, ot=> { ot._doc.times = _.reject(ot.times, time=>time < _time); if (!_.isEmpty(ot.times) && !_.contains(ids, ot._id)) { orderTimes.push(ot); } }); if (!_.isEmpty(orderTimes)) { o._doc.orderTimes = orderTimes; cb(null, o); } else { cb(null, null); } } else { cb(null, o); } }, (err, ret)=> err ? next(err) : res.json(_.compact(ret)));
var roomnameFilter = function(roomToFilter) { return _.where(messages, {roomname: roomToFilter}); };
console.log(_.filter([1,2,3,4,5,6], function(num){ return num % 2 == 0; })); // reject: 过滤集合中不符合条件的元素 console.log(_.reject([1,2,3,4,5,6], function(num){ return num % 2 == 0; })); // where: 遍历list, 返回新的对象数组 var list = [ {title: 'aaa', year: 1982}, {title: 'bbb', year: 1990}, {title: 'ccc', year: 1982} ]; console.log(_.where(list, {year: 1982})); // contains: 判断元素是否在list中 console.log(_.contains([1,2,3,4], 8)); // invoke: 在list里的每个元素上调用名为methodName的函数,任何附加的函数传入,invoke将会转给调用的函数 var v = _.invoke([[2,1,3], [4,5,2]], 'sort') console.log("v", v); // pluck: 提取一个集合里指定的属性值 var users = [ {name:'moe', age: 40}, {name:'larry', age: 50} ]; console.log(_.pluck(users, 'name')); // max, min, sortBy:取list中的最大,最小元素,自定义比较器
/** * Generates a "script" for the units given all the orders issued. * @param {sh.OrderCollection} orderCollection * @param {sh.Battle} battle * @param {Boolean} resetBattle Should the battle be cleaned up at the end. * @return {sh.Script} */ function createScript(orderCollection, battle, resetBattle) { var script, queue, changes, time, actors, actor, i, registerActionReturned = {}, turnDuration = battle.turnDuration, changesAtSameTime = []; script = new Script({turnDuration: turnDuration}); queue = []; function insertInQueue(item) { insertByTime(queue, item); } function registerAction(returned, time) { return function(action) { action.time = time; action.updateModelChanges(); script.actions.push(action); _.each(action.modelChanges, function(mc, index) { if (mc.time >= 0) { //Add actionIndex and index used by script.registerChange mc.actionIndex = script.actions.length - 1; mc.index = index; if (mc.time === action.time) { //apply immediate changes mc.apply(battle); script.registerChange(mc); returned.immediateChanges.push(action.toString()); } else { insertInQueue(mc); } } }); }; } //set the orders to the units battle.insertOrders(orderCollection); //null change to kick-start the process queue.push(getVoidModelChange(0)); _.each(battle.pendingActions, function(action) { registerAction({}, action.time - turnDuration)(action); }); //simulation loop (the battle gets modified and actions get added // to the script over time) while (queue.length > 0 && queue[0].time <= turnDuration) { time = queue[0].time; changes = _.where(queue, {time: time}); _.invoke(changes, 'apply', battle); _.each(changes, script.registerChange, script); queue = queue.slice(changes.length); if (time < turnDuration) { //actions can't start at end of turn registerActionReturned.immediateChanges = []; actors = battle.getActors(); for (i = 0; i < actors.length; i++) { actor = actors[i]; _.each(actor.getActions(time, battle), registerAction(registerActionReturned, time)); } if (registerActionReturned.immediateChanges.length > 0) { //If any actor returned any action with immediate model //changes, the loop enters again at the same time. changesAtSameTime.push( registerActionReturned.immediateChanges ); if (changesAtSameTime.length >= maxLoopsAtSameTime) { throw 'Too much model changes at the same time (' + time + 'ms). Changes stack: ' + changesAtSameTime .slice(changesAtSameTime.length - 11, changesAtSameTime.length - 1).toString() + ' ...'; } insertInQueue(getVoidModelChange(time)); } else { changesAtSameTime = []; } } } battle.pendingActions = _.chain(queue) .pluck('action') .uniq() .value(); script.pendingActionsJson = utils.mapToJson(battle.pendingActions); //clean up if (resetBattle) { battle.endOfTurnReset(); } return script; }