BUGFIX: Wrong error message when failing to recruit gang member (#1580)

This commit is contained in:
catloversg 2024-08-16 03:15:05 +07:00 committed by GitHub
parent 8e278f9262
commit a48bfba536
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 46 additions and 30 deletions

@ -27,6 +27,14 @@ import { PowerMultiplier } from "./data/power";
import { FactionName } from "@enums"; import { FactionName } from "@enums";
import { CONSTANTS } from "../Constants"; import { CONSTANTS } from "../Constants";
export enum RecruitmentResult {
Success = "Success",
EmptyName = "Member name cannot be an empty string",
DuplicatedName = "This name was used",
ExceedMaxNumber = "Your gang recruited maximum number of members",
NotEnoughRespect = "Your gang does not have enough respect to recruit more members",
}
export const GangPromise: PromisePair<number> = { promise: null, resolve: null }; export const GangPromise: PromisePair<number> = { promise: null, resolve: null };
export class Gang { export class Gang {
@ -301,9 +309,14 @@ export class Gang {
} }
} }
canRecruitMember(): boolean { canRecruitMember(): RecruitmentResult {
if (this.members.length >= GangConstants.MaximumGangMembers) return false; if (this.members.length >= GangConstants.MaximumGangMembers) {
return this.respect >= this.respectForNextRecruit(); return RecruitmentResult.ExceedMaxNumber;
}
if (this.respect < this.respectForNextRecruit()) {
return RecruitmentResult.NotEnoughRespect;
}
return RecruitmentResult.Success;
} }
/** @returns The respect threshold needed for the next member recruitment. Infinity if already at or above max members. */ /** @returns The respect threshold needed for the next member recruitment. Infinity if already at or above max members. */
@ -327,17 +340,24 @@ export class Gang {
return Math.min(membersRecruitabile, GangConstants.MaximumGangMembers) - this.members.length; return Math.min(membersRecruitabile, GangConstants.MaximumGangMembers) - this.members.length;
} }
recruitMember(name: string): boolean { recruitMember(name: string): RecruitmentResult {
name = String(name); if (name === "") {
if (name === "" || !this.canRecruitMember()) return false; return RecruitmentResult.EmptyName;
}
const resultOfCheckingIfGangCanRecruitMember = this.canRecruitMember();
if (resultOfCheckingIfGangCanRecruitMember !== RecruitmentResult.Success) {
return resultOfCheckingIfGangCanRecruitMember;
}
// Check for already-existing names // Check for already-existing names
const sameNames = this.members.filter((m) => m.name === name); if (this.members.some((m) => m.name === name)) {
if (sameNames.length >= 1) return false; return RecruitmentResult.DuplicatedName;
}
const member = new GangMember(name); const member = new GangMember(name);
this.members.push(member); this.members.push(member);
return true; return RecruitmentResult.Success;
} }
// Money and Respect gains multiplied by this number (< 1) // Money and Respect gains multiplied by this number (< 1)

