Skip to content

Commit

Permalink
Merge pull request #54 from HugovandeVelde/Fixes
Browse files Browse the repository at this point in the history
added captcha checks to prevent working through captchas, added auto …
  • Loading branch information
Sau1707 authored May 28, 2024
2 parents 8b8c643 + 1ee7068 commit 2b8d930
Show file tree
Hide file tree
Showing 7 changed files with 800 additions and 659 deletions.
57 changes: 39 additions & 18 deletions src/autoBuild.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// var r = Math.round(e.building.points * Math.pow(e.building.points_factor, e.next_level)) - Math.round(e.building.points * Math.pow(e.building.points_factor, e.level))

class AutoBuild extends ModernUtil {
constructor(c, s) {
super(c, s);
Expand All @@ -11,10 +9,34 @@ class AutoBuild extends ModernUtil {
this.shiftHeld = false;

/* Active always, check if the towns are in the active list */
this.enable = setInterval(this.main, 20000);
this.interval = setInterval(this.main.bind(this), 20000);

/* Add listener that change the Senate look */
uw.$.Observer(GameEvents.window.open).subscribe("modernSenate", this.updateSenate);

this.simulateCaptcha = false;
this.captchaActive = false;

/* Check for captcha conditions every 300ms */
this.checkCaptchaInterval = setInterval(() => {
if (this.simulateCaptcha || $('.botcheck').length || $('#recaptcha_window').length) {
if (!this.captchaActive) {
this.console.log('Captcha active, autobuild stopped working');
clearInterval(this.interval);
this.captchaActive = true;
}
} else {
if (this.captchaActive) {
this.console.log('Captcha resolved, autobuild resumed');
this.startInterval(); // Restart autobuild
this.captchaActive = false;
}
}
}, 300);
}

startInterval() {
this.interval = setInterval(this.main.bind(this), 20000);
}

settings = () => {
Expand Down Expand Up @@ -43,7 +65,7 @@ class AutoBuild extends ModernUtil {
<div class="game_border_corner corner2"></div>
<div class="game_border_corner corner3"></div>
<div class="game_border_corner corner4"></div>
<div id="auto_build_title" style="cursor: pointer; filter: ${this.enable ? 'brightness(100%) saturate(186%) hue-rotate(241deg)' : ''}" class="game_header bold" onclick="window.modernBot.autoBuild.toggle()"> Auto Build <span class="command_count"></span>
<div id="auto_build_title" style="cursor: pointer; filter: ${this.interval ? 'brightness(100%) saturate(186%) hue-rotate(241deg)' : ''}" class="game_header bold" onclick="window.modernBot.autoBuild.toggle()"> Auto Build <span class="command_count"></span>
<div style="position: absolute; right: 10px; top: 4px; font-size: 10px;"> (click to toggle) </div>
</div>
<div id="buildings_lvl_buttons"></div>
Expand All @@ -56,14 +78,13 @@ class AutoBuild extends ModernUtil {
if (handler.context !== "building_senate") return;

// Edit the width of the window to fit the new element
handler.wnd.setWidth(850)
handler.wnd.setWidth(850);

// Compute the id of the window
const id = `gpwnd_${handler.wnd.getID()}`
const id = `gpwnd_${handler.wnd.getID()}`;

// Loop until the element is found
const updateView = () => {

const interval = setInterval(() => {
const $window = $('#' + id);

Expand Down Expand Up @@ -100,22 +121,21 @@ class AutoBuild extends ModernUtil {
setTimeout(() => {
clearInterval(interval);
}, 100);
}
};

// subscribe to set content event
const oldSetContent = handler.wnd.setContent2;
handler.wnd.setContent2 = (...params) => {
updateView();
oldSetContent(...params);
}

}
};
};

/* Given the town id, set the polis in the settings menu */
setPolisInSettings = town_id => {
let town = uw.ITowns.towns[town_id];

/* If the town is in the active list set*/
/* If the town is in the active list set */
let town_buildings = this.towns_buildings?.[town_id] ?? { ...town.buildings()?.attributes } ?? {};
let buildings = { ...town.buildings().attributes };

Expand All @@ -134,7 +154,7 @@ class AutoBuild extends ModernUtil {
</div>`;
};

/* If the town is in a group, the the groups */
/* If the town is in a group, the groups */
const groups =
`(${Object.values(uw.ITowns.getTownGroups())
.filter(group => group.id > 0 && group.id !== -1 && group.towns[town_id])
Expand Down Expand Up @@ -273,20 +293,21 @@ class AutoBuild extends ModernUtil {
town_id: town_id,
};
uw.gpAjax.ajaxPost('frontend_bridge', 'execute', data);
this.console.log(`${town.getName()}: buildUp ${type}`);
await this.sleep(500);
this.console.log(`${town.getName()}: Build Up ${type}`);
await this.sleep(1234);
};

/* Make post request to tear building down */
postTearDown = async (type, town_id) => {
postTearDown = async (type, town_id, town) => {
let data = {
model_url: 'BuildingOrder',
action_name: 'tearDown',
arguments: { building_id: type },
town_id: town_id,
};
uw.gpAjax.ajaxPost('frontend_bridge', 'execute', data);
await this.sleep(500);
this.console.log(`${town.getName()}: Build Down ${type}`);
await this.sleep(1234);
};

/* return true if the quee is full */
Expand Down Expand Up @@ -358,7 +379,7 @@ class AutoBuild extends ModernUtil {
return false;
}
if (target[build] < buildings[build]) {
await this.postTearDown(build, town_id);
await this.postTearDown(build, town_id, town);
return true;
}
return false;
Expand Down
30 changes: 18 additions & 12 deletions src/autoGratis.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class AutoGratis extends ModernUtil {
<div class="left"></div>
<div class="right"></div>
<div class="caption js-caption">Gratis<div class="effect js-effect"></div></div>
</div> button (try every 4 seconds)
</div> button (try every 2.5 seconds)
</div>
</div>
`;
Expand All @@ -38,7 +38,7 @@ class AutoGratis extends ModernUtil {
'filter',
'brightness(100%) saturate(186%) hue-rotate(241deg)',
);
this.autogratis = setInterval(this.main, 4000);
this.autogratis = setInterval(this.main, 2500);
} else {
uw.$('#auto_gratis_title').css('filter', '');
clearInterval(this.autogratis);
Expand All @@ -49,14 +49,15 @@ class AutoGratis extends ModernUtil {

/* Main loop for the autogratis bot */
main = () => {
const el = uw.$('.type_building_queue.type_free').not('#dummy_free');
if (el.length) el.click();

const town = uw.ITowns.getCurrentTown();
for (let model of town.buildingOrders().models) {
if (model.attributes.building_time < 300) {
this.callGratis(town.id, model.id)
return;
const el = uw.$('.type_building_queue.type_free').not('.disabled').not('#dummy_free');
if (el.length) {
el.click();
const town = uw.ITowns.getCurrentTown();
for (let model of town.buildingOrders().models) {
if (model.attributes.building_time < 300) {
this.callGratis(town.id, model.id);
return;
}
}
}
};
Expand All @@ -70,7 +71,12 @@ class AutoGratis extends ModernUtil {
"order_id": order_id
},
"town_id": town_id
}
};

// Add console log
this.console.log(`${uw.ITowns.towns[town_id].getName()}: calling gratis for order ${order_id}`);

uw.gpAjax.ajaxPost('frontend_bridge', 'execute', data);
}
};

}
4 changes: 2 additions & 2 deletions src/autoHide.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class AutoHide extends ModernUtil {
<div style="position: absolute; right: 10px; top: 4px; font-size: 10px;"> (click to toggle) </div>
</div>
<div style="padding: 5px; font-weight: 600">
Check every 5 seconds, if there is more then 5000 iron store it in the hide
Check every 5 seconds, if there is more then 15000 iron store it in the hide
</div>
</div>
`;
Expand Down Expand Up @@ -96,7 +96,7 @@ class AutoHide extends ModernUtil {
if (this.activePolis == 0) return;
const town = uw.ITowns.towns[this.activePolis];
const { iron } = town.resources()
if (iron > 5000) {
if (iron > 15000) {
this.storeIron(this.activePolis, iron)
}
}
Expand Down
Loading

0 comments on commit 2b8d930

Please sign in to comment.