From 8445af5f2b598320e4b57f51c983b2f43e147c4c Mon Sep 17 00:00:00 2001 From: David Walker Date: Tue, 28 Mar 2023 21:30:27 -0700 Subject: [PATCH] Moved Player initialization point (#452) Now initialized as side effect in PlayerObject, instead of in Player file that is imported everywhere. --- src/Achievements/Achievements.ts | 3 ++- src/PersonObjects/Player/PlayerObject.ts | 3 +++ .../Player/PlayerObjectAugmentationMethods.ts | 3 ++- .../Player/PlayerObjectBladeburnerMethods.ts | 3 ++- .../Player/PlayerObjectCorporationMethods.ts | 3 ++- src/PersonObjects/Player/PlayerObjectGangMethods.ts | 3 ++- src/PersonObjects/Player/PlayerObjectGeneralMethods.ts | 3 ++- src/PersonObjects/Player/PlayerObjectServerMethods.ts | 4 ++-- src/PersonObjects/Player/PlayerObjectWorkMethods.ts | 3 ++- src/Player.ts | 9 +++++++-- src/Server/AllServers.ts | 1 + src/engine.tsx | 1 + test/jest/Imports/Hacknet.test.ts | 5 +++++ test/jest/Imports/Server.test.ts | 5 +++++ test/jest/Imports/WorkerScript.test.ts | 5 +++++ 15 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 test/jest/Imports/Hacknet.test.ts create mode 100644 test/jest/Imports/Server.test.ts create mode 100644 test/jest/Imports/WorkerScript.test.ts diff --git a/src/Achievements/Achievements.ts b/src/Achievements/Achievements.ts index 03e8a685d..81c535eb3 100644 --- a/src/Achievements/Achievements.ts +++ b/src/Achievements/Achievements.ts @@ -1,4 +1,3 @@ -import { PlayerObject } from "../PersonObjects/Player/PlayerObject"; import { AugmentationNames } from "../Augmentation/data/AugmentationNames"; import { SkillNames } from "../Bladeburner/data/SkillNames"; import { Skills } from "../Bladeburner/Skills"; @@ -26,6 +25,8 @@ import { BlackOperationNames } from "../Bladeburner/data/BlackOperationNames"; import { isClassWork } from "../Work/ClassWork"; import { BitNodeMultipliers } from "../BitNode/BitNodeMultipliers"; +import type { PlayerObject } from "../PersonObjects/Player/PlayerObject"; + // Unable to correctly cast the JSON data into AchievementDataJson type otherwise... const achievementData = ((data)).achievements; diff --git a/src/PersonObjects/Player/PlayerObject.ts b/src/PersonObjects/Player/PlayerObject.ts index 067eaf5b4..117a6fd0a 100644 --- a/src/PersonObjects/Player/PlayerObject.ts +++ b/src/PersonObjects/Player/PlayerObject.ts @@ -6,6 +6,7 @@ import * as generalMethods from "./PlayerObjectGeneralMethods"; import * as serverMethods from "./PlayerObjectServerMethods"; import * as workMethods from "./PlayerObjectWorkMethods"; +import { setPlayer } from "../../Player"; import { Sleeve } from "../Sleeve/Sleeve"; import { PlayerOwnedSourceFile } from "../../SourceFile/PlayerOwnedSourceFile"; import { Exploit } from "../../Exploits/Exploit"; @@ -172,4 +173,6 @@ export class PlayerObject extends Person implements IPlayer { } } +setPlayer(new PlayerObject()); + Reviver.constructors.PlayerObject = PlayerObject; diff --git a/src/PersonObjects/Player/PlayerObjectAugmentationMethods.ts b/src/PersonObjects/Player/PlayerObjectAugmentationMethods.ts index e116e5240..abcb38f8a 100644 --- a/src/PersonObjects/Player/PlayerObjectAugmentationMethods.ts +++ b/src/PersonObjects/Player/PlayerObjectAugmentationMethods.ts @@ -1,7 +1,8 @@ /** Augmentation-related methods for the Player class (PlayerObject) */ -import { PlayerObject } from "./PlayerObject"; import { calculateEntropy } from "../Grafting/EntropyAccumulation"; +import type { PlayerObject } from "./PlayerObject"; + export function applyEntropy(this: PlayerObject, stacks = 1): void { // Re-apply all multipliers this.reapplyAllAugmentations(); diff --git a/src/PersonObjects/Player/PlayerObjectBladeburnerMethods.ts b/src/PersonObjects/Player/PlayerObjectBladeburnerMethods.ts index 42a24914e..1dd8c4816 100644 --- a/src/PersonObjects/Player/PlayerObjectBladeburnerMethods.ts +++ b/src/PersonObjects/Player/PlayerObjectBladeburnerMethods.ts @@ -1,5 +1,6 @@ import { Bladeburner } from "../../Bladeburner/Bladeburner"; -import { PlayerObject } from "./PlayerObject"; + +import type { PlayerObject } from "./PlayerObject"; export function canAccessBladeburner(this: PlayerObject): boolean { return this.bitNodeN === 6 || this.bitNodeN === 7 || this.sourceFileLvl(6) > 0 || this.sourceFileLvl(7) > 0; diff --git a/src/PersonObjects/Player/PlayerObjectCorporationMethods.ts b/src/PersonObjects/Player/PlayerObjectCorporationMethods.ts index 2eba7702e..d99f94e25 100644 --- a/src/PersonObjects/Player/PlayerObjectCorporationMethods.ts +++ b/src/PersonObjects/Player/PlayerObjectCorporationMethods.ts @@ -3,9 +3,10 @@ import { CorporationUnlockUpgradeIndex, CorporationUnlockUpgrades, } from "../../Corporation/data/CorporationUnlockUpgrades"; -import { PlayerObject } from "./PlayerObject"; import { resetIndustryResearchTrees } from "../../Corporation/IndustryData"; +import type { PlayerObject } from "./PlayerObject"; + export function canAccessCorporation(this: PlayerObject): boolean { return this.bitNodeN === 3 || this.sourceFileLvl(3) > 0; } diff --git a/src/PersonObjects/Player/PlayerObjectGangMethods.ts b/src/PersonObjects/Player/PlayerObjectGangMethods.ts index 77d510068..a4b3dade1 100644 --- a/src/PersonObjects/Player/PlayerObjectGangMethods.ts +++ b/src/PersonObjects/Player/PlayerObjectGangMethods.ts @@ -1,10 +1,11 @@ import { Factions } from "../../Faction/Factions"; import { Faction } from "../../Faction/Faction"; import { Gang } from "../../Gang/Gang"; -import { PlayerObject } from "./PlayerObject"; 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; diff --git a/src/PersonObjects/Player/PlayerObjectGeneralMethods.ts b/src/PersonObjects/Player/PlayerObjectGeneralMethods.ts index 9850c15b1..bc74d889c 100644 --- a/src/PersonObjects/Player/PlayerObjectGeneralMethods.ts +++ b/src/PersonObjects/Player/PlayerObjectGeneralMethods.ts @@ -1,4 +1,3 @@ -import { PlayerObject } from "./PlayerObject"; import { applyAugmentation } from "../../Augmentation/AugmentationHelpers"; import { PlayerOwnedAugmentation } from "../../Augmentation/PlayerOwnedAugmentation"; import { AugmentationNames } from "../../Augmentation/data/AugmentationNames"; @@ -45,6 +44,8 @@ import { FactionNames } from "../../Faction/data/FactionNames"; import { isCompanyWork } from "../../Work/CompanyWork"; import { serverMetadata } from "../../Server/data/servers"; +import type { PlayerObject } from "./PlayerObject"; + export function init(this: PlayerObject): void { /* Initialize Player's home computer */ const t_homeComp = safelyCreateUniqueServer({ diff --git a/src/PersonObjects/Player/PlayerObjectServerMethods.ts b/src/PersonObjects/Player/PlayerObjectServerMethods.ts index 547b50efd..eb70c71a2 100644 --- a/src/PersonObjects/Player/PlayerObjectServerMethods.ts +++ b/src/PersonObjects/Player/PlayerObjectServerMethods.ts @@ -1,6 +1,4 @@ // Server and HacknetServer-related methods for the Player class (PlayerObject) -import { PlayerObject } from "./PlayerObject"; - import { CONSTANTS } from "../../Constants"; import { BitNodeMultipliers } from "../../BitNode/BitNodeMultipliers"; @@ -11,6 +9,8 @@ import { GetServer, AddToAllServers, createUniqueRandomIp } from "../../Server/A import { SpecialServers } from "../../Server/data/SpecialServers"; import { hasHacknetServers } from "../../Hacknet/HacknetHelpers"; +import type { PlayerObject } from "./PlayerObject"; + export function hasTorRouter(this: PlayerObject): boolean { return this.getHomeComputer().serversOnNetwork.includes(SpecialServers.DarkWeb); } diff --git a/src/PersonObjects/Player/PlayerObjectWorkMethods.ts b/src/PersonObjects/Player/PlayerObjectWorkMethods.ts index 41dfb2d30..928c5321e 100644 --- a/src/PersonObjects/Player/PlayerObjectWorkMethods.ts +++ b/src/PersonObjects/Player/PlayerObjectWorkMethods.ts @@ -1,5 +1,6 @@ import { Work } from "../../Work/Work"; -import { PlayerObject } from "./PlayerObject"; + +import type { PlayerObject } from "./PlayerObject"; export function startWork(this: PlayerObject, w: Work): void { if (this.currentWork !== null) { diff --git a/src/Player.ts b/src/Player.ts index dce46eb01..5ede04b58 100644 --- a/src/Player.ts +++ b/src/Player.ts @@ -1,9 +1,14 @@ -import { PlayerObject } from "./PersonObjects/Player/PlayerObject"; import { sanitizeExploits } from "./Exploits/Exploit"; import { Reviver } from "./utils/JSONReviver"; -export let Player = new PlayerObject(); +import type { PlayerObject } from "./PersonObjects/Player/PlayerObject"; + +export let Player: PlayerObject; + +export function setPlayer(playerObj: PlayerObject): void { + Player = playerObj; +} export function loadPlayer(saveString: string): void { Player = JSON.parse(saveString, Reviver); diff --git a/src/Server/AllServers.ts b/src/Server/AllServers.ts index 9b8029511..77dda5e8a 100644 --- a/src/Server/AllServers.ts +++ b/src/Server/AllServers.ts @@ -11,6 +11,7 @@ import { Reviver } from "../utils/JSONReviver"; import { isValidIPAddress } from "../utils/helpers/isValidIPAddress"; import { SpecialServers } from "./data/SpecialServers"; import { BitNodeMultipliers } from "../BitNode/BitNodeMultipliers"; +import "../Script/RunningScript"; // For reviver side-effect import type { RunningScript } from "../Script/RunningScript"; diff --git a/src/engine.tsx b/src/engine.tsx index cc57a6a6a..4a646de1a 100644 --- a/src/engine.tsx +++ b/src/engine.tsx @@ -12,6 +12,7 @@ import { processPassiveFactionRepGain, inviteToFaction } from "./Faction/Faction import { Router } from "./ui/GameRoot"; import { Page } from "./ui/Router"; import { SetupTextEditor } from "./ScriptEditor/ui/ScriptEditorRoot"; +import "./PersonObjects/Player/PlayerObject"; // For side-effect of creating Player import { getHackingWorkRepGain, diff --git a/test/jest/Imports/Hacknet.test.ts b/test/jest/Imports/Hacknet.test.ts new file mode 100644 index 000000000..8e21163e9 --- /dev/null +++ b/test/jest/Imports/Hacknet.test.ts @@ -0,0 +1,5 @@ +import { HacknetServer } from "../../../src/Hacknet/HacknetServer"; + +test("Can import only HacknetServer", () => { + new HacknetServer(); +}); diff --git a/test/jest/Imports/Server.test.ts b/test/jest/Imports/Server.test.ts new file mode 100644 index 000000000..8a8384fe6 --- /dev/null +++ b/test/jest/Imports/Server.test.ts @@ -0,0 +1,5 @@ +import { Server } from "../../../src/Server/Server"; + +test("Can import only Server", () => { + new Server(); +}); diff --git a/test/jest/Imports/WorkerScript.test.ts b/test/jest/Imports/WorkerScript.test.ts new file mode 100644 index 000000000..d2e499cc7 --- /dev/null +++ b/test/jest/Imports/WorkerScript.test.ts @@ -0,0 +1,5 @@ +import { WorkerScript } from "../../../src/Netscript/WorkerScript"; + +test("Can import only WorkerScript", () => { + WorkerScript; +});