Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to design when using Node.js for a gaming pairing

How to design when using Node.js for a gaming pairing

Problem

I'm using node.js for gaming pairing(for example, several client connect the servers. They are connected to each other if there are more than one players; or they will be kicked for 30sec).

Currently, I'm using socket for Connection (and this can detect lost connection unexpected). But I cannot figure out a elegant way for pairing:

var net = require('net');
var _clients = new Array();

net.createServer(function(_socket) {
  _socket.on('data', function(_data) {
    //Parse client data
    _clients.push(_socket);
    setTimeout(function(){
      _socket.write('kicked\n');
    },30*1000);
  _socket.on('close', function(data) {
    //Delete _socket from _clients
  }
}).listen(6969, '127.0.0.1');

setInterval(function(){
  var pair = new Array();
  while(_clients.length>2)
  {
    var _s1 = _clients.pop(), _s2 = _clients.pop();
    // Pair _s1 & _s2
  }
},2*1000);

The current code works, but it is really bad designed :(

(1)SetInterval is used, rather than asynchronous call. (2)Maintaining an array like _clients is inconvenient, since I have to deal with "kicked"/lost connection/pair or other situations.

PS. Currently I'm pairing clients by time order, but maybe random pair or other condition is needed to avoid always pairing the same people when the online players are not so much.

Problem courtesy of: Kanglai

Solution

Why not use something like the following?

Without Connection Pool

var net = require('net')
, pendingPair = null;

net.createServer(function(_socket) {
  _socket.on('data', function(_data) {
    //Parse client data
    if(!pendingPair) {
      pendingPair = _socket;
    } else {
      //  Now you have a pair!
      var p1 = pendingPair
        , p2 = _socket;

      pendingPair = null;
    }
}).listen(6969, '127.0.0.1');

You get automatic pairs as soon as connections are made. You will still need to track those sockets somewhere for kicking clients, and dropped connections, but you should be able to get rid of the setIntervals.

With Connection Pool

var net = require('net')
  , _ = require('underscore')._
  , clients = {}
  , games = {};

function setKickTimer(socket) {
    socket.kickTimer = setTimeout(function() {
        socket.write('kicked\n');
    }, 30 * 1000);
}

net.createServer(function(socket) {
    socket.id = Math.floor(Math.random() * 1000);
    setKickTimer(socket);
    clients[socket.id] = socket;

    socket.on('data', function(data) {
        socket.data = parseData(data);
    }

    socket.on('close', function(data) {
        var opponent = _.find(clients, function(client) { return client.opponentId === socket.id; });
        //  The opponent is no longer part of a pair.
        if(opponent) {
            delete opponent.opponentId;
            setKickTimer(opponent);
        }

        delete clients[socket.id];
    }

}).listen(6969, '127.0.0.1');

setInterval(function(){
    //  Get the client ids of clients who are not matched, and randomize the order
    var unmatchedClientIds = _.shuffle(_.keys(_.filter(clients, function(client) { return !client.opponentId; })));
    while(unmatchedClientIds > 2) {
        var c1 = unmatchedClientIds.pop(),
          , c2 = unmatchedClientIds.pop();

        clearTimeout(clients[c1].kickTimer);
        clearTimeout(clients[c2].kickTimer);
        clients[c1].opponentId = c2;
        clients[c2].opponentId = c1;
    }
},2*1000);

This isn't a complete working solution, but it should give you an idea of how to manage dropped connections and kicking users from the queue. Notice I'm using underscore.js to make the operations on the collections much easier.

Solution courtesy of: Timothy Strimple

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

How to design when using Node.js for a gaming pairing

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×