GRAFTING: Add new api for checking ongoing grafting (#1435)

This commit is contained in:
catloversg
2024-06-29 09:59:18 +07:00
committed by GitHub
parent 32eb6324fd
commit b1c1fc24a9
10 changed files with 97 additions and 10 deletions
+1
View File
@@ -388,6 +388,7 @@ const grafting = {
getAugmentationGraftTime: 3.75,
getGraftableAugmentations: 5,
graftAugmentation: 7.5,
waitForOngoingGrafting: 1,
} as const;
const corporation = {
+13
View File
@@ -97,5 +97,18 @@ export function NetscriptGrafting(): InternalAPI<IGrafting> {
helpers.log(ctx, () => `Began grafting Augmentation ${augName}.`);
return true;
},
waitForOngoingGrafting: (ctx) => () => {
checkGraftingAPIAccess(ctx);
if (!Player.currentWork) {
return Promise.resolve();
}
if (!(Player.currentWork instanceof GraftingWork)) {
return Promise.reject(
`The current work is not a grafting work. Type of current work: ${Player.currentWork.type}.`,
);
}
return Player.currentWork.completion;
},
};
}
+21 -3
View File
@@ -1690,6 +1690,7 @@ export interface GraftingTask {
type: "GRAFTING";
cyclesWorked: number;
augmentation: string;
completion: Promise<void>;
}
/**
@@ -4634,18 +4635,22 @@ export interface Grafting {
getAugmentationGraftPrice(augName: string): number;
/**
* Retrieves the time required to graft an aug.
* Retrieves the time required to graft an aug. Do not use this value to determine when the ongoing grafting finishes.
* The ongoing grafting is affected by current intelligence level and focus bonus. You should use
* {@link Grafting.waitForOngoingGrafting | waitForOngoingGrafting} for that purpose.
*
* @remarks
* RAM cost: 3.75 GB
*
* @param augName - Name of the aug to check the grafting time of. Must be an exact match.
* @returns The time required, in millis, to graft the named augmentation.
* @returns The time required, in milliseconds, to graft the named augmentation.
* @throws Will error if an invalid Augmentation name is provided.
*/
getAugmentationGraftTime(augName: string): number;
/**
* Retrieves a list of Augmentations that can be grafted.
*
* @remarks
* RAM cost: 5 GB
*
@@ -4657,7 +4662,9 @@ export interface Grafting {
getGraftableAugmentations(): string[];
/**
* Begins grafting the named aug. You must be in New Tokyo to use this.
* Begins grafting the named aug. You must be in New Tokyo to use this. When you call this API, the current work
* (grafting or other actions) will be canceled.
*
* @remarks
* RAM cost: 7.5 GB
*
@@ -4668,6 +4675,17 @@ export interface Grafting {
* @throws Will error if called while you are not in New Tokyo.
*/
graftAugmentation(augName: string, focus?: boolean): boolean;
/**
* Wait until the ongoing grafting finishes or is canceled.
*
* @remarks
* RAM cost: 1 GB
*
* @returns A promise that resolves when the current grafting finishes or is canceled. If there is no current work,
* the promise resolves immediately. If the current work is not a grafting work, the promise rejects immediately.
*/
waitForOngoingGrafting(): Promise<void>;
}
/**
+21 -2
View File
@@ -10,6 +10,8 @@ import { dialogBoxCreate } from "../ui/React/DialogBox";
import { constructorsForReviver, Generic_toJSON, Generic_fromJSON, IReviverValue } from "../utils/JSONReviver";
import { GraftableAugmentation } from "../PersonObjects/Grafting/GraftableAugmentation";
import { Augmentations } from "../Augmentation/Augmentations";
import { PromisePair } from "../Types/Promises";
import { getKeyList } from "../utils/helpers/getKeyList";
export const isGraftingWork = (w: Work | null): w is GraftingWork => w !== null && w.type === WorkType.GRAFTING;
@@ -22,6 +24,14 @@ export class GraftingWork extends Work {
augmentation: AugmentationName;
unitCompleted: number;
unitRate: number;
completionPromisePair: PromisePair<void> = { promise: null, resolve: null };
get completion(): Promise<void> {
if (!this.completionPromisePair.promise) {
this.completionPromisePair.promise = new Promise((r) => (this.completionPromisePair.resolve = r));
}
return this.completionPromisePair.promise;
}
constructor(params?: GraftingWorkParams) {
super(WorkType.GRAFTING, params?.singularity ?? true);
@@ -79,6 +89,12 @@ export class GraftingWork extends Work {
(CONSTANTS.IntelligenceGraftBaseExpGain * this.cyclesWorked * CONSTANTS.MilliPerCycle) / 10000,
);
}
if (this.completionPromisePair.resolve) {
this.completionPromisePair.resolve();
this.completionPromisePair.resolve = null;
this.completionPromisePair.promise = null;
}
}
APICopy() {
@@ -86,17 +102,20 @@ export class GraftingWork extends Work {
type: WorkType.GRAFTING as const,
cyclesWorked: this.cyclesWorked,
augmentation: this.augmentation,
completion: this.completion,
};
}
static savedKeys = getKeyList(GraftingWork, { removedKeys: ["completionPromisePair"] });
/** Serialize the current object to a JSON save state. */
toJSON(): IReviverValue {
return Generic_toJSON("GraftingWork", this);
return Generic_toJSON("GraftingWork", this, GraftingWork.savedKeys);
}
/** Initializes a GraftingWork object from a JSON save state. */
static fromJSON(value: IReviverValue): GraftingWork {
return Generic_fromJSON(GraftingWork, value.data);
return Generic_fromJSON(GraftingWork, value.data, GraftingWork.savedKeys);
}
}