Skip to content

Commit

Permalink
Moved the botManager to the cell controller
Browse files Browse the repository at this point in the history
  • Loading branch information
jondubois committed Jan 1, 2017
1 parent a6bc040 commit 59d8051
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 51 deletions.
29 changes: 13 additions & 16 deletions bot-manager.js
Expand Up @@ -8,7 +8,6 @@ var BOT_DEFAULT_CHANGE_DIRECTION_PROBABILITY = 0.01;
var BOT_DEFAULT_COLOR = 1000;

var BotManager = function (options) {
this.serverWorkerId = options.serverWorkerId;
this.worldWidth = options.worldWidth;
this.worldHeight = options.worldHeight;
this.botDiameter = options.botDiameter || BOT_DEFAULT_DIAMETER;
Expand All @@ -17,7 +16,6 @@ var BotManager = function (options) {
this.botColor = options.botColor || BOT_DEFAULT_COLOR;
this.botChangeDirectionProbability = options.botChangeDirectionProbability || BOT_DEFAULT_CHANGE_DIRECTION_PROBABILITY;

this.stateManager = options.stateManager;
this.bots = {};
this.botCount = 0;

Expand Down Expand Up @@ -46,11 +44,10 @@ BotManager.prototype.addBot = function (options) {
var radius = Math.round(diameter / 2);
var botId = uuid.v4();

var botData = {
var bot = {
id: botId,
type: 'player',
subtype: 'bot',
swid: this.serverWorkerId,
name: options.name || 'bot-' + Math.round(Math.random() * 10000),
color: options.color || this.botColor,
score: options.score || 0,
Expand All @@ -59,34 +56,34 @@ BotManager.prototype.addBot = function (options) {
width: diameter,
height: diameter,
changeDirProb: this.botChangeDirectionProbability,
op: {},
processed: Date.now()
op: {}
};
if (options.x && options.y) {
botData.x = options.x;
botData.y = options.y;
bot.x = options.x;
bot.y = options.y;
} else {
var position = this.generateRandomPosition(radius);
if (options.x) {
botData.x = options.x;
bot.x = options.x;
} else {
botData.x = position.x;
bot.x = position.x;
}
if (options.y) {
botData.y = options.y;
bot.y = options.y;
} else {
botData.y = position.y;
bot.y = position.y;
}
}
var bot = this.stateManager.create(botData);
this.bots[botId] = bot;

this.botCount++;

return bot;
};

BotManager.prototype.removeBot = function (botId) {
if (this.bots[botId]) {
this.stateManager.delete(this.bots[botId]);
var bot = this.bots[botId];
if (bot) {
bot.delete = 1;
delete this.bots[botId];
this.botCount--;
}
Expand Down
37 changes: 31 additions & 6 deletions cell.js
Expand Up @@ -38,6 +38,7 @@

var rbush = require('rbush');
var SAT = require('sat');
var BotManager = require('./bot-manager').BotManager;
var CoinManager = require('./coin-manager').CoinManager;

// This controller will be instantiated once for each
Expand All @@ -46,6 +47,35 @@ var CoinManager = require('./coin-manager').CoinManager;
var CellController = function (options) {
this.options = options;
this.cellIndex = options.cellIndex;

var cellData = options.cellData;

this.botManager = new BotManager({
worldWidth: options.worldWidth,
worldHeight: options.worldHeight,
botDiameter: options.botDiameter,
botMoveSpeed: options.botMoveSpeed,
botMass: options.botMass,
botColor: options.botColor,
botChangeDirectionProbability: options.botChangeDirectionProbability
});

if (!cellData.player) {
cellData.player = {};
}

for (var b = 0; b < options.botCount; b++) {
var bot = this.botManager.addBot();
cellData.player[bot.id] = bot;
}

this.botMoves = [
{u: 1},
{d: 1},
{r: 1},
{l: 1}
];

this.coinManager = new CoinManager({
cellData: options.cellData,
cellBounds: options.cellBounds,
Expand All @@ -55,12 +85,7 @@ var CellController = function (options) {
coinRadius: options.coinRadius
});
this.lastCoinDrop = 0;
this.botMoves = [
{u: 1},
{d: 1},
{r: 1},
{l: 1}
];

this.playerCompareFn = function (a, b) {
if (a.id < b.id) {
return -1;
Expand Down
1 change: 1 addition & 0 deletions coin-manager.js
Expand Up @@ -85,6 +85,7 @@ CoinManager.prototype.removeCoin = function (coinId) {
var coin = this.coins[coinId];
if (coin) {
coin.delete = 1;
delete this.coins[coinId];
this.coinCount--;
}
};
Expand Down
5 changes: 0 additions & 5 deletions public/index.html
Expand Up @@ -29,11 +29,6 @@
codecEngine: scCodecMinBin
});

socket.subscribe('debug-response').watch(function (data) {
console.log('DEBUG RESPONSE:', JSON.stringify(data, null, 2));
});


window.onload = function () {

// Note that this html file is set to pull down Phaser from our public/ directory.
Expand Down
4 changes: 3 additions & 1 deletion state-manager.js
Expand Up @@ -7,11 +7,13 @@ StateManager.prototype.create = function (state) {
var stateCellIndex = this.channelGrid.getCellIndex(state);
var stateRef = {
id: state.id,
swid: state.swid,
tcid: stateCellIndex, // Target cell index.
type: state.type,
create: state
};
if (state.swid != null) {
stateRef.swid = state.swid;
}
this.stateRefs[state.id] = stateRef;
return stateRef;
};
Expand Down
34 changes: 11 additions & 23 deletions worker.js
Expand Up @@ -6,7 +6,6 @@ var path = require('path');
var morgan = require('morgan');
var healthChecker = require('sc-framework-health-check');
var StateManager = require('./state-manager').StateManager;
var BotManager = require('./bot-manager').BotManager;
var uuid = require('uuid');
var ChannelGrid = require('./public/channel-grid').ChannelGrid;
var SAT = require('sat');
Expand Down Expand Up @@ -187,18 +186,6 @@ module.exports.run = function (worker) {
channelGrid: channelGrid
});

var botManager = new BotManager({
serverWorkerId: serverWorkerId,
worldWidth: WORLD_WIDTH,
worldHeight: WORLD_HEIGHT,
botDiameter: BOT_DIAMETER,
botMoveSpeed: BOT_MOVE_SPEED,
botMass: BOT_MASS,
botColor: BOT_COLOR,
botChangeDirectionProbability: BOT_CHANGE_DIRECTION_PROBABILITY,
stateManager: stateManager
});

if (WORLD_CELLS % worker.options.workers != 0) {
var errorMessage = 'The number of cells in your world (determined by WORLD_WIDTH, WORLD_HEIGHT, WORLD_CELL_WIDTH, WORLD_CELL_HEIGHT)' +
' should share a common factor with the number of workers or else the workload might get duplicated for some cells.';
Expand All @@ -225,13 +212,19 @@ module.exports.run = function (worker) {
cellIndex: cellIndex,
cellData: cellData[cellIndex],
cellBounds: channelGrid.getCellBounds(cellIndex),
worldWidth: WORLD_WIDTH,
worldHeight: WORLD_HEIGHT,
worldUpdateInterval: WORLD_UPDATE_INTERVAL,
coinPlayerNoDropRadius: COIN_PLAYER_NO_DROP_RADIUS,
coinMaxCount: Math.round(COIN_MAX_COUNT / WORLD_CELLS),
coinDropInterval: COIN_DROP_INTERVAL * WORLD_CELLS,
botCount: Math.round(BOT_COUNT / WORLD_CELLS),
coinRadius: COIN_RADIUS,
worldWidth: WORLD_WIDTH,
worldHeight: WORLD_HEIGHT,
worldUpdateInterval: WORLD_UPDATE_INTERVAL,
botDiameter: BOT_DIAMETER,
botMoveSpeed: BOT_MOVE_SPEED,
botMass: BOT_MASS,
botColor: BOT_COLOR,
botChangeDirectionProbability: BOT_CHANGE_DIRECTION_PROBABILITY,
playerMoveSpeed: PLAYER_MOVE_SPEED
});

Expand Down Expand Up @@ -296,8 +289,6 @@ module.exports.run = function (worker) {
return groupA.leader.id <= groupB.leader.id;
}

// TODO: Fix issue with blinking player/bots
// when one member in the group disconnects.
function getStateGroups() {
var groupMap = {};
Object.keys(cellData).forEach(function (cellIndex) {
Expand Down Expand Up @@ -663,6 +654,7 @@ module.exports.run = function (worker) {
}
workerStateRefList[swid].push(stateRef);
}
state.processed = Date.now();
}

if (state.delete) {
Expand Down Expand Up @@ -811,6 +803,7 @@ module.exports.run = function (worker) {

stateIds.forEach(function (id) {
var state = game.stateRefs[id];
// Don't include bots.
stateList.push(state);
});

Expand All @@ -837,11 +830,6 @@ module.exports.run = function (worker) {

setInterval(processInputStates, WORLD_UPDATE_INTERVAL);

var botsPerWorker = Math.round(BOT_COUNT / worker.options.workers);
for (var b = 0; b < botsPerWorker; b++) {
botManager.addBot();
}

/*
In here we handle our incoming realtime connections and listen for events.
*/
Expand Down

0 comments on commit 59d8051

Please sign in to comment.