Created
February 21, 2016 06:28
-
-
Save nikhiljha/b557cf6f2737356e469e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 MapCollection, Player, _; | |
_ = require('underscore'); | |
MapCollection = require('../../models/mapCollection'); | |
Player = require('../../models/server/player'); | |
module.exports = function() { | |
var connectNeighbors; | |
connectNeighbors = (function(_this) { | |
return function(region) { | |
var i, len, neighbor, ref, results; | |
ref = region.neighbors; | |
results = []; | |
for (i = 0, len = ref.length; i < len; i++) { | |
neighbor = ref[i]; | |
results.push((function(neighbor) { | |
var connect; | |
connect = function() { | |
return _this.connect(neighbor, function(server) { | |
return neighbor.connection = server.connection; | |
}); | |
}; | |
if (neighbor.id > _this.id && (neighbor.connection == null)) { | |
return setTimeout(connect, 2000); | |
} else { | |
return connect(); | |
} | |
})(neighbor)); | |
} | |
return results; | |
}; | |
})(this); | |
this.on('region:after', (function(_this) { | |
return function(e, region, options) { | |
var broadcastPlayer, broadcastRemovePlayer; | |
region.globalPlayers = new MapCollection({ | |
indexes: [ | |
{ | |
key: 'username', | |
replace: true | |
} | |
], | |
cellSize: 16 | |
}); | |
broadcastPlayer = function(player, connector) { | |
var i, len, neighbor, p, ref, results; | |
p = _.pick(player, 'username', 'position', 'id'); | |
if (connector) { | |
p.connector = _.omit(player.connector, 'connection'); | |
} | |
ref = region.neighbors; | |
results = []; | |
for (i = 0, len = ref.length; i < len; i++) { | |
neighbor = ref[i]; | |
if (neighbor.connection != null) { | |
results.push(neighbor.connection.emit('player', region.world.id, p)); | |
} else { | |
results.push(void 0); | |
} | |
} | |
return results; | |
}; | |
broadcastRemovePlayer = function(player) { | |
var i, len, neighbor, p, ref, results; | |
p = _.pick(player, 'username', 'position', 'id'); | |
ref = region.neighbors; | |
results = []; | |
for (i = 0, len = ref.length; i < len; i++) { | |
neighbor = ref[i]; | |
if (neighbor.connection != null) { | |
results.push(neighbor.connection.emit('removePlayer', region.world.id, p)); | |
} else { | |
results.push(void 0); | |
} | |
} | |
return results; | |
}; | |
connectNeighbors(region); | |
region.players.on('insert:after', function(e, player) { | |
var broadcastInterval; | |
broadcastPlayer(player, true); | |
broadcastInterval = setInterval(function() { | |
return broadcastPlayer(player); | |
}, 5000); | |
return player.on('leave:after', function() { | |
clearInterval(broadcastInterval); | |
return broadcastRemovePlayer(player); | |
}); | |
}); | |
return region.on('remap:after', function(e) { | |
var i, len, player, ref, results; | |
connectNeighbors(region); | |
ref = region.players.models; | |
results = []; | |
for (i = 0, len = ref.length; i < len; i++) { | |
player = ref[i]; | |
results.push(broadcastPlayer(player, true)); | |
} | |
return results; | |
}); | |
}; | |
})(this)); | |
return this.on('peer.server', (function(_this) { | |
return function(e, server, connection) { | |
var i, j, len, len1, neighbor, ref, ref1, region; | |
ref = _this.regions.models; | |
for (i = 0, len = ref.length; i < len; i++) { | |
region = ref[i]; | |
ref1 = region.neighbors; | |
for (j = 0, len1 = ref1.length; j < len1; j++) { | |
neighbor = ref1[j]; | |
if (neighbor.id === server.id) { | |
neighbor.connection = connection; | |
break; | |
} | |
} | |
} | |
connection.on('player', function(regionId, p) { | |
var player; | |
region = _this.regions.get('world.id', regionId); | |
player = region.globalPlayers.get(p.id); | |
if (player == null) { | |
return _this.connect(p.connector, function(connector) { | |
player = new Player(p); | |
player.connector = connector; | |
return region.globalPlayers.insert(player); | |
}); | |
} else if (!region.players.get(p.id)) { | |
_.extend(player, p); | |
return player.emit('move'); | |
} | |
}); | |
return connection.on('removePlayer', function(regionId, p) { | |
var player; | |
region = _this.regions.get('world.id', regionId); | |
player = region.globalPlayers.get(p.id); | |
if (player != null) { | |
return region.globalPlayers.remove(p.id); | |
} | |
}); | |
}; | |
})(this)); | |
}; |
This file contains hidden or 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
}; | |
})(this)); | |
this.on('join:before', (function(_this) { | |
return function(e, player, options) { | |
player.loadedChunks = new GridCollection(player.loadedChunks); | |
player.sendChunk = sendChunk.bind(player); | |
return player.sendChunks = sendChunks.bind(player); | |
}; | |
})(this)); | |
this.on('join:after', function(e, player) { | |
player.sendChunks(); | |
player.on('ready:after', player.sendChunks); | |
player.on('moveChunk:after', player.sendChunks); | |
return player.on('leave:before', function() { | |
var chunk, j, len, now, ref, results; | |
now = Date.now(); | |
ref = player.region.chunkList; | |
results = []; | |
for (j = 0, len = ref.length; j < len; j++) { | |
chunk = ref[j]; | |
results.push(player.loadedChunks.set(now, chunk.x, chunk.z)); | |
} | |
return results; | |
}); | |
}); | |
return this.on('update:after', (function(_this) { | |
return function(e, data, lastUpdate) { | |
var chunk, i, j, len, r, ref, region, results; | |
ref = _this.regions.models; | |
results = []; | |
for (i = j = 0, len = ref.length; j < len; i = ++j) { | |
region = ref[i]; | |
r = data.regions[i]; | |
results.push((function() { | |
var k, len1, ref1, results1; | |
ref1 = region.chunkList; | |
results1 = []; | |
for (k = 0, len1 = ref1.length; k < len1; k++) { | |
chunk = ref1[k]; | |
results1.push((function(_this) { | |
return function(chunk) { | |
return region.chunks.getChunk(chunk.x, chunk.z, function(err, c) { | |
var col, updateChunk; | |
if (err != null) { | |
return _this.error(err); | |
} | |
if (c.lastUpdate >= lastUpdate) { | |
col = r.chunks[chunk.x]; | |
if (col == null) { | |
col = r.chunks[chunk.x] = {}; | |
} | |
updateChunk = col[chunk.z]; | |
if (updateChunk == null) { | |
updateChunk = col[chunk.z] = {}; | |
} | |
return updateChunk.lastUpdate = c.lastUpdate; | |
} | |
}); | |
}; | |
})(this)(chunk)); | |
} | |
return results1; | |
}).call(_this)); | |
} | |
return results; | |
}; | |
})(this)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment