import * as express from 'express' ; import * as http from 'http' ; import * as socketIo from 'socket.io' ; import * as low from 'lowdb' ; import * as FileSync from 'lowdb/adapters/FileSync' ; import { GameState , GameStatePacket , Packet , CheatPacket , ScorePacket , } from '../../common/types' ; import { SetVolumeCheat } from '../../common/cheats' ; const app = express ( ) ; const server = http . createServer ( app ) ; const io = socketIo ( server ) ; const adapter = new FileSync ( '/var/lib/toomanycaptains/db.json' ) ; const db = low ( adapter ) ; db . defaults ( { highScore : 0 , volume : { master : 1 , music : 1 , } , plays : 0 , } ) . write ( ) ; function emitVolumesFromDatabase ( socket ) { console . log ( '💾  emitting high score packet' ) ; db . get ( 'volume' ) . forEach ( ( volume , target ) => { socket . emit ( 'packet' , { kind : 'cheat' , cheat : { code : 'set_volume' , volume , target , } as SetVolumeCheat , } as CheatPacket ) ; } ) . value ( ) ; } function emitHighScoreFromDatabase ( socket ) { console . log ( '💾  emitting volume packets' ) ; const points = db . get ( 'highScore' ) . value ( ) as number ; socket . emit ( 'packet' , { kind : 'score' , points , confirmedHighScore : true , } as ScorePacket ) ; } let gameState = 'wait_for_cards' ; app . use ( ( req , res , next ) => { res . header ( 'Access-Control-Allow-Origin' , '*' ) ; res . header ( 'Access-Control-Allow-Headers' , 'Origin, X-Requested-With, Content-Type, Accept' ) ; next ( ) ; } ) ; io . on ( 'connection' , socket => { console . log ( '⚡️  connected' ) ; console . log ( 'ℹ️   emitting gamestate packet' ) ; socket . emit ( 'packet' , { kind : 'gamestate' , state : gameState , } as GameStatePacket ) ; emitVolumesFromDatabase ( socket ) ; emitHighScoreFromDatabase ( socket ) ; socket . on ( 'packet' , ( packet ) => { if ( packet . kind === 'gamestate' ) { gameState = packet . state ; } else if ( packet . kind === 'cheat' && packet . cheat . code === 'set_volume' ) { db . set ( `volume.${ packet . cheat . target }` , packet . cheat . volume ) . write ( ) ; } else if ( packet . kind === 'score' ) { const currentHighScore = db . get ( 'highScore' ) . value ( ) as number ; if ( packet . points > currentHighScore ) { db . set ( 'highScore' , packet . points ) . write ( ) ; emitHighScoreFromDatabase ( socket ) ; } } socket . broadcast . emit ( 'packet' , packet ) ; console . log ( `👂  relaying packet: ${ JSON . stringify ( packet ) }` ) ; } ) ; socket . on ( 'disconnect' , ( ) => { console . log ( '🔌  disconnected' ) ; } ) ; } ) ; const port = process . env . PORT || 9000 ; server . listen ( port , ( ) => { console . log ( `👾  serving on port ${ port }` ) ; } ) ;