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;