Created
March 20, 2015 10:01
-
-
Save hackhat/2eddc2c9eeac0bbc75cb to your computer and use it in GitHub Desktop.
Example of a service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var constants = require('client/constants'); | |
var _ = require('lodash'); | |
var roomIdGenerator = require('client/logic/roomIdGenerator'); | |
/** | |
* Spawns a new player in the next available block. | |
* | |
* @param {Object} args | |
* @param {server.serviceCb} cb | |
*/ | |
module.exports = function(args, cb, session){ | |
// Should be stored on the player | |
var level = 0; | |
var player = this.s.m.Player.f_findById(session.loggedPlayerId).wait(); | |
if(player.stats.planetsOwnedN > 0) return cb('You own more than 0 planet.'); | |
var planets; | |
// Find an available block. | |
var block = this.s.m.Block.f_findOne({ | |
level : level, | |
openSlots : {$gte: constants.PLANETS_GIVEN_TO_NEW_PLAYERS}, | |
}).wait(); | |
// If no available block found, generate one. | |
if(!block){ | |
var lane = this.f_api('v1/lanes/findOrCreate', {x: level}).wait().lane; | |
var blockGenerateRes = this.f_api('v1/block/generate', { | |
x : level, | |
y : lane.y, | |
level : level, | |
}).wait(); | |
block = blockGenerateRes.block; | |
planets = blockGenerateRes.planets; | |
} | |
// If no planets yet, get them. | |
if(planets === void 0){ | |
planets = this.s.m.Planet.f_find({ | |
'block.id' : block._id, | |
'owner.username' : null, | |
suitableForSpawn : true, | |
}, null, { | |
limit : constants.PLANETS_GIVEN_TO_NEW_PLAYERS, | |
sort : {spawnOrder: 'asc'}, | |
}).wait(); | |
} | |
// Filter out not suitable planets. | |
planetsToGive = planets.filter(function(planet){ | |
return planet.suitableForSpawn && planet.spawnOrder !== void 0; | |
}) | |
// Sort by spawn order. | |
planetsToGive = _.sortBy(planets, 'spawnOrder'); | |
// Select the planets to give. | |
planetsToGive = planetsToGive.slice(0, constants.PLANETS_GIVEN_TO_NEW_PLAYERS); | |
planetsToGive.forEach(function(planet){ | |
// @todo: Use the object | |
planet.owner.username = player.username; | |
planet.owner.color = player.color; | |
planet.owner.colorB = player.colorB; | |
planet.owner.colorC = player.colorC; | |
planet.owner.isBot = player.bot.isBot; | |
planet.units = planet.getCapacity(); | |
planet.gold = Math.max(planet.gold, 5000); // At least 5K of gold. | |
planet.f_save().wait(); | |
}) | |
// Update the open slots in this block. | |
block.f_update({ | |
$inc: {openSlots: -planetsToGive.length} | |
}).wait(); | |
// Set player vars | |
player.lastPosition = planetsToGive[0].pos; | |
player.lastBlockId = block._id; | |
player.dateSpawned = new Date(); | |
player.stats.planetsOwnedN = planetsToGive.length; | |
player.unlockedBlocksIds = [block._id]; | |
player.unlockedBlocksIds.sort(); | |
player.ownsBlocksIds = [block._id]; | |
player.ownsBlocksIds.sort(); | |
player.f_save().wait(); | |
this.broadcastTo(roomIdGenerator.get('gameBlock', block._id), 'players.new', { | |
playerData : player, | |
planetsGiven : planetsToGive, | |
date : new Date(), | |
}) | |
cb(void 0, { | |
player : player, | |
planetsGiven : planetsToGive, | |
block : block | |
}); | |
} | |
module.exports.fibers = true; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment