From 487907fb85be3743df8fac9de63ff38e224caa8b Mon Sep 17 00:00:00 2001 From: Casper Warden <216465704+casperwardensl@users.noreply.github.com> Date: Sun, 27 Feb 2022 13:09:12 +0000 Subject: [PATCH] Improve Name2Key support --- .npmignore | 1 + examples/Grid/Name2Key.ts | 50 ++++++++ lib/classes/Circuit.ts | 7 -- lib/classes/commands/GridCommands.ts | 176 +++++++++++---------------- package.json | 2 +- 5 files changed, 122 insertions(+), 114 deletions(-) create mode 100644 examples/Grid/Name2Key.ts diff --git a/.npmignore b/.npmignore index 2a57fb3..512d487 100644 --- a/.npmignore +++ b/.npmignore @@ -14,3 +14,4 @@ testing/ localDeploy.js caspertech-node-metaverse-*.tgz dist/classes/cache +examples/loginParameters.json diff --git a/examples/Grid/Name2Key.ts b/examples/Grid/Name2Key.ts new file mode 100644 index 0000000..e5be6a0 --- /dev/null +++ b/examples/Grid/Name2Key.ts @@ -0,0 +1,50 @@ +import { ExampleBot } from '../ExampleBot'; + +class Name2Key extends ExampleBot +{ + async onConnected(): Promise + { + const test1 = await this.bot.clientCommands.grid.avatarName2KeyAndName('casperhelp'); + if (test1.avatarKey.toString() === '828f7198-42e5-41b4-bd60-926a33ea067b' && test1.avatarName === 'CasperHelp Resident') + { + console.log('Test 1 passed'); + } + const test2 = await this.bot.clientCommands.grid.avatarName2KeyAndName('casperhelp resident'); + if (test2.avatarKey.toString() === '828f7198-42e5-41b4-bd60-926a33ea067b' && test2.avatarName === 'CasperHelp Resident') + { + console.log('Test 2 passed'); + } + + const test3 = await this.bot.clientCommands.grid.avatarName2KeyAndName('casperhelp.resident'); + if (test3.avatarKey.toString() === '828f7198-42e5-41b4-bd60-926a33ea067b' && test3.avatarName === 'CasperHelp Resident') + { + console.log('Test 3 passed'); + } + + const test4 = await this.bot.clientCommands.grid.avatarName2KeyAndName('casperhelp', false); + if (test4.avatarKey.toString() === '828f7198-42e5-41b4-bd60-926a33ea067b' && test4.avatarName === 'CasperHelp Resident') + { + console.log('Test 4 passed'); + } + + const test5 = await this.bot.clientCommands.grid.avatarName2KeyAndName('casperhelp resident', false); + if (test5.avatarKey.toString() === '828f7198-42e5-41b4-bd60-926a33ea067b' && test5.avatarName === 'CasperHelp Resident') + { + console.log('Test 5 passed'); + } + + const test6 = await this.bot.clientCommands.grid.avatarName2KeyAndName('casperhelp.resident', false); + if (test6.avatarKey.toString() === '828f7198-42e5-41b4-bd60-926a33ea067b' && test6.avatarName === 'CasperHelp Resident') + { + console.log('Test 6 passed'); + } + } +} + +new Name2Key().run().then(() => +{ + +}).catch((err) => +{ + console.error(err); +}); diff --git a/lib/classes/Circuit.ts b/lib/classes/Circuit.ts index 728d9c5..280f945 100644 --- a/lib/classes/Circuit.ts +++ b/lib/classes/Circuit.ts @@ -184,10 +184,6 @@ export class Circuit let subscription: null | Subscription = null; let timeout: Timer | null = null; const receivedChunks: { [key: number]: Buffer } = {}; - const progress = setInterval(() => - { - console.log( ' ... Got ' + Object.keys(receivedChunks).length + ' packets'); - }, 5000); const resetTimeout = function(): void { if (timeout !== null) @@ -200,7 +196,6 @@ export class Circuit { subscription.unsubscribe(); } - clearInterval(progress); reject(new Error('Xfer Timeout')); }, 10000); }; @@ -241,7 +236,6 @@ export class Circuit { subscription.unsubscribe(); } - clearInterval(progress); reject(new Error('Xfer Aborted')); } break; @@ -300,7 +294,6 @@ export class Circuit { subscription.unsubscribe(); } - clearInterval(progress); const buf = Buffer.concat(conc); if (buf.length !== dataSize) { diff --git a/lib/classes/commands/GridCommands.ts b/lib/classes/commands/GridCommands.ts index 68d3ce1..ece551a 100644 --- a/lib/classes/commands/GridCommands.ts +++ b/lib/classes/commands/GridCommands.ts @@ -269,131 +269,95 @@ export class GridCommands extends CommandsBase }); } - avatarName2KeyAndName(name: string): Promise<{ avatarKey: UUID, avatarName: string }> + async avatarName2KeyAndName(name: string, useCap = true): Promise<{ avatarKey: UUID, avatarName: string }> { name = name.trim().replace('.', ' '); + name = name.toLowerCase(); if (name.trim().indexOf(' ') === -1) { name = name.trim() + ' resident'; } - name = name.toLowerCase(); - const queryID = UUID.random(); - return new Promise<{ avatarKey: UUID, avatarName: string }>((resolve, reject) => + if (useCap && await this.currentRegion.caps.isCapAvailable('AvatarPickerSearch')) { - const aprm = new AvatarPickerRequestMessage(); - aprm.AgentData = { - AgentID: this.agent.agentID, - SessionID: this.circuit.sessionID, - QueryID: queryID - }; - aprm.Data = { - Name: Utils.StringToBuffer(name) - }; - - this.circuit.sendMessage(aprm, PacketFlags.Reliable); - this.circuit.waitForMessage(Message.AvatarPickerReply, 10000, (apr: AvatarPickerReplyMessage): FilterResponse => + const trimmedName = name.replace(' resident', ''); + const result = await this.currentRegion.caps.capsGetXML(['AvatarPickerSearch', { page_size: '100', names: trimmedName }]); + if (result.agents) { - if (apr.AgentData.QueryID.toString() === queryID.toString()) + for (const agent of result.agents) { - return FilterResponse.Finish; - } - else - { - return FilterResponse.NoMatch; - } - }).then((apr: AvatarPickerReplyMessage) => - { - let foundKey: UUID | undefined; - let foundName: string | undefined; - for (const dataBlock of apr.Data) - { - const resultName = (Utils.BufferToStringSimple(dataBlock.FirstName) + ' ' + - Utils.BufferToStringSimple(dataBlock.LastName)); - if (resultName.toLowerCase() === name) + if (!agent.username) { - foundKey = dataBlock.AvatarID; - foundName = resultName; + continue; + } + const avatarName = agent.legacy_first_name + ' ' + agent.legacy_last_name; + if (avatarName.toLowerCase() === name) + { + return { + avatarName, + avatarKey: new UUID(agent.id.toString()), + } } } + } + } - if (foundKey !== undefined && foundName !== undefined) - { - resolve({ - avatarName: foundName, - avatarKey: foundKey - }); - } - else - { - reject('Name not found') - } - }).catch((err) => + const queryID = UUID.random(); + + + const aprm = new AvatarPickerRequestMessage(); + aprm.AgentData = { + AgentID: this.agent.agentID, + SessionID: this.circuit.sessionID, + QueryID: queryID + }; + aprm.Data = { + Name: Utils.StringToBuffer(name) + }; + + this.circuit.sendMessage(aprm, PacketFlags.Reliable); + const apr: AvatarPickerReplyMessage = await this.circuit.waitForMessage(Message.AvatarPickerReply, 10000, (apr: AvatarPickerReplyMessage): FilterResponse => + { + if (apr.AgentData.QueryID.toString() === queryID.toString()) { - reject(err); - }); + return FilterResponse.Finish; + } + else + { + return FilterResponse.NoMatch; + } }); + + let foundKey: UUID | undefined; + let foundName: string | undefined; + for (const dataBlock of apr.Data) + { + const resultName = (Utils.BufferToStringSimple(dataBlock.FirstName) + ' ' + + Utils.BufferToStringSimple(dataBlock.LastName)); + if (resultName.toLowerCase() === name) + { + foundKey = dataBlock.AvatarID; + foundName = resultName; + } + } + + if (foundKey !== undefined && foundName !== undefined) + { + return { + avatarName: foundName, + avatarKey: foundKey + }; + } + else + { + throw new Error('Name not found'); + } } - avatarName2Key(name: string): Promise + async avatarName2Key(name: string, useCap = true): Promise { - name = name.trim().replace('.', ' '); - if (name.trim().indexOf(' ') === -1) - { - name = name.trim() + ' resident'; - } - name = name.toLowerCase(); - - const queryID = UUID.random(); - return new Promise((resolve, reject) => - { - const aprm = new AvatarPickerRequestMessage(); - aprm.AgentData = { - AgentID: this.agent.agentID, - SessionID: this.circuit.sessionID, - QueryID: queryID - }; - aprm.Data = { - Name: Utils.StringToBuffer(name) - }; - - this.circuit.sendMessage(aprm, PacketFlags.Reliable); - this.circuit.waitForMessage(Message.AvatarPickerReply, 10000, (apr: AvatarPickerReplyMessage): FilterResponse => - { - if (apr.AgentData.QueryID.toString() === queryID.toString()) - { - return FilterResponse.Finish; - } - else - { - return FilterResponse.NoMatch; - } - }).then((apr: AvatarPickerReplyMessage) => - { - let found: UUID | null = null; - for (const dataBlock of apr.Data) - { - const resultName = (Utils.BufferToStringSimple(dataBlock.FirstName) + ' ' + - Utils.BufferToStringSimple(dataBlock.LastName)).toLowerCase(); - if (resultName === name) - { - found = dataBlock.AvatarID; - } - } - - if (found !== null) - { - resolve(found); - } - else - { - reject('Name not found') - } - }).catch((err) => - { - reject(err); - }); - }); + const result = await this.avatarName2KeyAndName(name, useCap); + return result.avatarKey; } async getBalance(): Promise diff --git a/package.json b/package.json index abb1255..51d77da 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@caspertech/node-metaverse", - "version": "0.5.29", + "version": "0.5.31", "description": "A node.js interface for Second Life.", "main": "dist/lib/index.js", "types": "dist/lib/index.d.ts",