<commit_before>'use strict';

module.exports = GameObject;


var config = require('config');
var utils = require('utils');


GameObject.prototype.TSID_INITIAL = 'G';
GameObject.prototype.__isGO = true;

/**
 * Generic constructor for both instantiating an existing game object
 * (from JSON data), and creating a new object.
 *
 * @param {object} [data] initialization values (properties are
 *        shallow-copied into the game object)
 * @constructor
 */
function GameObject(data) {
	if (!data) data = {};
	// initialize TSID/class ID (use deprecated properties if necessary, and
	// keep them as non-enumerable so they are available, but not persisted)
	this.tsid = data.tsid || data.id || utils.makeTsid(this.TSID_INITIAL,
		config.getGsid());
	utils.addNonEnumerable(this, 'id', this.tsid);  // deprecated
	if (data.class_tsid || data.class_id) {
		this.class_tsid = data.class_tsid || data.class_id;
		utils.addNonEnumerable(this, 'class_id', this.class_tsid);  // deprecated
	}
	// add non-enumerable internal properties
	utils.addNonEnumerable(this, 'deleted', false);
	// copy supplied data
	// TODO: remove 'dynamic' partition in fixture data, and get rid of special handling here
	var key;
	for (key in data.dynamic) {
		if (!(key in this)) {
			this[key] = data.dynamic[key];
		}
	}
	for (key in data) {
		if (key !== 'dynamic' && !(key in this)) {
			this[key] = data[key];
		}
	}
	if (!this.ts) {
		this.ts = new Date().getTime();
	}
}


/**
 * Creates a processed shallow copy of this game object's data,
 * prepared for serialization.
 *
 * The returned data only contains non-function-type direct ("own")
 * properties whose name does not start with a "!". Complex
 * `object`-type properties (specifically, references to other game
 * objects) are not handled separately here, i.e. the caller may need
 * to replace those with appropriate reference structures before actual
 * serialization (see {@link module:data/objrefProxy~refify|
 * objrefProxy.refify}).
 *
 * @returns {object} shallow copy of the game object, prepared for
 *          serialization
 */
GameObject.prototype.serialize = function serialize() {
	var ret = {};
	var keys = Object.keys(this);  // Object.keys only includes own properties
	for (var i = 0; i < keys.length; i++) {
		var k = keys[i];
		if (k[0] !== '!') {
			var val = this[k];
			if (typeof val !== 'function') {
				ret[k] = val;
			}
		}
	}
	return ret;
};


/**
 * @returns {string}
 */
GameObject.prototype.toString = function toString() {
	return '[' + this.constructor.name + '#' + this.tsid + ']';
};


/**
 * Schedules this object for deletion after the current request.
 */
GameObject.prototype.del = function del() {
	this.deleted = true;
};
<commit_msg>add GSJS helper functions to game object class<commit_after>'use strict';

module.exports = GameObject;


var config = require('config');
var utils = require('utils');


GameObject.prototype.TSID_INITIAL = 'G';
GameObject.prototype.__isGO = true;

/**
 * Generic constructor for both instantiating an existing game object
 * (from JSON data), and creating a new object.
 *
 * @param {object} [data] initialization values (properties are
 *        shallow-copied into the game object)
 * @constructor
 */
function GameObject(data) {
	if (!data) data = {};
	// initialize TSID/class ID (use deprecated properties if necessary, and
	// keep them as non-enumerable so they are available, but not persisted)
	this.tsid = data.tsid || data.id || utils.makeTsid(this.TSID_INITIAL,
		config.getGsid());
	utils.addNonEnumerable(this, 'id', this.tsid);  // deprecated
	if (data.class_tsid || data.class_id) {
		this.class_tsid = data.class_tsid || data.class_id;
		utils.addNonEnumerable(this, 'class_id', this.class_tsid);  // deprecated
	}
	// add non-enumerable internal properties
	utils.addNonEnumerable(this, 'deleted', false);
	// copy supplied data
	// TODO: remove 'dynamic' partition in fixture data, and get rid of special handling here
	var key;
	for (key in data.dynamic) {
		if (!(key in this)) {
			this[key] = data.dynamic[key];
		}
	}
	for (key in data) {
		if (key !== 'dynamic' && !(key in this)) {
			this[key] = data[key];
		}
	}
	if (!this.ts) {
		this.ts = new Date().getTime();
	}
}


/**
 * Creates a processed shallow copy of this game object's data,
 * prepared for serialization.
 *
 * The returned data only contains non-function-type direct ("own")
 * properties whose name does not start with a "!". Complex
 * `object`-type properties (specifically, references to other game
 * objects) are not handled separately here, i.e. the caller may need
 * to replace those with appropriate reference structures before actual
 * serialization (see {@link module:data/objrefProxy~refify|
 * objrefProxy.refify}).
 *
 * @returns {object} shallow copy of the game object, prepared for
 *          serialization
 */
GameObject.prototype.serialize = function serialize() {
	var ret = {};
	var keys = Object.keys(this);  // Object.keys only includes own properties
	for (var i = 0; i < keys.length; i++) {
		var k = keys[i];
		if (k[0] !== '!') {
			var val = this[k];
			if (typeof val !== 'function') {
				ret[k] = val;
			}
		}
	}
	return ret;
};


/**
 * @returns {string}
 */
GameObject.prototype.toString = function toString() {
	return '[' + this.constructor.name + '#' + this.tsid + ']';
};


/**
 * Schedules this object for deletion after the current request.
 */
GameObject.prototype.del = function del() {
	this.deleted = true;
};


/**
 * Helper function originally defined in <gsjs/common.js>. All the
 * functions there should really be added to all game object prototypes
 * in gsjsBridge (then this here wouldn't be necessary), but that would
 * require prefixing a zillion calls in GSJS code with 'this.'.
 * @private
 */
GameObject.prototype.getProp = function getProp(key) {
	return this[key];
};


/**
 * Helper function originally defined in <gsjs/common.js>. All the
 * functions there should really be added to all game object prototypes
 * in gsjsBridge (then this here wouldn't be necessary), but that would
 * require prefixing a zillion calls in GSJS code with 'this.'.
 * @private
 */
GameObject.prototype.setProp = function setProp(key, val) {
	this[key] = val;
};


/**
 * Helper function originally defined in <gsjs/common.js>. All the
 * functions there should really be added to all game object prototypes
 * in gsjsBridge (then this here wouldn't be necessary), but that would
 * require prefixing a zillion calls in GSJS code with 'this.'.
 * @private
 */
GameObject.prototype.setProps = function setProps(props) {
	for (var k in props) {
		this[k] = props[k];
	}
};

{'fraction_non_alphanumeric': 0.13154321918841455, 'fraction_numerical': 0.0006034092623321768, 'mean_word_length': 3.74588403722262, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 6, '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': '3675995', 'n_tokens_mistral': 2116, 'n_tokens_neox': 2026, 'n_words': 695}