import { EventEmitter } from 'events' ; import { IPlugin } from './IModLoaderAPI' ; export class NetworkEventBus extends EventEmitter { } export const NetworkBus = new NetworkEventBus ( ) ; export const NetworkChannelBus = new NetworkEventBus ( ) ; export const NetworkSendBus = new NetworkEventBus ( ) ; export const NetworkBusServer = new NetworkEventBus ( ) ; export const NetworkChannelBusServer = new NetworkEventBus ( ) ; export const NetworkSendBusServer = new NetworkEventBus ( ) ; export const NetworkQueryBusServer = new NetworkEventBus ( ) ; export function NetworkHandler ( key ) { return function ( this , target , propertyKey , descriptor ) { if ( target . ModLoader === undefined ) { target [ 'ModLoader' ] = { } ; } if ( target . ModLoader . NetworkHandler === undefined ) { target . ModLoader [ 'NetworkHandler' ] = { } ; } if ( target . ModLoader . NetworkHandler . PacketHandlers === undefined ) { target . ModLoader . NetworkHandler [ 'PacketHandlers' ] = new Map < string , string > ( ) ; } target . ModLoader . NetworkHandler . PacketHandlers . set ( key , propertyKey ) ; } ; } export function NetworkChannelHandler ( key ) { return function ( target , propertyKey , descriptor ) { if ( target . ModLoader === undefined ) { target [ 'ModLoader' ] = { } ; } if ( target . ModLoader . NetworkHandler === undefined ) { target . ModLoader [ 'NetworkHandler' ] = { } ; } if ( target . ModLoader . NetworkHandler . ChannelHandlers === undefined ) { target . ModLoader . NetworkHandler [ 'ChannelHandlers' ] = new Map < string , string > ( ) ; } target . ModLoader . NetworkHandler . ChannelHandlers . set ( key , propertyKey ) ; } ; } export function ServerNetworkHandler ( key ) { return function ( target , propertyKey , descriptor ) { if ( target . ModLoader === undefined ) { target [ 'ModLoader' ] = { } ; } if ( target . ModLoader . ServerNetworkHandler === undefined ) { target . ModLoader [ 'ServerNetworkHandler' ] = { } ; } if ( target . ModLoader . ServerNetworkHandler . PacketHandlers === undefined ) { target . ModLoader . ServerNetworkHandler [ 'PacketHandlers' ] = new Map < string , string > ( ) ; } target . ModLoader . ServerNetworkHandler . PacketHandlers . set ( key , propertyKey ) ; } ; } export function ServerNetworkChannelHandler ( key ) { return function ( target , propertyKey , descriptor ) { if ( target . ModLoader === undefined ) { target [ 'ModLoader' ] = { } ; } if ( target . ModLoader . ServerNetworkHandler === undefined ) { target . ModLoader [ 'ServerNetworkHandler' ] = { } ; } if ( target . ModLoader . ServerNetworkHandler . ChannelHandlers === undefined ) { target . ModLoader . ServerNetworkHandler [ 'ChannelHandlers' ] = new Map < string , string > ( ) ; } target . ModLoader . ServerNetworkHandler . ChannelHandlers . set ( key , propertyKey ) ; } ; } export interface INetwork { sendPacket ( packet ) : void ; sendPacketToSpecificPlayer ( packet , dest ) : void ; isPlayerConnected ( player ) : boolean ; } export interface INetworkClient extends Omit < INetwork , 'isPlayerConnected' > { } export interface IToPlayer { packet : IPacketHeader ; player : INetworkPlayer ; } export interface IConnectionCheckEvt { player : INetworkPlayer ; connected : boolean ; } export class Server implements INetwork { sendPacket ( packet ) { NetworkSendBusServer . emit ( 'msg' , packet ) ; } sendPacketToSpecificPlayer ( packet , dest ) { NetworkSendBusServer . emit ( 'toPlayer' , { packet , player : dest } as IToPlayer ) ; } isPlayerConnected ( player ) { let evt = { player , connected : true } ; NetworkQueryBusServer . emit ( 'isPlayerConnected' , evt ) ; return evt . connected ; } } export class Client implements INetworkClient { sendPacket ( packet ) { NetworkSendBus . emit ( 'msg' , packet ) ; } sendPacketToSpecificPlayer ( packet , dest ) { NetworkSendBus . emit ( 'toPlayer' , { packet , player : dest } ) ; } } export const ServerController = new Server ( ) ; export const ClientController = new Client ( ) ; export interface INetworkPlayer { nickname : string ; uuid : string ; data : any ; } export const enum SocketType { TCP , UDP } export interface IPacketHeader { packet_id : string ; lobby : string ; channel : string ; player : INetworkPlayer ; forward : boolean ; socketType : SocketType ; } export class LobbyData { name : string ; key : string ; data : any ; constructor ( name , key ) { this . name = name ; this . key = key ; this . data = { } ; } } export interface ILobbyStorage { config : LobbyData ; owner : string ; data : any ; } export interface ILobbyManager { getLobbyStorage ( lobbyName , plugin ) : any ; createLobbyStorage ( lobbyName , plugin , obj ) : void ; getAllLobbies ( ) : any ; } export function setupNetworkHandlers ( instance ) { let p = Object . getPrototypeOf ( instance ) ; if ( p . hasOwnProperty ( 'ModLoader' ) ) { if ( p . ModLoader . hasOwnProperty ( "hasBeenProcessed" ) ) { return ; } if ( p . ModLoader . hasOwnProperty ( 'NetworkHandler' ) ) { if ( p . ModLoader . NetworkHandler . hasOwnProperty ( 'PacketHandlers' ) !== null ) { p . ModLoader . NetworkHandler . PacketHandlers . forEach ( function ( value , key ) { let a = ( instance as any ) [ value ] . bind ( instance ) ; NetworkBus . addListener ( key , a ) ; } ) ; } if ( p . ModLoader . NetworkHandler . hasOwnProperty ( 'ChannelHandlers' ) ) { p . ModLoader . NetworkHandler . ChannelHandlers . forEach ( function ( value , key ) { let a = ( instance as any ) [ value ] . bind ( instance ) ; NetworkChannelBus . addListener ( key , a ) ; } ) ; } } if ( p . ModLoader . hasOwnProperty ( 'ServerNetworkHandler' ) ) { if ( p . ModLoader . ServerNetworkHandler . hasOwnProperty ( 'PacketHandlers' ) !== null ) { p . ModLoader . ServerNetworkHandler . PacketHandlers . forEach ( function ( value , key ) { let a = ( instance as any ) [ value ] . bind ( instance ) ; NetworkBusServer . addListener ( key , a ) ; } ) ; } if ( p . ModLoader . ServerNetworkHandler . hasOwnProperty ( 'ChannelHandlers' ) ) { p . ModLoader . ServerNetworkHandler . ChannelHandlers . forEach ( function ( value , key ) { let a = ( instance as any ) [ value ] . bind ( instance ) ; NetworkChannelBusServer . addListener ( key , a ) ; } ) ; } } } }