import express = require ( "express" ) ; import provideSessionToSocketIo = require ( "express-socket.io-session" ) ; import redis = require ( "redis" ) ; import * as socketIoRedisAdapter from "@socket.io/redis-adapter" ; import * as SocketIO from "socket.io" ; import { CombatStats } from "../common/CombatStats" ; import { PlayerViewSettings } from "../common/PlayerViewSettings" ; import { getDefaultSettings } from "../common/Settings" ; import { PlayerViewManager } from "./playerviewmanager" ; import { EncounterState } from "../common/EncounterState" ; import { PlayerViewCombatantState } from "../common/PlayerViewCombatantState" ; import { ValidateEncounterId } from "../common/ValidateEncounterId" ; interface SocketWithSessionData { handshake : { session : Express . Session ; } ; } export default function ( io , session , playerViews ) { if ( process . env . REDIS_URL ) { const pubClient = redis . createClient ( process . env . REDIS_URL ) ; const subClient = pubClient . duplicate ( ) ; io . adapter ( socketIoRedisAdapter . createAdapter ( pubClient , subClient ) ) ; } io . use ( provideSessionToSocketIo ( session ) ) ; io . on ( "connection" , function ( socket ) { function joinEncounter ( id ) { socket . handshake . session . encounterId = id ; socket . join ( id ) ; } socket . on ( "update encounter" , function ( id , updatedEncounter ) { if ( ! socket . handshake . session . hasEpicInitiative ) { resetEpicInitiativeEncounterFeatures ( updatedEncounter ) ; } joinEncounter ( id ) ; playerViews . UpdateEncounter ( id , updatedEncounter ) ; socket . broadcast . to ( id ) . volatile . emit ( "encounter updated" , updatedEncounter ) ; } ) ; socket . on ( "update settings" , ( id , updatedSettings ) => { if ( ! socket . handshake . session . hasEpicInitiative ) { resetEpicInitiativeSettings ( updatedSettings ) ; } joinEncounter ( id ) ; playerViews . UpdateSettings ( id , updatedSettings ) ; socket . broadcast . to ( id ) . volatile . emit ( "settings updated" , updatedSettings ) ; } ) ; socket . on ( "join encounter" , function ( id ) { joinEncounter ( id ) ; } ) ; socket . on ( "request custom id" , async function ( id , callback ) { if ( ! socket . handshake . session . hasEpicInitiative || ! ValidateEncounterId ( id ) ) { return callback ( false ) ; } const idAvailable = await playerViews . IdAvailable ( id ) ; if ( idAvailable ) { const oldId = socket . handshake . session . encounterId ; playerViews . Destroy ( oldId ) ; joinEncounter ( id ) ; return callback ( true ) ; } else { return callback ( false ) ; } } ) ; socket . on ( "suggest damage" , function ( id , suggestedCombatantIds , suggestedDamage , suggester ) { joinEncounter ( id ) ; socket . broadcast . to ( id ) . emit ( "suggest damage" , suggestedCombatantIds , suggestedDamage , suggester ) ; } ) ; socket . on ( "suggest tag" , function ( id , suggestedCombatantIds , suggestedTag , suggester ) { joinEncounter ( id ) ; socket . broadcast . to ( id ) . emit ( "suggest tag" , suggestedCombatantIds , suggestedTag , suggester ) ; } ) ; socket . on ( "combat stats" , function ( id , combatStats ) { joinEncounter ( id ) ; socket . broadcast . to ( id ) . emit ( "combat stats" , combatStats ) ; } ) ; } ) ; } const emptyCustomStyles = getDefaultSettings ( ) . PlayerView . CustomStyles ; function resetEpicInitiativeSettings ( settings ) { settings . CustomCSS = "" ; settings . CustomStyles = emptyCustomStyles ; settings . DisplayPortraits = false ; settings . SplashPortraits = false ; settings . AllowTagSuggestions = false ; } function resetEpicInitiativeEncounterFeatures ( encounterState ) { encounterState . BackgroundImageUrl = undefined ; }