import _ 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} */ 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} */ 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} */ 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} */ 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>} */ 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 }) }