@ -5,6 +5,7 @@ import { useGang } from "./Context";
import Typography from "@mui/material/Typography"; import Typography from "@mui/material/Typography";
import Button from "@mui/material/Button"; import Button from "@mui/material/Button";
import Box from "@mui/material/Box"; import Box from "@mui/material/Box";
import { RecruitmentResult } from "../Gang";
interface IProps { interface IProps {
onRecruit: () => void; onRecruit: () => void;
@ -16,7 +17,7 @@ export function RecruitButton(props: IProps): React.ReactElement {
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const recruitsAvailable = gang.getRecruitsAvailable(); const recruitsAvailable = gang.getRecruitsAvailable();
if (!gang.canRecruitMember()) { if (gang.canRecruitMember() !== RecruitmentResult.Success) {
const respectNeeded = gang.respectForNextRecruit(); const respectNeeded = gang.respectForNextRecruit();
return ( return (
<Box display="flex" alignItems="center" sx={{ mx: 1 }}> <Box display="flex" alignItems="center" sx={{ mx: 1 }}>

@ -6,6 +6,7 @@ import Typography from "@mui/material/Typography";
import TextField from "@mui/material/TextField"; import TextField from "@mui/material/TextField";
import Button from "@mui/material/Button"; import Button from "@mui/material/Button";
import { KEY } from "../../utils/helpers/keyCodes"; import { KEY } from "../../utils/helpers/keyCodes";
import { RecruitmentResult } from "../Gang";
interface IRecruitPopupProps { interface IRecruitPopupProps {
open: boolean; open: boolean;
@ -18,13 +19,14 @@ export function RecruitModal(props: IRecruitPopupProps): React.ReactElement {
const gang = useGang(); const gang = useGang();
const [name, setName] = useState(""); const [name, setName] = useState("");
const disabled = name === "" || !gang.canRecruitMember(); const disabled = name === "" || gang.canRecruitMember() !== RecruitmentResult.Success;
function recruit(): void { function recruit(): void {
if (disabled) return; if (disabled) {
// At this point, the only way this can fail is if you already return;
// have a gang member with the same name }
if (!gang.recruitMember(name) && name !== "") { const result = gang.recruitMember(name);
dialogBoxCreate("You already have a gang member with this name!"); if (result !== RecruitmentResult.Success) {
dialogBoxCreate(result);
return; return;
} }

@ -4,7 +4,7 @@ import type { GangMember } from "../Gang/GangMember";
import type { GangMemberTask } from "../Gang/GangMemberTask"; import type { GangMemberTask } from "../Gang/GangMemberTask";
import type { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper"; import type { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper";
import { GangPromise } from "../Gang/Gang"; import { GangPromise, RecruitmentResult } from "../Gang/Gang";
import { Player } from "@player"; import { Player } from "@player";
import { FactionName } from "@enums"; import { FactionName } from "@enums";
import { GangConstants } from "../Gang/data/Constants"; import { GangConstants } from "../Gang/data/Constants";
@ -160,7 +160,7 @@ export function NetscriptGang(): InternalAPI<IGang> {
}, },
canRecruitMember: (ctx) => () => { canRecruitMember: (ctx) => () => {
const gang = getGang(ctx); const gang = getGang(ctx);
return gang.canRecruitMember(); return gang.canRecruitMember() === RecruitmentResult.Success;
}, },
getRecruitsAvailable: (ctx) => () => { getRecruitsAvailable: (ctx) => () => {
const gang = getGang(ctx); const gang = getGang(ctx);
@ -173,20 +173,13 @@ export function NetscriptGang(): InternalAPI<IGang> {
recruitMember: (ctx) => (_memberName) => { recruitMember: (ctx) => (_memberName) => {
const memberName = helpers.string(ctx, "memberName", _memberName); const memberName = helpers.string(ctx, "memberName", _memberName);
const gang = getGang(ctx); const gang = getGang(ctx);
const recruited = gang.recruitMember(memberName); const result = gang.recruitMember(memberName);
if (memberName === "") { if (result !== RecruitmentResult.Success) {
ctx.workerScript.log("gang.recruitMember", () => `Failed to recruit Gang Member. Name must be provided.`); ctx.workerScript.log("gang.recruitMember", () => `Failed to recruit gang member '${memberName}'. ${result}.`);
return false; return false;
} else if (recruited) {
ctx.workerScript.log("gang.recruitMember", () => `Successfully recruited Gang Member '${memberName}'`);
return recruited;
} else {
ctx.workerScript.log(
"gang.recruitMember",
() => `Failed to recruit Gang Member '${memberName}'. Name already used.`,
);
return recruited;
} }
ctx.workerScript.log("gang.recruitMember", () => `Successfully recruited gang member '${memberName}'`);
return true;
}, },
getTaskNames: (ctx) => () => { getTaskNames: (ctx) => () => {
const gang = getGang(ctx); const gang = getGang(ctx);