Something went wrong. Try again.
Your trusty automation assistant in TLE Community misadventures!
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495import _ from 'lodash'
import lacuna from './client'import call from './call'import util from './util'
/** * Normalise a body's buildings into an array of `{ ..., id }` objects. Accepts * either a body id (fetched from the server) or an already-fetched buildings * collection (array or id-keyed object). Replaces the old `lacuna.body.buildings`. * * @param {string|number|object|array} idOrBuildings * @return {Promise<array>} */export async function bodyBuildings(idOrBuildings) { if (_.isString(idOrBuildings) || _.isNumber(idOrBuildings)) { const result = await call(() => lacuna.body.getBuildings({ body_id: idOrBuildings })) return util.objectToArray(result.buildings, 'id') }
return util.objectToArray(idOrBuildings, 'id')}
/** * Every 100%-efficiency building of the given name on a body, level-coerced and * sorted ascending by level. Replaces `lacuna.body.findBuildings`. * * @param {string|number|object|array} idOrBuildings - body id or pre-fetched buildings * @param {string} name - the building name to match * @return {Promise<array>} */export function findBuildings(idOrBuildings, name) { return bodyBuildings(idOrBuildings).then((buildings) => _.chain(buildings) .filter({ name, efficiency: 100 }) .map((b) => { b.level = util.int(b.level) return b }) .sortBy('level') .value() )}
/** * The highest-level 100%-efficiency building of the given name on a body, or * `undefined`. Replaces `lacuna.body.findBuilding`. * * @param {string|number|object|array} idOrBuildings - body id or pre-fetched buildings * @param {string} name - the building name to match * @return {Promise<object|undefined>} */export function findBuilding(idOrBuildings, name) { return findBuildings(idOrBuildings, name).then((matches) => _.first(matches.reverse()))}
/** * The lowest-level 100%-efficiency building of the given name on a body, or `undefined`. * * @param {string|number|object|array} idOrBuildings - body id or pre-fetched buildings * @param {string} name - the building name to match * @return {Promise<object|undefined>} */export function findLeastDevelopedBuilding(idOrBuildings, name) { return findBuildings(idOrBuildings, name).then((matches) => _.first(matches))}
/** * A `{ [glyphName]: quantity }` map for an Archaeology Ministry. Replaces * `lacuna.buildings.archaeology.getInventory`. Goes through `callLegacy` because * `archaeology/get_glyphs` has no `/v2` method and the typed `get_glyph_summary` * response drops the per-glyph `name` the callers key on. * * @param {string|number} buildingId * @return {Promise<Object.<string, number>>} */export function archGetInventory(buildingId) { return call(() => lacuna.server.callLegacy({ module: 'archaeology', method: 'get_glyphs', params: [buildingId], addSession: true, }) ).then((result) => { const summary = {}
_.each(result.glyphs, (glyph) => { summary[glyph.name] = util.int(glyph.quantity) })
return summary })}