Permalink
Browse files

impremented duel matching

  • Loading branch information...
supercaracal committed May 12, 2012
1 parent d9e3ee0 commit baef95eaf4dda97bd8bb74990091aa08c03e8565
Showing with 212 additions and 97 deletions.
  1. +67 −0 app/duel-matching-controller.js
  2. +75 −0 app/duel-messenger.js
  3. +45 −0 app/duelist.js
  4. +7 −54 app/messenger.js
  5. +15 −0 app/routing.js
  6. +0 −22 js/duel-shooting.js
  7. +1 −5 js/sync/synchronizer.js
  8. +0 −14 routing.js
  9. +2 −2 web.js
@@ -0,0 +1,67 @@
+function DuelMatchingController() {
+ setInterval(this.match.bind(this), 100);
+}
+
+DuelMatchingController.prototype.duelist = require('./duelist');
+
+DuelMatchingController.prototype.whiteQueue = [];
+DuelMatchingController.prototype.redQueue = [];
+
+DuelMatchingController.prototype.duelists = {};
+DuelMatchingController.prototype.bugoutFuckers = {};
+
+DuelMatchingController.prototype.add = function(id) {
+ var duelist = new this.duelist(id);
+ var isWhite = !this.whiteQueue.length || (this.whiteQueue.length <= this.redQueue.length);
+ isWhite ? duelist.setColorWhite() : duelist.setColorRed();
+ isWhite ? this.whiteQueue.push(duelist) : this.redQueue.push(duelist);
+};
+
+DuelMatchingController.prototype.get = function(id) {
+ return this.duelists[id];
+};
+
+DuelMatchingController.prototype.remove = function(id) {
+ delete this.duelists[id];
+ this.bugoutFuckers[id] = true;
+};
+
+DuelMatchingController.prototype.match = function() {
+ var white = this.shiftWhiteQueue();
+ var red = this.shiftRedQueue();
+ if (!white || !red) {
+ if (white) this.whiteQueue.unshift(white);
+ if (red) this.redQueue.unshift(red);
+ return;
+ }
+ white.setFoe(red.getId());
+ red.setFoe(white.getId());
+ var roomName = white.getId() + '-' + red.getId();
+ white.setRoom(roomName);
+ red.setRoom(roomName);
+ this.duelists[white.getId()] = white;
+ this.duelists[red.getId()] = red;
+ return true;
+};
+
+DuelMatchingController.prototype.shiftWhiteQueue = function() {
+ var white = this.whiteQueue.shift();
+ if (!white) return null;
+ if (this.bugoutFuckers[white.getId()]) {
+ delete this.bugoutFuckers[white.getId()];
+ white = null;
+ }
+ return white;
+};
+
+DuelMatchingController.prototype.shiftRedQueue = function() {
+ var red = this.redQueue.shift();
+ if (!red) return null;
+ if (this.bugoutFuckers[red.getId()]) {
+ delete this.bugoutFuckers[red.getId()];
+ red = null;
+ }
+ return red;
+};
+
+module.exports = DuelMatchingController;
View
@@ -0,0 +1,75 @@
+function DuelMessenger(socket, ctrl){
+ this.socket = socket;
+ this.ctrl = ctrl;
+ this.observe();
+}
+
+DuelMessenger.prototype.observe = function() {
+ this.socket.on('duty', this.onDuty.bind(this));
+ this.socket.on('I have control', this.onIHaveControl.bind(this));
+ this.socket.on('disconnect', this.onDisconnect.bind(this));
+ this.socket.on('critical white', this.onCriticalWhite.bind(this));
+ this.socket.on('critical red', this.onCriticalRed.bind(this));
+ this.socket.on('white', this.onWhite.bind(this));
+ this.socket.on('red', this.onRed.bind(this));
+};
+
+DuelMessenger.prototype.onDuty = function(data) {
+ var duelist = this.ctrl.get(this.socket.id);
+ if (!duelist) {
+ this.socket.emit('You have no control', {});
+ return;
+ }
+ if (duelist.isWhiteColor()) {
+ this.socket.join(duelist.getRoom());
+ this.socket.emit('You have control', {ship: 'white'});
+ return;
+ }
+ if (duelist.isRedColor()) {
+ this.socket.join(duelist.getRoom());
+ this.socket.emit('You have control', {ship: 'red'});
+ return;
+ }
+};
+
+DuelMessenger.prototype.onIHaveControl = function(data) {
+ console.log('>>>>>>>>>>>>>>> ready: ' + this.socket.id + ' control ' + data.ship);
+ this.socket.emit('ready', {});
+};
+
+DuelMessenger.prototype.onDisconnect = function() {
+ var duelist = this.ctrl.get(this.socket.id);
+ if (!duelist) return;
+ this.socket.leave(duelist.getRoom());
+ this.ctrl.remove(this.socket.id);
+};
+
+DuelMessenger.prototype.onCriticalWhite = function(data) {
+ var duelist = this.ctrl.get(this.socket.id);
+ if (!duelist) return;
+ this.socket.broadcast
+ .to(this.ctrl.get(this.socket.id).getRoom())
+ .emit('critical white', data);
+};
+
+DuelMessenger.prototype.onCriticalRed = function(data) {
+ var duelist = this.ctrl.get(this.socket.id);
+ if (!duelist) return;
+ this.socket.broadcast.to(duelist.getRoom()).emit('critical red', data);
+};
+
+DuelMessenger.prototype.onWhite = function(data) {
+ this.socket.emit('white', data);
+ var duelist = this.ctrl.get(this.socket.id);
+ if (!duelist) return;
+ this.socket.broadcast.to(duelist.getRoom()).emit('white', data);
+};
+
+DuelMessenger.prototype.onRed = function(data) {
+ this.socket.emit('red', data);
+ var duelist = this.ctrl.get(this.socket.id);
+ if (!duelist) return;
+ this.socket.broadcast.to(duelist.getRoom()).emit('red', data);
+};
+
+module.exports = DuelMessenger;
View
@@ -0,0 +1,45 @@
+function Duelist(id) {
+ this.id = id;
+}
+
+Duelist.prototype.color = '';
+Duelist.prototype.foe = null;
+Duelist.prototype.room = null;
+
+Duelist.prototype.setColorWhite = function () {
+ this.color = 'white';
+};
+
+Duelist.prototype.setColorRed = function() {
+ this.color = 'red';
+};
+
+Duelist.prototype.setFoe = function(id) {
+ this.foe = id
+};
+
+Duelist.prototype.setRoom = function(name) {
+ this.room = name;
+};
+
+Duelist.prototype.getId = function() {
+ return this.id;
+};
+
+Duelist.prototype.isWhiteColor = function() {
+ return this.color === 'white';
+};
+
+Duelist.prototype.isRedColor = function() {
+ return this.color === 'red';
+};
+
+Duelist.prototype.getFoe = function() {
+ return this.foe;
+};
+
+Duelist.prototype.getRoom = function() {
+ return this.room;
+};
+
+module.exports = Duelist;
View
@@ -1,60 +1,13 @@
function Messenger(io) {
- io.sockets.on('connection', this.observe);
+ this.duelMsg = require('./duel-messenger');
+ var ctrl = require('./duel-matching-controller');
+ this.duelCtrl = new ctrl();
+ io.sockets.on('connection', this.handler.bind(this));
}
-var white = null;
-var red = null;
-
-Messenger.prototype.observe = function(socket) {
- if (!white) {
- white = socket.id;
- } else if (!red && white != socket.id) {
- red = socket.id;
- }
- socket.on('duty', function(data) {
- if (socket.id == white) {
- socket.emit('You have control', {ship: 'white'});
- } else if (socket.id == red) {
- socket.emit('You have control', {ship: 'red'});
- } else {
- socket.emit('You have no control', {});
- }
- });
- socket.on('I have control', function(data) {
- console.log('>>>>>>>>>>>>>>> ready: ' + data.ship);
- socket.emit('ready', {});
- });
- socket.on('I have no control', function(data) {
- socket.emit('ready', {});
- });
- socket.on('disconnect', function() {
- if (socket.id == white) {
- white = null;
- }
- if (socket.id == red) {
- red = null;
- }
- });
- socket.on('critical white', function(data) {
- socket.broadcast.emit('critical white', data);
- });
- socket.on('critical red', function(data) {
- socket.broadcast.emit('critical red', data);
- });
- socket.on('white', function(data) {
- if (socket.id != white) {
- return;
- }
- socket.emit('white', data);
- socket.broadcast.emit('white', data);
- });
- socket.on('red', function(data) {
- if (socket.id != red) {
- return;
- }
- socket.emit('red', data);
- socket.broadcast.emit('red', data);
- });
+Messenger.prototype.handler = function(socket) {
+ this.duelCtrl.add(socket.id);
+ new this.duelMsg(socket, this.duelCtrl);
};
module.exports = Messenger;
View
@@ -0,0 +1,15 @@
+function Routing(app, dir) {
+ this.dir = dir;
+ app.get('/', this.home.bind(this));
+ app.get('/*', this.other.bind(this));
+};
+
+Routing.prototype.home = function(req, res) {
+ res.sendfile(this.dir + '/index.html');
+};
+
+Routing.prototype.other = function(req, res) {
+ res.sendfile(this.dir + req.url);
+};
+
+module.exports = Routing;
View
@@ -49,9 +49,6 @@ var DuelShooting = Class.create({
case 'red':
this.setupShipAsRed();
break;
- default:
- this.setupShipAsReadOnly();
- break;
}
},
@@ -109,25 +106,6 @@ var DuelShooting = Class.create({
this.sync.setShipRedWeapon(this.weapons.ship);
},
- setupShipAsReadOnly: function() {
- this.ship = new ShipWhite(false);
- this.enemy = new ShipRed(true);
-
- this.weapons = {};
- this.weapons.ship = new Weapon(this.ship, this.enemy);
- this.weapons.enemy = new Weapon(this.enemy, this.ship);
- this.weapons.enemy.addIField();
- this.weapons.enemy.addFunnelDefences();
-
- this.cmd = {};
- this.cmd.ship = new CommandShipWhite(this.ship, this.weapons.ship);
- this.cmd.enemy = new CommandShipRed(this.enemy, this.weapons.enemy);
-
- this.sync.listenShipWhiteCommand(this.cmd.ship, this.ship);
- this.sync.listenShipRedCommand(this.cmd.enemy, this.enemy);
- this.sync.setShipRedWeapon(this.weapons.enemy);
- },
-
setupTimeKeeper: function() {
this.timeKeeper = new TimeKeeper();
},
View
@@ -161,11 +161,7 @@ var Synchronizer = Class.create({
},
youHaveNoControl: function(data) {
- this.controlShip = null;
- this.socket.emit('I have no control', {});
- if (typeof console !== 'undefined') {
- console.log('I have no control');
- }
+ (function() { this.socket.emit('duty', {}); }).bind(this).delay(5);
},
white: function(data) {
View
@@ -1,14 +0,0 @@
-function Routing(app) {
- app.get('/', this.home);
- app.get('/*', this.other);
-};
-
-Routing.prototype.home = function(req, res) {
- res.sendfile(__dirname + '/index.html');
-};
-
-Routing.prototype.other = function(req, res) {
- res.sendfile(__dirname + req.url);
-};
-
-module.exports = Routing;
View
4 web.js
@@ -6,8 +6,8 @@ var app
app = require('express').createServer();
app.listen(process.env.PORT || 8080);
-routing = require('./routing');
-new routing(app);
+routing = require('./app/routing');
+new routing(app, __dirname);
io = require('socket.io').listen(app);
msg = require('./app/messenger');

0 comments on commit baef95e

Please sign in to comment.