import { PrecalculatedBot , PortalType , getPortalConfigBotID , Bot , registerBuiltinPortal , } from '@casual-simulation/aux-common' ; import { BrowserSimulation } from './BrowserSimulation' ; import { never , Observable , of } from 'rxjs' ; import { switchMap , first , map , distinctUntilChanged , filter , flatMap , } from 'rxjs/operators' ; import { LoginManager , BotWatcher , UpdatedBotInfo , BotHelper , } from '@casual-simulation/aux-vm' ; import { PortalManager } from '@casual-simulation/aux-vm/managers' ; import { RemoteSimulation } from '@casual-simulation/aux-vm-client' ; param simulation export function userBotChanged ( simulation ) { return userBotChangedCore ( simulation . login , simulation . watcher ) . pipe ( map ( ( u ) => u . bot ) ) ; } param simulation export function userBotTagsChanged ( simulation ) { return userBotChangedCore ( simulation . login , simulation . watcher ) ; } export function userBotChangedCore ( login , watcher ) { return login . userChanged . pipe ( switchMap ( ( user ) => { if ( user ) { return watcher . botTagsChanged ( user . id ) ; } else { return never ( ) ; } } ) ) ; } param simulation export function getUserBotAsync ( simulation ) { return userBotChanged ( simulation ) . pipe ( first ( ( bot ) => ! ! bot ) ) ; } param simulation param portal export function getPortalConfigBot ( simulation , portal ) { const data = simulation . portals . portalBots . get ( portal ) ; if ( data && data . botId ) { return simulation . helper . botsState [ data . botId ] ?? null ; } return null ; } param simulation param portal param shouldRegisterBuiltinPortal export function watchPortalConfigBot ( simulation , portal , shouldRegisterBuiltinPortal = true ) { return watchPortalConfigBotCore ( simulation . watcher , simulation . portals , simulation . helper , portal , shouldRegisterBuiltinPortal ) ; } param watcher param portals param helper param portal export function watchPortalConfigBotCore ( watcher , portals , helper , portal , shouldRegisterBuiltinPortal = true ) { if ( shouldRegisterBuiltinPortal ) { helper . transaction ( registerBuiltinPortal ( portal ) ) ; } return portals . portalBotIdUpdated . pipe ( flatMap ( ( p ) => p ) , filter ( ( p ) => p . portalId === portal ) , map ( ( p ) => p . botId ) , distinctUntilChanged ( ) , switchMap ( ( id ) => ( id ? watcher . botChanged ( id ) : of ( null ) ) ) ) ; }