var _ = require('underscore'),
    spawn = require('child_process').spawn,
    Game = require('../engine/game.js').Game;

/**
 *
 * @param commands
 * @param workingDirs
 * @constructor
 */
function Runner(commands, workingDirs) {
    this.gameResult = {
        log: ""
        , result: ""
        , replay: [new Date().getTime()]
    };
    this.commands = commands;
    this.workingDirs = workingDirs;
    this.n_survivors = 0;
    this.n_ready = 0;
}

/**
 * Sets up game, and runs it till completion
 * @param done
 */
Runner.prototype.runGame = function (done) {
    var game = new Game(this.gameResult.seed);
    game.initialize(4);
    this.n_survivors = 4;

    var ais = [];
    for (var i = 0; i < 4; i++) {
        var commandAndParameters = this.commands[i].split(' ');
        var command = _.first(commandAndParameters);
        var parameters = _.rest(commandAndParameters);
        var workingDir = this.workingDirs ? this.workingDirs[i] : undefined;
        ais.push({
            process: spawn(command, parameters, { cwd: workingDir }),
            command: [],
            ready: false,
            expired: false,
            TO: null,
            id: i
        });
    }

    var self = this;
    _.each(ais, function (ai) {
        ai.process.stdout.on('data', function (data) {
            console.log("AI" + ai.id + ">>" + 'stdout: ' + data);
            if (ai.expired)
                return;
            ai.command = data.toString().trim().split(' ');
            ai.ready = true;
            self.n_ready += 1;
            clearTimeout(ai.TO);
            onReady.call(self, game, ais, done);
        });

        ai.process.stderr.on('data', function (data) {
            console.log("AI" + ai.id + ">>" + 'stderr: ' + data);
        });

        ai.process.on('close', function (code) {
            console.log("AI" + ai.id + ">>" + 'child process exited with code ' + code);
        });
    });

    doTurn.call(this, game, ais, done);
};

/**
 * Post-turn handling, and set up of new turn processing
 * @param game
 * @param ais
 * @param done
 */
function onReady(game, ais, done) {
    if (this.n_ready == this.n_survivors) {
        console.log('Turn ended');
        var commands = _.map(ais, function (ai) {
            return ai.expired ? "" : ai.command;
        });

        game.processTurn(commands);
        this.gameResult.replay.push(commands);
        this.gameResult.log += game.getStatus();

        doTurn.call(this, game, ais, done);
    }
}

/**
 * Processes a turn
 * @param game
 * @param ais
 * @param done
 */
function doTurn(game, ais, done) {
    this.n_ready = 0;
    if (game.isFinished()) {
        _.each(ais, function (ai) {
            if (!ai.expired)
                ai.process.stdin.write('-1\n');
        });

        this.gameResult.result += game.getRanking();
        done();
    } else {
        var self = this;
        _.each(ais, function (ai) {
            if (ai.expired)
                return;
            ai.ready = false;
            ai.process.stdin.write(game.getStatusForAI(ai.id));
            console.log("AI" + ai.id + ">>" + 'writing 1 to stdin, waiting for stdout');

            var TO = setTimeout(function () {
                console.log("AI" + ai.id + ">>" + 'killing due to TLE');
                ai.expired = true;

                self.n_survivors -= 1;
                ai.process.kill('SIGINT');
                onReady.call(self, game, ais, done);
            }, 1000);
            ai.TO = TO;
        });
    }
};

exports.Runner = Runner;

@@ -260,24 +260,46 @@
     log: %22%22%0A
+        , content: %22%22%0A
         , re
@@ -629,12 +629,17 @@
 ult.
-seed
+replay%5B0%5D
 );%0A 
@@ -1332,36 +1332,47 @@
             
-console.log(
+self.gameResult.log += 
 %22AI%22 + ai.id
@@ -1390,33 +1390,32 @@
 stdout: ' + data
-)
 ;%0A            if
@@ -1730,36 +1730,47 @@
             
-console.log(
+self.gameResult.log += 
 %22AI%22 + ai.id
@@ -1796,17 +1796,16 @@
 ' + data
-)
 ;%0A      
@@ -1868,36 +1868,47 @@
             
-console.log(
+self.gameResult.log += 
 %22AI%22 + ai.id
@@ -1957,17 +1957,16 @@
 ' + code
-)
 ;%0A      
@@ -2221,28 +2221,39 @@
         
-console.log(
+this.gameResult.log += 
 'Turn en
@@ -2256,17 +2256,16 @@
 n ended'
-)
 ;%0A      
@@ -2473,27 +2473,31 @@
 .gameResult.
-log
+content
  += game.get
@@ -3141,36 +3141,47 @@
             
-console.log(
+self.gameResult.log += 
 %22AI%22 + ai.id
@@ -3230,17 +3230,16 @@
  stdout'
-)
 ;%0A%0A     
@@ -3299,20 +3299,31 @@
     
-console.log(
+self.gameResult.log += 
 %22AI%22
@@ -3360,17 +3360,16 @@
  to TLE'
-)
 ;%0A      

Use gameResult.log instead of console.log.