let extensionPort; let sleep = ms => new Promise(resolve => setTimeout(resolve, ms + (Math.random() * 2.5 * 1000))); function townSwitch(townId) { let TownSwitch = require('helpers/town_switch'); (new TownSwitch()).townSwitch(townId); } function toIslandView() { $.Observer(GameEvents.ui.bull_eye.radiobutton.island_view.click).publish(); } function toCityOverview() { $.Observer(GameEvents.ui.bull_eye.radiobutton.city_overview.click).publish(); } let getCurrentTown = () => MM.getCollections().Town[0].getCurrentTown(); let getAllTownIds = () => MM.getCollections().Town[0].models.map(m => m.id); let getPlayerGods = () => MM.getModels().PlayerGods[Game.player_id]; //example: getTownResource.call(getCurrentTown(), 'stone') //NOTE: Unused function function getTownResource(resource) { return this.resources.getResource(resource); } let getFarmTownPlayerRelation = farmTownId => MM.getCollections().FarmTownPlayerRelation[0].getRelationForFarmTown(farmTownId); let getCurrentTownFarmTownIds = () => MM.getCollections().FarmTown[0].getAllForIslandViaXY(getCurrentTown().getIslandX(), getCurrentTown().getIslandY()).map(f => f.id); function claimResources(farmTownId, option, success, error) { let farmTownPlayerRelation = getFarmTownPlayerRelation(farmTownId); farmTownPlayerRelation.claim("resources", option, { success: success.bind(this), error: error.bind(this) }); } function willWasteResources(farmTownId, option) { let farmTownPlayerRelation = getFarmTownPlayerRelation(farmTownId); let claimResourceValues = farmTownPlayerRelation.getClaimResourceValues(); let claimResourceValue = claimResourceValues[option - 1]; let WastedResourcesHelper = require('WastedResourcesHelper') wr_helper = new WastedResourcesHelper(getCurrentTown(), getPlayerGods()); return wr_helper.hasWastedResources({ wood : claimResourceValue, stone : claimResourceValue, iron : claimResourceValue }); } function canClaim(farmTownId) { let farmTownPlayerRelation = getFarmTownPlayerRelation(farmTownId); return farmTownPlayerRelation.isLootable(); } async function claimAll(option) { let originalId = Game.townId; toIslandView(); for (townId of getAllTownIds()) { await sleep(1000); townSwitch(townId); for (farmTownId of getCurrentTownFarmTownIds()) { await sleep(1000); if (!canClaim(farmTownId)) { continue; } //TODO: Fix this if (willWasteResources(farmTownId, option)) { //TODO: Notification? continue; } claimResources( farmTownId, option, () => { }, () => console.log(`Couldn't claim farm town ${farmTownId} for town ${townId} !`) ); } } await sleep(1000); townSwitch(originalId); toCityOverview(); extensionPort.postMessage({ action: 'endClaimAll', args: [] }); } extensionPort = chrome.runtime.connect(extensionId); extensionPort.onMessage.addListener(message => { if (message.action == 'claimAll') { claimAll(message.args[0]); } });