TYPESAFETY: FactionName (#644)

This commit is contained in:
Snarling
2023-06-25 22:53:35 -04:00
committed by GitHub
parent 1de676972f
commit 9a0a843ffc
31 changed files with 295 additions and 751 deletions
+3 -3
View File
@@ -17,7 +17,7 @@ import * as serverMethods from "./PlayerObjectServerMethods";
import * as workMethods from "./PlayerObjectWorkMethods";
import { setPlayer } from "../../Player";
import { LocationName } from "@enums";
import { FactionName, LocationName } from "@enums";
import { HashManager } from "../../Hacknet/HashManager";
import { MoneySourceTracker } from "../../utils/MoneySourceTracker";
import { constructorsForReviver, Generic_toJSON, Generic_fromJSON, IReviverValue } from "../../utils/JSONReviver";
@@ -35,8 +35,8 @@ export class PlayerObject extends Person implements IPlayer {
gang: Gang | null = null;
bladeburner: Bladeburner | null = null;
currentServer = "";
factions: string[] = [];
factionInvitations: string[] = [];
factions: FactionName[] = [];
factionInvitations: FactionName[] = [];
hacknetNodes: (HacknetNode | string)[] = []; // HacknetNode object or hostname of Hacknet Server
has4SData = false;
has4SDataTixApi = false;
@@ -1,11 +1,12 @@
import type { PlayerObject } from "./PlayerObject";
import type { FactionName } from "@enums";
import type { Faction } from "../../Faction/Faction";
import { Factions } from "../../Faction/Factions";
import { Faction } from "../../Faction/Faction";
import { Gang } from "../../Gang/Gang";
import { GangConstants } from "../../Gang/data/Constants";
import { isFactionWork } from "../../Work/FactionWork";
import type { PlayerObject } from "./PlayerObject";
export function canAccessGang(this: PlayerObject): boolean {
if (this.bitNodeN === 2) {
return true;
@@ -36,12 +37,12 @@ export function getGangName(this: PlayerObject): string {
return gang ? gang.facName : "";
}
export function hasGangWith(this: PlayerObject, facName: string): boolean {
export function hasGangWith(this: PlayerObject, facName: FactionName): boolean {
const gang = this.gang;
return gang ? gang.facName === facName : false;
}
export function startGang(this: PlayerObject, factionName: string, hacking: boolean): void {
export function startGang(this: PlayerObject, factionName: FactionName, hacking: boolean): void {
// isFactionWork handles null internally, finishWork might need to be run with true
if (isFactionWork(this.currentWork) && this.currentWork.factionName === factionName) this.finishWork(false);
@@ -158,7 +158,7 @@ export function prestigeSourceFile(this: PlayerObject): void {
this.augmentations = [];
}
export function receiveInvite(this: PlayerObject, factionName: string): void {
export function receiveInvite(this: PlayerObject, factionName: FactionName): void {
if (this.factionInvitations.includes(factionName) || this.factions.includes(factionName)) {
return;
}
@@ -1099,34 +1099,34 @@ export function gainCodingContractReward(
reward: ICodingContractReward | null,
difficulty = 1,
): string {
if (reward == null || reward.type == null) {
return `No reward for this contract`;
}
if (!reward) return `No reward for this contract`;
/* eslint-disable no-case-declarations */
switch (reward.type) {
case CodingContractRewardType.FactionReputation:
if (reward.name == null || !Factions[reward.name]) {
// If no/invalid faction was designated, just give rewards to all factions
reward.type = CodingContractRewardType.FactionReputationAll;
return this.gainCodingContractReward(reward);
case CodingContractRewardType.FactionReputation: {
if (!Factions[reward.name]) {
return this.gainCodingContractReward({ type: CodingContractRewardType.FactionReputationAll });
}
const repGain = CONSTANTS.CodingContractBaseFactionRepGain * difficulty;
Factions[reward.name].playerReputation += repGain;
return `Gained ${repGain} faction reputation for ${reward.name}`;
case CodingContractRewardType.FactionReputationAll:
}
case CodingContractRewardType.FactionReputationAll: {
const totalGain = CONSTANTS.CodingContractBaseFactionRepGain * difficulty;
// Ignore Bladeburners and other special factions for this calculation
const specialFactions = [FactionName.Bladeburners as string];
const specialFactions = [
FactionName.Bladeburners,
FactionName.ShadowsOfAnarchy,
FactionName.ChurchOfTheMachineGod,
];
const factions = this.factions.slice().filter((f) => {
return !specialFactions.includes(f);
});
// If the player was only part of the special factions, we'll just give money
if (factions.length == 0) {
reward.type = CodingContractRewardType.Money;
return this.gainCodingContractReward(reward, difficulty);
return this.gainCodingContractReward({ type: CodingContractRewardType.Money }, difficulty);
}
const gainPerFaction = Math.floor(totalGain / factions.length);
@@ -1135,11 +1135,11 @@ export function gainCodingContractReward(
Factions[facName].playerReputation += gainPerFaction;
}
return `Gained ${gainPerFaction} reputation for each of the following factions: ${factions.join(", ")}`;
}
case CodingContractRewardType.CompanyReputation: {
if (reward.name == null || !Companies[reward.name]) {
if (!Companies[reward.name]) {
//If no/invalid company was designated, just give rewards to all factions
reward.type = CodingContractRewardType.FactionReputationAll;
return this.gainCodingContractReward(reward);
return this.gainCodingContractReward({ type: CodingContractRewardType.FactionReputationAll });
}
const repGain = CONSTANTS.CodingContractBaseCompanyRepGain * difficulty;
Companies[reward.name].playerReputation += repGain;
+18 -25
View File
@@ -292,38 +292,31 @@ export class Sleeve extends Person implements SleevePerson {
return true;
}
/**
* Start work for one of the player's factions
* Returns boolean indicating success
*/
workForFaction(factionName: string, workType: string): boolean {
/** TODO 2.4: Make this take in type correct data */
workForFaction(_factionName: string, _workType: string): boolean {
const factionName = getEnumHelper("FactionName").fuzzyGetMember(_factionName);
if (!factionName) return false;
const faction = Factions[factionName];
if (factionName === "" || !faction || !Player.factions.includes(factionName)) {
return false;
}
const workType = getEnumHelper("FactionWorkType").fuzzyGetMember(_workType);
if (!workType) return false;
const factionInfo = faction.getInfo();
// Set type of work (hacking/field/security), and the experience gains
const sanitizedWorkType = workType.toLowerCase();
let factionWorkType: FactionWorkType;
if (sanitizedWorkType.includes("hack")) {
if (!factionInfo.offerHackingWork) return false;
factionWorkType = FactionWorkType.hacking;
} else if (sanitizedWorkType.includes("field")) {
if (!factionInfo.offerFieldWork) return false;
factionWorkType = FactionWorkType.field;
} else if (sanitizedWorkType.includes("security")) {
if (!factionInfo.offerSecurityWork) return false;
factionWorkType = FactionWorkType.security;
} else {
return false;
switch (workType) {
case FactionWorkType.field:
if (!factionInfo.offerFieldWork) return false;
break;
case FactionWorkType.hacking:
if (!factionInfo.offerHackingWork) return false;
break;
case FactionWorkType.security:
if (!factionInfo.offerSecurityWork) return false;
break;
}
this.startWork(
new SleeveFactionWork({
factionWorkType: factionWorkType,
factionName: faction.name,
factionWorkType: workType,
factionName: factionName,
}),
);
@@ -7,11 +7,11 @@ import { Factions } from "../../../Faction/Factions";
import { calculateFactionExp, calculateFactionRep } from "../../../Work/Formulas";
import { Faction } from "../../../Faction/Faction";
import { scaleWorkStats, WorkStats } from "../../../Work/WorkStats";
import { findEnumMember } from "../../../utils/helpers/enum";
import { getEnumHelper } from "../../../utils/EnumHelper";
interface SleeveFactionWorkParams {
factionWorkType: FactionWorkType;
factionName: string;
factionName: FactionName;
}
export const isSleeveFactionWork = (w: SleeveWorkClass | null): w is SleeveFactionWork =>
@@ -20,7 +20,7 @@ export const isSleeveFactionWork = (w: SleeveWorkClass | null): w is SleeveFacti
export class SleeveFactionWork extends SleeveWorkClass {
type: SleeveWorkType.FACTION = SleeveWorkType.FACTION;
factionWorkType: FactionWorkType;
factionName: string;
factionName: FactionName;
constructor(params?: SleeveFactionWorkParams) {
super();
@@ -67,8 +67,8 @@ export class SleeveFactionWork extends SleeveWorkClass {
/** Initializes a FactionWork object from a JSON save state. */
static fromJSON(value: IReviverValue): SleeveFactionWork {
const factionWork = Generic_fromJSON(SleeveFactionWork, value.data);
factionWork.factionWorkType =
findEnumMember(FactionWorkType, factionWork.factionWorkType) ?? FactionWorkType.hacking;
factionWork.factionWorkType = getEnumHelper("FactionWorkType").fuzzyGetMember(factionWork.factionWorkType, true);
factionWork.factionName = getEnumHelper("FactionName").fuzzyGetMember(factionWork.factionName, true);
return factionWork;
}
}
+2 -3
View File
@@ -137,10 +137,9 @@ const tasks: {
return {
first: factions,
second: (s1: string) => {
second: (s1) => {
if (!getEnumHelper("FactionName").isMember(s1)) return ["------"];
const faction = Factions[s1];
if (!faction) return ["------"];
const facInfo = faction.getInfo();
const options: string[] = [];
if (facInfo.offerHackingWork) {