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

Commit

Permalink
feat(ui): Reflect the amount of stocked resources
Browse files Browse the repository at this point in the history
The UI should reflect if a configured "stock" amount of resources has been reached or not.

This feature existed in v1.5 and has been reimplemented from scratch.

Fixes #121
  • Loading branch information
oliversalzburg committed Sep 22, 2022
1 parent 810df8f commit 1fd0caa
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
38 changes: 38 additions & 0 deletions packages/userscript/source/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ReligionManager } from "./ReligionManager";
import { ScienceManager } from "./ScienceManager";
import { SpaceManager } from "./SpaceManager";
import { TimeManager } from "./TimeManager";
import { objectEntries } from "./tools/Entries";
import { mustExist } from "./tools/Maybe";
import { TradeManager } from "./TradeManager";
import { ResourceCraftable } from "./types";
Expand Down Expand Up @@ -173,14 +174,19 @@ export class Engine {
this.promote();
}
}

// Time automations (Tempus Fugit & Shatter TC)
if (this._host.options.auto.timeCtrl.enabled) {
this.timeCtrl();
}

// Miscelaneous automations.
if (subOptions.enabled) {
this.miscOptions();
}

this.refreshStock();

// Reset automation.
if (
this._host.options.auto.timeCtrl.enabled &&
Expand Down Expand Up @@ -400,4 +406,36 @@ export class Engine {
}
}
}

/**
* Maintains the CSS classes in the resource indicators in the game UI to
* reflect if the amount of resource in stock is below or above the desired
* total amount to keep in stock.
* The user can configure this in the Workshop automation section.
*/
refreshStock() {
for (const [name, resource] of objectEntries(this._host.options.auto.craft.resources)) {
if (resource.stock === 0) {
continue;
}

const isBelow = this._host.gamePage.resPool.get(name).value < resource.stock;

const resourceCells = [
// Resource table on the top.
...$(`#game .res-row.resource_${name} .res-cell.resAmount`),
// Craft table on the bottom.
...$(`#game .res-row.resource_${name} .res-cell.resource-value`),
];

if (!resourceCells) {
continue;
}

for (const resourceCell of resourceCells) {
resourceCell.classList.add(isBelow ? "ks-stock-below" : "ks-stock-above");
resourceCell.classList.remove(isBelow ? "ks-stock-above" : "ks-stock-below");
}
}
}
}
3 changes: 3 additions & 0 deletions packages/userscript/source/ui/UserInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ export class UserInterface {

// Ensure the right column gets a scrollbar, when our content extends it too far down.
this._addRule("body #gamePageContainer #game #rightColumn { overflow-y: auto }");

this._addRule("#game .res-row .res-cell.ks-stock-above { color: green; }");
this._addRule("#game .res-row .res-cell.ks-stock-below { color: red; }");
}

private _addRule(rule: string) {
Expand Down

0 comments on commit 1fd0caa

Please sign in to comment.