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

Commit

Permalink
refactor(core): Simplify logging code path
Browse files Browse the repository at this point in the history
  • Loading branch information
oliversalzburg committed Oct 6, 2022
1 parent 7a8909d commit 539a980
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 83 deletions.
5 changes: 2 additions & 3 deletions packages/userscript/source/BonfireManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BulkPurchaseHelper } from "./helper/BulkPurchaseHelper";
import { BonfireItem, BonfireSettings, BonfireSettingsItem } from "./options/BonfireSettings";
import { TabManager } from "./TabManager";
import { objectEntries } from "./tools/Entries";
import { cwarn } from "./tools/Log";
import { isNil, mustExist } from "./tools/Maybe";
import { BuildButton, Building, BuildingExt, BuildingMeta, GameTab } from "./types";
import { UserScript } from "./UserScript";
Expand Down Expand Up @@ -266,9 +267,7 @@ export class BonfireManager implements Automation {
const label = this._getBuildLabel(build.meta, stage);
amount = this._bulkManager.construct(button.model, button, amount);
if (amount !== amountTemp) {
this._host.engine.warning(
`${label} Amount ordered: ${amountTemp} Amount Constructed: ${amount}`
);
cwarn(`${label} Amount ordered: ${amountTemp} Amount Constructed: ${amount}`);
}
this._host.engine.storeForSummary(label, amount, "build");

Expand Down
101 changes: 38 additions & 63 deletions packages/userscript/source/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { ScienceManager } from "./ScienceManager";
import { SpaceManager } from "./SpaceManager";
import { TimeControlManager } from "./TimeControlManager";
import { TimeManager } from "./TimeManager";
import { cdebug, clog, cwarn } from "./tools/Log";
import { cdebug, cwarn } from "./tools/Log";
import { mustExist } from "./tools/Maybe";
import { TradeManager } from "./TradeManager";
import { DefaultLanguage, UserScript } from "./UserScript";
Expand Down Expand Up @@ -136,7 +136,7 @@ export class Engine {
);
})
.catch(error => {
this._host.engine.warning(error as string);
cwarn(error as string);
});
};
this._intervalMainLoop = setTimeout(loop, this._host.engine.settings.interval);
Expand Down Expand Up @@ -326,31 +326,12 @@ export class Engine {
return value;
}

