Coder Social home page Coder Social logo

mta-ase-query's Introduction

MTA-ASE-Query

Library for send requests to Multi Theft Auto: San Andreas servers and get basic info.
Working around ASE protocol (EYE1).

Also, possible request all servers info with "partially" fields from those API.

// These objects is returned by getServerInfo()
interface MTAServerResponse {
    name: string;
    ip: string;
    port: number;
    gamemode: string;
    map: string;
    version: string;
    private: boolean;
    players: number;
    max_players: number;
    rules: {
        name: string;
        value: string;
    }[],
    players_list: {
        name: string;
        ping: number;
        score: number;
    }[]
}

// These objects is returned by getServers()
interface MTAServerResponseLite {
    name: string;
    ip: string;
    port: number;
    version: string;
    private: boolean;
    players: number;
    max_players: number;
}

What is a "Rules"? (MTA:SA Wiki)
What is a "Score"? (MTA:SA Wiki)

Installing:

npm install @bsnext/mta-ase-query

API:

getServerInfo(
    serverAdress: string, serverPort: number = 22003, requestTimeout: number = 7500
): Promise<MTAServerResponse>;

// serverAdress - Server adress, can be IP or Domain.
// serverPort - Server port, 22003 by default.
// requestTimeout - Timeout for close long-time requests.
getServers(): Promise<MTAServerResponseLite>;

Usage:

import { getServerInfo } from "@bsnext/mta-ase-query";
const serverInfo = await getServerInfo(`lime.dayzmta.ru`, 22003);

/*
serverInfo = {
    name: '██ #3 | RU █ [BS] DAYZ ULTIMATE: LIME █ PVP, EASY, LOOT X3',
    gamemode: 'RU ██ DayZ Ultimate 1.5',
    map: 'None',
    version: '1.6',
    private: false,
    players: 1,
    max_players: 80,
    rules: [],
    players_list: [ 
        { name: 'WildDove83', ping: 7, score: 0 } 
    ]
}
*/
import { getServers } from "@bsnext/mta-ase-query";
const allServersInfo = await getServers();

/*
allServersInfo = [
    ...,
    {
        name: '██ #3 | RU █ [BS] DAYZ ULTIMATE: LIME █ PVP, EASY, LOOT X3',
        version: '1.6',
        private: false,
        players: 4,
        max_players: 80,
    },
    {
        name: '██ #4 | EU █ [BS] DAYZ ULTIMATE: MINT █ PVP, EASY, LOOT X3',
        version: '1.6',
        private: false,
        players: 16,
        max_players: 80,
    },
    ...
]
*/

Example:

Basic example of request.

const result: MTAServerResponse = await getServerInfo(`lime.dayzmta.ru`, 22003);

console.log(`Server Name: ${result.name}`);
console.log(`Gamemode: ${result.gamemode}`);
console.log(`Map: ${result.map}`);
console.log(`Version: ${result.version}`);
console.log(`Is Passworded: ${result.private ? `Yes` : `No`}`);
console.log(`Players: ${result.players}/${result.max_players}`);

if (result.rules.length > 0) {
    console.log(`Rules:`);
    for (const rule of result.rules) {
        console.log(`- ${rule.name}: ${rule.value}`);
    }
}

if (result.players_list.length > 0) {
    console.log(`Players List:`);
    for (const player of result.players_list) {
        console.log(`- ${player.name}: ${player.score} (${player.ping} ms)`);
    }
}

Catch the request error.

try {
    const result: MTAServerResponse = await getServerInfo(`not-existed-server.dayzmta.ru`, 22003);
    console.log(`Server Name: ${result.name}`);
} catch(e) {
    console.error(e);
}

Request all servers info, and filter by name.

const result: MTAServerResponseLite[] = await getServers();

const filteredResult = result.filter(
    function (value) {
        return value.name.search(`DAYZ ULTIMATE`) > -1;
    }
);

for (const serverInfo of filteredResult) {
    console.log(`----------------`);
    console.log(`Server Name: ${serverInfo.name}`);
    console.log(`Version: ${serverInfo.version}`);
    console.log(`Is Passworded: ${serverInfo.private ? `Yes` : `No`}`);
    console.log(`Players: ${serverInfo.players}/${serverInfo.max_players}`);
}

mta-ase-query's People

Contributors

salwador avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.