Skip to content

Commit

Permalink
fix(trade): Embassy limit not respected
Browse files Browse the repository at this point in the history
KS would build more embassies than the configured limit. This should now be fixed.

Fixes #34
  • Loading branch information
oliversalzburg committed Dec 17, 2022
1 parent a9a0cb4 commit 799a007
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions packages/userscript/source/TradeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,16 +345,19 @@ export class TradeManager implements Automation {
for (let raceIndex = 0; raceIndex < bulkTracker.length; raceIndex++) {
const name = bulkTracker[raceIndex];
const emBulk = mustExist(embassyBulk[name]);

if (emBulk.max <= emBulk.currentEm + emBulk.val) {
bulkTracker.splice(raceIndex, 1);
--raceIndex;
continue;
}

const nextPrice = emBulk.basePrice * Math.pow(1.15, emBulk.currentEm + emBulk.val);
if (nextPrice <= cultureVal) {
cultureVal -= nextPrice;
emBulk.priceSum += nextPrice;
emBulk.val += 1;
refreshRequired = true;

if (emBulk.max <= emBulk.val) {
continue;
}
} else {
bulkTracker.splice(raceIndex, 1);
--raceIndex;
Expand All @@ -370,8 +373,12 @@ export class TradeManager implements Automation {
if (cultureVal < emBulk.priceSum) {
cwarn("Something has gone horribly wrong.", emBulk.priceSum, cultureVal);
}
this._host.gamePage.resPool.resources[13].value -= emBulk.priceSum;
// We don't want to invoke the embassy build action multiple times, as
// that would cause lots of log messages.
// Instead, we replicate the behavior of the game here and purchase in bulk.
this._workshopManager.getResource("culture").value -= emBulk.priceSum;
emBulk.race.embassyLevel += emBulk.val;

this._host.engine.storeForSummary("embassy", emBulk.val);
if (emBulk.val !== 1) {
this._host.engine.iactivity("build.embassies", [emBulk.val, emBulk.race.title], "ks-build");
Expand Down

0 comments on commit 799a007

Please sign in to comment.