import * as WebSocket from 'ws' ; import { GameClient , Group , IParticipant , ISceneDataArray , setWebSocket , } from '../lib' ; import { makeControls } from './util' ; if ( process . argv . length < 4 ) { console . log ( 'Usage gameClient.exe <token> <versionId>' ) ; console . log ( process . argv ) ; process . exit ( ) ; } setWebSocket ( WebSocket ) ; const client = new GameClient ( ) ; client . on ( 'open' , ( ) => { console . log ( 'Connected to Interactive!' ) ; client . synchronizeState ( ) . then ( ( ) => client . ready ( true ) ) . then ( createScenes ) . then ( createGroups ) . catch ( ( reason ) => console . error ( 'Promise rejected' , reason ) ) ; } ) ; const participantTimers = new Map < string , number > ( ) ; const delayTime = 2000 ; /**
 * Swaps the group the current participant is in between secondGroup and default.
 */ function swapParticipantGroup ( participant ) { if ( participant . groupID === 'default' ) { participant . groupID = 'secondGroup' ; } else { participant . groupID = 'default' ; } return client . updateParticipants ( { participants : [ participant ] , } ) ; } /**
 * Removes the participant info from the participantTimers map and stops their timer.
 */ function removeParticipant ( participantSessionId ) { if ( participantTimers . has ( participantSessionId ) ) { clearInterval ( participantTimers [ participantSessionId ] ) ; delete participantTimers [ participantSessionId ] ; } } /**
 * Create the scenes used by the application.
 */ function createScenes ( ) { const defaultScene = client . state . getScene ( 'default' ) ; defaultScene . createControls ( makeControls ( 1 , ( ) => 'Scene: default' ) ) ; const secondScene = { sceneID : 'secondScene' , controls : makeControls ( 1 , ( ) => 'Scene: second' ) , } ; return client . createScenes ( { scenes : [ secondScene ] , } ) ; } /**
 * Create and setup the groups used by the application.
 */ function createGroups ( ) { const defaultGroup = client . state . getGroup ( 'default' ) ; defaultGroup . sceneID = 'default' ; const secondGroup = new Group ( { groupID : 'secondGroup' , sceneID : 'secondScene' , } ) ; const thirdGroup = new Group ( { groupID : 'thirdGroup' , sceneID : 'default' , } ) ; return ( client . updateGroups ( { groups : [ defaultGroup ] , } ) . then ( ( ) => client . createGroups ( { groups : [ secondGroup , thirdGroup ] , } ) ) . then ( ( ) => client . deleteGroup ( { groupID : thirdGroup . groupID , reassignGroupID : defaultGroup . groupID , } ) ) ) ; } client . open ( { authToken : process . argv [ 2 ] , versionId : parseInt ( process . argv [ 3 ] , 10 ) , } ) . catch ( ( reason ) => console . error ( 'Promise rejected' , reason ) ) ; client . state . on ( 'participantJoin' , ( participant ) => { console . log ( `${ participant . username }(${ participant . sessionID }) Joined` ) ; if ( ! participantTimers . has ( participant . sessionID ) ) { participantTimers [ participant . sessionID ] = setInterval ( ( ) => { const p = client . state . getParticipantBySessionID ( participant . sessionID ) ; if ( p ) { swapParticipantGroup ( p ) ; } else { removeParticipant ( participant . sessionID ) ; } } , delayTime ) ; } } ) ; client . state . on ( 'participantLeave' , ( participantSessionID , participant ) => { console . log ( `${ participant . username }(${ participantSessionID }) Left` ) ; removeParticipant ( participantSessionID ) ; } ) ;