From 8b36f456f3fe435d333bf71f58be75fcfbffa1da Mon Sep 17 00:00:00 2001 From: Natalie Rose Date: Wed, 27 Aug 2025 12:05:10 +1000 Subject: [PATCH] Ditching bluebird promises polyfill (what a throwback) --- lib/lacuna/modules/body.js | 1 - lib/lacuna/modules/captcha.js | 2 - lib/lacuna/modules/empire.js | 45 ++++++++-------- lib/tasks/build-ships.js | 70 ++++++++++++------------- lib/tasks/docked-ships.js | 1 - lib/tasks/docks.js | 1 - lib/tasks/glyph-report.ts | 1 - lib/tasks/halls-cost.js | 1 - lib/tasks/make-halls.js | 9 ++-- lib/tasks/make-spies.js | 9 ++-- lib/tasks/push-buildings-up.js | 9 ++-- lib/tasks/push-glyphs.js | 1 - lib/tasks/scuttle-ships.js | 1 - lib/tasks/spy-trainer.js | 68 +++++++++++------------- lib/tasks/upgrade-buildings.js | 19 ++++--- lib/tasks/view-laws.js | 1 - package.json | 1 - test/scripts/lacuna-click-limit-test.js | 2 - 18 files changed, 108 insertions(+), 134 deletions(-) diff --git a/lib/lacuna/modules/body.js b/lib/lacuna/modules/body.js index 111dcaf..8b6de76 100644 --- a/lib/lacuna/modules/body.js +++ b/lib/lacuna/modules/body.js @@ -1,5 +1,4 @@ import _ from 'lodash' -import Promise from 'bluebird' import util from '../../util' diff --git a/lib/lacuna/modules/captcha.js b/lib/lacuna/modules/captcha.js index 4caca4c..3dd02da 100644 --- a/lib/lacuna/modules/captcha.js +++ b/lib/lacuna/modules/captcha.js @@ -1,5 +1,3 @@ -import Promise from 'bluebird' - import Module from '../module' import log from '../../log' diff --git a/lib/lacuna/modules/empire.js b/lib/lacuna/modules/empire.js index 4779438..493b684 100644 --- a/lib/lacuna/modules/empire.js +++ b/lib/lacuna/modules/empire.js @@ -1,4 +1,3 @@ -import Promise from 'bluebird' import _ from 'lodash' import Module from '../module' @@ -105,22 +104,22 @@ class Empire extends Module { // Invert so we can key by name instead of ID let planets = _.invert(status.empire.planets) - return Promise.mapSeries(arr, (planetName) => { - return new Promise((resolve, reject) => { - let planetId = planets[planetName] + return Promise.all( + arr.map((planetName) => { + return new Promise((resolve, reject) => { + let planetId = planets[planetName] - if (planetId) { - resolve({ - id: planetId, - name: planetName, - }) - } else { - reject(`Planet ${planetName} not found`) - } + if (planetId) { + resolve({ + id: planetId, + name: planetName, + }) + } else { + reject(`Planet ${planetName} not found`) + } + }) }) - }).then((planets) => { - resolve(planets) - }) + ) }) } }) @@ -168,14 +167,14 @@ class Empire extends Module { * @param {function} callback - a thenable to run on each planet * @param {object} opts - options (set `force` to true to ignore unhappiness checks here) */ - eachPlanet(planets, callback, opts) { + async eachPlanet(planets, callback, opts) { opts = _.defaults(opts || {}, { force: false }) - return Promise.each(planets, (planet) => { + for (const planet of planets) { log.newline() log.info(`Looking at ${planet.name}`) - return lacuna.body.getBuildings([planet.id]).then((result) => { + await lacuna.body.getBuildings([planet.id]).then((result) => { let buildings = util.objectToArray(result.buildings, 'id') // Don't do anything on planets that are unhappy. When a planet's happniess is negative @@ -193,7 +192,7 @@ class Empire extends Module { log.info('Moving on') }) }) - }) + } } planets() { @@ -237,14 +236,14 @@ class Empire extends Module { let result = [] this.colonies() - .then((colonies) => { - return Promise.mapSeries(colonies, (colony) => { + .then(async (colonies) => { + for (const colony of colonies) { log.info(`Looking at ${colony.name}`) - return lacuna.body.buildings(colony.id).then((buildings) => { + await lacuna.body.buildings(colony.id).then((buildings) => { result = result.concat(buildings) }) - }) + } }) .then(() => { resolve(result) diff --git a/lib/tasks/build-ships.js b/lib/tasks/build-ships.js index 8c2007e..1c14cef 100644 --- a/lib/tasks/build-ships.js +++ b/lib/tasks/build-ships.js @@ -1,4 +1,3 @@ -import Promise from 'bluebird' import _ from 'lodash' import lacuna from '../lacuna' @@ -77,51 +76,48 @@ class BuildShips { } findShipBuildings(colony, buildings) { - return new Promise((resolve, reject) => { - Promise.join( - lacuna.body.findBuilding(buildings, 'Shipyard'), - lacuna.body.findBuilding(buildings, 'Space Port') - ).spread((sy, sp) => { - if (!sy) { - reject(`No Shipyard found on ${colony.name}`) - } else if (!sp) { - reject(`No Space Port found on ${colony.name}`) - } else { - resolve([sy, sp]) - } - }) + return new Promise(async (resolve, reject) => { + const sy = await lacuna.body.findBuilding(buildings, 'Shipyard') + const sp = await lacuna.body.findBuilding(buildings, 'Space Port') + + if (!sy) { + reject(`No Shipyard found on ${colony.name}`) + } else if (!sp) { + reject(`No Space Port found on ${colony.name}`) + } else { + resolve([sy, sp]) + } }) } - handleColony(colony, buildings) { - return this.findShipBuildings(colony, buildings).spread((sy, sp) => { - return this.determineToBuild(this.options.quantity, sy, sp).then((toBuild) => { - if (this.options.quantity > toBuild) { - // Explain to the user why we're building less than they asked for. - - if (this.options.topoff) { - let msg = '' + async handleColony(colony, buildings) { + const [sy, sp] = await this.findShipBuildings(colony, buildings) + await this.determineToBuild(this.options.quantity, sy, sp).then((toBuild) => { + if (this.options.quantity > toBuild) { + // Explain to the user why we're building less than they asked for. - if (toBuild === 0) { - msg = `no need to build more` - } else { - msg = `building ${toBuild} more` - } + if (this.options.topoff) { + let msg = '' - log.info(`Some ships already built or under construction, ${msg}`) + if (toBuild === 0) { + msg = `no need to build more` } else { - let quantityPlural = util.handlePlurality(this.options.quantity, this.options.type) - log.info( - `Insufficient docks/queue space for ${this.options.quantity} ${quantityPlural}` + - `, building ${toBuild} instead` - ) + msg = `building ${toBuild} more` } - } - if (toBuild > 0) { - return this.buildShips(colony, sy, toBuild) + log.info(`Some ships already built or under construction, ${msg}`) + } else { + let quantityPlural = util.handlePlurality(this.options.quantity, this.options.type) + log.info( + `Insufficient docks/queue space for ${this.options.quantity} ${quantityPlural}` + + `, building ${toBuild} instead` + ) } - }) + } + + if (toBuild > 0) { + return this.buildShips(colony, sy, toBuild) + } }) } diff --git a/lib/tasks/docked-ships.js b/lib/tasks/docked-ships.js index d441571..c6ec183 100644 --- a/lib/tasks/docked-ships.js +++ b/lib/tasks/docked-ships.js @@ -1,4 +1,3 @@ -import Promise from 'bluebird' import _ from 'lodash' import Table from 'cli-table3' diff --git a/lib/tasks/docks.js b/lib/tasks/docks.js index 32682a2..3aac42d 100644 --- a/lib/tasks/docks.js +++ b/lib/tasks/docks.js @@ -1,4 +1,3 @@ -import Promise from 'bluebird' import _ from 'lodash' import Table from 'cli-table3' diff --git a/lib/tasks/glyph-report.ts b/lib/tasks/glyph-report.ts index fff68f3..40373dc 100644 --- a/lib/tasks/glyph-report.ts +++ b/lib/tasks/glyph-report.ts @@ -1,5 +1,4 @@ import _ from 'lodash' -import Promise from 'bluebird' import lacuna from '../lacuna' import log from '../log' diff --git a/lib/tasks/halls-cost.js b/lib/tasks/halls-cost.js index 4e669d3..76e82e3 100644 --- a/lib/tasks/halls-cost.js +++ b/lib/tasks/halls-cost.js @@ -1,4 +1,3 @@ -import Promise from 'bluebird' import _ from 'lodash' import util from '../util' diff --git a/lib/tasks/make-halls.js b/lib/tasks/make-halls.js index e9cbe39..46d2f87 100644 --- a/lib/tasks/make-halls.js +++ b/lib/tasks/make-halls.js @@ -1,5 +1,4 @@ import _ from 'lodash' -import Promise from 'bluebird' import lacuna from '../lacuna' import log from '../log' @@ -52,10 +51,10 @@ class MakeHalls { }) } - makeHalls(colony, archaeology, inventory) { - return Promise.each(RECIPES, (recipe) => { - return this.makeRecipe(colony, archaeology, inventory, recipe) - }) + async makeHalls(colony, archaeology, inventory) { + for (const recipe of RECIPES) { + await this.makeRecipe(colony, archaeology, inventory, recipe) + } } handleColony(colony, buildings) { diff --git a/lib/tasks/make-spies.js b/lib/tasks/make-spies.js index cd71154..e261d7c 100644 --- a/lib/tasks/make-spies.js +++ b/lib/tasks/make-spies.js @@ -1,4 +1,3 @@ -import Promise from 'bluebird' import _ from 'lodash' import lacuna from '../lacuna' @@ -12,7 +11,7 @@ class MakeSpies { this.spiesTrained = 0 } - makeSpies(intelId, quantity) { + async makeSpies(intelId, quantity) { // We can only train spies in batches of 1, 2, 3, 4 or 5. // If you see this and can think of a better way to do it, please // feel free to send in a PR - 'cause this is shit. @@ -29,12 +28,12 @@ class MakeSpies { log.debug('Training spies:', arr) - return Promise.mapSeries(arr, (num) => { + for (const num of arr) { let plural = util.handlePlurality(num, 'spy') log.info(`Making ${num} ${plural}`) - return lacuna.buildings.intelligence.trainSpy([intelId, num]).then((result) => { + await lacuna.buildings.intelligence.trainSpy([intelId, num]).then((result) => { if (util.int(result.not_trained) > 0) { return new Promise((resolve, reject) => { reject(result.reason_not_trained.message) @@ -43,7 +42,7 @@ class MakeSpies { this.spiesTrained += util.int(result.trained) } }) - }) + } } handleColony(colony, buildings) { diff --git a/lib/tasks/push-buildings-up.js b/lib/tasks/push-buildings-up.js index 53e69d5..d125925 100644 --- a/lib/tasks/push-buildings-up.js +++ b/lib/tasks/push-buildings-up.js @@ -1,4 +1,3 @@ -import Promise from 'bluebird' import _ from 'lodash' import lacuna from '../lacuna' @@ -71,15 +70,15 @@ class PushBuildingsUp { this.timelist = new Timelist() } - upgradeBuildings(buildings) { - return Promise.mapSeries(buildings, (b) => { + async upgradeBuildings(buildings) { + for (const b of buildings) { log.info(`Upgrading ${b.name} from level ${b.level} to ${b.level + 1}`) if (this.options.dryRun) { return } - return lacuna.buildings + await lacuna.buildings .generic(b.url) .upgrade([b.id]) .then((result) => { @@ -96,7 +95,7 @@ class PushBuildingsUp { util.handlePromiseError(err) } }) - }) + } } handleColony(colony, buildings) { diff --git a/lib/tasks/push-glyphs.js b/lib/tasks/push-glyphs.js index c6834e1..1f78d85 100644 --- a/lib/tasks/push-glyphs.js +++ b/lib/tasks/push-glyphs.js @@ -1,4 +1,3 @@ -import Promise from 'bluebird' import _ from 'lodash' import lacuna from '../lacuna' diff --git a/lib/tasks/scuttle-ships.js b/lib/tasks/scuttle-ships.js index ba42f4e..57ada14 100644 --- a/lib/tasks/scuttle-ships.js +++ b/lib/tasks/scuttle-ships.js @@ -1,5 +1,4 @@ import _ from 'lodash' -import Promise from 'bluebird' import lacuna from '../lacuna' import log from '../log' diff --git a/lib/tasks/spy-trainer.js b/lib/tasks/spy-trainer.js index c5ee79d..3d34a43 100644 --- a/lib/tasks/spy-trainer.js +++ b/lib/tasks/spy-trainer.js @@ -1,4 +1,3 @@ -import Promise from 'bluebird' import _ from 'lodash' import lacuna from '../lacuna' @@ -38,16 +37,16 @@ class SpyTrainer { } } - trainSpies(intelMin, toTrain) { + async trainSpies(intelMin, toTrain) { log.newline() - return Promise.each(SKILLS, (skill) => { + for (const skill of SKILLS) { let spies = util.array(toTrain[skill]) - return Promise.each(spies, (spy) => { - return this.trainSpy(intelMin, spy, skill) - }) - }) + for (const spy of spies) { + await this.trainSpy(intelMin, spy, skill) + } + } } numTraining(spies, skill) { @@ -132,45 +131,42 @@ class SpyTrainer { } findSpyBuildings(colony, buildings) { - return lacuna.body.buildings(colony.id).then((buildings) => { - let props = { - intelMin: lacuna.body.findBuilding(buildings, 'Intelligence Ministry'), - intel: lacuna.body.findBuilding(buildings, 'Intel Training'), - mayhem: lacuna.body.findBuilding(buildings, 'Mayhem Training'), - politics: lacuna.body.findBuilding(buildings, 'Politics Training'), - theft: lacuna.body.findBuilding(buildings, 'Theft Training'), + return lacuna.body.buildings(colony.id).then(async (buildings) => { + let intelBuildings = { + intelMin: await lacuna.body.findBuilding(buildings, 'Intelligence Ministry'), + intel: await lacuna.body.findBuilding(buildings, 'Intel Training'), + mayhem: await lacuna.body.findBuilding(buildings, 'Mayhem Training'), + politics: await lacuna.body.findBuilding(buildings, 'Politics Training'), + theft: await lacuna.body.findBuilding(buildings, 'Theft Training'), } - return Promise.props(props).then((result) => { - let intelMin = result.intelMin - let skillBuildings = _.pick(result, SKILLS) - - if (!intelMin) { - return new Promise((resolve, reject) => { - reject(`No Intelligence Ministry found on ${colony.name}`) - }) - } + let intelMin = intelBuildings.intelMin + let skillBuildings = _.pick(intelBuildings, SKILLS) - // Warn about missing skill buildings. - _.each(SKILLS, (skill) => { - if (!skillBuildings[skill]) { - log.warn(`No ${MISSION_NAMES[skill]} found on ${colony.name}`) - } + if (!intelMin) { + return new Promise((resolve, reject) => { + reject(`No Intelligence Ministry found on ${colony.name}`) }) + } - return [intelMin, skillBuildings] + // Warn about missing skill buildings. + _.each(SKILLS, (skill) => { + if (!skillBuildings[skill]) { + log.warn(`No ${MISSION_NAMES[skill]} found on ${colony.name}`) + } }) + + return [intelMin, skillBuildings] }) } - handleColony(colony, buildings) { - return this.findSpyBuildings(colony, buildings).spread((intelMin, skillBuildings) => { - let maxSkillLevels = this.getMaxSkillLevels(skillBuildings) + async handleColony(colony, buildings) { + const [intelMin, skillBuildings] = await this.findSpyBuildings(colony, buildings) + let maxSkillLevels = this.getMaxSkillLevels(skillBuildings) - return this.getSpies(intelMin, maxSkillLevels, colony).then((spies) => { - let toTrain = this.distributeSkills(spies, maxSkillLevels) - return this.trainSpies(intelMin, toTrain) - }) + return this.getSpies(intelMin, maxSkillLevels, colony).then((spies) => { + let toTrain = this.distributeSkills(spies, maxSkillLevels) + return this.trainSpies(intelMin, toTrain) }) } diff --git a/lib/tasks/upgrade-buildings.js b/lib/tasks/upgrade-buildings.js index 4d272b8..9efdc43 100644 --- a/lib/tasks/upgrade-buildings.js +++ b/lib/tasks/upgrade-buildings.js @@ -1,5 +1,4 @@ import _ from 'lodash' -import Promise from 'bluebird' import lacuna from '../lacuna' import log from '../log' @@ -435,27 +434,27 @@ class UpgradeBuildings { // - making sure that one section is upgraded at a time // - makue sure we don't try to upgrade a building thats already upgrading // - upgradeBuildings(buildings) { + async upgradeBuildings(buildings) { // This variable is used to ensure that one section is upgraded at a time. let sectionCompleted = true - return Promise.each(TEMPLATE, (section) => { + for (const section of TEMPLATE) { // If the section isn't complete, don't go any further in the template. if (!sectionCompleted) { return } - return Promise.each(section, (build) => { + for (const build of section) { // A build is an upgrade that should be done according to the template. // Here, we go through each one and see if it can be done. - return lacuna.body.findBuildings(buildings, build.name).then((matches) => { + await lacuna.body.findBuildings(buildings, build.name).then(async (matches) => { // A 'match' is a building that matches the current 'build' within // the current 'section'. matches = _.sortBy(matches, (b) => util.int(b.level)) - return Promise.each(matches, (match) => { + for (const match of matches) { if (build.level > match.level) { // A section is considered incomplete when there's still more // that needs upgrading. @@ -466,12 +465,12 @@ class UpgradeBuildings { return } - return this.upgradeBuilding(match) + await this.upgradeBuilding(match) } - }) + } }) - }) - }) + } + } } handleSleep() { diff --git a/lib/tasks/view-laws.js b/lib/tasks/view-laws.js index bfb20d8..ef81140 100644 --- a/lib/tasks/view-laws.js +++ b/lib/tasks/view-laws.js @@ -1,6 +1,5 @@ import _ from 'lodash' import Table from 'cli-table3' -import Promise from 'bluebird' import lacuna from '../lacuna' import log from '../log' diff --git a/package.json b/package.json index 74fc663..d3b8490 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,6 @@ }, "dependencies": { "@tlecommunity/client": "^0.5.0", - "bluebird": "^3.7.2", "browser-request": "^0.3.3", "camelize": "^1.0.0", "cli-table3": "^0.6.3", diff --git a/test/scripts/lacuna-click-limit-test.js b/test/scripts/lacuna-click-limit-test.js index 44688c4..13cd67a 100644 --- a/test/scripts/lacuna-click-limit-test.js +++ b/test/scripts/lacuna-click-limit-test.js @@ -4,8 +4,6 @@ import lacuna from './test-instance' import log from '../../lib/log' -import Promise from 'bluebird' - let num = 0 let getStatus = () => { -- 2.51.2