Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

let Outfit.from take a Requirement input #98

Merged
merged 2 commits into from
Jul 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/outfit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,30 @@ export class Outfit {
* @param error An error to throw if we fail to equip the outfit; if this parameter is null, the return type will be Outfit | null
* @returns A new outfit containing the inputted spec, or null if that is impossible.
*/
static from(spec: OutfitSpec): Outfit | null;
static from(spec: OutfitSpec, error: null): Outfit | null;
static from(spec: OutfitSpec, error: Error): Outfit;
static from(spec: OutfitSpec, error: Error | null = null): Outfit | null {
static from(spec: OutfitSpec | Requirement): Outfit | null;
static from(spec: OutfitSpec | Requirement, error: null): Outfit | null;
static from(spec: OutfitSpec | Requirement, error: Error): Outfit;
static from(spec: OutfitSpec | Requirement, error: Error | null = null): Outfit | null {
const outfit = new Outfit();

if (spec instanceof Requirement) {
const result: OutfitSpec = {};
result.modifier = spec.maximizeParameters;
if (spec.maximizeOptions.forceEquip?.length) {
result.equip = spec.maximizeOptions.forceEquip;
}
result.avoid = spec.maximizeOptions.preventEquip;
result.bonuses = spec.maximizeOptions.bonusEquip;
if (spec.maximizeOptions.modes) {
result.modes = convertFromLibramModes(spec.maximizeOptions.modes);
}
// Not sure if this is necessary
const cleanedResult = Object.fromEntries(
[...Object.entries(result)].filter(([, v]) => v !== undefined)
);
return Outfit.from(cleanedResult);
}

const success = outfit.equip(spec);
if (!success && error) throw error;
return success ? outfit : null;
Expand Down Expand Up @@ -803,6 +821,10 @@ export function convertToLibramModes(modes: Modes): LibramModes {
};
}

export function convertFromLibramModes(modes: LibramModes): Modes {
return (modes.retrocape ? { ...modes, retrocape: modes.retrocape.split(" ") } : modes) as Modes;
}

/**
* Get the current modes of all items.
*
Expand Down