<commit_before>/**
 * # Logic code for Meritocracy Game
 * Copyright(c) 2014 <NAME>
 * MIT Licensed
 *
 * Info:
 * Matching, and stepping can be done in different ways. It can be
 * centralized, and the logic tells the clients when to step, or
 * clients can synchronize themselves and step automatically.
 *
 * In this game, the logic is synchronized with the clients. The logic
 * will send automatically game-commands to start and step
 * through the game plot whenever it enters a new game step.
 *
 * http://www.nodegame.org
 * ---
 */

var path = require('path');

var Database = require('nodegame-db').Database;

var ngc = require('nodegame-client');
var Stager = ngc.Stager;
var stepRules = ngc.stepRules;
var GameStage = ngc.GameStage;
var J = ngc.JSUS;

var stager = new Stager();

// Variable registered outside of the export function are shared among all
// instances of game logics.
var counter = 0;

var settings = require(__dirname + '/game.shared.js');

var EXCHANGE_RATE = settings.EXCHANGE_RATE;

// Variable registered outside of the export function are shared among all
// instances of game logics.
var counter = settings.SESSION_ID;

// Group names.
var groupNames = settings.GROUP_NAMES;

// Here we export the logic function. Receives three parameters:
// - node: the NodeGameClient object.
// - channel: the ServerChannel object in which this logic will be running.
// - gameRoom: the GameRoom object in which this logic will be running. 
module.exports = function(node, channel, gameRoom) {

    var DUMP_DIR, DUMP_DIR_JSON, DUMP_DIR_CSV;
    DUMP_DIR = path.resolve(__dirname, '..', 'data') + '/' + counter + '/';
    DUMP_DIR_JSON = DUMP_DIR + 'json/';
    DUMP_DIR_CSV = DUMP_DIR + 'csv/';

    // Recursively create directories, sub-trees and all.
    J.mkdirSyncRecursive(DUMP_DIR_JSON, 0777);
    J.mkdirSyncRecursive(DUMP_DIR_CSV, 0777);

    console.log(gameRoom.runtimeConf);
    console.log('=====================');

    // outgoing messages will be saved.
    node.socket.journalOn = true;

    var nbRequiredPlayers = gameRoom.runtimeConf.MIN_PLAYERS;

    // Client game to send to reconnecting players.
    var client = channel.require(__dirname + '/game.client', { ngc: ngc });
    
    // Reads in descil-mturk configuration.
    var confPath = path.resolve(__dirname, '..', 'descil.conf.js');
    var dk = require('descil-mturk')(confPath);

    function codesNotFound() {
        if (!dk.codes.size()) {
            throw new Error('game.logic: no codes found.');
        }
    }

    if (settings.AUTH === 'MTURK') {
        dk.getCodes(codesNotFound);
    }
    else {
        dk.readCodes(codesNotFound);
    }

    var treatment = gameRoom.group;

    // Not so nice. We need to delete the cache, because treatments is
    // using an old node object otherwise.
    // TODO: find a better way.
    // See http://stackoverflow.com/questions/9210542/node-js-require-cache-possible-to-invalidate
    delete require.cache[require.resolve(__dirname + '/treatments.js')]
    var treatments = channel.require(__dirname + '/treatments.js', {
        node: node,
        treatment: treatment,
        groupNames: groupNames,
        dk: dk,
        SUBGROUP_SIZE: gameRoom.runtimeConf.SUBGROUP_SIZE
    });

    var ngdb = new Database(node);
    var mdb = ngdb.getLayer('MongoDB', {
        dbName: 'meritocracy_db',
        collectionName: 'user_data'
    });

    mdb.connect(function() {});

    node.on.data('questionnaire', function(msg) {
        var saveObject = {
            session: node.nodename,
            condition: treatment,
            stage: msg.stage,
            player: msg.from,
            created: e.created,
            gameName: msg.data.gameName,<commit_msg>minor<commit_after>/**
 * # Logic code for Meritocracy Game
 * Copyright(c) 2014 <NAME>
 * MIT Licensed
 *
 * Info:
 * Matching, and stepping can be done in different ways. It can be
 * centralized, and the logic tells the clients when to step, or
 * clients can synchronize themselves and step automatically.
 *
 * In this game, the logic is synchronized with the clients. The logic
 * will send automatically game-commands to start and step
 * through the game plot whenever it enters a new game step.
 *
 * http://www.nodegame.org
 * ---
 */

var path = require('path');

var Database = require('nodegame-db').Database;

var ngc = require('nodegame-client');
var Stager = ngc.Stager;
var stepRules = ngc.stepRules;
var GameStage = ngc.GameStage;
var J = ngc.JSUS;

var stager = new Stager();

// Variable registered outside of the export function are shared among all
// instances of game logics.
var counter = 0;

var settings = require(__dirname + '/game.shared.js');

var EXCHANGE_RATE = settings.EXCHANGE_RATE;

// Variable registered outside of the export function are shared among all
// instances of game logics.
var counter = settings.SESSION_ID;

// Group names.
var groupNames = settings.GROUP_NAMES;

// Here we export the logic function. Receives three parameters:
// - node: the NodeGameClient object.
// - channel: the ServerChannel object in which this logic will be running.
// - gameRoom: the GameRoom object in which this logic will be running. 
module.exports = function(node, channel, gameRoom) {

    var DUMP_DIR, DUMP_DIR_JSON, DUMP_DIR_CSV;
    DUMP_DIR = path.resolve(__dirname, '..', 'data') + '/' + counter + '/';
    DUMP_DIR_JSON = DUMP_DIR + 'json/';
    DUMP_DIR_CSV = DUMP_DIR + 'csv/';

    // Recursively create directories, sub-trees and all.
    J.mkdirSyncRecursive(DUMP_DIR_JSON, 0777);
    J.mkdirSyncRecursive(DUMP_DIR_CSV, 0777);

    console.log(gameRoom.runtimeConf);
    console.log('=====================');

    // outgoing messages will be saved.
    node.socket.journalOn = true;

    var nbRequiredPlayers = gameRoom.runtimeConf.MIN_PLAYERS;

    // Client game to send to reconnecting players.
    var client = channel.require(__dirname + '/game.client', { ngc: ngc });
    
    // Reads in descil-mturk configuration.
    var confPath = path.resolve(__dirname, '..', 'descil.conf.js');
    var dk = require('descil-mturk')(confPath);

    function codesNotFound() {
        if (!dk.codes.size()) {
            throw new Error('game.logic: no codes found.');
        }
    }

    if (settings.AUTH === 'MTURK') {
        dk.getCodes(codesNotFound);
    }
    else {
        dk.readCodes(codesNotFound);
    }

    var treatment = gameRoom.group;

    // Not so nice. We need to delete the cache, because treatments is
    // using an old node object otherwise.
    // TODO: find a better way.
    // See http://stackoverflow.com/questions/9210542/node-js-require-cache-possible-to-invalidate
    delete require.cache[require.resolve(__dirname + '/treatments.js')]
    var treatments = channel.require(__dirname + '/treatments.js', {
        node: node,
        treatment: treatment,
        groupNames: groupNames,
        dk: dk,
        SUBGROUP_SIZE: gameRoom.runtimeConf.SUBGROUP_SIZE
    });

    var ngdb = new Database(node);
    var mdb = ngdb.getLayer('MongoDB', {
        dbName: 'meritocracy_db',
        collectionName: 'user_data'
    });

    mdb.connect(function() {});

    node.on.data('questionnaire', function(msg) {
        var saveObject = {
            session: node.nodename,
            condition: treatment,
            stage: msg.stage,
            player: msg.from,
            created: msg.created,
            gameName: msg.data.gameName,
{'fraction_non_alphanumeric': 0.11979586355089981, 'fraction_numerical': 0.005372011818426001, 'mean_word_length': 2.938127974616605, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1529036', 'n_tokens_mistral': 2312, 'n_tokens_neox': 2206, 'n_words': 669}