<commit_before>'use strict';
const debug = require('debug')('sockbot:mafia:autoGM');
const SockMafia = require('sockmafia');
const Moment = require('moment');
const fs = require('fs');
const rimrafPromise = require('rimraf-promise');

let Forum, game;

const internals = {
    forum: Forum,
    game: game,
    scum: [],
    myName: '',
    timer: {
        nextAlert: undefined,
        callback: undefined,
        handle: undefined
    }
};
exports.internals = internals;

exports.defaultConfig = {
    category: 22,
    minPlayers: 6,
    phases: {
        init: '48 hours',
        day: '72 hours',
        night: '24 hours'
    }
};

/**
 * Sockbot 3.0 Activation function
 * @returns {Promise} A promise that will resolve when the activation is complete
 */
exports.activate = function activate() {
    debug('Activating');
    internals.timer.handle = setInterval(timer, 10);
    internals.forum.on('mafia:playerLynched', exports.onLynch);
    return exports.init();
};

exports.deactivate = function deactivate() {
    debug('Deactivating');
    internals.forum.removeListener('mafia:playerLynched', exports.onLynch);
    clearInterval(internals.timer.handle);
    return Promise.resolve();
};

function endGame() {
    internals.game = undefined;
    return rimrafPromise('autoGMdata');
}


/**
 * Sockbot 3.0 Plugin function
 * @param  {Forum} forum  The forum provider's Forum class
 * @param  {Object} config The plugin-specific configuration
 * @returns {Object}        A temporary object representing this instance of the forum
 */
exports.plugin = function plugin(forum, config) {
    internals.forum = forum;
    internals.myName = forum.username;
    
    if (config === null || typeof config !== 'object') {
		config = {};
	}
	
	Object.keys(exports.defaultConfig).forEach((key) => {
		if (!config[key]) {
			config[key] = exports.defaultConfig[key];
		}
	});
    
    internals.config = config;
    return {
		activate: exports.activate,
		deactivate: exports.deactivate
	};
};

exports.setTimer = function setTimer(expires, callback) {
    debug('Setting timer for ' + expires);
    if (!Moment.isMoment(expires)) {
        const parts = expires.split(' ');
        expires = new Moment().add(parts[0], parts[1]);
    }
    internals.timer.callback = callback;
    internals.timer.nextAlert = expires;
    return exports.save();
};

exports.cancelTimer = function cancelTimer() {
    internals.timer.nextAlert = undefined;
    internals.timer.callback = undefined;
};

function timer() {
    if (internals.timer.nextAlert && Moment().isSameOrAfter(internals.timer.nextAlert)) {
        debug('Timer expired!');<commit_msg>Fix: minimum players cannot be below 2 or the scum pick routine will fail<commit_after>'use strict';
const debug = require('debug')('sockbot:mafia:autoGM');
const SockMafia = require('sockmafia');
const Moment = require('moment');
const fs = require('fs');
const rimrafPromise = require('rimraf-promise');

let Forum, game;

const internals = {
    forum: Forum,
    game: game,
    scum: [],
    myName: '',
    timer: {
        nextAlert: undefined,
        callback: undefined,
        handle: undefined
    }
};
exports.internals = internals;

exports.defaultConfig = {
    category: 22,
    minPlayers: 6,
    phases: {
        init: '48 hours',
        day: '72 hours',
        night: '24 hours'
    }
};

/**
 * Sockbot 3.0 Activation function
 * @returns {Promise} A promise that will resolve when the activation is complete
 */
exports.activate = function activate() {
    debug('Activating');
    internals.timer.handle = setInterval(timer, 10);
    internals.forum.on('mafia:playerLynched', exports.onLynch);
    return exports.init();
};

exports.deactivate = function deactivate() {
    debug('Deactivating');
    internals.forum.removeListener('mafia:playerLynched', exports.onLynch);
    clearInterval(internals.timer.handle);
    return Promise.resolve();
};

function endGame() {
    internals.game = undefined;
    return rimrafPromise('autoGMdata');
}


/**
 * Sockbot 3.0 Plugin function
 * @param  {Forum} forum  The forum provider's Forum class
 * @param  {Object} config The plugin-specific configuration
 * @returns {Object}        A temporary object representing this instance of the forum
 */
exports.plugin = function plugin(forum, config) {
    internals.forum = forum;
    internals.myName = forum.username;
    
    if (config === null || typeof config !== 'object') {
		config = {};
	}
	
	Object.keys(exports.defaultConfig).forEach((key) => {
		if (!(key in config)) {
			config[key] = exports.defaultConfig[key];
		}
	});
    
    internals.config = config;
    
    //Enforce absolute minimum
    if (internals.config.minPlayers < 2) {
        internals.config.minPlayers = 2;
    }
    
    return {
		activate: exports.activate,
		deactivate: exports.deactivate
	};
};

exports.setTimer = function setTimer(expires, callback) {
    debug('Setting timer for ' + expires);
    if (!Moment.isMoment(expires)) {
        const parts = expires.split(' ');
        expires = new Moment().add(parts[0], parts[1]);
    }
    internals.timer.callback = callback;
    internals.timer.nextAlert = expires;
    return exports.save();
};

exports.cancelTimer = function cancelTimer() {
    internals.timer.nextAlert = undefined;
    internals.timer.callback = undefined;
};

function timer() {
    if (internals.timer.nextAlert && Moment().isSameOrAfter(internals.timer.nextAlert)) {
        debug('Timer expired!');
{'fraction_non_alphanumeric': 0.13615623288921336, 'fraction_numerical': 0.006753057127212995, 'mean_word_length': 3.4336569579288025, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '565428', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1628, 'n_words': 397}