1.0.1 • Published 2 years ago

bf4rcon v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

skuIIs/bf-rcon

GitHub release (latest SemVer) GitHub top language npm bundle size (scoped) GitHub Sponsors GitHub

BF4 RCon in pure JavaScript

Installation

You can use npm or yarn to install this package into your project

npm install bf4rcon
yarn add bf4rcon

Usage

const { BFrcon } = require('bf4rcon')

async function init () {
  const rcon = new BFrcon()

  // Connects to server(s), can be an object or an array of objects

  // *** Example for a single server ***
  const server = await rcon.connect({
    host: '127.0.0.1',
    port: 47200,
    password: 'password'
  })

  server.on('ready', async function () {
    console.log('Server: ' + this.server_id + ' added to listener!')

    // Run some command
    const players = await this.listPlayers()
    console.log(players)

    // Listen for some event
    this.on('player.onAuthenticated', function (player) {
      console.log(player)
    })
  }.bind(server))

  // *** Example for a multiple servers ***
  const servers = await rcon.connect([
    {
      host: '127.0.0.1',
      port: 47200,
      password: 'password'
    },
    {
      host: '127.0.0.1',
      port: 47201,
      password: 'password'
    },
  ])

  for (var i = 0, len = servers.length; i < len; i++) {
    servers[i].on('ready', async function () {
      console.log('Server: ' + this.server_id + ' added to listener!')

      // Run some command
      const players = await this.listPlayers()
      console.log(players)

      // Listen for some event
      this.on('player.onAuthenticated', function (player) {
        console.log(player)
      })
    }.bind(servers[i]))
  }
}

init()

Commands

