import { flatten , last } from 'lodash' ; import { Client } from './client' ; import { panels } from './panels' ; import { buttons } from './buttons' ; import { LightColor , Light } from './types' ; import { ButtonController } from './ButtonController' ; import { PanelController } from './PanelController' ; import { LightController } from './LightController' ; import { GameState , Packet } from '../../common/types' ; ( function main ( ) { let gameState = 'wait_for_cards' ; function onPacket ( packet ) { if ( packet . kind === 'gamestate' ) { gameState = packet . state ; panelController . resetConnections ( ) ; console . info ( 'new game state: ' , gameState ) ; updatePanelLights ( ) ; } } function sendPacket ( packet ) { updatePanelLights ( ) ; client . sendPacket ( packet ) ; } const url = process . env . GANGLIA_SERVER_URL || 'http://starship:9000' ; const client = new Client ( url , onPacket ) ; const panelController = new PanelController ( panels , sendPacket , ( ) => gameState ) ; const buttonController = new ButtonController ( buttons , sendPacket , ( ) => gameState ) ; const numLights = last ( last ( panels ) ! . lightIndicies ) ! ; const lightController = new LightController ( numLights ) ; function updatePanelLights ( ) { let lights = [ ] ; if ( gameState === 'wait_for_cards' ) { lightController . setAllLightToColor ( LightColor . green ) ; } else if ( gameState === 'game_over' ) { lightController . startFlashingLights ( LightColor . red ) ; } else if ( gameState === 'in_game' ) { lights = flatten ( panelController . panels . map ( panel => panel . lights ) ) ; lightController . setLights ( lights ) ; } else { lightController . setAllLightToColor ( 0 ) ; } } console . info ( `\nWire poll rate: ${ 1000 / panelController . pollRateMsec } Hz` ) ; console . info ( `Button poll rate: ${ 1000 / buttonController . pollRateMsec } Hz` ) ; console . info ( `Server ${ client . url }\n` ) ; updatePanelLights ( ) ; function teardownAndExitCleanly ( ) { lightController . teardown ( ) ; process . nextTick ( ( ) => process . exit ( 0 ) ) ; } process . on ( 'SIGINT' , teardownAndExitCleanly ) ; process . on ( 'SIGTERM' , teardownAndExitCleanly ) ; } ) ( ) ;