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..cfe2f02 100644 --- a/examples/groups.ts +++ b/examples/groups.ts @@ -3,14 +3,14 @@ import * as WebSocket from 'ws'; import { GameClient, - IButtonData, - IControlData, - ISceneDataArray, + Group, IParticipant, + ISceneDataArray, setWebSocket, - Group } from '../lib'; +import { makeControls } from './util'; + if (process.argv.length < 4) { console.log('Usage gameClient.exe '); console.log(process.argv); @@ -37,45 +37,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 +67,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/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/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 => { diff --git a/examples/util.ts b/examples/util.ts new file mode 100644 index 0000000..14d791b --- /dev/null +++ b/examples/util.ts @@ -0,0 +1,42 @@ +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++) { + controls.push({ + controlID: `${i}`, + kind: 'button', + text: textGenerator(i), + cost: 1, + position: getGridPlacement(i, 0, size), + }); + } + 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, + }, + ]; +}