private _printOutput(
cssClasses: "ks-activity" | `ks-activity ${ActivityTypeClass}` | "ks-default" | "ks-summary",
color: string,
...args: Array<number | string>
iactivity(
i18nLiteral: keyof typeof i18nData[SupportedLanguages],
i18nArgs: Array<number | string> = [],
logStyle?: ActivityClass
): void {
if (this.settings.filters.enabled) {
for (const filterItem of Object.values(this.settings.filters.items)) {
if (filterItem.enabled && filterItem.variant === cssClasses) {
return;
}
}
}

// update the color of the message immediately after adding
const msg = this._host.gamePage.msg(...args, cssClasses);
$(msg.span).css("color", color);

cdebug(args);
}

private _message(...args: Array<number | string>): void {
this._printOutput("ks-default", "#aa50fe", ...args);
}

private _activity(text: string, logStyle?: ActivityClass): void {
const text = this.i18n(i18nLiteral, i18nArgs);
if (logStyle) {
const activityClass: ActivityTypeClass = `type_${logStyle}` as const;
this._printOutput(`ks-activity ${activityClass}` as const, "#e65C00", text);
Expand All @@ -359,58 +340,52 @@ export class Engine {
}
}

private _summary(...args: Array<number | string>): void {
this._printOutput("ks-summary", "#009933", ...args);
}

warning(...args: Array<number | string>): void {
args.unshift("Warning!");
if (console) {
clog(args);
}
}

imessage(
i18nLiteral: keyof typeof i18nData[SupportedLanguages],
i18nArgs: Array<number | string> = []
): void {
this._message(this.i18n(i18nLiteral, i18nArgs));
}
iactivity(
i18nLiteral: keyof typeof i18nData[SupportedLanguages],
i18nArgs: Array<number | string> = [],
logStyle?: ActivityClass
): void {
this._activity(this.i18n(i18nLiteral, i18nArgs), logStyle);
}
private _isummary(
i18nLiteral: keyof typeof i18nData[SupportedLanguages],
i18nArgs: Array<number | string>
): void {
this._summary(this.i18n(i18nLiteral, i18nArgs));
}
private _iwarning(
i18nLiteral: keyof typeof i18nData[SupportedLanguages],
i18nArgs: Array<number | string>
): void {
this.warning(this.i18n(i18nLiteral, i18nArgs));
}

resetActivitySummary(): void {
this._activitySummary.resetActivity();
this._printOutput("ks-default", "#aa50fe", this.i18n(i18nLiteral, i18nArgs));
}

storeForSummary(name: string, amount = 1, section: ActivitySummarySection = "other"): void {
this._activitySummary.storeActivity(name, amount, section);
}

getSummary() {
return this._activitySummary.renderSummary();
}

displayActivitySummary(): void {
const summary = this._activitySummary.renderSummary();
const summary = this.getSummary();
for (const summaryLine of summary) {
this._summary(summaryLine);
this._printOutput("ks-summary", "#009933", summaryLine);
}

// Clear out the old activity
this.resetActivitySummary();
}

resetActivitySummary(): void {
this._activitySummary.resetActivity();
}

private _printOutput(
cssClasses: "ks-activity" | `ks-activity ${ActivityTypeClass}` | "ks-default" | "ks-summary",
color: string,
...args: Array<number | string>
): void {
if (this.settings.filters.enabled) {
for (const filterItem of Object.values(this.settings.filters.items)) {
if (filterItem.enabled && filterItem.variant === cssClasses) {
return;
}
}
}

// update the color of the message immediately after adding
const msg = this._host.gamePage.msg(...args, cssClasses);
$(msg.span).css("color", color);

cdebug(args);
}
}
5 changes: 2 additions & 3 deletions packages/userscript/source/ReligionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { BonfireSettingsItem } from "./options/BonfireSettings";
import { FaithItem, ReligionSettings, ReligionSettingsItem } from "./options/ReligionSettings";
import { TabManager } from "./TabManager";
import { objectEntries } from "./tools/Entries";
import { cwarn } from "./tools/Log";
import { mustExist } from "./tools/Maybe";
import {
BuildButton,
Expand Down Expand Up @@ -518,9 +519,7 @@ export class ReligionManager implements Automation {
const label = build.label;
amount = this._bulkManager.construct(button.model, button, amount);
if (amount !== amountTemp) {
this._host.engine.warning(
`${label} Amount ordered: ${amountTemp} Amount Constructed: ${amount}`
);
cwarn(`${label} Amount ordered: ${amountTemp} Amount Constructed: ${amount}`);
}

if (variant === UnicornItemVariant.OrderOfTheSun) {
Expand Down
5 changes: 2 additions & 3 deletions packages/userscript/source/SpaceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { SettingMax } from "./options/Settings";
import { SpaceSettings } from "./options/SpaceSettings";
import { TabManager } from "./TabManager";
import { objectEntries } from "./tools/Entries";
import { cwarn } from "./tools/Log";
import { BuildButton, SpaceBuildingInfo, SpaceBuildings, SpaceTab } from "./types";
import { UserScript } from "./UserScript";
import { WorkshopManager } from "./WorkshopManager";
Expand Down Expand Up @@ -125,9 +126,7 @@ export class SpaceManager implements Automation {
const label = build.label;
amount = this._bulkManager.construct(button.model, button, amount);
if (amount !== amountTemp) {
this._host.engine.warning(
`${label} Amount ordered: ${amountTemp} Amount Constructed: ${amount}`
);
cwarn(`${label} Amount ordered: ${amountTemp} Amount Constructed: ${amount}`);
}
this._host.engine.storeForSummary(label, amount, "build");

Expand Down
5 changes: 2 additions & 3 deletions packages/userscript/source/TimeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BulkPurchaseHelper } from "./helper/BulkPurchaseHelper";
import { TimeItem, TimeSettings, TimeSettingsItem } from "./options/TimeSettings";
import { TabManager } from "./TabManager";
import { objectEntries } from "./tools/Entries";
import { cwarn } from "./tools/Log";
import { mustExist } from "./tools/Maybe";
import {
BuildButton,
Expand Down Expand Up @@ -108,9 +109,7 @@ export class TimeManager {
const label = build.label;
amount = this._bulkManager.construct(button.model, button, amount);
if (amount !== amountTemp) {
this._host.engine.warning(
`${label} Amount ordered: ${amountTemp} Amount Constructed: ${amount}`
);
cwarn(`${label} Amount ordered: ${amountTemp} Amount Constructed: ${amount}`);
}
this._host.engine.storeForSummary(label, amount, "build");

Expand Down
11 changes: 4 additions & 7 deletions packages/userscript/source/TradeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TradeSettings } from "./options/TradeSettings";
import { TabManager } from "./TabManager";
import { objectEntries } from "./tools/Entries";
import { ucfirst } from "./tools/Format";
import { cwarn } from "./tools/Log";
import { isNil, Maybe, mustExist } from "./tools/Maybe";
import { BuildButton, Race, RaceInfo, Resource, TradeInfo, TradeTab } from "./types";
import { UserScript } from "./UserScript";
Expand Down Expand Up @@ -329,11 +330,7 @@ export class TradeManager implements Automation {
}
cultureVal = this._workshopManager.getValueAvailable("culture", true);
if (cultureVal < emBulk.priceSum) {
this._host.engine.warning(
"Something has gone horribly wrong.",
emBulk.priceSum,
cultureVal
);
cwarn("Something has gone horribly wrong.", emBulk.priceSum, cultureVal);
}
this._host.gamePage.resPool.resources[13].value -= emBulk.priceSum;
emBulk.race.embassyLevel += emBulk.val;
Expand Down Expand Up @@ -509,7 +506,7 @@ export class TradeManager implements Automation {
*/
trade(name: Race, amount: number): void {
if (!name || 1 > amount) {
this._host.engine.warning(
cwarn(
"KS trade checks are not functioning properly, please create an issue on the github page."
);
}
Expand All @@ -518,7 +515,7 @@ export class TradeManager implements Automation {
const button = this.getTradeButton(race.name);

if (!button.model.enabled || !this.settings.items[name].enabled) {
this._host.engine.warning(
cwarn(
"KS trade checks are not functioning properly, please create an issue on the github page."
);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/userscript/source/ui/SettingsSectionUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ReligionSettingsItem } from "../options/ReligionSettings";
import { SettingMax, SettingTrigger } from "../options/Settings";
import { SettingsSection } from "../options/SettingsSection";
import { ucfirst } from "../tools/Format";
import { cwarn } from "../tools/Log";
import { mustExist } from "../tools/Maybe";
import { Resource } from "../types";
import { UserScript } from "../UserScript";
Expand Down Expand Up @@ -367,7 +368,7 @@ export abstract class SettingsSectionUi {

setConsumeRate(name: Resource, value: number): void {
if (value < 0.0 || 1.0 < value) {
this._host.engine.warning(`ignoring non-numeric or invalid consume rate ${value}`);
cwarn(`ignoring non-numeric or invalid consume rate ${value}`);
return;
}

Expand Down

0 comments on commit 539a980

Please sign in to comment.