After connecting to a server(s) you can use the exec function to execute a command, or use the built-in API for commands made with promises. See ./services/commands/* for available commands.

...
  this.exec('serverInfo', function (error, response) {
    if (error) return reject(error)
    // Do something with response. Note here you must parse things yourself!
  })
...

Is the same as:

...
  const info = await this.serverInfo()
  // Already parsed for you!
  console.log(info)
...

Events

Listen for events from the server is simple. After connecting to the server(s) just use a standard on listener.

  this.on('player.onChat', function (chat) {
    console.log(chat)
  })

Caveats

  • This has only been tested for BF4. This could work for BF3 or BFH.

  • This package is meant for long-running processes and also doesn't handle reconnecting after disconnects or errors. You must handle those on your own. Example:

  const config = {
    host: '127.0.0.1',
    port: 47200,
    password: 'password'
  }

  // Initial connection
  const server = await rcon.connect(config)

  server.on('ready', async function () {
    // Code
  }.bind(server))

  // Socket connection was ended
  server.on('end', function () {
    // Reconnect!
    server = await rcon.connect(config)
  })
  • If you want to issue a command on a server and exit then it can be done, you just have to connect, issue the command after the connection is ready, and then manually disconnect. Example:
  const server = await rcon.connect({
    host: '127.0.0.1',
    port: 47200,
    password: 'password'
  })

  server.on('ready', async function () {

    // Run some command
    const players = await this.listPlayers()
    console.log(players)

    // Disconnect
    this.disconnect()
  }.bind(server))

Modules

Classes

Geo

Geo Helper module

module.exports(ip) ⇒ String | Null

Get the country from an IP address

Kind: Exported function
Returns: String | Null - returns a string of the country if found or null
Throws:

  • TypeError throws if ip is not a string
ParamTypeDescription
ipStringIP address to lookup

Map

Map Helper module

module.exports(map) ⇒ String | Null

Converts rcon map to full map string

Kind: Exported function
Returns: String | Null - returns full map string or null if not found
Throws:

  • TypeError throws if map is not a string
ParamTypeDescription
mapStringrcon map to convert

Mode

Mode Helper module

module.exports(mode) ⇒ String | Null

Converts rcon mode to full mode string

Kind: Exported function
Returns: String | Null - returns full mode string or null if not found
Throws:

  • TypeError throws if mode is not a string
ParamTypeDescription
modeStringrcon mode to convert

Weapon

Weapon Helper module

module.exports(weapon) ⇒ Object.<WeaponObject> | Null

Converts rcon weapon to full weapon object

Kind: Exported function
Returns: Object.<WeaponObject> | Null - returns full weapon object or null if not found
Throws:

  • TypeError throws if weapon is not a string
ParamTypeDescription
weaponStringrcon mode to convert

module.exports~WeaponObject : Object

Kind: inner typedef of module.exports
Properties

NameTypeDescription
weapon_keyString
typeStringprimary, secondary, auxiliary
kitStringassault, engineer, support, recon, none
groupStringsuicide, melee, impact, nonlethal, assaultrifle, carbine, smg, lmg, shotgun, dmr, sniperrifle, handgun, explosive, projectileexplosive, vehiclepersonal, vehicletransport, vehiclestationary, vehiclelight, vehicleheavy, vehicleair, vehiclewater, none

BFRcon

BFRcon class

Kind: global class

bfRcon.connect(servers) ⇒ Object

Connects to server(s)

Kind: instance method of BFRcon

ParamTypeDescription
serversArray | Objectan array of servers or a single server to connect to

bfRcon.exec(command, callback)

Executes a command

Kind: instance method of BFRcon

ParamTypeDescription
commandStringcommand to execute
callbackfunctioncallback

"player.onAuthenticated" ⇒ Object

Player authenticated event

Kind: event emitted by BFRcon
Returns: Object - {event: String, name: String, refresh: Boolean}

"player.onJoin" ⇒ Object

Player join event

Kind: event emitted by BFRcon
Returns: Object - {event: String, name: String, guid: String, refresh: Boolean}

"player.onLeave" ⇒ Object

Player leave event

Kind: event emitted by BFRcon
Returns: Object - {event: String, name: String, info: String, refresh: Boolean}

"player.onDisconnect" ⇒ Object

Player disconnect event

Kind: event emitted by BFRcon
Returns: Object - {event: String, name: String, refresh: Boolean}

"player.onSpawn" ⇒ Object

Player spawn event

Kind: event emitted by BFRcon
Returns: Object - {event: String, name: String, team: Number, refresh: Boolean}

"player.onKill" ⇒ Object

Player kill event

Kind: event emitted by BFRcon
Returns: Object - {event: String, name: String, victim: String, weapon: {weapon_key: String, type: String, kit: String, group: String}, headshot: Boolean, refresh: Boolean}

"player.onChat" ⇒ Object

Player chat event

Kind: event emitted by BFRcon
Returns: Object - {event: String, name: String, text: String, subset: String, refresh: Boolean}

"player.onSquadChange" ⇒ Object

Player squad change event

Kind: event emitted by BFRcon
Returns: Object - {event: String, name: String, team: Number, squad: Number, refresh: Boolean}

"player.onTeamChange" ⇒ Object

Player team change event

Kind: event emitted by BFRcon
Returns: Object - {event: String, name: String, team: Number, squad: Number, refresh: Boolean}

"server.onMaxPlayerCountChange" ⇒ Object

Server max player count change event

Kind: event emitted by BFRcon
Returns: Object - {event: String, count: Number, refresh: Boolean}

"server.onLevelLoaded" ⇒ Object

Server level loaded event

Kind: event emitted by BFRcon
Returns: Object - {event: String, level: String, mode: String, played: Number, total: Number, refresh: Boolean}

"server.onRoundOver" ⇒ Object

Server round over event

Kind: event emitted by BFRcon
Returns: Object - {event: String, winner: Number, refresh: Boolean}

"server.onRoundOverPlayers" ⇒ Object

Server round over players event

Kind: event emitted by BFRcon
Returns: Object - {event: String, players: Array, refresh: Boolean}

"server.onRoundOverTeamScores" ⇒ Object

Server round over team scores event

Kind: event emitted by BFRcon
Returns: Object - {event: String, scores: Array, refresh: Boolean}

"punkbuster.onComputed" ⇒ Object

Server punkbuster computed event

Kind: event emitted by BFRcon
Returns: Object - {event: String, message: String, name: String, guid: String, ip: String, country: String, refresh: Boolean}

"punkbuster.onViolation" ⇒ Object

Server punkbuster violation event

Kind: event emitted by BFRcon
Returns: Object - {event: String, message: String, name: String, guid: String, ip: String, country: String, type: String, number: Number, refresh: Boolean}

Contributing

Pull requests are welcome for bug fixes or feature requests.

Sponsors

Support this project and possibly other open-source projects by becoming a sponsor. Higher tier sponsor will appear here with a logo and link to your website. Become a sponsor

License

MIT