<commit_before>// Firing Solution     //
// Generic server core //

var sys = require('sys'),
	fs = require('fs'),
	path = require('path'),
	url = require('url'),
	http = require('http'),
	ws = require('./lib/ws'),
	util = require('./serverutil'),
	eutil = require('../engine/util'),
	assert = eutil.assert;

//////////////
// Manager //
////////////

function Manager() {
	// Games by game ID
	this.games = {};
	// Games sorted from the first to wake up to the last to wake up as a
	// delayed shift priority queue.
	this.wakeQueue = [];
	// The number of empty items at the start of the queue
	this.emptySpace = 0;
}

Manager.prototype.gameWithId = function (id) {
	return this.games[id] || null;
};

Manager.prototype.add = function (game) {
	assert(!this.games[game.id], 'Manager.addGame: duplicate game ID');
	this.games[game.id] = game;
	this.enqueue(game);
};

Manager.prototype.enqueue = function (game) {
	if (game.wakeAt > 0) {
		for (var i = this.wakeQueue.length - 1; i >= this.emptySpace; --i) {
			if (this.wakeQueue[i].wakeAt <= game.wakeAt) {
				this.wakeQueue.splice(i + 1, 0, game);
				return;
			}
		}
		// The game will be inserted at the beginning of the queue, so fall through
	}
	if (this.emptySpace > 0) {
		this.wakeQueue[--this.emptySpace] = game;
	} else {
		this.wakeQueue.unshift(game);
	}
};

Manager.prototype.tryDequeue = function (now) {
	if (this.emptySpace >= this.wakeQueue.length ||
			this.wakeQueue[this.emptySpace].wakeAt > now) {
		return null;
	}
	if (this.emptySpace * 2 < this.wakeQueue.length) {
		return this.wakeQueue[this.emptySpace++];<commit_msg>Minor fixes and some documentation for Manager<commit_after>// Firing Solution     //
// Generic server core //

var sys = require('sys'),
	fs = require('fs'),
	path = require('path'),
	url = require('url'),
	http = require('http'),
	ws = require('./lib/ws'),
	util = require('./serverutil'),
	eutil = require('../engine/util'),
	assert = eutil.assert;

//////////////
// Manager //
////////////

// Keeps track of active games on this server and when they need to be woken up
// to do whatever periodical processing they need to do.
function Manager() {
	// Games by game ID
	this.games = {};
	// Games sorted from the first to wake up to the last to wake up as a
	// delayed shift priority queue.
	this.wakeQueue = [];
	// The number of empty items at the start of the queue
	this.emptySpace = 0;
}

// Returns the active game with the specified id, or null, if no such game
// exists on this server.
Manager.prototype.gameWithId = function (id) {
	if (this.games.hasOwnProperty(id)) {
		return this.games[id] || null;
	}
	return null;
};

// Adds a game to the list of active games and enqueues it to wake up when it
// wants.
Manager.prototype.add = function (game) {
	assert(!this.games.hasOwnProperty(game.id), 'Manager.add: duplicate game ID');
	this.games[game.id] = game;
	this.enqueue(game);
};

// Enqueues a game to the wake queue, a priority queue of games sorted by the
// time they want to wake up the next time, starting from the earliest.
Manager.prototype.enqueue = function (game) {
	if (game.wakeAt > 0) {
		for (var i = this.wakeQueue.length - 1; i >= this.emptySpace; --i) {
			if (this.wakeQueue[i].wakeAt <= game.wakeAt) {
				this.wakeQueue.splice(i + 1, 0, game);
				return;
			}
		}
		// The game will be inserted at the beginning of the queue, so fall through
	}
	if (this.emptySpace > 0) {
		this.wakeQueue[--this.emptySpace] = game;
	} else {
		this.wakeQueue.unshift(game);
	}
};

// If there are one or more games that want to wake up at or after the current
// time (passed in as the 'now' parameter), returns the first such game.
// Otherwise, returns null.
Manager.prototype.tryDequeue = function (now) {
	if (this.emptySpace >= this.wakeQueue.length ||
			this.wakeQueue[this.emptySpace].wakeAt > now) {
		return null;
	}
	if (this.emptySpace * 2 < this.wakeQueue.length) {
		return this.wakeQueue[this.emptySpace++];
{'fraction_non_alphanumeric': 0.15587044534412955, 'fraction_numerical': 0.00354251012145749, 'mean_word_length': 3.1610526315789476, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '471625', 'n_tokens_mistral': 1346, 'n_tokens_neox': 1307, 'n_words': 416}