From 6e0d79715e9d977ec5f7ece340379993ce4b3707 Mon Sep 17 00:00:00 2001 From: Casper Warden <216465704+casperwardensl@users.noreply.github.com> Date: Tue, 1 Dec 2020 16:46:33 +0000 Subject: [PATCH] #24 Ability to restart regions, complete with example --- examples/Region/Estate.ts | 28 ++++++++++++++++ lib/classes/Comms.ts | 3 +- lib/classes/commands/RegionCommands.ts | 45 ++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 examples/Region/Estate.ts diff --git a/examples/Region/Estate.ts b/examples/Region/Estate.ts new file mode 100644 index 0000000..02dcb06 --- /dev/null +++ b/examples/Region/Estate.ts @@ -0,0 +1,28 @@ +import { ExampleBot } from '../ExampleBot'; + +class Estate extends ExampleBot +{ + wait(ms: number): Promise + { + return new Promise((resolve, reject) => + { + setTimeout(() => + { + resolve(); + }, ms) + }); + } + async onConnected() + { + console.log('Sending a message'); + await this.bot.clientCommands.region.simulatorMessage('In about 10 seconds, the region will begin to restart. This is only a test, and the restart will be cancelled.'); + await this.wait(10000); + console.log('Restarting'); + await this.bot.clientCommands.region.restartRegion(120); + await this.wait(10000); + console.log('Canceling restart'); + await this.bot.clientCommands.region.cancelRestart(); + } +} + +new Estate().run().then(() => {}).catch((err) => { console.error(err) }); diff --git a/lib/classes/Comms.ts b/lib/classes/Comms.ts index 757d2a9..a2541ce 100644 --- a/lib/classes/Comms.ts +++ b/lib/classes/Comms.ts @@ -299,9 +299,8 @@ export class Comms { // TODO: this isn't finished const alertm = packet.message as AlertMessageMessage; - const alertMessage = Utils.BufferToStringSimple(alertm.AlertData.Message); - + console.log('AlertMessage: ' + alertMessage); for (const info of alertm.AlertInfo) { const alertInfoMessage = Utils.BufferToStringSimple(info.Message); diff --git a/lib/classes/commands/RegionCommands.ts b/lib/classes/commands/RegionCommands.ts index 706d356..2df8212 100644 --- a/lib/classes/commands/RegionCommands.ts +++ b/lib/classes/commands/RegionCommands.ts @@ -33,6 +33,7 @@ import { ObjectResolver } from '../ObjectResolver'; import Timer = NodeJS.Timer; import Timeout = NodeJS.Timeout; import { Avatar } from '../public/Avatar'; +import { EstateOwnerMessageMessage } from '../messages/EstateOwnerMessage'; export class RegionCommands extends CommandsBase { @@ -116,6 +117,50 @@ export class RegionCommands extends CommandsBase }); } + async estateMessage(method: string, params: string[]) + { + const msg = new EstateOwnerMessageMessage(); + msg.AgentData = { + AgentID: this.agent.agentID, + SessionID: this.circuit.sessionID, + TransactionID: UUID.zero() + }; + msg.MethodData = { + Invoice: UUID.random(), + Method: Utils.StringToBuffer(method), + } + msg.ParamList = []; + for (const param of params) + { + msg.ParamList.push({ + Parameter: Utils.StringToBuffer(param) + }) + } + const sequenceID = this.circuit.sendMessage(msg, PacketFlags.Reliable); + return this.circuit.waitForAck(sequenceID, 10000); + } + + restartRegion(secs: number): Promise + { + return this.estateMessage('restart', [String(secs)]); + } + + async simulatorMessage(message: string) + { + return this.estateMessage('simulatormessage', [ + '-1', + '-1', + this.agent.agentID.toString(), + this.agent.firstName + ' ' + this.agent.lastName, + message + ]); + } + + cancelRestart(): Promise + { + return this.restartRegion(-1); + } + getAvatarsInRegion(): Avatar[] { return Object.values(this.currentRegion.agents);