Prettier
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
import Color from 'color'
|
||||
import { encrypt } from 'romulus-js'
|
||||
import { DEFAULT_KEY, MessageTypes } from '../common'
|
||||
import { numberToUint16BE } from '../utilities/number'
|
||||
import { SmartBuffer } from '../utilities/smart-buffer'
|
||||
import { packOutgoingPacket } from './packet'
|
||||
import Color from "color";
|
||||
import { encrypt } from "romulus-js";
|
||||
import { DEFAULT_KEY, MessageTypes } from "../common";
|
||||
import { numberToUint16BE } from "../utilities/number";
|
||||
import { SmartBuffer } from "../utilities/smart-buffer";
|
||||
import { packOutgoingPacket } from "./packet";
|
||||
|
||||
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.UserDataRequest)
|
||||
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.UserDataRequest);
|
||||
|
||||
export interface UserDataRequestMessage {
|
||||
username: string
|
||||
colour: Color
|
||||
clientId: string
|
||||
username: string;
|
||||
colour: Color;
|
||||
clientId: string;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -19,33 +19,36 @@ export interface UserDataRequestMessage {
|
||||
* @param key The key to encrypt the data with.
|
||||
* @returns An outgoing user data request (0x0002) packet.
|
||||
*/
|
||||
export function packUserDataRequestMessage (properties: UserDataRequestMessage, key: Uint8Array = DEFAULT_KEY): Uint8Array {
|
||||
const encoder = new TextEncoder()
|
||||
export function packUserDataRequestMessage(
|
||||
properties: UserDataRequestMessage,
|
||||
key: Uint8Array = DEFAULT_KEY,
|
||||
): Uint8Array {
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
// Prepare data in correct format.
|
||||
const username = encoder.encode(properties.username)
|
||||
const usernameLength = numberToUint16BE(username.length)
|
||||
const colour = new Uint8Array(properties.colour.array())
|
||||
const clientId = encoder.encode(properties.clientId)
|
||||
const clientIdLength = numberToUint16BE(clientId.length)
|
||||
const username = encoder.encode(properties.username);
|
||||
const usernameLength = numberToUint16BE(username.length);
|
||||
const colour = new Uint8Array(properties.colour.array());
|
||||
const clientId = encoder.encode(properties.clientId);
|
||||
const clientIdLength = numberToUint16BE(clientId.length);
|
||||
|
||||
// Pack data.
|
||||
const packedData = new SmartBuffer()
|
||||
packedData.writeBytes(usernameLength)
|
||||
packedData.writeBytes(username)
|
||||
packedData.pad(32 - username.length)
|
||||
packedData.writeBytes(colour)
|
||||
packedData.writeBytes(clientIdLength)
|
||||
packedData.writeBytes(clientId)
|
||||
packedData.pad(32 - clientId.length)
|
||||
const packedData = new SmartBuffer();
|
||||
packedData.writeBytes(usernameLength);
|
||||
packedData.writeBytes(username);
|
||||
packedData.pad(32 - username.length);
|
||||
packedData.writeBytes(colour);
|
||||
packedData.writeBytes(clientIdLength);
|
||||
packedData.writeBytes(clientId);
|
||||
packedData.pad(32 - clientId.length);
|
||||
|
||||
// Encrypt the data.
|
||||
const data = encrypt(packedData.data, MESSAGE_TYPE, key)
|
||||
const data = encrypt(packedData.data, MESSAGE_TYPE, key);
|
||||
|
||||
return packOutgoingPacket({
|
||||
messageType: MESSAGE_TYPE,
|
||||
data: data
|
||||
})
|
||||
data: data,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,23 +56,25 @@ export function packUserDataRequestMessage (properties: UserDataRequestMessage,
|
||||
* @param data The decrypted data section of an incoming user data request (0x0002) message.
|
||||
* @returns An unpacked user data request (0x0002) message.
|
||||
*/
|
||||
export function unpackUserDataRequestMessage (data: Uint8Array): UserDataRequestMessage {
|
||||
export function unpackUserDataRequestMessage(
|
||||
data: Uint8Array,
|
||||
): UserDataRequestMessage {
|
||||
// Unpack and read data in correct format.
|
||||
const packedData = SmartBuffer.from(data)
|
||||
const packedData = SmartBuffer.from(data);
|
||||
|
||||
const usernameLength = packedData.readUInt16()
|
||||
const username = packedData.readBytes(usernameLength)
|
||||
packedData.cursor = 34
|
||||
const colour = packedData.readBytes(3)
|
||||
const clientIdLength = packedData.readUInt16()
|
||||
const clientId = packedData.readBytes(clientIdLength)
|
||||
const usernameLength = packedData.readUInt16();
|
||||
const username = packedData.readBytes(usernameLength);
|
||||
packedData.cursor = 34;
|
||||
const colour = packedData.readBytes(3);
|
||||
const clientIdLength = packedData.readUInt16();
|
||||
const clientId = packedData.readBytes(clientIdLength);
|
||||
|
||||
const decoder = new TextDecoder()
|
||||
const decoder = new TextDecoder();
|
||||
|
||||
// Return data in correct format.
|
||||
return {
|
||||
username: decoder.decode(username),
|
||||
colour: Color.rgb(colour),
|
||||
clientId: decoder.decode(clientId)
|
||||
}
|
||||
clientId: decoder.decode(clientId),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user