Skip to content

Instantly share code, notes, and snippets.

@tmzt

tmzt/.js

Created Jun 21, 2011
Embed
What would you like to do?
var server = ws.createServer();
server.on('connection', function(conn) {
conn.on('message', function(msg) {
console.log('client msg: ', msg);
try {
var data = JSON.parse(msg);
} catch(ex) { var data = null; };
if (data != null) {
var sessionID = data.sessionID;
var user_id = data.user_id;
getplayer(sessionID, user_id, function(player) {
if (player != null) {
player.socket = conn;
var observer = new WebsocketObserver({game: player.game, socket: conn});
};
});
};
});
});
var EventEmitter = require('events').EventEmitter;
var _ = require('underscore');
var models = require('models');
var mongoose = models.mongoose;
var User = mongoose.model('User');
var Village = mongoose.model('Village');
var Attack = mongoose.model('Attack');
var Villager = mongoose.model('Villager');
var Building = mongoose.model('Building');
var BuildOrder = mongoose.model('BuildOrder');
var MapOverlay = require('./game/mapoverlay');
var Player = function(options) { _.extend(this,options); this.initialize(); };
_.extend(Player.prototype, {
initialize: function() {
console.log('player sid: ', this.sid);
console.log('player sessionID: ', this.sessionID);
console.log('player user_id: ', this.user_id);
this.game = new Game({sid: this.sid, user_id: this.user_id });
},
lookup: function(cb) {
var player = this;
User.findById(player.user_id, function(err, user) {
console.log('err: ', err);
console.log('user: ' , user);
if (user != null) {
player.user = user;
console.log('player.user_id: ', player.user_id);
Village.findOne({user_id: player.user_id}, function(err, village) {
if (village != null) {
player.village = village;
player.village_id = village._id;
console.log('village_id: ', village._id);
cb(player);
} else { cb(null); }
});
} else { cb(null); };
});
},
});
module.exports.Player = Player;
var Game = function(options) { _.extend(this, options); this.initialize(); };
Game.prototype = new EventEmitter;
_.extend(Game.prototype, {
initialize: function() {
this.regionsMapOverlay = new MapOverlay({game: this});
this.buildOrderObserver = new BuildOrderObserver({game: this});
},
buildOrder: function(order,cb) {
console.log('building order: ', order);
var building = new Building({
user_id: this.user_id,
village_id: this.village_id,
type: order.type, x: order.x, y: order.y,
level: 1,
completion: 0,
});
building.save();
this.emit('buildStarted', {building: building, completeLevel: building.level});
cb(true, building);
},
destroyOrder: function(order) {
console.log('destroy order: ', order);
},
upgradeOrder: function(order) {
console.log('upgrade order: ', order);
},
deployOrder: function(order) {
console.log('deploy order: ', order);
},
collectOrder: function(order) {
console.log('collect order: ', order);
},
attackOrder: function(order) {
console.log('attack order: ', order);
},
});
module.exports.Game = Game;
var BuildOrderObserver = function(options) { _.extend(this,options); this.initialize(); };
BuildOrderObserver.prototype = new EventEmitter;
_.extend(BuildOrderObserver.prototype, {
initialize: function() {
this.game.on('buildStarted', this.buildStarted);
},
buildStarted: function(data) {
var observer = this;
var building = data.building;
console.log('build started on building: ', building);
console.log('data: ', data);
/*
if (data.startLevel == null) startLevel = 0;
var startCompletionTime = Math.exp(startLevel+2);
var completeLevel = data.completeLevel;
var endCompletionTime = Math.exp(completeLevel+2);
var completionTime = endCompletionTime - startCompletionTime;
*/
var completeLevel = data.completeLevel;
if (data.startLevel == null) {
startLevel = 0;
completeLevel = 0;
};
var completionTime = Math.exp(completeLevel+2);
var startedAt = (new Date()).getTime();
var completeTime = startedAt + completionTime;
console.log('startedAt: ', startedAt);
console.log('completeTime: ', completeTime);
var order = new BuildOrder({
building_id: building._id,
startedAt: startedAt,
completeTime: completeTime,
completeLevel: completeLevel,
});
order.save(function(err,result){
console.log('save order: ', result);
if (result != null) {
/*
observer.emit('buildTimerStarted', {
buildorder_id: result._id,
startedAt: result.startedAt,
completeTime: result.completeTime
});
*/
};
});
observer.emit('buildTimerStarted', {building_id: building._id, startedAt: startedAt, completeTime: completeTime});
},
});
module.exports.BuildOrderObserver = BuildOrderObserver;
var WebsocketObserver = function(options) { _.extend(this,options); this.initialize(); };
_.extend(WebsocketObserver.prototype, {
initialize: function() {
console.log('[websocketobserver]');
var observer = this;
if (observer.socket != null && observer.game != null) {
var game = observer.game;
//game.websocketObserver = observer;
if (game.buildOrderObserver != null) {
var buildOrderObserver = game.buildOrderObserver;
console.log('watching buildOrderObserver: ', buildOrderObserver);
console.log('events: ', buildOrderObserver._events);
buildOrderObserver.on('buildTimerStarted', function(data) {
console.log('buildTimerStarted: ', data);
game.socket.send(JSON.stringify({event: 'buildTimerStarted', data: data}));
});
console.log('events: ', buildOrderObserver._events);
};
};
},
});
module.exports.WebsocketObserver = WebsocketObserver;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment