/** @module Message */ import { EntryType } from "./constants"; import ByteArray from "./structures/ByteArray"; /** * Represents a message sent between client and server. * A message consists of a string type, and a payload of zero or more typed parameters. * @example This is how to create a simple message that we send to the server indicating that this player is ready: * * //Create a message of type ready with no payload: * let m = connection.createMessage("ready"); * * //Send the message to the server: * connection.sendMessage(m); * * * @example Usually, it's much easier to simply use the convenience methods: * * //Send the server a message of which maps this player selected: * connection.Send("mapsselected", "fields-of-glory", "small-skirmish"); * * //Send a chat message to the server, that it can broadcast: * connection.Send("chat", "Hey guys, are you ready to start the game?"); * * @example You can also build up messages as you go, if you don't know the exact payload until runtime. * In this example, imagine the player has multiple pieces and we send in the list of moves this player wants to do in a single turn. * * //Create a new message of type moves: * let m = connection.createMessage("moves"); * * //Add all pending moves to the message: * foreach(let move in moves) { * m.add(move.pieceid, move.x, move.y); * } * * //Send the message to the server: * connection.sendMessage(m); * */ export default class Message { /** * Type of the message. */ type: T; protected types: EntryType[]; protected objects: (string | number | boolean | ByteArray)[]; constructor(type: T); /** * The number of entries in this message. */ get length(): number; /** * Adds data entries to the Message object * @param {any[]} args Entries to add. Valid types are Number, String, Boolean and ByteArray. If a Number is passed, and it is an integer, it will be added to the first type it fits in this order: Int, UInt, Long, ULong. If it doesn't fit in any integer type, or if it's not an integer, it will be added as a Double. */ add(...args: any): void; /** * Add a value encoded as an int to the message * @param {number} value The number to add */ addInt(value: number): void; /** Add a value encoded as a uint to the message * @param {number} value The number to add */ addUInt(value: number): void; /** Add a value encoded as a long to the message * @param {number} value The number to add */ addLong(value: number): void; /** Add a value encoded as a ulong to the message * @param {number} value The number to add */ addULong(value: number): void; /** Add a boolean value to the message * @param {boolean} value The bool to add */ addBoolean(value: boolean): void; /** Add a value encoded as a float to the message * @param {number} value The number to add */ addFloat(value: number): void; /** Add a value encoded as a double to the message * @param {number} value The number to add */ addDouble(value: number): void; /** Add a byte array value to the message * @param {number[]} value The byte array to add */ addByteArray(value: number[] | Uint8Array | Buffer | ByteArray): void; /** Add a string value to the message * @param {string} value The string to add */ addString(value: string): void; /** Get the int from the message at the given index * @param {number} index The zero-based index of the entry to get * @return {number} */ getInt(index: number): number; /** Get the uint from the message at the given index * @param {number} index The zero-based index of the entry to get * @return {number} */ getUInt(index: number): number; /** Get the long from the message at the given index * @param {number} index The zero-based index of the entry to get * @return {number} */ getLong(index: number): number; /** Get the ulong from the message at the given index * @param {number} index The zero-based index of the entry to get * @return {number} */ getULong(index: number): number; /** Get the bool from the message at the given index * @param {number} index The zero-based index of the entry to get * @return {boolean} */ getBoolean(index: number): Boolean; /** Get the double from the message at the given index * @param {number} index The zero-based index of the entry to get * @return {number} */ getDouble(index: number): number; /** * Get the number (int, uint, long, ulong, float, double) from the message at the given index. * @param {number} index The zero-based index of the entry to get * @returns */ getNumber(index: number): number; /** Get the float from the message at the given index * @param {number} index The zero-based index of the entry to get * @return {number} */ getFloat(index: number): number; /** Get the int from the message at the given index * @param {number} index The zero-based index of the entry to get * @return {number} */ getByteArray(index: number): ByteArray; /** Get the string from the message at the given index * @param {number} index The zero-based index of the entry to get * @return {string} */ getString(index: number): String; /** Get a string representation of the message * @return {string} */ toString(): string; _internal_(method: "get-types"): EntryType[]; _internal_(method: "get-objects"): (string | number | boolean | ByteArray)[]; addT(check: boolean, value: any, type: EntryType, errorMessage: string): void; get(index: number, type: EntryType.Boolean): boolean; get(index: number, type: EntryType.ByteArray): ByteArray; get(index: number, type: EntryType.Double | EntryType.Float | EntryType.Integer | EntryType.Long | EntryType.Number | EntryType.UnsignedInteger | EntryType.UnsignedLong): number; get(index: number, type: EntryType.String): string; getTypeString(type?: EntryType): string; /** * @param {Buffer} value * @returns */ isByteArray(value: Uint8Array | Buffer | number[] | ByteArray): boolean; }