Skip to content

Instantly share code, notes, and snippets.

@totty90
Created September 8, 2014 16:19
Show Gist options
  • Save totty90/35e32c5b3442948a43bf to your computer and use it in GitHub Desktop.
Save totty90/35e32c5b3442948a43bf to your computer and use it in GitHub Desktop.
easy express example
define(function(require, exports, module){
return function(args, cb, session){
var constants = require('client/constants');
var async = require('async');
var utils = require('client/utils');
var planetsToGive = args.planetsToGive;
var blockId = args.blockId;
var player = args.player;
var blockId;
var _this = this;
async.series({
assignPlanets: function(cb){
if(planetsToGive){
_this.log('some planets have been provided')
cb()
}
else if(blockId !== void 0){
_this.log('requesting in this block ' + blockId)
_this.s.m.Planet.find({'block.id': blockId, 'owner.username': null}).limit(constants.PLANETS_GIVEN_TO_NEW_PLAYERS).exec(function(err, planets){
if(err) return cb(err);
if(!planets || planets.length === 0){
_this.log('no available planets found in this block')
return cb('no planets found');
}
_this.log('found planets in this block')
planetsToGive = planets;
blockId = planets[0].block.id;
cb();
})
}
else{
_this.log('no planets given, get some')
_this.runService('getNextAvailableBlockPlanets', null, function(err, data){
if(err){return cb(err)};
planetsToGive = data.availablePlanets;
blockId = data.availableBlockId;
cb();
})
}
},
givePlanetsToPlayer: function(cb){
if(!planetsToGive || planetsToGive.length === 0) return cb('no planets available found')
_this.log('planetsToGive', planetsToGive.map(function(p){return p._id}).join('; '))
async.map(planetsToGive, function(planet, cb){
planet.owner.username = player.username;
planet.owner.color = player.color;
planet.owner.colorB = player.colorB;
planet.owner.isBot = player.bot.isBot;
planet.units = planet.capacity;
planet.gold = Math.max(planet.gold, 5000);
planet.save(cb);
}, cb);
},
saveLastPlayerPosition: function(cb){
_this.log('planets given to user')
var PlanetGenerator = require('client/PlanetGenerator');
player.lastPosition = planetsToGive[0].pos;
player.dateSpawned = new Date();
player.planetsOwnedN = planetsToGive.length;
_this.log('saving player', JSON.stringify(player));
player.save(cb)
}
}, function(err, results){
if(err){
cb(err)
}else{
cb(null, {
player : player,
planetsGiven : planetsToGive,
blockId : blockId
});
}
})
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment