feat: strip Colors (#469)

This commit is contained in:
CosminPerRam 2024-01-16 01:39:07 +02:00 committed by GitHub
parent 0a3338320e
commit 5ae12dd494
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 47 additions and 38 deletions

View File

@ -32,6 +32,7 @@ see next point) and `tshock` (which is `terraria`).
* Minimum Supported Deno Version: `1.39.2`. * Minimum Supported Deno Version: `1.39.2`.
* `deno run --allow-net bin/gamedig.js --type tf2 127.0.0.1` * `deno run --allow-net bin/gamedig.js --type tf2 127.0.0.1`
* Added code examples. * Added code examples.
* New option: `stripColors` (defaults to `true`) for protocols that strips colors: unreal2, savage2, quake3, nadeo, gamespy2, doom3, armagetron.
#### Games #### Games
* Removed the players::setNum method, the library will no longer add empty players as * Removed the players::setNum method, the library will no longer add empty players as

View File

@ -31,14 +31,14 @@ Confused on how this works, or you want to see more? Checkout the [examples](/ex
## Required Fields ## Required Fields
| Field | Type | Description | | Field | Type | Description |
|:---|:---|:---| |:---------|:-------|:---------------------------------------------------------------|
| **type** | string | One of the game IDs listed in the [games list](GAMES_LIST.md). | | **type** | string | One of the game IDs listed in the [games list](GAMES_LIST.md). |
| **host** | string | Hostname or IP of the game server. | | **host** | string | Hostname or IP of the game server. |
## Optional Fields ## Optional Fields
| Field | Type | Default | Description | | Field | Type | Default | Description |
|:---|:---|:---|:---| |:---------------------------|:--------|:----------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| **port** | number | Game port | Connection port or query port for the game server. Some games utilize a separate "query" port. If specifying the game port does not seem to work as expected, passing in this query port may work instead. | | **port** | number | Game port | Connection port or query port for the game server. Some games utilize a separate "query" port. If specifying the game port does not seem to work as expected, passing in this query port may work instead. |
| **maxAttempts** | number | 1 | Number of attempts to query server in case of failure. | | **maxAttempts** | number | 1 | Number of attempts to query server in case of failure. |
| **socketTimeout** | number | 2000 | Milliseconds to wait for a single packet. Beware that increasing this will cause many queries to take longer even if the server is online. | | **socketTimeout** | number | 2000 | Milliseconds to wait for a single packet. Beware that increasing this will cause many queries to take longer even if the server is online. |
@ -49,13 +49,14 @@ Confused on how this works, or you want to see more? Checkout the [examples](/ex
| **requestRules** | boolean | false | Valve games only. Additional 'rules' may be fetched into the `raw` key. | | **requestRules** | boolean | false | Valve games only. Additional 'rules' may be fetched into the `raw` key. |
| **requestRulesRequired** | boolean | false | Valve games only. `requestRules` is always required to have a response or the query will timeout. | | **requestRulesRequired** | boolean | false | Valve games only. `requestRules` is always required to have a response or the query will timeout. |
| **requestPlayersRequired** | boolean | false | Valve games only. Querying players is always required to have a response or the query will timeout. Some [games](GAMES_LIST.md) may not provide a players response. | | **requestPlayersRequired** | boolean | false | Valve games only. Querying players is always required to have a response or the query will timeout. Some [games](GAMES_LIST.md) may not provide a players response. |
| **stripColors** | boolean | true | Enables stripping colors for protocols: unreal2, savage2, quake3, nadeo, gamespy2, doom3, armagetron. |
## Query Response ## Query Response
The returned state object will contain the following keys: The returned state object will contain the following keys:
| Key | Type | Description | | Key | Type | Description |
|:---|:---|:---| |:-----------------|:-----------------|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| **name** | string | Server name. | | **name** | string | Server name. |
| **map** | string | Server map. | | **map** | string | Server map. |
| **password** | boolean | If a password is required. | | **password** | boolean | If a password is required. |

View File

@ -6,8 +6,11 @@ import Minimist from 'minimist'
import { GameDig } from './../lib/index.js' import { GameDig } from './../lib/index.js'
const argv = Minimist(process.argv.slice(2), { const argv = Minimist(process.argv.slice(2), {
boolean: ['pretty', 'debug', 'givenPortOnly', 'requestRules', 'requestRulesRequired', 'requestPlayersRequired'], boolean: ['pretty', 'debug', 'givenPortOnly', 'requestRules', 'requestRulesRequired', 'requestPlayersRequired', 'stripColors'],
string: ['guildId', 'listenUdpPort', 'ipFamily'] string: ['guildId', 'listenUdpPort', 'ipFamily'],
default: {
stripColors: true
}
}) })
const options = {} const options = {}

View File

@ -6,6 +6,7 @@ const defaultOptions = {
socketTimeout: 2000, socketTimeout: 2000,
attemptTimeout: 10000, attemptTimeout: 10000,
maxAttempts: 1, maxAttempts: 1,
stripColors: true,
ipFamily: 0 ipFamily: 0
} }

View File

@ -60,6 +60,6 @@ export default class armagetron extends Core {
} }
stripColorCodes (str) { stripColorCodes (str) {
return str.replace(/0x[0-9a-f]{6}/g, '') return this.options.stripColors ? str.replace(/0x[0-9a-f]{6}/g, '') : str
} }
} }

