spaghetti's fixes
This commit is contained in:
parent
6aaf98a693
commit
10270d0af9
|
@ -8,23 +8,21 @@ let lastClaimTime = Date.now();
|
||||||
|
|
||||||
let injectionPort;
|
let injectionPort;
|
||||||
|
|
||||||
chrome.runtime.onConnectExternal.addListener(function(port) {
|
chrome.runtime.onConnectExternal.addListener(port => {
|
||||||
injectionPort = port;
|
injectionPort = port;
|
||||||
|
|
||||||
injectionPort.onMessage.addListener(function(message) {
|
injectionPort.onMessage.addListener(message => {
|
||||||
if(message.action == 'endClaimAll') {
|
if (message.action == 'endClaimAll') {
|
||||||
onEndClaimAll();
|
onEndClaimAll();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
function getClaimOptionLength(option) {
|
let getClaimOptionLength = option => [0, 10, 40, 3 * 60, 8 * 60][option] * 60 * 1000;
|
||||||
return [0, 10, 40, 3 * 60, 8 * 60][option] * 60 * 1000;
|
|
||||||
}
|
|
||||||
|
|
||||||
function updateBrowserActionStatus() {
|
function updateBrowserActionStatus() {
|
||||||
chrome.tabs.query({ url: '*://*.grepolis.com/game/index*' }, function(tabs) {
|
chrome.tabs.query({ url: '*://*.grepolis.com/game/index*' }, tabs => {
|
||||||
if(tabs.length == 0) {
|
if (tabs.length == 0) {
|
||||||
chrome.browserAction.disable();
|
chrome.browserAction.disable();
|
||||||
|
|
||||||
onEndClaimAll();
|
onEndClaimAll();
|
||||||
|
@ -39,9 +37,9 @@ chrome.tabs.onCreated.addListener(updateBrowserActionStatus);
|
||||||
chrome.tabs.onUpdated.addListener(updateBrowserActionStatus);
|
chrome.tabs.onUpdated.addListener(updateBrowserActionStatus);
|
||||||
chrome.tabs.onRemoved.addListener(updateBrowserActionStatus);
|
chrome.tabs.onRemoved.addListener(updateBrowserActionStatus);
|
||||||
|
|
||||||
chrome.browserAction.onClicked.addListener(function() {
|
chrome.browserAction.onClicked.addListener(() => {
|
||||||
chrome.tabs.query({ url: '*://*.grepolis.com/game/index*' }, function(tabs) {
|
chrome.tabs.query({ url: '*://*.grepolis.com/game/index*' }, tabs => {
|
||||||
if(tabs.length == 0) {
|
if (tabs.length == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,7 +48,7 @@ chrome.browserAction.onClicked.addListener(function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
function claimAll(option) {
|
function claimAll(option) {
|
||||||
if(claiming) {
|
if (claiming) {
|
||||||
console.log('Error: already claiming!');
|
console.log('Error: already claiming!');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -67,8 +65,11 @@ function onEndClaimAll() {
|
||||||
claiming = false;
|
claiming = false;
|
||||||
|
|
||||||
lastClaimTime = Date.now();
|
lastClaimTime = Date.now();
|
||||||
if(state == 'automatic') {
|
if (state == 'automatic') {
|
||||||
autoClaimTimeout = setTimeout(function() { claimAll(autoClaimOption); }, getClaimOptionLength(autoClaimOption));
|
autoClaimTimeout = setTimeout(
|
||||||
|
() => claimAll(autoClaimOption),
|
||||||
|
getClaimOptionLength(autoClaimOption)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
updateBadgeText();
|
updateBadgeText();
|
||||||
|
@ -78,19 +79,19 @@ function updateBadgeText() {
|
||||||
let timeSinceClaim = Date.now() - lastClaimTime;
|
let timeSinceClaim = Date.now() - lastClaimTime;
|
||||||
let lastClaimLength = getClaimOptionLength(lastClaimOption);
|
let lastClaimLength = getClaimOptionLength(lastClaimOption);
|
||||||
|
|
||||||
if(state == 'automatic' && claiming) {
|
if (state == 'automatic' && claiming) {
|
||||||
chrome.browserAction.setBadgeText({ text: '…' });
|
chrome.browserAction.setBadgeText({ text: '…' });
|
||||||
} else if(state == 'manual' && timeSinceClaim >= lastClaimLength) {
|
} else if (state == 'manual' && timeSinceClaim >= lastClaimLength) {
|
||||||
chrome.browserAction.setBadgeText({ text: '(∞)' });
|
chrome.browserAction.setBadgeText({ text: '(∞)' });
|
||||||
} else {
|
} else {
|
||||||
let timeLeft = lastClaimLength - timeSinceClaim;
|
let timeLeft = lastClaimLength - timeSinceClaim;
|
||||||
|
|
||||||
let timeText;
|
let timeText;
|
||||||
if(timeLeft <= 0) {
|
if (timeLeft <= 0) {
|
||||||
timeText = '0s';
|
timeText = '0s';
|
||||||
} else if(timeLeft < 60 * 1000) {
|
} else if (timeLeft < 60 * 1000) {
|
||||||
timeText = Math.trunc(timeLeft / 1000) + 's';
|
timeText = Math.trunc(timeLeft / 1000) + 's';
|
||||||
} else if(timeLeft < 60 * 60 * 1000) {
|
} else if (timeLeft < 60 * 60 * 1000) {
|
||||||
timeText = Math.trunc(timeLeft / 60 / 1000) + 'm';
|
timeText = Math.trunc(timeLeft / 60 / 1000) + 'm';
|
||||||
} else {
|
} else {
|
||||||
timeText = Math.trunc(timeLeft / 60 / 60 / 1000) + 'h' + Math.trunc((timeLeft % (60 * 60 * 1000)) / 60 / 1000) + 'm';
|
timeText = Math.trunc(timeLeft / 60 / 60 / 1000) + 'h' + Math.trunc((timeLeft % (60 * 60 * 1000)) / 60 / 1000) + 'm';
|
||||||
|
@ -105,22 +106,20 @@ function claimAllWhenReady(option) {
|
||||||
let lastClaimLength = getClaimOptionLength(lastClaimOption);
|
let lastClaimLength = getClaimOptionLength(lastClaimOption);
|
||||||
let timeLeft = lastClaimLength - timeSinceClaim;
|
let timeLeft = lastClaimLength - timeSinceClaim;
|
||||||
|
|
||||||
setTimeout(function() { claimAll(option); }, timeLeft);
|
setTimeout(() => claimAll(option), timeLeft);
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateState(newState) {
|
function updateState(newState) {
|
||||||
state = newState;
|
state = newState;
|
||||||
|
|
||||||
if(newState == 'automatic') {
|
if (newState == 'automatic') {
|
||||||
let timeSinceClaim = Date.now() - lastClaimTime;
|
let timeSinceClaim = Date.now() - lastClaimTime;
|
||||||
let lastClaimLength = getClaimOptionLength(lastClaimOption);
|
let lastClaimLength = getClaimOptionLength(lastClaimOption);
|
||||||
let timeLeft = lastClaimLength - timeSinceClaim;
|
let timeLeft = lastClaimLength - timeSinceClaim;
|
||||||
setTimeout(function() {
|
setTimeout(() => claimAll(autoClaimOption), timeLeft);
|
||||||
claimAll(autoClaimOption);
|
|
||||||
}, timeLeft);
|
|
||||||
|
|
||||||
chrome.browserAction.setBadgeBackgroundColor({ color: 'green' });
|
chrome.browserAction.setBadgeBackgroundColor({ color: 'green' });
|
||||||
} else if(newState == 'manual') {
|
} else if (newState == 'manual') {
|
||||||
clearTimeout(autoClaimTimeout);
|
clearTimeout(autoClaimTimeout);
|
||||||
|
|
||||||
chrome.browserAction.setBadgeBackgroundColor({ color: 'red' });
|
chrome.browserAction.setBadgeBackgroundColor({ color: 'red' });
|
||||||
|
@ -135,7 +134,7 @@ chrome.contextMenus.create({
|
||||||
type: 'radio',
|
type: 'radio',
|
||||||
title: 'Automatic',
|
title: 'Automatic',
|
||||||
contexts: ['browser_action'],
|
contexts: ['browser_action'],
|
||||||
onclick: function() { updateState('automatic'); }
|
onclick: () => updateState('automatic')
|
||||||
});
|
});
|
||||||
|
|
||||||
chrome.contextMenus.create({
|
chrome.contextMenus.create({
|
||||||
|
@ -143,35 +142,35 @@ chrome.contextMenus.create({
|
||||||
type: 'radio',
|
type: 'radio',
|
||||||
title: 'Manual',
|
title: 'Manual',
|
||||||
contexts: ['browser_action'],
|
contexts: ['browser_action'],
|
||||||
onclick: function() { updateState('manual'); }
|
onclick: () => updateState('manual')
|
||||||
});
|
});
|
||||||
|
|
||||||
chrome.contextMenus.create({
|
chrome.contextMenus.create({
|
||||||
type: 'normal',
|
type: 'normal',
|
||||||
title: 'Claim for 10m',
|
title: 'Claim for 10m',
|
||||||
contexts: ['browser_action'],
|
contexts: ['browser_action'],
|
||||||
onclick: function() { updateState('manual'); claimAllWhenReady(1); }
|
onclick: () => { updateState('manual'); claimAllWhenReady(1); }
|
||||||
});
|
});
|
||||||
|
|
||||||
chrome.contextMenus.create({
|
chrome.contextMenus.create({
|
||||||
type: 'normal',
|
type: 'normal',
|
||||||
title: 'Claim for 40m',
|
title: 'Claim for 40m',
|
||||||
contexts: ['browser_action'],
|
contexts: ['browser_action'],
|
||||||
onclick: function() { updateState('manual'); claimAllWhenReady(2); }
|
onclick: () => { updateState('manual'); claimAllWhenReady(2); }
|
||||||
});
|
});
|
||||||
|
|
||||||
chrome.contextMenus.create({
|
chrome.contextMenus.create({
|
||||||
type: 'normal',
|
type: 'normal',
|
||||||
title: 'Claim for 3h',
|
title: 'Claim for 3h',
|
||||||
contexts: ['browser_action'],
|
contexts: ['browser_action'],
|
||||||
onclick: function() { updateState('manual'); claimAllWhenReady(3); }
|
onclick: () => { updateState('manual'); claimAllWhenReady(3); }
|
||||||
});
|
});
|
||||||
|
|
||||||
chrome.contextMenus.create({
|
chrome.contextMenus.create({
|
||||||
type: 'normal',
|
type: 'normal',
|
||||||
title: 'Claim for 8h',
|
title: 'Claim for 8h',
|
||||||
contexts: ['browser_action'],
|
contexts: ['browser_action'],
|
||||||
onclick: function() { updateState('manual'); claimAllWhenReady(4); }
|
onclick: () => { updateState('manual'); claimAllWhenReady(4); }
|
||||||
});
|
});
|
||||||
|
|
||||||
updateBrowserActionStatus();
|
updateBrowserActionStatus();
|
||||||
|
|
42
injection.js
42
injection.js
|
@ -1,8 +1,6 @@
|
||||||
let extensionPort;
|
let extensionPort;
|
||||||
|
|
||||||
function sleep(ms) {
|
let sleep = ms => new Promise(resolve => setTimeout(resolve, ms + (Math.random() * 2.5 * 1000)));
|
||||||
return new Promise(resolve => setTimeout(resolve, ms + (Math.random() * 2.5 * 1000)));
|
|
||||||
}
|
|
||||||
|
|
||||||
function townSwitch(townId) {
|
function townSwitch(townId) {
|
||||||
let TownSwitch = require('helpers/town_switch');
|
let TownSwitch = require('helpers/town_switch');
|
||||||
|
@ -18,17 +16,11 @@ function toCityOverview() {
|
||||||
$.Observer(GameEvents.ui.bull_eye.radiobutton.city_overview.click).publish();
|
$.Observer(GameEvents.ui.bull_eye.radiobutton.city_overview.click).publish();
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCurrentTown() {
|
let getCurrentTown = () => MM.getCollections().Town[0].getCurrentTown();
|
||||||
return MM.getCollections().Town[0].getCurrentTown();
|
|
||||||
}
|
|
||||||
|
|
||||||
function getAllTownIds() {
|
let getAllTownIds = () => MM.getCollections().Town[0].models.map(m => m.id);
|
||||||
return MM.getCollections().Town[0].models.map(m => m.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getPlayerGods() {
|
let getPlayerGods = () => MM.getModels().PlayerGods[Game.player_id];
|
||||||
return MM.getModels().PlayerGods[Game.player_id];
|
|
||||||
}
|
|
||||||
|
|
||||||
//example: getTownResource.call(getCurrentTown(), 'stone')
|
//example: getTownResource.call(getCurrentTown(), 'stone')
|
||||||
//NOTE: Unused function
|
//NOTE: Unused function
|
||||||
|
@ -36,13 +28,9 @@ function getTownResource(resource) {
|
||||||
return this.resources.getResource(resource);
|
return this.resources.getResource(resource);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFarmTownPlayerRelation(farmTownId) {
|
let getFarmTownPlayerRelation = farmTownId => MM.getCollections().FarmTownPlayerRelation[0].getRelationForFarmTown(farmTownId);
|
||||||
return MM.getCollections().FarmTownPlayerRelation[0].getRelationForFarmTown(farmTownId);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getCurrentTownFarmTownIds() {
|
let getCurrentTownFarmTownIds = () => MM.getCollections().FarmTown[0].getAllForIslandViaXY(getCurrentTown().getIslandX(), getCurrentTown().getIslandY()).map(f => f.id);
|
||||||
return MM.getCollections().FarmTown[0].getAllForIslandViaXY(getCurrentTown().getIslandX(), getCurrentTown().getIslandY()).map(f => f.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
function claimResources(farmTownId, option, success, error) {
|
function claimResources(farmTownId, option, success, error) {
|
||||||
let farmTownPlayerRelation = getFarmTownPlayerRelation(farmTownId);
|
let farmTownPlayerRelation = getFarmTownPlayerRelation(farmTownId);
|
||||||
|
@ -75,19 +63,19 @@ async function claimAll(option) {
|
||||||
let originalId = Game.townId;
|
let originalId = Game.townId;
|
||||||
toIslandView();
|
toIslandView();
|
||||||
|
|
||||||
for(townId of getAllTownIds()) {
|
for (townId of getAllTownIds()) {
|
||||||
await sleep(1000);
|
await sleep(1000);
|
||||||
|
|
||||||
townSwitch(townId);
|
townSwitch(townId);
|
||||||
|
|
||||||
for(farmTownId of getCurrentTownFarmTownIds()) {
|
for (farmTownId of getCurrentTownFarmTownIds()) {
|
||||||
await sleep(1000);
|
await sleep(1000);
|
||||||
|
|
||||||
if(!canClaim(farmTownId)) {
|
if (!canClaim(farmTownId)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
//TODO: Fix this
|
//TODO: Fix this
|
||||||
if(willWasteResources(farmTownId, option)) {
|
if (willWasteResources(farmTownId, option)) {
|
||||||
//TODO: Notification?
|
//TODO: Notification?
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -95,8 +83,8 @@ async function claimAll(option) {
|
||||||
claimResources(
|
claimResources(
|
||||||
farmTownId,
|
farmTownId,
|
||||||
option,
|
option,
|
||||||
function() { },
|
() => { },
|
||||||
function() { console.log('Couldn\'t claim farm town ' + farmTownId + ' for town ' + townId + '!') }
|
() => console.log(`Couldn't claim farm town ${farmTownId} for town ${townId} !`)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -105,13 +93,13 @@ async function claimAll(option) {
|
||||||
|
|
||||||
townSwitch(originalId);
|
townSwitch(originalId);
|
||||||
toCityOverview();
|
toCityOverview();
|
||||||
|
|
||||||
extensionPort.postMessage({ action: 'endClaimAll', args: [] });
|
extensionPort.postMessage({ action: 'endClaimAll', args: [] });
|
||||||
}
|
}
|
||||||
|
|
||||||
extensionPort = chrome.runtime.connect(extensionId);
|
extensionPort = chrome.runtime.connect(extensionId);
|
||||||
extensionPort.onMessage.addListener(function(message) {
|
extensionPort.onMessage.addListener(message => {
|
||||||
if(message.action == 'claimAll') {
|
if (message.action == 'claimAll') {
|
||||||
claimAll(message.args[0]);
|
claimAll(message.args[0]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue