import webapi from '@/services/webapi' ; import { GenericResponse , GenericResponseWithData } from '@/services/responses/genericResponse' ; import { ServerResponse } from '@/services/responses/serverResponse' ; import store from '@/store' ; import { ipcRenderer } from 'electron' ; import ServerArgs from '@/electronMain/models/serverArgs' ; export default class ServerListService { static async addServer ( id , name , ipAddress , port ) { store . commit ( 'addServer' , { id , name , ipAddress , port } ) ; return await this . writeServersToFile ( ) ; } static async removeServer ( name , ipAddress , port ) { store . commit ( 'removeServer' , { name , ipAddress , port } ) ; return await this . writeServersToFile ( ) ; } static async writeServersToFile ( ) { ipcRenderer . send ( 'write-server-file' , { location : store . state . gameInstallInfo . location , servers : store . state . serverList } as ServerArgs ) ; return new Promise < boolean > ( ( resolve , reject ) => { ipcRenderer . once ( 'write-server-file' , ( event , args ) => { args ? resolve ( args ) : reject ( new Error ( `Couldn't write servers to file at ${ store . state . gameInstallInfo . location }` ) ) ; } ) ; } ) ; } static async getServerInfo ( id ) { const response = ( await webapi ( ) . get ( `/api/serverlist/${ id }` ) ) . data ; if ( response . success ) { return ( response as GenericResponseWithData < ServerResponse > ) . data ; } } static async getServerListPaginated ( page ) { const response = ( await webapi ( ) . get ( `/api/serverlist/page/${ page }` ) ) . data ; if ( response . success ) { return ( response as GenericResponseWithData < ServerResponse [ ] > ) . data ; } } static dot2num ( dot ) { const d = dot . split ( '.' ) ; return ( ( ( ( ( ( + d [ 0 ] ) * 256 ) + ( + d [ 1 ] ) ) * 256 ) + ( + d [ 2 ] ) ) * 256 ) + ( + d [ 3 ] ) ; } static num2dot ( num ) { return ( ( num >>> 24 ) + '.' + ( num >> 16 & 255 ) + '.' + ( num >> 8 & 255 ) + '.' + ( num & 255 ) ) ; } static dnsQuery ( query ) { ipcRenderer . send ( 'dns-query' , query ) ; return new Promise ( ( resolve ) => { ipcRenderer . once ( 'dns-query' , ( event , payload ) => { resolve ( payload ) ; } ) ; } ) ; } static async pingServer ( ipAddress , port ) { ipcRenderer . send ( 'ping-server' , { ipAddress , port } ) ; return new Promise < boolean > ( ( resolve , reject ) => { ipcRenderer . once ( 'ping-server' , ( event , args ) => { args ? resolve ( args ) : reject ( new Error ( `Couldn't ping server at ${ this . num2dot ( ipAddress ) }:${ port }` ) ) ; } ) ; } ) ; } }