mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-05-14 11:30:08 +02:00
SLEEVE: Add a nextCompletion promise to SleeveBladeburnerWork (#916)
This commit is contained in:
@@ -5,6 +5,7 @@ import { applySleeveGains, SleeveWorkClass, SleeveWorkType } from "./Work";
|
||||
import { CONSTANTS } from "../../../Constants";
|
||||
import { GeneralActions } from "../../../Bladeburner/data/GeneralActions";
|
||||
import { scaleWorkStats } from "../../../Work/WorkStats";
|
||||
import { getKeyList } from "../../../utils/helpers/getKeyList";
|
||||
|
||||
interface SleeveBladeburnerWorkParams {
|
||||
type: "General" | "Contracts";
|
||||
@@ -19,9 +20,14 @@ export class SleeveBladeburnerWork extends SleeveWorkClass {
|
||||
cyclesWorked = 0;
|
||||
actionType: "General" | "Contracts";
|
||||
actionName: string;
|
||||
signalCompletion = () => {
|
||||
// Intentionally empty function, this is just an initial value and will never be used.
|
||||
};
|
||||
nextCompletion: Promise<void>;
|
||||
|
||||
constructor(params?: SleeveBladeburnerWorkParams) {
|
||||
super();
|
||||
this.nextCompletion = new Promise((r) => (this.signalCompletion = r));
|
||||
this.actionType = params?.type ?? "General";
|
||||
this.actionName = params?.name ?? "Field Analysis";
|
||||
}
|
||||
@@ -32,6 +38,10 @@ export class SleeveBladeburnerWork extends SleeveWorkClass {
|
||||
return ret / CONSTANTS.MilliPerCycle;
|
||||
}
|
||||
|
||||
finish() {
|
||||
this.signalCompletion();
|
||||
}
|
||||
|
||||
process(sleeve: Sleeve, cycles: number) {
|
||||
if (!Player.bladeburner) return sleeve.stopWork();
|
||||
this.cyclesWorked += cycles;
|
||||
@@ -43,7 +53,7 @@ export class SleeveBladeburnerWork extends SleeveWorkClass {
|
||||
if (action.count < 1) return sleeve.stopWork();
|
||||
}
|
||||
|
||||
while (this.cyclesWorked > this.cyclesNeeded(sleeve)) {
|
||||
while (this.cyclesWorked >= this.cyclesNeeded(sleeve)) {
|
||||
if (this.actionType === "Contracts") {
|
||||
const action = Player.bladeburner.getActionObject(actionIdent);
|
||||
if (!action) throw new Error(`Error getting ${this.actionName} action object`);
|
||||
@@ -60,6 +70,10 @@ export class SleeveBladeburnerWork extends SleeveWorkClass {
|
||||
applySleeveGains(sleeve, scaleWorkStats(retValue, sleeve.shockBonus(), false));
|
||||
}
|
||||
this.cyclesWorked -= this.cyclesNeeded(sleeve);
|
||||
// Resolve and reset nextCompletion promise
|
||||
const resolver = this.signalCompletion;
|
||||
this.nextCompletion = new Promise((r) => (this.signalCompletion = r));
|
||||
resolver();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,17 +84,20 @@ export class SleeveBladeburnerWork extends SleeveWorkClass {
|
||||
actionName: this.actionName,
|
||||
cyclesWorked: this.cyclesWorked,
|
||||
cyclesNeeded: this.cyclesNeeded(sleeve),
|
||||
nextCompletion: this.nextCompletion,
|
||||
};
|
||||
}
|
||||
|
||||
static savedKeys = getKeyList(SleeveBladeburnerWork, { removedKeys: ["signalCompletion", "nextCompletion"] });
|
||||
|
||||
/** Serialize the current object to a JSON save state. */
|
||||
toJSON(): IReviverValue {
|
||||
return Generic_toJSON("SleeveBladeburnerWork", this);
|
||||
return Generic_toJSON("SleeveBladeburnerWork", this, SleeveBladeburnerWork.savedKeys);
|
||||
}
|
||||
|
||||
/** Initializes a BladeburnerWork object from a JSON save state. */
|
||||
static fromJSON(value: IReviverValue): SleeveBladeburnerWork {
|
||||
return Generic_fromJSON(SleeveBladeburnerWork, value.data);
|
||||
return Generic_fromJSON(SleeveBladeburnerWork, value.data, SleeveBladeburnerWork.savedKeys);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user