From 4951dfe2fe6b43d8153bfd0be71679b927847dbf Mon Sep 17 00:00:00 2001 From: Richard Fox Date: Fri, 21 Jul 2017 18:42:21 +0200 Subject: [PATCH 1/6] move make controls to its own file --- examples/basic.ts | 49 +++---------------------------------- examples/dynamicControls.ts | 49 +++---------------------------------- examples/groups.ts | 45 +++------------------------------- examples/util.ts | 38 ++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 131 deletions(-) create mode 100644 examples/util.ts diff --git a/examples/basic.ts b/examples/basic.ts index ed358aa..dfc5402 100644 --- a/examples/basic.ts +++ b/examples/basic.ts @@ -4,12 +4,12 @@ import * as WebSocket from 'ws'; import { GameClient, IButton, - IButtonData, - IControlData, IParticipant, setWebSocket, } from '../lib'; +import { makeControls } from './util'; + if (process.argv.length < 4) { console.log('Usage gameClient.exe '); process.exit(); @@ -28,47 +28,6 @@ client.on('message', (err: any) => console.log('<<<', err)); client.on('send', (err: any) => console.log('>>>', err)); // client.on('error', (err: any) => console.log(err)); -/** - * This makes button objects, it will make the amount of buttons we tell it to - * we'll use it to create controls dynamically! - */ -function makeControls(amount: number): IControlData[] { - const controls: IButtonData[] = []; - const size = 10; - for (let i = 0; i < amount; i++) { - controls.push({ - controlID: `${i}`, - kind: 'button', - text: `Button ${i}`, - cost: 1, - position: [ - { - size: 'large', - width: size, - height: size / 2, - x: i * size, - y: 1, - }, - { - size: 'small', - width: size, - height: size / 2, - x: i * size, - y: 1, - }, - { - size: 'medium', - width: size, - height: size, - x: i * size, - y: 1, - }, - ], - }, - ); - } - return controls; -} // Now we open the connection passing in our authentication details and an experienceId. client.open({ authToken: process.argv[2], @@ -78,7 +37,7 @@ client.open({ // Every Interactive Experience has a "default" scene so we'll add them there there. return client.createControls({ sceneID: 'default', - controls: makeControls(5), + controls: makeControls(5, i => `Button ${i}`), }); }).then(controls => { @@ -103,7 +62,7 @@ client.open({ }); }); // Controls don't appear unless we tell Interactive that we are ready! - client.ready(true); + return client.ready(true); }); client.state.on('participantJoin', participant => { diff --git a/examples/dynamicControls.ts b/examples/dynamicControls.ts index 97ce4ac..28737a9 100644 --- a/examples/dynamicControls.ts +++ b/examples/dynamicControls.ts @@ -6,12 +6,12 @@ import * as faker from 'faker'; import { delay, GameClient, - IButtonData, - IControlData, IParticipant, setWebSocket, } from '../lib'; +import { makeControls } from './util'; + if (process.argv.length < 4) { console.log('Usage gameClient.exe '); process.exit(); @@ -30,47 +30,6 @@ client.on('open', () => console.log('Connected to interactive')); // client.on('send', (err: any) => console.log('>>>', err)); // client.on('error', (err: any) => console.log(err)); -/** - * This makes button objects, it will make the amount of buttons we tell it to - * we'll use it to create controls dynamically! - */ -function makeControls(amount: number): IControlData[] { - const controls: IButtonData[] = []; - const size = 10; - for (let i = 0; i < amount; i++) { - controls.push({ - controlID: `${i}`, - kind: 'button', - text: faker.name.firstName(), - cost: 1, - position: [ - { - size: 'large', - width: size, - height: size / 2, - x: i * size, - y: 1, - }, - { - size: 'small', - width: size, - height: size / 2, - x: i * size, - y: 1, - }, - { - size: 'medium', - width: size, - height: size, - x: i * size, - y: 1, - }, - ], - }, - ); - } - return controls; -} const delayTime = 2000; /* Loop creates 5 controls and adds them to the default scene. @@ -79,7 +38,7 @@ const delayTime = 2000; */ function loop() { const scene = client.state.getScene('default'); - scene.createControls(makeControls(5)) + scene.createControls(makeControls(5, () => faker.name.firstName())) .then(() => delay(delayTime)) .then(() => scene.deleteAllControls()) .then(() => delay(delayTime)) @@ -92,7 +51,7 @@ client.open({ versionId: parseInt(process.argv[3], 10), }) .then(() => { - /* Pull in the scenes stored on the server + /* Pull in the state stored on the server * then call ready so our controls show up. * then call loop() to begin our loop. */ diff --git a/examples/groups.ts b/examples/groups.ts index 3100067..efa0f03 100644 --- a/examples/groups.ts +++ b/examples/groups.ts @@ -11,6 +11,8 @@ import { Group } from '../lib'; +import { makeControls } from './util'; + if (process.argv.length < 4) { console.log('Usage gameClient.exe '); console.log(process.argv); @@ -37,45 +39,6 @@ const delayTime = 2000; // client.on('send', (err: any) => console.log('>>>', err)); // client.on('error', (err: any) => console.log(err)); -/** - * This makes button objects with the text set to the name of the scene. - */ -function makeControls(scene: string): IControlData[] { - const controls: IButtonData[] = []; - const size = 30; - controls.push({ - controlID: 'control0', - kind: 'button', - text: `Scene: ${scene}`, - cost: 1, - position: [ - { - size: 'large', - width: size, - height: size / 2, - x: 1, - y: 1, - }, - { - size: 'small', - width: size, - height: size / 2, - x: 1, - y: 1, - }, - { - size: 'medium', - width: size, - height: size, - x: 1, - y: 1, - }, - ], - }, - ); - return controls; -} - /** * Swaps the group the current participant is in between secondGroup and default. */ @@ -106,11 +69,11 @@ function removeParticipant(participantSessionId: string): void { */ function createScenes(): Promise { const defaultScene = client.state.getScene('default'); - defaultScene.createControls(makeControls('default')); + defaultScene.createControls(makeControls(1, () => 'Scene: default')); const secondScene = { sceneID: 'secondScene', - controls: makeControls('second') + controls: makeControls(1, () => 'Scene: second'), }; return client.createScenes({ diff --git a/examples/util.ts b/examples/util.ts new file mode 100644 index 0000000..7692776 --- /dev/null +++ b/examples/util.ts @@ -0,0 +1,38 @@ +import { IButtonData, IControlData } from '../lib'; +export function makeControls(amount: number = 5, textGenerator: (i:number) => string):IControlData[] { + const controls: IButtonData[] = []; + const size = 10; + for (let i = 0; i < amount; i++) { + controls.push({ + controlID: `${i}`, + kind: 'button', + text: textGenerator(i), + cost: 1, + position: [ + { + size: 'large', + width: size, + height: size / 2, + x: i * size, + y: 1, + }, + { + size: 'small', + width: size, + height: size / 2, + x: i * size, + y: 1, + }, + { + size: 'medium', + width: size, + height: size, + x: i * size, + y: 1, + }, + ], + }, + ); + } + return controls; +} From dc12183382a9514efdd3d733cd0990f9fb871504 Mon Sep 17 00:00:00 2001 From: Richard Fox Date: Fri, 21 Jul 2017 18:53:41 +0200 Subject: [PATCH 2/6] ring the terminal bell --- examples/basic.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/basic.ts b/examples/basic.ts index dfc5402..048aa3d 100644 --- a/examples/basic.ts +++ b/examples/basic.ts @@ -50,6 +50,9 @@ client.open({ // Let's tell the user who they are, and what they pushed. console.log(`${participant.username} pushed, ${inputEvent.input.controlID}`); + //Make a sound, its a soundboard \o/ + console.log('\x07'); + // Did this push involve a spark cost? if (inputEvent.transactionID) { From 388d998531bf99948ae946e1062f758dd22b69d0 Mon Sep 17 00:00:00 2001 From: ProbablePrime Date: Mon, 7 Aug 2017 20:56:36 +0200 Subject: [PATCH 3/6] move getGridPlacement into utils --- examples/keyboardControls.ts | 30 ++----------------- examples/util.ts | 58 +++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 55 deletions(-) diff --git a/examples/keyboardControls.ts b/examples/keyboardControls.ts index 8dddf50..610149a 100644 --- a/examples/keyboardControls.ts +++ b/examples/keyboardControls.ts @@ -1,11 +1,11 @@ /* tslint:disable:no-console */ import * as WebSocket from 'ws'; +import { getGridPlacement } from './util'; import { GameClient, IButton, IButtonData, - IGridPlacement, setWebSocket, } from '../lib'; @@ -23,33 +23,7 @@ const client = new GameClient(); // Log when we're connected to interactive client.on('open', () => console.log('Connected to interactive')); -// This makes grid placement objects dynamically, to be used for our controls below. -function getGridPlacement(x: number, y: number): IGridPlacement[] { - const size = 10; - return [ - { - size: 'large', - width: size, - height: size, - x: x * size, - y: y * size, - }, - { - size: 'medium', - width: size, - height: size, - x: x * size, - y: y * size, - }, - { - size: 'small', - width: size, - height: size, - x: x * size, - y: y * size, - }, - ]; -} + /** * These are our controls. The "keyCode" property is the JavaScript key code associated diff --git a/examples/util.ts b/examples/util.ts index 7692776..d4e1b4e 100644 --- a/examples/util.ts +++ b/examples/util.ts @@ -1,5 +1,5 @@ -import { IButtonData, IControlData } from '../lib'; -export function makeControls(amount: number = 5, textGenerator: (i:number) => string):IControlData[] { +import { IButtonData, IControlData, IGridPlacement } from '../lib'; +export function makeControls(amount: number = 5, textGenerator: (i: number) => string): IControlData[] { const controls: IButtonData[] = []; const size = 10; for (let i = 0; i < amount; i++) { @@ -8,31 +8,35 @@ export function makeControls(amount: number = 5, textGenerator: (i:number) => st kind: 'button', text: textGenerator(i), cost: 1, - position: [ - { - size: 'large', - width: size, - height: size / 2, - x: i * size, - y: 1, - }, - { - size: 'small', - width: size, - height: size / 2, - x: i * size, - y: 1, - }, - { - size: 'medium', - width: size, - height: size, - x: i * size, - y: 1, - }, - ], - }, - ); + position: getGridPlacement(i * size, 1, 10), + }); } return controls; } + +// This makes grid placement objects dynamically, to be used for our controls below. +export function getGridPlacement(x: number, y: number, size: number = 10): IGridPlacement[] { + return [ + { + size: 'large', + width: size, + height: size, + x: x * size, + y: y * size, + }, + { + size: 'medium', + width: size, + height: size, + x: x * size, + y: y * size, + }, + { + size: 'small', + width: size, + height: size, + x: x * size, + y: y * size, + }, + ]; +} From 77c8cac0eb12e84503781b0d36d89fb94ad2ea0c Mon Sep 17 00:00:00 2001 From: ProbablePrime Date: Mon, 7 Aug 2017 21:02:41 +0200 Subject: [PATCH 4/6] on second thoughts no --- examples/basic.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/examples/basic.ts b/examples/basic.ts index 048aa3d..dfc5402 100644 --- a/examples/basic.ts +++ b/examples/basic.ts @@ -50,9 +50,6 @@ client.open({ // Let's tell the user who they are, and what they pushed. console.log(`${participant.username} pushed, ${inputEvent.input.controlID}`); - //Make a sound, its a soundboard \o/ - console.log('\x07'); - // Did this push involve a spark cost? if (inputEvent.transactionID) { From 6488469ea5cebdf147a407681ce04355b1c6ac99 Mon Sep 17 00:00:00 2001 From: ProbablePrime Date: Mon, 7 Aug 2017 21:34:06 +0200 Subject: [PATCH 5/6] fixup! correct some derps --- examples/groups.ts | 6 ++---- examples/util.ts | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/examples/groups.ts b/examples/groups.ts index efa0f03..cfe2f02 100644 --- a/examples/groups.ts +++ b/examples/groups.ts @@ -3,12 +3,10 @@ import * as WebSocket from 'ws'; import { GameClient, - IButtonData, - IControlData, - ISceneDataArray, + Group, IParticipant, + ISceneDataArray, setWebSocket, - Group } from '../lib'; import { makeControls } from './util'; diff --git a/examples/util.ts b/examples/util.ts index d4e1b4e..14d791b 100644 --- a/examples/util.ts +++ b/examples/util.ts @@ -8,7 +8,7 @@ export function makeControls(amount: number = 5, textGenerator: (i: number) => s kind: 'button', text: textGenerator(i), cost: 1, - position: getGridPlacement(i * size, 1, 10), + position: getGridPlacement(i, 0, size), }); } return controls; From ec629b41b162b24e95702cb11bbf1e258141d77c Mon Sep 17 00:00:00 2001 From: ProbablePrime Date: Tue, 8 Aug 2017 19:40:13 +0200 Subject: [PATCH 6/6] use makeControls in updatecontrols --- examples/updateControl.ts | 47 +++------------------------------------ 1 file changed, 3 insertions(+), 44 deletions(-) diff --git a/examples/updateControl.ts b/examples/updateControl.ts index 866f65c..43cfdc6 100644 --- a/examples/updateControl.ts +++ b/examples/updateControl.ts @@ -4,12 +4,12 @@ import * as WebSocket from 'ws'; import { GameClient, IButton, - IButtonData, - IControlData, IParticipant, setWebSocket, } from '../lib'; +import { makeControls } from './util'; + if (process.argv.length < 4) { console.log('Usage gameClient.exe '); process.exit(); @@ -28,47 +28,6 @@ client.on('message', (err: any) => console.log('<<<', err)); client.on('send', (err: any) => console.log('>>>', err)); // client.on('error', (err: any) => console.log(err)); -/** - * This makes button objects, it will make the amount of buttons we tell it to - * we'll use it to create controls dynamically! - */ -function makeControls(amount: number): IControlData[] { - const controls: IButtonData[] = []; - const size = 10; - for (let i = 0; i < amount; i++) { - controls.push({ - controlID: `${i}`, - kind: 'button', - text: `Button ${i}`, - cost: 1, - position: [ - { - size: 'large', - width: size, - height: size / 2, - x: i * size, - y: 1, - }, - { - size: 'small', - width: size, - height: size / 2, - x: i * size, - y: 1, - }, - { - size: 'medium', - width: size, - height: size, - x: i * size, - y: 1, - }, - ], - }, - ); - } - return controls; -} // Now we open the connection passing in our authentication details and an experienceId. client.open({ authToken: process.argv[2], @@ -79,7 +38,7 @@ client.open({ // Every Interactive Experience has a "default" scene so we'll add them there there. return client.createControls({ sceneID: 'default', - controls: makeControls(5), + controls: makeControls(5, i => `Button ${i}`), }); }) .then(controls => {