ddnet
Version:
A typescript npm package for interacting with data from ddnet.org
548 lines • 23 kB
JavaScript
import axios from 'axios';
import { Finish } from './classes/other/Finish.js';
import { _Schema_maps_latest } from './schemas/maps/latest.js';
import { ServerType } from './classes/players/Servers.js';
/**
* Wrapper class for library errors, used as a catch-all and to provide context.
*/
export class DDNetError extends Error {
constructor(reason, context) {
super(reason ?? 'No error message provided, see cause for more info.', {
cause: context
});
this.name = 'DDNetError';
if (Error.captureStackTrace) {
Error.captureStackTrace(this, DDNetError);
}
}
}
/**
* Converts a number of seconds to a DDNet finish time string.
*
* @example "03:23"
*/
export function timeString(
/**
* The time in seconds to convert.
*/
totalSeconds) {
if (totalSeconds < 0)
return '--:--';
const pad = (s) => (s.length < 2 ? `0${s}` : s);
const hours = Math.floor(totalSeconds / 3600).toString();
const remainingSecondsAfterHours = totalSeconds % 3600;
const minutes = Math.floor(remainingSecondsAfterHours / 60).toString();
const seconds = Math.floor(remainingSecondsAfterHours % 60).toString();
return hours === '0' ? `${pad(minutes)}:${pad(seconds)}` : `${pad(hours)}:${pad(minutes)}:${pad(seconds)}`;
}
/**
* Slugifies a name to safely use it in a url.
*
* @see
* https://github.com/ddnet/ddnet-scripts/blob/master/servers/scripts/ddnet.py#L185
*/
export function slugify(name) {
const x = '[\t !"#$%&\'()*\\-/<=>?@[\\]^_`{|},.:]+';
let string = '';
for (const c of name) {
if (c.match(x) || c.charCodeAt(0) >= 128) {
string += `-${c.charCodeAt(0)}-`;
}
else {
string += c;
}
}
return string;
}
/**
* Converts a python datetime string or timestamp into a valid javascript timestamp.
*/
export function dePythonifyTime(
/**
* The time to convert.
*/
time) {
if (typeof time === 'string')
return new Date(time).getTime();
const d = new Date(time);
// hacky fix
if (d.getFullYear() < 2000)
return new Date(time * 1000).getTime();
return d.getTime();
}
/**
* Represents server regions.
*
* @see
* https://github.com/ddnet/ddnet-web/tree/master/www/countryflags
*/
export var ServerRegion;
(function (ServerRegion) {
ServerRegion["ARG"] = "ARG";
ServerRegion["AUS"] = "AUS";
ServerRegion["BRA"] = "BRA";
ServerRegion["BHR"] = "BHR";
ServerRegion["CAN"] = "CAN";
ServerRegion["CHL"] = "CHL";
ServerRegion["CHN"] = "CHN";
ServerRegion["COL"] = "COL";
ServerRegion["CRI"] = "CRI";
ServerRegion["EUR"] = "EUR";
ServerRegion["FRA"] = "FRA";
ServerRegion["GER"] = "GER";
ServerRegion["FIN"] = "FIN";
ServerRegion["IND"] = "IND";
ServerRegion["IRN"] = "IRN";
ServerRegion["JAP"] = "JAP";
ServerRegion["KOR"] = "KOR";
ServerRegion["KSA"] = "KSA";
ServerRegion["MEX"] = "MEX";
ServerRegion["NLD"] = "NLD";
ServerRegion["PER"] = "PER";
ServerRegion["POL"] = "POL";
ServerRegion["RUS"] = "RUS";
ServerRegion["SAU"] = "SAU";
ServerRegion["SGP"] = "SGP";
ServerRegion["TUR"] = "TUR";
ServerRegion["TWN"] = "TWN";
ServerRegion["UAE"] = "UAE";
ServerRegion["UKR"] = "UKR";
ServerRegion["USA"] = "USA";
ServerRegion["ZAF"] = "ZAF";
ServerRegion["UNK"] = "UNK"; // custom unknown region
})(ServerRegion || (ServerRegion = {}));
/**
* Represents player countries and their ID's.
*
* @see
* https://github.com/ddnet/ddnet/tree/master/data/countryflags
*
* @see
* https://github.com/ddnet/ddnet/blob/master/data/countryflags/index.txt
*/
export var PlayerCountry;
(function (PlayerCountry) {
// default
PlayerCountry[PlayerCountry["default"] = -1] = "default";
// ISO 3166-2 subdivisions
PlayerCountry[PlayerCountry["GB-ENG"] = 901] = "GB-ENG";
PlayerCountry[PlayerCountry["GB-NIR"] = 902] = "GB-NIR";
PlayerCountry[PlayerCountry["GB-SCT"] = 903] = "GB-SCT";
PlayerCountry[PlayerCountry["GB-WLS"] = 904] = "GB-WLS";
PlayerCountry[PlayerCountry["ES-CT"] = 906] = "ES-CT";
PlayerCountry[PlayerCountry["ES-GA"] = 907] = "ES-GA";
// ISO 3166/MA exceptional reservations
PlayerCountry[PlayerCountry["EU"] = 905] = "EU";
// ISO 3166-1 based
PlayerCountry[PlayerCountry["AF"] = 4] = "AF";
PlayerCountry[PlayerCountry["AX"] = 248] = "AX";
PlayerCountry[PlayerCountry["AL"] = 8] = "AL";
PlayerCountry[PlayerCountry["DZ"] = 12] = "DZ";
PlayerCountry[PlayerCountry["AS"] = 16] = "AS";
PlayerCountry[PlayerCountry["AD"] = 20] = "AD";
PlayerCountry[PlayerCountry["AO"] = 24] = "AO";
PlayerCountry[PlayerCountry["AI"] = 660] = "AI";
PlayerCountry[PlayerCountry["AQ"] = 10] = "AQ";
PlayerCountry[PlayerCountry["AG"] = 28] = "AG";
PlayerCountry[PlayerCountry["AR"] = 32] = "AR";
PlayerCountry[PlayerCountry["AM"] = 51] = "AM";
PlayerCountry[PlayerCountry["AW"] = 533] = "AW";
PlayerCountry[PlayerCountry["AU"] = 36] = "AU";
PlayerCountry[PlayerCountry["AT"] = 40] = "AT";
PlayerCountry[PlayerCountry["AZ"] = 31] = "AZ";
PlayerCountry[PlayerCountry["BS"] = 44] = "BS";
PlayerCountry[PlayerCountry["BH"] = 48] = "BH";
PlayerCountry[PlayerCountry["BD"] = 50] = "BD";
PlayerCountry[PlayerCountry["BB"] = 52] = "BB";
PlayerCountry[PlayerCountry["BY"] = 112] = "BY";
PlayerCountry[PlayerCountry["BE"] = 56] = "BE";
PlayerCountry[PlayerCountry["BZ"] = 84] = "BZ";
PlayerCountry[PlayerCountry["BJ"] = 204] = "BJ";
PlayerCountry[PlayerCountry["BM"] = 60] = "BM";
PlayerCountry[PlayerCountry["BT"] = 64] = "BT";
PlayerCountry[PlayerCountry["BO"] = 68] = "BO";
// 'BQ' = 535,
PlayerCountry[PlayerCountry["BA"] = 70] = "BA";
PlayerCountry[PlayerCountry["BW"] = 72] = "BW";
// 'BV' = 74,
PlayerCountry[PlayerCountry["BR"] = 76] = "BR";
PlayerCountry[PlayerCountry["IO"] = 86] = "IO";
PlayerCountry[PlayerCountry["BN"] = 96] = "BN";
PlayerCountry[PlayerCountry["BG"] = 100] = "BG";
PlayerCountry[PlayerCountry["BF"] = 854] = "BF";
PlayerCountry[PlayerCountry["BI"] = 108] = "BI";
PlayerCountry[PlayerCountry["KH"] = 116] = "KH";
PlayerCountry[PlayerCountry["CM"] = 120] = "CM";
PlayerCountry[PlayerCountry["CA"] = 124] = "CA";
PlayerCountry[PlayerCountry["CV"] = 132] = "CV";
PlayerCountry[PlayerCountry["KY"] = 136] = "KY";
PlayerCountry[PlayerCountry["CF"] = 140] = "CF";
PlayerCountry[PlayerCountry["TD"] = 148] = "TD";
PlayerCountry[PlayerCountry["CL"] = 152] = "CL";
PlayerCountry[PlayerCountry["CN"] = 156] = "CN";
PlayerCountry[PlayerCountry["CX"] = 162] = "CX";
PlayerCountry[PlayerCountry["CC"] = 166] = "CC";
PlayerCountry[PlayerCountry["CO"] = 170] = "CO";
PlayerCountry[PlayerCountry["KM"] = 174] = "KM";
PlayerCountry[PlayerCountry["CG"] = 178] = "CG";
PlayerCountry[PlayerCountry["CD"] = 180] = "CD";
PlayerCountry[PlayerCountry["CK"] = 184] = "CK";
PlayerCountry[PlayerCountry["CR"] = 188] = "CR";
PlayerCountry[PlayerCountry["CI"] = 384] = "CI";
PlayerCountry[PlayerCountry["HR"] = 191] = "HR";
PlayerCountry[PlayerCountry["CU"] = 192] = "CU";
PlayerCountry[PlayerCountry["CW"] = 531] = "CW";
PlayerCountry[PlayerCountry["CY"] = 196] = "CY";
PlayerCountry[PlayerCountry["CZ"] = 203] = "CZ";
PlayerCountry[PlayerCountry["DK"] = 208] = "DK";
PlayerCountry[PlayerCountry["DJ"] = 262] = "DJ";
PlayerCountry[PlayerCountry["DM"] = 212] = "DM";
PlayerCountry[PlayerCountry["DO"] = 214] = "DO";
PlayerCountry[PlayerCountry["EC"] = 218] = "EC";
PlayerCountry[PlayerCountry["EG"] = 818] = "EG";
PlayerCountry[PlayerCountry["SV"] = 222] = "SV";
PlayerCountry[PlayerCountry["GQ"] = 226] = "GQ";
PlayerCountry[PlayerCountry["ER"] = 232] = "ER";
PlayerCountry[PlayerCountry["EE"] = 233] = "EE";
PlayerCountry[PlayerCountry["ET"] = 231] = "ET";
PlayerCountry[PlayerCountry["FK"] = 238] = "FK";
PlayerCountry[PlayerCountry["FO"] = 234] = "FO";
PlayerCountry[PlayerCountry["FJ"] = 242] = "FJ";
PlayerCountry[PlayerCountry["FI"] = 246] = "FI";
PlayerCountry[PlayerCountry["FR"] = 250] = "FR";
PlayerCountry[PlayerCountry["GF"] = 254] = "GF";
PlayerCountry[PlayerCountry["PF"] = 258] = "PF";
PlayerCountry[PlayerCountry["TF"] = 260] = "TF";
PlayerCountry[PlayerCountry["GA"] = 266] = "GA";
PlayerCountry[PlayerCountry["GM"] = 270] = "GM";
PlayerCountry[PlayerCountry["GE"] = 268] = "GE";
PlayerCountry[PlayerCountry["DE"] = 276] = "DE";
PlayerCountry[PlayerCountry["GH"] = 288] = "GH";
PlayerCountry[PlayerCountry["GI"] = 292] = "GI";
PlayerCountry[PlayerCountry["GR"] = 300] = "GR";
PlayerCountry[PlayerCountry["GL"] = 304] = "GL";
PlayerCountry[PlayerCountry["GD"] = 308] = "GD";
PlayerCountry[PlayerCountry["GP"] = 312] = "GP";
PlayerCountry[PlayerCountry["GU"] = 316] = "GU";
PlayerCountry[PlayerCountry["GT"] = 320] = "GT";
PlayerCountry[PlayerCountry["GG"] = 831] = "GG";
PlayerCountry[PlayerCountry["GN"] = 324] = "GN";
PlayerCountry[PlayerCountry["GW"] = 624] = "GW";
PlayerCountry[PlayerCountry["GY"] = 328] = "GY";
PlayerCountry[PlayerCountry["HT"] = 332] = "HT";
// 'HM' = 334,
PlayerCountry[PlayerCountry["VA"] = 336] = "VA";
PlayerCountry[PlayerCountry["HN"] = 340] = "HN";
PlayerCountry[PlayerCountry["HK"] = 344] = "HK";
PlayerCountry[PlayerCountry["HU"] = 348] = "HU";
PlayerCountry[PlayerCountry["IS"] = 352] = "IS";
PlayerCountry[PlayerCountry["IN"] = 356] = "IN";
PlayerCountry[PlayerCountry["ID"] = 360] = "ID";
PlayerCountry[PlayerCountry["IR"] = 364] = "IR";
PlayerCountry[PlayerCountry["IQ"] = 368] = "IQ";
PlayerCountry[PlayerCountry["IE"] = 372] = "IE";
PlayerCountry[PlayerCountry["IM"] = 833] = "IM";
PlayerCountry[PlayerCountry["IL"] = 376] = "IL";
PlayerCountry[PlayerCountry["IT"] = 380] = "IT";
PlayerCountry[PlayerCountry["JM"] = 388] = "JM";
PlayerCountry[PlayerCountry["JP"] = 392] = "JP";
PlayerCountry[PlayerCountry["JE"] = 832] = "JE";
PlayerCountry[PlayerCountry["JO"] = 400] = "JO";
PlayerCountry[PlayerCountry["KZ"] = 398] = "KZ";
PlayerCountry[PlayerCountry["KE"] = 404] = "KE";
PlayerCountry[PlayerCountry["KI"] = 296] = "KI";
PlayerCountry[PlayerCountry["KP"] = 408] = "KP";
PlayerCountry[PlayerCountry["KR"] = 410] = "KR";
PlayerCountry[PlayerCountry["KW"] = 414] = "KW";
PlayerCountry[PlayerCountry["KG"] = 417] = "KG";
PlayerCountry[PlayerCountry["LA"] = 418] = "LA";
PlayerCountry[PlayerCountry["LV"] = 428] = "LV";
PlayerCountry[PlayerCountry["LB"] = 422] = "LB";
PlayerCountry[PlayerCountry["LS"] = 426] = "LS";
PlayerCountry[PlayerCountry["LR"] = 430] = "LR";
PlayerCountry[PlayerCountry["LY"] = 434] = "LY";
PlayerCountry[PlayerCountry["LI"] = 438] = "LI";
PlayerCountry[PlayerCountry["LT"] = 440] = "LT";
PlayerCountry[PlayerCountry["LU"] = 442] = "LU";
PlayerCountry[PlayerCountry["MO"] = 446] = "MO";
PlayerCountry[PlayerCountry["MK"] = 807] = "MK";
PlayerCountry[PlayerCountry["MG"] = 450] = "MG";
PlayerCountry[PlayerCountry["MW"] = 454] = "MW";
PlayerCountry[PlayerCountry["MY"] = 458] = "MY";
PlayerCountry[PlayerCountry["MV"] = 462] = "MV";
PlayerCountry[PlayerCountry["ML"] = 466] = "ML";
PlayerCountry[PlayerCountry["MT"] = 470] = "MT";
PlayerCountry[PlayerCountry["MH"] = 584] = "MH";
PlayerCountry[PlayerCountry["MQ"] = 474] = "MQ";
PlayerCountry[PlayerCountry["MR"] = 478] = "MR";
PlayerCountry[PlayerCountry["MU"] = 480] = "MU";
// 'YT' = 175,
PlayerCountry[PlayerCountry["MX"] = 484] = "MX";
PlayerCountry[PlayerCountry["FM"] = 583] = "FM";
PlayerCountry[PlayerCountry["MD"] = 498] = "MD";
PlayerCountry[PlayerCountry["MC"] = 492] = "MC";
PlayerCountry[PlayerCountry["MN"] = 496] = "MN";
PlayerCountry[PlayerCountry["ME"] = 499] = "ME";
PlayerCountry[PlayerCountry["MS"] = 500] = "MS";
PlayerCountry[PlayerCountry["MA"] = 504] = "MA";
PlayerCountry[PlayerCountry["MZ"] = 508] = "MZ";
PlayerCountry[PlayerCountry["MM"] = 104] = "MM";
PlayerCountry[PlayerCountry["NA"] = 516] = "NA";
PlayerCountry[PlayerCountry["NR"] = 520] = "NR";
PlayerCountry[PlayerCountry["NP"] = 524] = "NP";
PlayerCountry[PlayerCountry["NL"] = 528] = "NL";
PlayerCountry[PlayerCountry["NC"] = 540] = "NC";
PlayerCountry[PlayerCountry["NZ"] = 554] = "NZ";
PlayerCountry[PlayerCountry["NI"] = 558] = "NI";
PlayerCountry[PlayerCountry["NE"] = 562] = "NE";
PlayerCountry[PlayerCountry["NG"] = 566] = "NG";
PlayerCountry[PlayerCountry["NU"] = 570] = "NU";
PlayerCountry[PlayerCountry["NF"] = 574] = "NF";
PlayerCountry[PlayerCountry["MP"] = 580] = "MP";
PlayerCountry[PlayerCountry["NO"] = 578] = "NO";
PlayerCountry[PlayerCountry["OM"] = 512] = "OM";
PlayerCountry[PlayerCountry["PK"] = 586] = "PK";
PlayerCountry[PlayerCountry["PW"] = 585] = "PW";
PlayerCountry[PlayerCountry["PA"] = 591] = "PA";
PlayerCountry[PlayerCountry["PG"] = 598] = "PG";
PlayerCountry[PlayerCountry["PY"] = 600] = "PY";
PlayerCountry[PlayerCountry["PE"] = 604] = "PE";
PlayerCountry[PlayerCountry["PH"] = 608] = "PH";
PlayerCountry[PlayerCountry["PN"] = 612] = "PN";
PlayerCountry[PlayerCountry["PL"] = 616] = "PL";
PlayerCountry[PlayerCountry["PT"] = 620] = "PT";
PlayerCountry[PlayerCountry["PR"] = 630] = "PR";
PlayerCountry[PlayerCountry["PS"] = 275] = "PS";
PlayerCountry[PlayerCountry["QA"] = 634] = "QA";
PlayerCountry[PlayerCountry["RE"] = 638] = "RE";
PlayerCountry[PlayerCountry["RO"] = 642] = "RO";
PlayerCountry[PlayerCountry["RU"] = 643] = "RU";
PlayerCountry[PlayerCountry["RW"] = 646] = "RW";
PlayerCountry[PlayerCountry["BL"] = 652] = "BL";
PlayerCountry[PlayerCountry["SH"] = 654] = "SH";
PlayerCountry[PlayerCountry["KN"] = 659] = "KN";
PlayerCountry[PlayerCountry["LC"] = 662] = "LC";
PlayerCountry[PlayerCountry["MF"] = 663] = "MF";
PlayerCountry[PlayerCountry["PM"] = 666] = "PM";
PlayerCountry[PlayerCountry["VC"] = 670] = "VC";
PlayerCountry[PlayerCountry["WS"] = 882] = "WS";
PlayerCountry[PlayerCountry["SM"] = 674] = "SM";
PlayerCountry[PlayerCountry["ST"] = 678] = "ST";
PlayerCountry[PlayerCountry["SA"] = 682] = "SA";
PlayerCountry[PlayerCountry["SN"] = 686] = "SN";
PlayerCountry[PlayerCountry["RS"] = 688] = "RS";
PlayerCountry[PlayerCountry["SC"] = 690] = "SC";
PlayerCountry[PlayerCountry["SL"] = 694] = "SL";
PlayerCountry[PlayerCountry["SG"] = 702] = "SG";
PlayerCountry[PlayerCountry["SX"] = 534] = "SX";
PlayerCountry[PlayerCountry["SK"] = 703] = "SK";
PlayerCountry[PlayerCountry["SI"] = 705] = "SI";
PlayerCountry[PlayerCountry["SB"] = 90] = "SB";
PlayerCountry[PlayerCountry["SO"] = 706] = "SO";
PlayerCountry[PlayerCountry["SS"] = 737] = "SS";
PlayerCountry[PlayerCountry["ZA"] = 710] = "ZA";
PlayerCountry[PlayerCountry["GS"] = 239] = "GS";
PlayerCountry[PlayerCountry["ES"] = 724] = "ES";
PlayerCountry[PlayerCountry["LK"] = 144] = "LK";
PlayerCountry[PlayerCountry["SD"] = 736] = "SD";
PlayerCountry[PlayerCountry["SR"] = 740] = "SR";
// 'SJ' = 744,
PlayerCountry[PlayerCountry["SZ"] = 748] = "SZ";
PlayerCountry[PlayerCountry["SE"] = 752] = "SE";
PlayerCountry[PlayerCountry["CH"] = 756] = "CH";
PlayerCountry[PlayerCountry["SY"] = 760] = "SY";
PlayerCountry[PlayerCountry["TW"] = 158] = "TW";
PlayerCountry[PlayerCountry["TJ"] = 762] = "TJ";
PlayerCountry[PlayerCountry["TZ"] = 834] = "TZ";
PlayerCountry[PlayerCountry["TH"] = 764] = "TH";
PlayerCountry[PlayerCountry["TL"] = 626] = "TL";
PlayerCountry[PlayerCountry["TG"] = 768] = "TG";
PlayerCountry[PlayerCountry["TK"] = 772] = "TK";
PlayerCountry[PlayerCountry["TO"] = 776] = "TO";
PlayerCountry[PlayerCountry["TT"] = 780] = "TT";
PlayerCountry[PlayerCountry["TN"] = 788] = "TN";
PlayerCountry[PlayerCountry["TR"] = 792] = "TR";
PlayerCountry[PlayerCountry["TM"] = 795] = "TM";
PlayerCountry[PlayerCountry["TC"] = 796] = "TC";
PlayerCountry[PlayerCountry["TV"] = 798] = "TV";
PlayerCountry[PlayerCountry["UG"] = 800] = "UG";
PlayerCountry[PlayerCountry["UA"] = 804] = "UA";
PlayerCountry[PlayerCountry["AE"] = 784] = "AE";
PlayerCountry[PlayerCountry["GB"] = 826] = "GB";
PlayerCountry[PlayerCountry["US"] = 840] = "US";
// 'UM' = 581,
PlayerCountry[PlayerCountry["UY"] = 858] = "UY";
PlayerCountry[PlayerCountry["UZ"] = 860] = "UZ";
PlayerCountry[PlayerCountry["VU"] = 548] = "VU";
PlayerCountry[PlayerCountry["VE"] = 862] = "VE";
PlayerCountry[PlayerCountry["VN"] = 704] = "VN";
PlayerCountry[PlayerCountry["VG"] = 92] = "VG";
PlayerCountry[PlayerCountry["VI"] = 850] = "VI";
PlayerCountry[PlayerCountry["WF"] = 876] = "WF";
PlayerCountry[PlayerCountry["EH"] = 732] = "EH";
PlayerCountry[PlayerCountry["YE"] = 887] = "YE";
PlayerCountry[PlayerCountry["ZM"] = 894] = "ZM";
PlayerCountry[PlayerCountry["ZW"] = 716] = "ZW";
})(PlayerCountry || (PlayerCountry = {}));
/**
* Represents map tiles.
*
* @see
* https://github.com/ddnet/ddnet-web/tree/master/www/tiles
*/
export var Tile;
(function (Tile) {
Tile["BONUS"] = "BONUS";
Tile["BOOST"] = "BOOST";
Tile["CHECKPOINT_FIRST"] = "CHECKPOINT_FIRST";
Tile["CRAZY_SHOTGUN"] = "CRAZY_SHOTGUN";
Tile["DEATH"] = "DEATH";
Tile["DFREEZE"] = "DFREEZE";
Tile["DOOR"] = "DOOR";
Tile["DRAGGER"] = "DRAGGER";
Tile["EHOOK_START"] = "EHOOK_START";
Tile["HIT_END"] = "HIT_END";
Tile["JETPACK_START"] = "JETPACK_START";
Tile["JUMP"] = "JUMP";
Tile["LASER_STOP"] = "LASER_STOP";
Tile["NPC_START"] = "NPC_START";
Tile["NPH_START"] = "NPH_START";
Tile["OLDLASER"] = "OLDLASER";
Tile["PLASMAE"] = "PLASMAE";
Tile["PLASMAF"] = "PLASMAF";
Tile["PLASMAU"] = "PLASMAU";
Tile["POWERUP_NINJA"] = "POWERUP_NINJA";
Tile["SOLO_START"] = "SOLO_START";
Tile["STOP"] = "STOP";
Tile["SUPER_START"] = "SUPER_START";
Tile["SWITCH"] = "SWITCH";
Tile["SWITCH_TIMED"] = "SWITCH_TIMED";
Tile["TELECHECK"] = "TELECHECK";
Tile["TELECHECKIN"] = "TELECHECKIN";
Tile["TELEIN"] = "TELEIN";
Tile["TELEINEVIL"] = "TELEINEVIL";
Tile["TELEINHOOK"] = "TELEINHOOK";
Tile["TELEINWEAPON"] = "TELEINWEAPON";
Tile["TELE_GRENADE"] = "TELE_GRENADE";
Tile["TELE_GUN"] = "TELE_GUN";
Tile["TELE_LASER"] = "TELE_LASER";
Tile["THROUGH"] = "THROUGH";
Tile["THROUGH_ALL"] = "THROUGH_ALL";
Tile["TUNE"] = "TUNE";
Tile["WALLJUMP"] = "WALLJUMP";
Tile["WEAPON_GRENADE"] = "WEAPON_GRENADE";
Tile["WEAPON_RIFLE"] = "WEAPON_RIFLE";
Tile["WEAPON_SHOTGUN"] = "WEAPON_SHOTGUN";
Tile["UNKNOWN_TILE"] = "UNKNOWN_TILE";
})(Tile || (Tile = {}));
/**
* @param kind The kind of image wanted, this is used at run-time to determine which url gets returned for overlapping members.
* @typeParam T Used in combination with the {@link kind} param to also determine the kind of image wanted at compile-time.
*/
export function getImageUrl(eMem, kind) {
if (kind === 'country')
return `https://raw.githubusercontent.com/ddnet/ddnet/master/data/countryflags/${eMem}.png`;
if (kind === 'region')
return `https://raw.githubusercontent.com/ddnet/ddnet-web/master/www/countryflags/${eMem}.png`;
if (kind === 'tile')
return `https://raw.githubusercontent.com/ddnet/ddnet-web/master/www/tiles/${eMem}.png`;
throw new DDNetError(`Unknown error.`);
}
/**
* Splits a mapper name string into an array of mapper names.
*
* @remarks
* It does the reverse of {@link formatStringList}
*
* @see
* https://github.com/ddnet/ddnet-scripts/blob/8e0909edbeb5d7a6446349dc66a3beb0f5ddccc7/servers/scripts/ddnet.py#L213
*/
export function splitMappers(mapperNames) {
let names = mapperNames.split(', ');
if (names.length) {
names = names.slice(0, -1).concat(names[names.length - 1].split(' & '));
}
return names;
}
/**
* Calculates map points reward based on difficulty (server type) and star count.
*
* @see
* https://ddnet.org/ranks/fun/#points
*/
export function calculatePoints(type, stars) {
// [multiplier, offset]
const multiplierAndOffsetMap = {
[ServerType.novice]: [1, 0],
[ServerType.moderate]: [2, 5],
[ServerType.brutal]: [3, 15],
[ServerType.insane]: [4, 30],
[ServerType.dummy]: [5, 5],
[ServerType.ddmaxEasy]: [4, 0],
[ServerType.ddmaxNext]: [4, 0],
[ServerType.ddmaxPro]: [4, 0],
[ServerType.ddmaxNut]: [4, 0],
[ServerType.oldschool]: [6, 0],
[ServerType.solo]: [4, 0],
[ServerType.race]: [2, 0],
[ServerType.fun]: [0, 0],
[ServerType.unknown]: [-1, -1]
};
const [multiplier, offset] = multiplierAndOffsetMap[type];
return stars * multiplier + offset;
}
/**
* Formats an array of strings into a list.
*
* @example
* ```ts
* const authors = ["Sans3108", "urg"];
* console.log(formatStringList(authors)); // 'Sans3108 & urg'
*
* const authors = ["Sans3108", "urg", "Meloƞ"];
* console.log(formatStringList(authors)); // 'Sans3108, urg & Meloƞ'
* ```
*/
export function formatStringList(strings) {
if (strings.length === 0)
return '';
if (strings.length === 1)
return strings[0];
if (strings.length === 2)
return `${strings[0]} & ${strings[1]}`;
return `${strings.slice(0, strings.length - 1).join(', ')} & ${strings[strings.length - 1]}`;
}
/**
* Fetch the latest map finishes.
*/
export async function getLatestFinishes(
/**
* Filtering options for latest finishes.
*/
filters) {
const { region, serverType } = filters ?? {};
let url = `https://ddnet.org/maps/?latest=1`;
if (region)
url += `&country=${region}`;
if (serverType)
url += `&server=${serverType}`;
const response = await axios.get(url).catch((err) => new DDNetError('An error has occurred while fetching the map data from ddnet.org!', err));
if (response instanceof DDNetError)
throw response;
const responseData = response.data;
if (typeof responseData === 'string')
throw new DDNetError(`Invalid response!`, responseData);
const parsed = _Schema_maps_latest.safeParse(responseData);
if (!parsed.success)
throw new DDNetError(parsed.error.message, parsed.error);
const { data } = parsed;
return data.map(f => new Finish({
timestamp: dePythonifyTime(f.timestamp),
mapName: f.map,
players: [f.name],
timeSeconds: f.time,
region: ServerRegion[f.server] ?? ServerRegion.UNK,
rank: { placement: -1, points: -1 }
}));
}
//# sourceMappingURL=util.js.map