/**
* Class for representing a telegram, an envelope which contains a message
* and certain metadata like sender and receiver. Part of the messaging system
* for game entities.
*
* @author {@link https://github.com/Mugen87|Mugen87}
*/
class Telegram {
/**
* Constructs a new telegram object.
*
* @param {GameEntity} sender - The sender.
* @param {GameEntity} receiver - The receiver.
* @param {String} message - The actual message.
* @param {Number} delay - A time value in millisecond used to delay the message dispatching.
* @param {Object} data - An object for custom data.
*/
constructor( sender, receiver, message, delay, data ) {
/**
* The sender.
* @type {GameEntity}
*/
this.sender = sender;
/**
* The receiver.
* @type {GameEntity}
*/
this.receiver = receiver;
/**
* The actual message.
* @type {String}
*/
this.message = message;
/**
* A time value in millisecond used to delay the message dispatching.
* @type {Number}
*/
this.delay = delay;
/**
* An object for custom data.
* @type {Object}
*/
this.data = data;
}
/**
* Transforms this instance into a JSON object.
*
* @return {Object} The JSON object.
*/
toJSON() {
return {
type: this.constructor.name,
sender: this.sender.uuid,
receiver: this.receiver.uuid,
message: this.message,
delay: this.delay,
data: this.data
};
}
/**
* Restores this instance from the given JSON object.
*
* @param {Object} json - The JSON object.
* @return {Telegram} A reference to this telegram.
*/
fromJSON( json ) {
this.sender = json.sender;
this.receiver = json.receiver;
this.message = json.message;
this.delay = json.delay;
this.data = json.data;
return this;
}
/**
* Restores UUIDs with references to GameEntity objects.
*
* @param {Map<String,GameEntity>} entities - Maps game entities to UUIDs.
* @return {Telegram} A reference to this telegram.
*/
resolveReferences( entities ) {
this.sender = entities.get( this.sender );
this.receiver = entities.get( this.receiver );
return this;
}
}
export { Telegram };