grepolis-bot/injection.js

119 lines
3.3 KiB
JavaScript

let extensionPort;
function sleep(ms) {
return 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();
}
function getCurrentTown() {
return MM.getCollections().Town[0].getCurrentTown();
}
function getAllTownIds() {
return MM.getCollections().Town[0].models.map(m => m.id);
}
function getPlayerGods() {
return MM.getModels().PlayerGods[Game.player_id];
}
//example: getTownResource.call(getCurrentTown(), 'stone')
//NOTE: Unused function
function getTownResource(resource) {
return this.resources.getResource(resource);
}
function getFarmTownPlayerRelation(farmTownId) {
return MM.getCollections().FarmTownPlayerRelation[0].getRelationForFarmTown(farmTownId);
}
function getCurrentTownFarmTownIds() {
return 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,
function() { },
function() { 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(function(message) {
if(message.action == 'claimAll') {
claimAll(message.args[0]);
}
});