Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
fix(trade): Undesired embassy construction
Browse files Browse the repository at this point in the history
Embassies would be constructed when the option is disabled. The trade section switch was also not taken into account.

Fixes: #99
  • Loading branch information
oliversalzburg committed Jun 16, 2022
1 parent 85d308d commit b1e22be
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 23 deletions.
30 changes: 16 additions & 14 deletions packages/userscript/source/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,8 @@ export class Engine {
trade(): void {
this._tradeManager.manager.render();

this.buildEmbassies();

// If we can't make any trades, bail out.
if (!this._tradeManager.singleTradePossible()) {
return;
Expand Down Expand Up @@ -1902,29 +1904,19 @@ export class Engine {
}
}

/**
* All automations that didn't have a better place.
* - Building embassies
* - Fixing cryochambers
* - Turn on Steamworks (they're always off initially)
*/
miscOptions(): void {
const craftManager = this._craftManager;
const buildManager = this._buildManager;
const optionVals = this._host.options.auto.options.items;

buildEmbassies() {
// Tries to calculate how many embassies for which races it can buy,
// then it buys them. Code should be straight-forward.
AutoEmbassy: if (
this._host.options.auto.trade.addition.buildEmbassies.enabled &&
!!this._host.gamePage.diplomacy.races[0].embassyPrices
) {
const culture = craftManager.getResource("culture");
const culture = this._craftManager.getResource("culture");
let cultureVal = 0;
const subTrigger = this._host.options.auto.trade.addition.buildEmbassies.subTrigger ?? 0;
if (subTrigger <= culture.value / culture.maxValue) {
const racePanels = this._host.gamePage.diplomacyTab.racePanels;
cultureVal = craftManager.getValueAvailable("culture", true);
cultureVal = this._craftManager.getValueAvailable("culture", true);

const embassyBulk: Partial<
Record<
Expand Down Expand Up @@ -1983,7 +1975,7 @@ export class Engine {
if (emBulk.val === 0) {
continue;
}
cultureVal = craftManager.getValueAvailable("culture", true);
cultureVal = this._craftManager.getValueAvailable("culture", true);
if (cultureVal < emBulk.priceSum) {
this._host.warning("Something has gone horribly wrong.", emBulk.priceSum, cultureVal);
}
Expand All @@ -2002,6 +1994,16 @@ export class Engine {
}
}
}
}

/**
* All automations that didn't have a better place.
* - Fixing cryochambers
* - Turn on Steamworks (they're always off initially)
*/
miscOptions(): void {
const buildManager = this._buildManager;
const optionVals = this._host.options.auto.options.items;

// Fix used cryochambers
// If the option is enabled and we have used cryochambers...
Expand Down
2 changes: 2 additions & 0 deletions packages/userscript/source/options/KittenStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type ToggleLimitedJobItem = `toggle-limited-${Job}`;
type ToggleLimitedRaceItem = `toggle-limited-${Race}`;
type ToggleLimitedResourceItem = `toggle-limited-${Resource}`;
type ToggleOptionsItem = `toggle-${OptionsItem}`;
type ToggleBuildEmbassies = "toggle-buildEmbassies";
type TogglePolicyItem = `toggle-${Policy}`;
type ToggleRaceItem = `toggle-${Race}`;
type ToggleRaceSeasonItem = `toggle-${Race}-${Season}`;
Expand Down Expand Up @@ -59,6 +60,7 @@ export type KittenStorageType = {
Partial<Record<SetSubtriggerReligionItem, number>> &
Partial<Record<SetSubtriggerTimeCtrlItem, number>> &
Partial<Record<ToggleBuildingItem, boolean>> &
Partial<Record<ToggleBuildEmbassies, boolean>> &
Partial<Record<ToggleFaithUnicornItem, boolean>> &
Partial<Record<ToggleFilterItem, boolean>> &
Partial<Record<ToggleJobItem, boolean>> &
Expand Down
5 changes: 5 additions & 0 deletions packages/userscript/source/options/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ export class Options {
subject.items[`toggle-${name}-summer` as const] = item.summer;
subject.items[`toggle-${name}-winter` as const] = item.winter;
}
subject.items["toggle-buildEmbassies" as const] =
optionsObject.auto.trade.addition.buildEmbassies.enabled;
for (const [name, item] of objectEntries(optionsObject.auto.unlock.items)) {
subject.items[`toggle-${name}` as const] = item.enabled;
}
Expand Down Expand Up @@ -379,6 +381,9 @@ export class Options {
item.summer = subject.items[`toggle-${name}-summer` as const] ?? item.summer;
item.winter = subject.items[`toggle-${name}-winter` as const] ?? item.winter;
}
result.auto.trade.addition.buildEmbassies.enabled =
subject.items["toggle-buildEmbassies" as const] ??
result.auto.trade.addition.buildEmbassies.enabled;
for (const [name, item] of objectEntries(result.auto.unlock.items)) {
item.enabled = subject.items[`toggle-${name}` as const] ?? item.enabled;
}
Expand Down
9 changes: 6 additions & 3 deletions packages/userscript/source/options/TradingSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ export type TradingSettingsItem = {

require: Requirement;
};

export type TradeAdditionSettings = {
buildEmbassies: OptionsSettingsItem;
};

export class TradingSettings extends SettingsSection {
trigger = 0.98;
$trigger?: JQuery<HTMLElement>;

addition: {
buildEmbassies: OptionsSettingsItem;
} = { buildEmbassies: { enabled: true, subTrigger: 0.9 } };
addition: TradeAdditionSettings = { buildEmbassies: { enabled: true, subTrigger: 0.9 } };

items: {
[item in Race]: TradingSettingsItem;
Expand Down
21 changes: 15 additions & 6 deletions packages/userscript/source/ui/TradingSettingsUi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { TradingSettings, TradingSettingsItem } from "../options/TradingSettings";
import {
TradeAdditionSettings,
TradingSettings,
TradingSettingsItem,
} from "../options/TradingSettings";
import { objectEntries } from "../tools/Entries";
import { ucfirst } from "../tools/Format";
import { mustExist } from "../tools/Maybe";
Expand Down Expand Up @@ -125,7 +129,7 @@ export class TradingSettingsUi extends SettingsSectionUi<TradingSettings> {

list.append(...optionButtons);

const additionOptions = this.getAdditionOptions();
const additionOptions = this.getAdditionOptions(this._options.addition);

element.panel.append(triggerButton);
element.panel.append(list);
Expand Down Expand Up @@ -232,21 +236,21 @@ export class TradingSettingsUi extends SettingsSectionUi<TradingSettings> {
return element;
}

getAdditionOptions(): Array<JQuery<HTMLElement>> {
getAdditionOptions(addition: TradeAdditionSettings): Array<JQuery<HTMLElement>> {
const nodeHeader = this._getHeader("Additional options");

const nodeEmbassies = this._getOption(
"embassies",
this._options.addition.buildEmbassies,
addition.buildEmbassies,
this._host.i18n("option.embassies"),
false,
{
onCheck: () => {
this._options.addition.buildEmbassies.enabled = true;
this._host.updateOptions(() => (this._options.addition.buildEmbassies.enabled = true));
this._host.imessage("status.sub.enable", [this._host.i18n("option.embassies")]);
},
onUnCheck: () => {
this._options.addition.buildEmbassies.enabled = false;
this._host.updateOptions(() => (this._options.addition.buildEmbassies.enabled = false));
this._host.imessage("status.sub.disable", [this._host.i18n("option.embassies")]);
},
}
Expand All @@ -267,6 +271,7 @@ export class TradingSettingsUi extends SettingsSectionUi<TradingSettings> {
setState(state: TradingSettings): void {
this._options.enabled = state.enabled;
this._options.trigger = state.trigger;
this._options.addition.buildEmbassies.enabled = state.addition.buildEmbassies.enabled;

for (const [name, option] of objectEntries(this._options.items)) {
option.enabled = state.items[name].enabled;
Expand All @@ -282,6 +287,10 @@ export class TradingSettingsUi extends SettingsSectionUi<TradingSettings> {
refreshUi(): void {
mustExist(this._options.$enabled).prop("checked", this._options.enabled);
mustExist(this._options.$trigger)[0].title = this._options.trigger.toFixed(2);
mustExist(this._options.addition.buildEmbassies.$enabled).prop(
"checked",
this._options.addition.buildEmbassies.enabled
);

for (const [name, option] of objectEntries(this._options.items)) {
mustExist(option.$enabled).prop("checked", this._options.items[name].enabled);
Expand Down
3 changes: 3 additions & 0 deletions packages/userscript/source/ui/UserInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ export class UserInterface {
}

private _installCss(): void {
// This development panel overlays the UI in the Sleek theme.
this._addRule("#devPanel { display: none !important; }");

// Basic layout for our own list-based options menus.
this._addRule("#ks-options ul { list-style: none; margin: 0 0 5px; padding: 0; }");
this._addRule('#ks-options ul:after { clear: both; content: " "; display: block; height: 0; }');
Expand Down

0 comments on commit b1e22be

Please sign in to comment.