View File

@ -143,6 +143,6 @@ export default class doom3 extends Core {
stripColors (str) { stripColors (str) {
// uses quake 3 color codes // uses quake 3 color codes
return str.replace(/\^(X.{6}|.)/g, '') return this.options.stripColors ? str.replace(/\^(X.{6}|.)/g, '') : str
} }
} }

View File

@ -49,7 +49,7 @@ export default class gamespy2 extends Core {
if (state.raw.gamename === 'armygame') { if (state.raw.gamename === 'armygame') {
const stripColor = (str) => { const stripColor = (str) => {
// uses unreal 2 color codes // uses unreal 2 color codes
return str.replace(/\x1b...|[\x00-\x1a]/g, '') return this.options.stripColors ? str.replace(/\x1b...|[\x00-\x1a]/g, '') : str
} }
state.name = stripColor(state.name) state.name = stripColor(state.name)
state.map = stripColor(state.map) state.map = stripColor(state.map)

View File

@ -81,6 +81,6 @@ export default class nadeo extends Core {
} }
stripColors (str) { stripColors (str) {
return str.replace(/\$([0-9a-f]{3}|[a-z])/gi, '') return this.options.stripColors ? str.replace(/\$([0-9a-f]{3}|[a-z])/gi, '') : str
} }
} }

View File

@ -16,9 +16,12 @@ export default class quake3 extends quake2 {
for (const player of state.players) { for (const player of state.players) {
player.name = this.stripColors(player.name) player.name = this.stripColors(player.name)
} }
for (const bot of state.bots) {
bot.name = this.stripColors(bot.name)
}
} }
stripColors (str) { stripColors (str) {
return str.replace(/\^(X.{6}|.)/g, '') return this.options.stripColors ? str.replace(/\^(X.{6}|.)/g, '') : str
} }
} }

View File

@ -20,6 +20,6 @@ export default class savage2 extends Core {
} }
stripColorCodes (str) { stripColorCodes (str) {
return str.replace(/\^./g, '') return this.options.stripColors ? str.replace(/\^./g, '') : str
} }
} }

View File

@ -125,7 +125,7 @@ export default class unreal2 extends Core {
out = out.substring(0, out.length - 1) out = out.substring(0, out.length - 1)
} }
if (stripColor) { if (stripColor && this.options.stripColors) {
out = out.replace(/\x1b...|[\x00-\x1a]/gus, '') out = out.replace(/\x1b...|[\x00-\x1a]/gus, '')
} }