import { Channel } from 'channel/channel' ; import { ChannelServer } from 'channel/channelserver' ; import { ExtendedSocket } from 'extendedsocket' ; import { InRequestRoomListPacket } from 'packets/in/requestroomlist' ; import { OutLobbyPacket } from 'packets/out/lobby' ; import { OutRoomListPacket } from 'packets/out/roomlist' ; import { OutServerListPacket } from 'packets/out/serverlist' ; import { UserSession } from 'user/usersession' ; import { RoomHandler } from 'handlers/roomhandler' ; const roomHandler = new RoomHandler ( ) ; class export class ChannelManager { param sourceConn public static onChannelListPacket ( sourceConn ) { const session = sourceConn . session ; if ( session == null ) { console . warn ( `uuid ${ sourceConn . uuid } tried to get channels without a session` ) ; return false ; } console . log ( `user ID ${ session . user . id } requested server list` ) ; this . sendChannelListTo ( sourceConn ) ; return true ; } param packetData param sourceSocket param users public static onRoomListPacket ( packetData , sourceConn ) { const session = sourceConn . session ; if ( session == null ) { console . warn ( 'uuid ' + sourceConn . uuid + ' tried to get rooms without a session' ) ; return false ; } const listReq = new InRequestRoomListPacket ( packetData ) ; const server = ChannelManager . getServerByIndex ( listReq . channelServerIndex ) ; if ( server == null ) { console . warn ( `user ID ${ session . user . id } requested room list, but it isn't in a channel server` ) ; return false ; } const channel = server . getChannelByIndex ( listReq . channelIndex ) ; if ( channel == null ) { console . warn ( `user ID ${ session . user . id } requested room list, but it isn't in a channel` ) ; return false ; } console . log ( `user ${ session . user . id } requested room list successfully, sending it...` ) ; this . setUserChannel ( sourceConn , channel ) ; return true ; } public static onRoomRequest ( reqData , sourceConn ) { return roomHandler . onRoomRequest ( reqData , sourceConn ) ; } param conn public static sendChannelListTo ( conn ) { conn . send ( new OutServerListPacket ( this . channelServers ) ) ; } param index public static getServerByIndex ( index ) { for ( const server of this . channelServers ) { if ( server . index === index ) { return server ; } } return null ; } public static sendRoomListTo ( conn , channel ) { conn . send ( OutLobbyPacket . joinRoom ( ) ) ; conn . send ( OutRoomListPacket . getFullList ( channel . rooms ) ) ; } private static channelServers : ChannelServer [ ] = [ new ChannelServer ( 'Test server' , 1 , 1 , 1 ) ] ; param conn param channel private static setUserChannel ( conn , channel ) { const session = conn . session ; if ( session . currentChannel != null ) { channel . OnUserLeft ( conn ) ; } session . currentChannel = channel ; channel . OnUserJoined ( conn ) ; this . sendRoomListTo ( conn , channel ) ; } }