<commit_before>/**
 * Handles the communication with the backend.
 */
var Backend = {

	/**
	 * GET data.
	 */
	get: function (action) {
		var data = null;

		if (typeof Routes !== 'undefined' && Routes[action]) {
			var url = Routes[action]();

			var getter = function () {
				$.ajax(url, { async: false })
					.done(function (data) {
						data = JSON.parse(data);
						EventSystem.publish(GLOBAL.EVENT.connection, [true]);
					})
					.fail(function () {
						EventSystem.publish(GLOBAL.EVENT.connection, [false]);
						setTimeout(function () {
							getter();
						}, 1000);
					});
			};
			getter();
		}

		return data;
	},

	/**
	 * PUT data.
	 */
	put: function (action, data) {
		if (typeof Routes !== 'undefined' && Routes[action]) {
			var url = Routes[action]();
			var jsonData = JSON.stringify(data);

			var poster = function () {
				$.post(url, jsonData)
					.done(function () {
						EventSystem.publish(GLOBAL.EVENT.connection, [true]);
					})
					.fail(function () {
						EventSystem.publish(GLOBAL.EVENT.connection, [false]);
						setTimeout(function () {
							poster();
						}, 1000);
					});
			};
			poster();
		}
	},


	/**
	 * GET the data of the player.
	 * @returns {Object} An object with data about the player.
	 */
	getPlayer: function () {
		var data = this.get('current_api_players_path');
		if (!data) {
			data = {
				agent: 0,
				water: 2
			};
		}

		return data;
	},

	/**
	 * GET the garden appearance.
	 * @returns {Object} An object with data about the garden.
	 */
	getGarden: function () {
		var data = [
			{ id: '00', x: 0, y: 0, level: 1 },
			{ id: '20', x: 2, y: 0, level: 3 }
		];

		return data;
	},

	/**
	 * GET the next game that should be played.
	 * @returns {Object} An object with data about the next game.
	 */
	getScenario: function () {
		var data = {
			subgame: 1,
			method: 0,
			representation: [0],
			range: [1, 4],
			mode: [0, 1, 2],
			roundsPerMode: 3
		};

		if (!Array.isArray(data.representation)) {
			data.representation = [data.representation];
		}
		if (!Array.isArray(data.range)) {
			data.range = [data.range];
		}
		if (data.mode) {
			if (!Array.isArray(data.mode)) {
				data.mode = [data.mode];
			}

			// Add intro and outro for the game.
			data.mode.unshift(GLOBAL.MODE.intro);
			data.mode.push(GLOBAL.MODE.outro);
		}

		return data;
	},


	/**
	 * PUT player session results.
	 */
	putSession: function (data) {
		this.put('register_api_player_sessions_path', data);
	},

	/**
	 * PUT garden updates.
	 */
	putGardenUpdates: function (data) {
		this.put('', data);
	}
};<commit_msg>Backend: use combined ajax function<commit_after>/**
 * Handles the communication with the backend.
 */
var Backend = {

	/**
	 * Basic ajax call.
	 * @param {Object} settings - An object with settings to jQuery ajax call
	 */
	ajax: function (settings) {
		var _this = this;
		return $.ajax(settings).fail(function (jqXHR) {
			console.log(jqXHR.status + ' ' + jqXHR.statusText);
			EventSystem.publish(GLOBAL.EVENT.connection, [false]);

			// TODO: Monitor error codes, such as 403 or 500
			// TODO: Create a function that is called if this takes too long.
			setTimeout(function () { _this.ajax(settings); }, 1000);
		});
	},

	/**
	 * GET data.
	 * @param {String} routeName - The name of the route function
	 * @param {*} parameters - optional parameter for the route action
	 */
	get: function (routeName, parameters) {
		var data = null;

		if (typeof Routes !== 'undefined' && Routes[routeName]) {
			var settings = {
				url: Routes[routeName](parameters),
				async: false
			};

			this.ajax(settings).done(function (data) {
				data = JSON.parse(data);
				EventSystem.publish(GLOBAL.EVENT.connection, [true]);
			});
		}

		return data;
	},

	/**
	 * PUT data.
	 * @param {String} routeName - The name of the route function
	 * @param {Object} data - The data to send (will be transformed to JSON-format)
	 */
	put: function (routeName, data) {
		if (typeof Routes !== 'undefined' && Routes[routeName]) {
			var settings = {
				url: Routes[routeName](),
				type: 'POST',
				data: JSON.stringify(data)
			};

			this.ajax(settings).done(function () {
				EventSystem.publish(GLOBAL.EVENT.connection, [true]);
			});
		}
	},


	/**
	 * GET the data of the player.
	 * @returns {Object} An object with data about the player.
	 */
	getPlayer: function () {
		var data = this.get('current_api_players_path');
		if (!data) {
			data = {
				agent: 0,
				water: 2
			};
		}

		return data;
	},

	/**
	 * GET the garden appearance.
	 * @returns {Object} An object with data about the garden.
	 */
	getGarden: function () {
		var data = [
			{ id: '00', x: 0, y: 0, level: 1 },
			{ id: '20', x: 2, y: 0, level: 3 }
		];

		return data;
	},

	/**
	 * GET the next game that should be played.
	 * @returns {Object} An object with data about the next game.
	 */
	getScenario: function () {
		var data = {
			subgame: 1,
			method: 0,
			representation: [0],
			range: [1, 4],
			mode: [0, 1, 2],
			roundsPerMode: 3
		};

		if (!Array.isArray(data.representation)) {
			data.representation = [data.representation];
		}
		if (!Array.isArray(data.range)) {
			data.range = [data.range];
		}
		if (data.mode) {
			if (!Array.isArray(data.mode)) {
				data.mode = [data.mode];
			}

			// Add intro and outro for the game.
			data.mode.unshift(GLOBAL.MODE.intro);
			data.mode.push(GLOBAL.MODE.outro);
		}

		return data;
	},



	/**
	 * PUT garden updates.
	 * @param {Object} data - The garden updates
	 */
	putGardenUpdates: function (data) {
		this.put('', data);
	},

	/**
	 * PUT player session results.
	 * @param {Object} data - The session results
	 */
	putSession: function (data) {
		this.put('register_api_player_sessions_path', data);
	}
};
{'fraction_non_alphanumeric': 0.16133518776077885, 'fraction_numerical': 0.01043115438108484, 'mean_word_length': 2.7140090380890896, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5478189', 'n_tokens_mistral': 2367, 'n_tokens_neox': 1933, 'n_words': 528}