import { Region } from "./region" ; import { PolusBuffer } from "./polusBuffer" ; export class RegionList { selected : number = 0 ; name : string ; address : string ; servers : Region [ ] = [ ] ; constructor ( name = "" , address = "" ) { this . name = name ; this . address = address ; } public addServer ( name , address , port ) { this . servers . push ( new Region ( name , address , port ) ) ; } public selectServer ( index ) { this . selected = index ; } public getBuffer ( ) { let buffer = new PolusBuffer ( ) ; buffer . write32 ( this . selected ) ; buffer . writeString ( this . name ) ; buffer . writeString ( this . address ) ; buffer . write32 ( this . servers . length ) ; for ( let server of this . servers ) { buffer . writeString ( server . name ) ; buffer . writeBytes ( server . address . split ( "." ) . map ( ( e ) => parseInt ( e ) ) ) ; buffer . write16 ( server . port ) ; buffer . write32 ( server . failures ) ; } return buffer ; } public static fromBuffer ( buffer ) { let selected = buffer . read32 ( ) ; let name = buffer . readString ( ) ; let address = buffer . readString ( ) ; let servers = new Array ( buffer . read32 ( ) ) ; let regionList = new RegionList ( name , address ) ; regionList . selected = selected ; regionList . servers = servers ; for ( let i = 0 ; i < regionList . servers . length ; i ++ ) { let name = buffer . readString ( ) ; let address = buffer . readBytes ( 4 ) . buf . join ( "." ) ; let port = buffer . read16 ( ) ; let failures = buffer . read32 ( ) ; regionList . servers [ i ] = new Region ( name , address , port ) ; regionList . servers [ i ] . failures = failures ; } return regionList ; } }