<commit_before>// Copyright © 2011 <NAME> <<EMAIL>> unless otherwise noted

define(['engine/util/Event', 'engine/util/Channel', 'engine/world/Scenario'], function (Event, Channel, Scenario) {

	function Game(isLocal) {
		this.localPlayer = null;
		//
		// Actor handling
		//
		this.actors = [];
		this.additionQueue = [];
		this.deletionQueue = [];
		this.actorByIdHash = {};
		// FIXME: Use some other method to create unique IDs
		this.previousId = 10000;
		//
		// Pacing information
		//
		// Should the game be running, according to the server or the user
		this.running = false;
		// Is the game really running (it might not be, even if it should, if
		// somebody is lagging).
		this.reallyRunning = false;
		this.lastProcessedTick = 0;
		this.lastPermittedTick = 0;
		this.ticksPerSecond = 0;
		this.msecsPerTick = 0;
		this.cappedToFps = 0;
		// How far into the current tick we are
		this.msecsSinceTick = 0;
		this.lastConsideredTick = 0;
		this.lastTickAt = 0;
		this.setTicksPerSecond(5);
		//
		// Scenario information
		//
		this.scenario = null;
		this.players = [];   // FIXME: [] or {}?
		this.map = null;
		this.fieldWidth = 0;
		this.fieldHeight = 0;
		this.unitTypes = {};
		//
		// Server communication
		//
		this.isLocal = isLocal;
		this.channel = new Channel();
		this.channel.didReceiveMessage.register(this.handleMessage, this);
		this.channel.didEncounterError.register(this._channelDidEncounterError, this);
		// The last item is an array (queue) of commands to process at the next tick.
		// The commands received during this turn are pushed to item 0.
		this.commandQueues = [[]];
		this.acknowledgeAt = 0;
		this.notifyAssetReadyAt = 0;
		//
		// Events
		//
		// Emitted after a tick has been processed
		this.onTick = new Event();
	}

	Game.prototype._channelDidEncounterError = function (errorString) {
		console.error('Channel error: ' + errorString);
	};

	/**
	 * Loads scenario information and starts the game when everything is ready.
	 * @param {object} gameSpec
	 * @param {number} localPlayerId
	 */
	Game.prototype.loadScenario = function (gameSpec, localPlayerId) {
		this.scenario = new Scenario(this);
		this.scenario.didLoadAsset.register(this._didLoadAsset, this);
		this.scenario.didLoadAllAssets.register(this._didLoadAllAssets, this);
		this.scenario.load(gameSpec, localPlayerId);
	};

	/**
	 * Called by the scenario object when an asset has finished loading.
	 * @param {string} assetName
	 * @param {number} queued The number of assets queued so far
	 * @param {number} loaded The number of assets loaded so far
	 */
	Game.prototype._didLoadAsset = function (assetName, queued, loaded) {
		if (!this.isLocal) {
			// Send acknowledgement at most every 1000 milliseconds
			if (this.notifyAssetReadyAt <= 0) {
				// FIXME: Last of these has to be a guaranteed message
				this.notifyAssetReadyAt = (new Date()).getTime() + 1000;
			}
		}
	};

	/**
	 * Called by the scenario object when all assets have finished loading.
	 * @param {number} loaded Total number of loaded assets
	 */
	Game.prototype._didLoadAllAssets = function (loaded) {
		if (this.isLocal) {
			this.setRunning(true);
		}
	};

	/**
	 * Adds a player to the list of players. Typically call by the scenario
	 * object in the course of loading the scenario.
	 * @param {Player} player
	 */
	Game.prototype.addPlayer = function (player) {
		this.players[player.publicId] = player;
	};

	/**
	 * Sets the current map. Typically called by the scenario object in the
	 * course of loading the scenario.
	 * @param {Map} map
	 */
	Game.prototype.setMap = function (map) {
		this.map = map;
		this.fieldWidth = map.width * map.tileSize;
		this.fieldHeight = map.height * map.tileSize;
	};

	/**
	 * Adds an unit type to the list of unit types. Typically called by the
	 * scenario object in the course of loading the scenario.
	 * @param {string} unitTypeName
	 * @param {object} unitType
	 */
	Game.prototype.addUnitType = function (unitTypeName, unitType) {
		this.unitTypes[unitTypeName] = unitType;
	};

	/**
	 * Sets the local player. Typically called by the scenario object in the
	 * course of loading the scenario.
	 * @param {number} publicId
	 */
	Game.prototype.setLocalPlayerId = function (publicId) {
		assert(!this.localPlayer, 'Game.setLocalPlayerId: local player is already set');
		var player = this.playerWithPublicId(publicId);
		assert(player, 'Game.setLocalPlayerId: player not found');
		this.localPlayer = player;
	};
<commit_msg>Add check for unit type name conflicts<commit_after>// Copyright © 2011 <NAME> <<EMAIL>> unless otherwise noted

define(['engine/util/Event', 'engine/util/Channel', 'engine/world/Scenario'], function (Event, Channel, Scenario) {

	function Game(isLocal) {
		this.localPlayer = null;
		//
		// Actor handling
		//
		this.actors = [];
		this.additionQueue = [];
		this.deletionQueue = [];
		this.actorByIdHash = {};
		// FIXME: Use some other method to create unique IDs
		this.previousId = 10000;
		//
		// Pacing information
		//
		// Should the game be running, according to the server or the user
		this.running = false;
		// Is the game really running (it might not be, even if it should, if
		// somebody is lagging).
		this.reallyRunning = false;
		this.lastProcessedTick = 0;
		this.lastPermittedTick = 0;
		this.ticksPerSecond = 0;
		this.msecsPerTick = 0;
		this.cappedToFps = 0;
		// How far into the current tick we are
		this.msecsSinceTick = 0;
		this.lastConsideredTick = 0;
		this.lastTickAt = 0;
		this.setTicksPerSecond(5);
		//
		// Scenario information
		//
		this.scenario = null;
		this.players = [];   // FIXME: [] or {}?
		this.map = null;
		this.fieldWidth = 0;
		this.fieldHeight = 0;
		this.unitTypes = {};
		//
		// Server communication
		//
		this.isLocal = isLocal;
		this.channel = new Channel();
		this.channel.didReceiveMessage.register(this.handleMessage, this);
		this.channel.didEncounterError.register(this._channelDidEncounterError, this);
		// The last item is an array (queue) of commands to process at the next tick.
		// The commands received during this turn are pushed to item 0.
		this.commandQueues = [[]];
		this.acknowledgeAt = 0;
		this.notifyAssetReadyAt = 0;
		//
		// Events
		//
		// Emitted after a tick has been processed
		this.onTick = new Event();
	}

	Game.prototype._channelDidEncounterError = function (errorString) {
		console.error('Channel error: ' + errorString);
	};

	/**
	 * Loads scenario information and starts the game when everything is ready.
	 * @param {object} gameSpec
	 * @param {number} localPlayerId
	 */
	Game.prototype.loadScenario = function (gameSpec, localPlayerId) {
		this.scenario = new Scenario(this);
		this.scenario.didLoadAsset.register(this._didLoadAsset, this);
		this.scenario.didLoadAllAssets.register(this._didLoadAllAssets, this);
		this.scenario.load(gameSpec, localPlayerId);
	};

	/**
	 * Called by the scenario object when an asset has finished loading.
	 * @param {string} assetName
	 * @param {number} queued The number of assets queued so far
	 * @param {number} loaded The number of assets loaded so far
	 */
	Game.prototype._didLoadAsset = function (assetName, queued, loaded) {
		if (!this.isLocal) {
			// Send acknowledgement at most every 1000 milliseconds
			if (this.notifyAssetReadyAt <= 0) {
				// FIXME: Last of these has to be a guaranteed message
				this.notifyAssetReadyAt = (new Date()).getTime() + 1000;
			}
		}
	};

	/**
	 * Called by the scenario object when all assets have finished loading.
	 * @param {number} loaded Total number of loaded assets
	 */
	Game.prototype._didLoadAllAssets = function (loaded) {
		if (this.isLocal) {
			this.setRunning(true);
		}
	};

	/**
	 * Adds a player to the list of players. Typically call by the scenario
	 * object in the course of loading the scenario.
	 * @param {Player} player
	 */
	Game.prototype.addPlayer = function (player) {
		this.players[player.publicId] = player;
	};

	/**
	 * Sets the current map. Typically called by the scenario object in the
	 * course of loading the scenario.
	 * @param {Map} map
	 */
	Game.prototype.setMap = function (map) {
		this.map = map;
		this.fieldWidth = map.width * map.tileSize;
		this.fieldHeight = map.height * map.tileSize;
	};

	/**
	 * Adds an unit type to the list of unit types. Typically called by the
	 * scenario object in the course of loading the scenario.
	 * @param {string} unitTypeName
	 * @param {object} unitType
	 */
	Game.prototype.addUnitType = function (unitTypeName, unitType) {
		assert(!Object.prototype.hasOwnProperty.call(this.unitTypes, unitTypeName), 'Game.addUnitType: unit type with name ' + unitTypeName + ' already exists');
		this.unitTypes[unitTypeName] = unitType;
	};

	/**
	 * Sets the local player. Typically called by the scenario object in the
	 * course of loading the scenario.
	 * @param {number} publicId
	 */
	Game.prototype.setLocalPlayerId = function (publicId) {
		assert(!this.localPlayer, 'Game.setLocalPlayerId: local player is already set');
		var player = this.playerWithPublicId(publicId);
		assert(player, 'Game.setLocalPlayerId: player not found');
		this.localPlayer = player;
	};

{'fraction_non_alphanumeric': 0.11662830209360955, 'fraction_numerical': 0.007015236216156966, 'mean_word_length': 3.47693817468106, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, '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': '5362268', 'n_tokens_mistral': 3109, 'n_tokens_neox': 2770, 'n_words': 866}