UNPKG

ddnet

Version:

A typescript npm package for interacting with data from ddnet.org

119 lines 3.86 kB
import axios from 'axios'; import { _Schema_maps_qmapper } from '../../schemas/maps/qmapper.js'; import { DDNetError, slugify } from '../../util.js'; import { CacheManager } from '../other/CacheManager.js'; import { Releases } from '../other/Releases.js'; import { Player } from '../players/Player.js'; /** * Represents a map author/mapper. */ export class Mapper { /** * Mapper query responses cache. (2h default TTL) */ static cache = new CacheManager('qmapper-cache', 2 * 60 * 60 * 1000); /** * Sets the TTL (Time-To-Live) for objects in cache. */ static setTTL = this.cache.setTTL; /** * Clears the {@link Player.cache}. */ static clearCache = this.cache.clearCache; /** * The name of this author. */ name; /** * The ddnet.org url to this author's map showcase page. */ mapShowcaseUrl; /** * The ddnet.org url to this author's player page. */ playerUrl; /** * Construct a new {@link Mapper} instance. */ constructor(data) { this.name = data.name; this.mapShowcaseUrl = `https://ddnet.org/mappers/${slugify(this.name)}`; this.playerUrl = `https://ddnet.org/players/${encodeURIComponent(this.name)}`; } /** * Returns an array of releases by this author. */ async getMaps( /** * If provided, the method will only return releases of this type. */ type) { const releases = await Releases.new(); const maps = releases.maps.filter(r => r.mappers.map(a => a.name).includes(this.name)); if (type) { return maps.filter(m => m.type === type); } return maps; } /** * Returns a new {@link Player} object from this author. */ async toPlayer() { return await Player.new(this.name); } /** * Returns the name and mapShowcaseUrl of this Author in markdown format. */ toString() { return `[${this.name}](${this.mapShowcaseUrl})`; } /** * Search for a mapper. */ static async search( /** * The value to search for. */ value, /** * Wether to bypass the cache. */ force = false) { const url = `https://ddnet.org/maps/?qmapper=${encodeURIComponent(value)}`; const parse = (data) => { const parsed = _Schema_maps_qmapper.safeParse(data); if (!parsed.success) throw new DDNetError(`Failed to parse data.`, parsed.error); return parsed.data; }; if (!force) { if (await this.cache.has(url)) { const data = await this.cache.get(url); if (data) { const parsed = parse(data); return parsed.map(mapper => ({ name: mapper.mapper, mapCount: mapper.num_maps, toPlayer: async () => await Player.new(mapper.mapper), toMapper: () => new Mapper({ name: mapper.mapper }) })); } } } const response = await axios.get(url).catch((err) => new DDNetError('An error has occurred while fetching the response from ddnet.org!', err)); if (response instanceof DDNetError) throw response; const data = response.data; if (typeof data === 'string') throw new DDNetError(`Invalid response!`, data); await this.cache.set(url, data); const parsed = parse(data); return parsed.map(mapper => ({ name: mapper.mapper, mapCount: mapper.num_maps, toPlayer: async () => await Player.new(mapper.mapper), toMapper: () => new Mapper({ name: mapper.mapper }) })); } } //# sourceMappingURL=Mapper.js.map