Skip to content

Commit

Permalink
BLADEBURNER: Change skill.canUpgrade and Skills UI (#1744)
Browse files Browse the repository at this point in the history
  • Loading branch information
catloversg authored Nov 4, 2024
1 parent 5786d7a commit bc02d4f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
14 changes: 5 additions & 9 deletions src/Bladeburner/Bladeburner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,17 @@ export class Bladeburner implements OperationTeam {
/** Attempts to perform a skill upgrade, gives a message on both success and failure */
upgradeSkill(skillName: BladeburnerSkillName, count = 1): Attempt<{ message: string }> {
const currentSkillLevel = this.skills[skillName] ?? 0;
const actualCount = currentSkillLevel + count - currentSkillLevel;
if (actualCount === 0) {
return {
message: `Cannot upgrade ${skillName}: Due to floating-point inaccuracy and the small value of specified "count", your skill cannot be upgraded.`,
};
}
const availability = Skills[skillName].canUpgrade(this, actualCount);
const availability = Skills[skillName].canUpgrade(this, count);
if (!availability.available) {
return { message: `Cannot upgrade ${skillName}: ${availability.error}` };
}
this.skillPoints -= availability.cost;
this.setSkillLevel(skillName, currentSkillLevel + actualCount);
this.setSkillLevel(skillName, currentSkillLevel + availability.actualCount);
return {
success: true,
message: `Upgraded skill ${skillName} by ${actualCount} level${actualCount > 1 ? "s" : ""}`,
message: `Upgraded skill ${skillName} by ${availability.actualCount} level${
availability.actualCount > 1 ? "s" : ""
}`,
};
}

Expand Down
20 changes: 13 additions & 7 deletions src/Bladeburner/Skill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,25 @@ export class Skill {
return result - 1;
}

canUpgrade(bladeburner: Bladeburner, count = 1): Availability<{ cost: number }> {
canUpgrade(bladeburner: Bladeburner, count = 1): Availability<{ actualCount: number; cost: number }> {
const currentLevel = bladeburner.skills[this.name] ?? 0;
if (!isPositiveInteger(count)) {
return { error: `Invalid upgrade count ${count}` };
const actualCount = currentLevel + count - currentLevel;
if (actualCount === 0) {
return {
error: `Cannot upgrade ${this.name}: Due to floating-point inaccuracy and the small value of specified "count", your skill cannot be upgraded.`,
};
}
if (!isPositiveInteger(actualCount)) {
return { error: `Invalid upgrade count ${actualCount}` };
}
if (currentLevel + count > this.maxLvl) {
return { error: `Upgraded level ${currentLevel + count} exceeds max` };
if (currentLevel + actualCount > this.maxLvl) {
return { error: `Upgraded level ${currentLevel + actualCount} exceeds max` };
}
const cost = this.calculateCost(currentLevel, count);
const cost = this.calculateCost(currentLevel, actualCount);
if (cost > bladeburner.skillPoints) {
return { error: `Insufficient skill points for upgrade` };
}
return { available: true, cost };
return { available: true, actualCount, cost };
}

getMultiplier(name: BladeburnerMultName): number {
Expand Down
14 changes: 10 additions & 4 deletions src/Bladeburner/ui/SkillElem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ export function SkillElem({ skill, bladeburner, onUpgrade }: SkillElemProps): Re
const skillName = skill.name;
const skillLevel = bladeburner.getSkillLevel(skillName);
const pointCost = useMemo(() => skill.calculateCost(skillLevel), [skill, skillLevel]);

const canLevel = bladeburner.skillPoints >= pointCost;
// No need to support "+1" button when the skill level reaches Number.MAX_SAFE_INTEGER.
const isSupported = skillLevel < Number.MAX_SAFE_INTEGER;
// Use skill.canUpgrade() instead of reimplementing all conditional checks.
const canLevel = isSupported ? skill.canUpgrade(bladeburner, 1).available ?? false : false;
/**
* maxLvl is only useful when we check if we should show "MAX LEVEL". For the check of the icon button, we don't need
* it. This condition is checked in skill.canUpgrade().
*/
const maxLvl = skill.maxLvl ? skillLevel >= skill.maxLvl : false;

function onClick(): void {
Expand All @@ -31,7 +37,7 @@ export function SkillElem({ skill, bladeburner, onUpgrade }: SkillElemProps): Re
<Paper sx={{ my: 1, p: 1 }}>
<Box display="flex" flexDirection="row" alignItems="center">
<CopyableText variant="h6" color="primary" value={skillName} />
{!canLevel || maxLvl ? (
{!canLevel ? (
<IconButton disabled>
<CloseIcon />
</IconButton>
Expand All @@ -45,7 +51,7 @@ export function SkillElem({ skill, bladeburner, onUpgrade }: SkillElemProps): Re
{maxLvl ? (
<Typography>MAX LEVEL</Typography>
) : (
<Typography>Skill Points required: {formatBigNumber(pointCost)}</Typography>
<Typography>Skill Points required: {isSupported ? formatBigNumber(pointCost) : "N/A"}</Typography>
)}
<Typography>{skill.desc}</Typography>
</Paper>
Expand Down

0 comments on commit bc02d4f

Please sign in to comment.