Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let msgpackState = { players: {} };
const fixedUnitId = nanoid(9);
for (let i=0; i<100; i++) {
const id = (i === 50) ? fixedUnitId : nanoid(9);
const name = "Player " + i;
const x = Math.floor(Math.random() * 200);
const y = Math.floor(Math.random() * 200);
state.players[ id ] = new Player(name, x, y);
msgpackState.players[id] = { name, x, y };
}
const firstStateEncoded = msgpack.encode(msgpackState);
let encoded = state.encode();
const decodedState = new State();
decodedState.decode(encoded);
console.log("(@colyseus/state) INITIAL STATE SIZE:", encoded.length);
console.log("(msgpack) INITIAL STATE SIZE:", firstStateEncoded.length);
console.log("");
{
// CHANGE X/Y OF A SINGLE ENTITY
const x = Math.floor(Math.random() * 200);
const y = Math.floor(Math.random() * 200);
state.players[fixedUnitId].x = x
state.players[fixedUnitId].y = y
msgpackState.players[fixedUnitId].x = x
// for (let id in msgpackState.players) {
// const x = Math.floor(Math.random() * 200);
// const y = Math.floor(Math.random() * 200);
// state.players[id].x = x
// state.players[id].y = y
// msgpackState.players[id].x = x
// msgpackState.players[id].y = y
// i++;
// }
// }
encoded = state.encode();
decodedState.decode(encoded);
console.log("(@colyseus/state) SUBSEQUENT PATCH:", encoded.length);
console.log("(msgpack) SUBSEQUENT PATCH:", fossildelta.create(firstStateEncoded, msgpack.encode(msgpackState)).length);
const serializeCommand = (message: Messages): ArrayBuffer => {
const bytes = encode(message.toJSON())
const payload = encode([getName(message), bytes]).slice()
const data = BinaryMessageFormat.write(payload)
return data
}
const serializeCommand = (message: Messages): ArrayBuffer => {
const bytes = encode(message.toJSON())
const payload = encode([getName(message), bytes]).slice()
const data = BinaryMessageFormat.write(payload)
return data
}
private encodeToBuffer(value: unknown) {
const encoded = encode(value, { extensionCodec: this.extensionCodec });
return Buffer.from(encoded.buffer, encoded.byteOffset, encoded.byteLength);
}
send(resp: any, isError?: boolean): void {
if (this.sent) {
throw new Error(`Response to id ${this.requestId} already sent`);
}
const encoded = encode([
1,
this.requestId,
isError ? resp : null,
!isError ? resp : null,
]);
this.encoder.write(
Buffer.from(encoded.buffer, encoded.byteOffset, encoded.byteLength)
);
this.sent = true;
}
}
async _send(name, payload) {
let packet = msgpack.encode(payload);
this.bytes_sent += packet.length;
let socket = this.sockets[name];
if (this.socketConfigs[name].type === Radio) {
return await socket.send(packet, { group: '' });
}
return await socket.send(packet);
}
const {options, fileFs} = buildDatabase();
const db = new databaseModule.Database(options, fileFs);
const folderStub = buildFolder();
db.initAccount({type: "protonmail", login: "login1"})
.folders[folderStub.pk] = await validateEntity("folders", folderStub, "protonmail");
t.false(encryptionAdapterWriteSpy.called);
await db.saveToFile();
const dump = JSON.parse(JSON.stringify(db.readonlyDbInstance()));
t.is(1, encryptionAdapterWriteSpy.callCount);
t.deepEqual(
Buffer.from(
msgpack.encode(dump),
),
encryptionAdapterWriteSpy.getCall(0).args[0],
);
});