From dcfa0ff2dfaeb1ee9a98731225d86b4331f05b33 Mon Sep 17 00:00:00 2001 From: Gary Rutland Date: Fri, 9 Dec 2022 14:01:13 +0000 Subject: [PATCH 01/10] Moved Chorus functionality to a class --- src/chorus.ts | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/game.ts | 52 +++++++++--------------------------------- 2 files changed, 72 insertions(+), 42 deletions(-) create mode 100644 src/chorus.ts diff --git a/src/chorus.ts b/src/chorus.ts new file mode 100644 index 0000000..72f5e46 --- /dev/null +++ b/src/chorus.ts @@ -0,0 +1,62 @@ +import { signedFetch } from '@decentraland/SignedFetch' +import * as utils from '@dcl/ecs-scene-utils' + +export class Chorus { + private readonly _entity: Entity + private readonly _stream: string + private readonly _interval: number + + private static URL = 'https://chorus.lickd.co' + + public constructor(entity: Entity, stream: string, interval: number = 60000) { + this._entity = entity + this._stream = stream + this._interval = interval + } + + public async start() { + try { + const response = await signedFetch(Chorus.URL + '/api/session/create/dcl', { + headers: { 'Content-Type': 'application/json' }, + method: 'POST', + body: JSON.stringify({ + stream: this._stream + }) + }) + + const json = await JSON.parse(response.text || '{}') + + if (json.token) { + this._entity.addComponent(new AudioStream(Chorus.URL + this._stream + '?token=' + json.token)) + this._entity.addComponent(new utils.Interval(this._interval, async () => await this.heartbeat(json.token))) + } + } catch (e) { + log(e) + } + } + + public async stop() { + this._entity.removeComponent(AudioStream) + this._entity.removeComponent(utils.Interval) + } + + private async heartbeat(token: string) { + let active = false + + try { + const response = await signedFetch(Chorus.URL + '/api/session/heartbeat/dcl', { + headers: { 'Content-Type': 'application/json' }, + method: 'POST', + body: JSON.stringify({ token }) + }) + + active = response.status === 200 + } catch (e) { + log(e) + } + + if (!active) { + await this.stop() + } + } +} diff --git a/src/game.ts b/src/game.ts index 1d524c5..ce7461c 100644 --- a/src/game.ts +++ b/src/game.ts @@ -1,54 +1,22 @@ +import { Chorus } from './chorus' import { getUserData } from '@decentraland/Identity' -import { signedFetch } from '@decentraland/SignedFetch' -import * as utils from '@dcl/ecs-scene-utils' - -const stationURL = 'https://chorus.lickd.co' -const stream = '/lickd/test.mp3' void executeTask(async () => { + const chorusEntity = new Entity() + const chorus = new Chorus(chorusEntity, '/genre/chill.mp3', 10000) const me = await getUserData() onEnterSceneObservable.add(async (player) => { if (player.userId === me?.userId) { - await radioStart() + await chorus.start() } }) -}) - -async function radioStart() { - try { - const response = await signedFetch(stationURL + '/api/session/create/dcl', { - headers: { 'Content-Type': 'application/json' }, - method: 'POST', - body: JSON.stringify({ stream }) - }) - - if (response.text) { - const json = await JSON.parse(response.text) - - if (json.token) { - const streamSource = new Entity() - const audioStream = new AudioStream(stationURL + stream + '?token=' + json.token) - streamSource.addComponent(audioStream) - streamSource.addComponent(new utils.Interval(60000, async () => await heartbeat(json.token))) - - engine.addEntity(streamSource) - } + onLeaveSceneObservable.add(async (player) => { + if (player.userId === me?.userId) { + await chorus.stop() } - } catch (e) { - log(e) - } -} + }) -async function heartbeat(token: string) { - try { - await signedFetch(stationURL + '/api/session/heartbeat/dcl', { - headers: { 'Content-Type': 'application/json' }, - method: 'POST', - body: JSON.stringify({ stream, token }) - }) - } catch (e) { - log(e) - } -} + engine.addEntity(chorusEntity) +}) From 569d0ccc7445e386c13658abdb22d5c1290d9802 Mon Sep 17 00:00:00 2001 From: Gary Rutland Date: Wed, 11 Jan 2023 14:31:10 +0000 Subject: [PATCH 02/10] Updated to use library --- .npmrc | 3 ++- package.json | 4 ++-- src/chorus.ts | 62 --------------------------------------------------- src/game.ts | 10 +++++---- tsconfig.json | 4 ++++ 5 files changed, 14 insertions(+), 69 deletions(-) delete mode 100644 src/chorus.ts diff --git a/.npmrc b/.npmrc index 4fd0219..c866519 100644 --- a/.npmrc +++ b/.npmrc @@ -1 +1,2 @@ -engine-strict=true \ No newline at end of file +engine-strict=true +@lickdltd:registry=https://npm.pkg.github.com diff --git a/package.json b/package.json index 2eb4c86..3f7c3b6 100644 --- a/package.json +++ b/package.json @@ -22,9 +22,9 @@ "yarn": "please use npm" }, "dependencies": { - "@dcl/ecs-scene-utils": "^1.7.5" + "@lickdltd/chorus-dcl": "0.1.0" }, "bundleDependencies": [ - "@dcl/ecs-scene-utils" + "@lickdltd/chorus-dcl" ] } diff --git a/src/chorus.ts b/src/chorus.ts deleted file mode 100644 index 72f5e46..0000000 --- a/src/chorus.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { signedFetch } from '@decentraland/SignedFetch' -import * as utils from '@dcl/ecs-scene-utils' - -export class Chorus { - private readonly _entity: Entity - private readonly _stream: string - private readonly _interval: number - - private static URL = 'https://chorus.lickd.co' - - public constructor(entity: Entity, stream: string, interval: number = 60000) { - this._entity = entity - this._stream = stream - this._interval = interval - } - - public async start() { - try { - const response = await signedFetch(Chorus.URL + '/api/session/create/dcl', { - headers: { 'Content-Type': 'application/json' }, - method: 'POST', - body: JSON.stringify({ - stream: this._stream - }) - }) - - const json = await JSON.parse(response.text || '{}') - - if (json.token) { - this._entity.addComponent(new AudioStream(Chorus.URL + this._stream + '?token=' + json.token)) - this._entity.addComponent(new utils.Interval(this._interval, async () => await this.heartbeat(json.token))) - } - } catch (e) { - log(e) - } - } - - public async stop() { - this._entity.removeComponent(AudioStream) - this._entity.removeComponent(utils.Interval) - } - - private async heartbeat(token: string) { - let active = false - - try { - const response = await signedFetch(Chorus.URL + '/api/session/heartbeat/dcl', { - headers: { 'Content-Type': 'application/json' }, - method: 'POST', - body: JSON.stringify({ token }) - }) - - active = response.status === 200 - } catch (e) { - log(e) - } - - if (!active) { - await this.stop() - } - } -} diff --git a/src/game.ts b/src/game.ts index ce7461c..a498818 100644 --- a/src/game.ts +++ b/src/game.ts @@ -1,20 +1,22 @@ -import { Chorus } from './chorus' import { getUserData } from '@decentraland/Identity' +import * as chorus from '@lickdltd/chorus-dcl' void executeTask(async () => { const chorusEntity = new Entity() - const chorus = new Chorus(chorusEntity, '/genre/chill.mp3', 10000) + const chorusPlayer = new chorus.Player(chorusEntity, '/lickd/test.mp3') const me = await getUserData() + await chorusPlayer.start() + onEnterSceneObservable.add(async (player) => { if (player.userId === me?.userId) { - await chorus.start() + await chorusPlayer.start() } }) onLeaveSceneObservable.add(async (player) => { if (player.userId === me?.userId) { - await chorus.stop() + await chorusPlayer.stop() } }) diff --git a/tsconfig.json b/tsconfig.json index a38f9cf..e65adad 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,6 +6,10 @@ "paths": { "@dcl/ecs-scene-utils": [ "node_modules/@dcl/ecs-scene-utils/dist/index.d.ts" + ], + "@lickdltd/chorus-dcl": [ + "node_modules/@lickdltd/chorus-dcl/dist/index.d.ts", + "../chorus-dcl/dist/index.d.ts" ] }, "baseUrl": "." From 8567dbc1b1b6e1a56bb71892cee2b5e190f6aa75 Mon Sep 17 00:00:00 2001 From: Gary Rutland Date: Wed, 11 Jan 2023 14:36:25 +0000 Subject: [PATCH 03/10] Update ci.yml --- .github/workflows/ci.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b371d86..aecfe52 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,13 +6,19 @@ on: jobs: lint-and-build: + name: Lint & build runs-on: ubuntu-latest + permissions: + contents: read + packages: write steps: - uses: actions/checkout@v2 - name: Use Node.js 16.x uses: actions/setup-node@v1 with: node-version: 16.x + registry-url: https://npm.pkg.github.com + scope: '@lickdltd' - name: install decentraland run: npm i -g decentraland@latest - name: install dependencies From 3260a507cc88fbf063383f2be3e19be02db54408 Mon Sep 17 00:00:00 2001 From: Gary Rutland Date: Wed, 11 Jan 2023 14:40:40 +0000 Subject: [PATCH 04/10] Update ci.yml --- .github/workflows/ci.yml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index aecfe52..640b5b7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,17 +8,12 @@ jobs: lint-and-build: name: Lint & build runs-on: ubuntu-latest - permissions: - contents: read - packages: write steps: - uses: actions/checkout@v2 - name: Use Node.js 16.x uses: actions/setup-node@v1 with: node-version: 16.x - registry-url: https://npm.pkg.github.com - scope: '@lickdltd' - name: install decentraland run: npm i -g decentraland@latest - name: install dependencies From 4710aa88a74305bbb43f8f99e805626c1ff54713 Mon Sep 17 00:00:00 2001 From: Gary Rutland Date: Wed, 11 Jan 2023 14:41:49 +0000 Subject: [PATCH 05/10] Update ci.yml --- .github/workflows/ci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 640b5b7..781d684 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,6 +8,9 @@ jobs: lint-and-build: name: Lint & build runs-on: ubuntu-latest + permissions: + contents: read + packages: read steps: - uses: actions/checkout@v2 - name: Use Node.js 16.x From 30a69f6cb2e55bae4e0d25ce1d02b73b9de00fd7 Mon Sep 17 00:00:00 2001 From: Gary Rutland Date: Wed, 11 Jan 2023 14:43:52 +0000 Subject: [PATCH 06/10] Update ci.yml --- .github/workflows/ci.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 781d684..94cc361 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,10 +17,14 @@ jobs: uses: actions/setup-node@v1 with: node-version: 16.x + registry-url: https://npm.pkg.github.com + scope: '@lickdltd' - name: install decentraland run: npm i -g decentraland@latest - name: install dependencies run: npm i + env: + NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: lint run: npm run lint - name: build From 97229015a72e85c53f4a7e1aa2146efc0a3f6d72 Mon Sep 17 00:00:00 2001 From: Gary Rutland Date: Wed, 11 Jan 2023 14:49:31 +0000 Subject: [PATCH 07/10] Update settings.yml --- .github/settings.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/settings.yml b/.github/settings.yml index 7188767..46ea225 100644 --- a/.github/settings.yml +++ b/.github/settings.yml @@ -30,7 +30,7 @@ branches: required_status_checks: strict: true contexts: - - lint-and-build + - test-build / Lint & build enforce_admins: false required_linear_history: true restrictions: From 10632f198963ba6068ae046477485e97a46885a3 Mon Sep 17 00:00:00 2001 From: Gary Rutland Date: Wed, 11 Jan 2023 15:48:27 +0000 Subject: [PATCH 08/10] Corrected paths --- package.json | 4 ++-- tsconfig.json | 10 +++------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 3f7c3b6..c3a5ac5 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ }, "devDependencies": { "@dcl/eslint-config": "^1.0.1", - "decentraland-ecs": "latest" + "decentraland-ecs": "^6.11.11" }, "engines": { "node": ">=14.0.0", @@ -22,7 +22,7 @@ "yarn": "please use npm" }, "dependencies": { - "@lickdltd/chorus-dcl": "0.1.0" + "@lickdltd/chorus-dcl": "^0.1.0" }, "bundleDependencies": [ "@lickdltd/chorus-dcl" diff --git a/tsconfig.json b/tsconfig.json index e65adad..33c5a9d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,16 +3,12 @@ "outFile": "./bin/game.js", "allowJs": true, "strict": true, + "baseUrl": ".", "paths": { - "@dcl/ecs-scene-utils": [ - "node_modules/@dcl/ecs-scene-utils/dist/index.d.ts" - ], "@lickdltd/chorus-dcl": [ - "node_modules/@lickdltd/chorus-dcl/dist/index.d.ts", - "../chorus-dcl/dist/index.d.ts" + "node_modules/@lickdltd/chorus-dcl/dist/index.d.ts" ] - }, - "baseUrl": "." + } }, "include": [ "src/**/*.ts" From edc1d5fa3a25dbafeaeff99291385ad952d91008 Mon Sep 17 00:00:00 2001 From: Gary Rutland Date: Wed, 11 Jan 2023 16:14:36 +0000 Subject: [PATCH 09/10] Implementation corrections --- package.json | 4 +++- tsconfig.json | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index c3a5ac5..bb4ad76 100644 --- a/package.json +++ b/package.json @@ -22,9 +22,11 @@ "yarn": "please use npm" }, "dependencies": { - "@lickdltd/chorus-dcl": "^0.1.0" + "@dcl/ecs-scene-utils": "^1.7.5", + "@lickdltd/chorus-dcl": "^0.1.1" }, "bundleDependencies": [ + "@dcl/ecs-scene-utils", "@lickdltd/chorus-dcl" ] } diff --git a/tsconfig.json b/tsconfig.json index 33c5a9d..6d9e25b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,6 +7,9 @@ "paths": { "@lickdltd/chorus-dcl": [ "node_modules/@lickdltd/chorus-dcl/dist/index.d.ts" + ], + "@dcl/ecs-scene-utils": [ + "node_modules/@dcl/ecs-scene-utils/dist/index.d.ts" ] } }, From c2db379350fe5f8aa4e131b32ab71df0266cff30 Mon Sep 17 00:00:00 2001 From: Gary Rutland Date: Wed, 11 Jan 2023 16:49:21 +0000 Subject: [PATCH 10/10] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index bb4ad76..bc9befc 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ }, "dependencies": { "@dcl/ecs-scene-utils": "^1.7.5", - "@lickdltd/chorus-dcl": "^0.1.1" + "@lickdltd/chorus-dcl": "^0.2.0" }, "bundleDependencies": [ "@dcl/ecs-scene-utils",