Revert "Add toPreviousPage in router"

This reverts commit b0bc3236fdcd2c9f6cfb9c540010e9520c2cbe01.
This commit is contained in:
Martin Fournier 2022-01-12 18:49:53 -05:00
parent 2dee036e90
commit 0a2187bdf5
6 changed files with 45 additions and 145 deletions

@ -2582,8 +2582,6 @@ function installAugmentations(): boolean {
augmentationList += aug.name + level + "<br>"; augmentationList += aug.name + level + "<br>";
} }
Player.queuedAugmentations = []; Player.queuedAugmentations = [];
Router.clearHistory();
dialogBoxCreate( dialogBoxCreate(
"You slowly drift to sleep as scientists put you under in order " + "You slowly drift to sleep as scientists put you under in order " +
"to install the following Augmentations:<br>" + "to install the following Augmentations:<br>" +

@ -306,7 +306,5 @@ export function prestigeSourceFile(flume: boolean): void {
// Gain int exp // Gain int exp
if (SourceFileFlags[5] !== 0 && !flume) Player.gainIntelligenceExp(300); if (SourceFileFlags[5] !== 0 && !flume) Player.gainIntelligenceExp(300);
Router.clearHistory();
resetPidCounter(); resetPidCounter();
} }

@ -1,5 +1,5 @@
import React, { useState, useEffect } from "react"; import React, { useState, useEffect } from "react";
import { cloneDeep } from "lodash";
import { IPlayer } from "../PersonObjects/IPlayer"; import { IPlayer } from "../PersonObjects/IPlayer";
import { IEngine } from "../IEngine"; import { IEngine } from "../IEngine";
import { ITerminal } from "../Terminal/ITerminal"; import { ITerminal } from "../Terminal/ITerminal";
@ -86,11 +86,6 @@ interface IProps {
engine: IEngine; engine: IEngine;
} }
interface PageHistoryEntry {
page: Page;
args: any[];
}
const useStyles = makeStyles((theme: Theme) => const useStyles = makeStyles((theme: Theme) =>
createStyles({ createStyles({
root: { root: {
@ -110,15 +105,6 @@ export let Router: IRouter = {
page: () => { page: () => {
throw new Error("Router called before initialization"); throw new Error("Router called before initialization");
}, },
previousPage: () => {
throw new Error("Router called before initialization");
},
clearHistory: () => {
throw new Error("Router called before initialization");
},
toPreviousPage: (): boolean => {
throw new Error("Router called before initialization");
},
toActiveScripts: () => { toActiveScripts: () => {
throw new Error("Router called before initialization"); throw new Error("Router called before initialization");
}, },
@ -217,9 +203,7 @@ function determineStartPage(player: IPlayer): Page {
export function GameRoot({ player, engine, terminal }: IProps): React.ReactElement { export function GameRoot({ player, engine, terminal }: IProps): React.ReactElement {
const classes = useStyles(); const classes = useStyles();
const [{ files, vim }, setEditorOptions] = useState({ files: {}, vim: false }); const [{ files, vim }, setEditorOptions] = useState({ files: {}, vim: false });
const startPage = determineStartPage(player); const [page, setPage] = useState(determineStartPage(player));
const [page, setPage] = useState(startPage);
const [pageHistory, setPageHistory] = useState<PageHistoryEntry[]>([{ page: startPage, args: [] }]);
const setRerender = useState(0)[1]; const setRerender = useState(0)[1];
const [faction, setFaction] = useState<Faction>( const [faction, setFaction] = useState<Faction>(
player.currentWorkFactionName ? Factions[player.currentWorkFactionName] : (undefined as unknown as Faction), player.currentWorkFactionName ? Factions[player.currentWorkFactionName] : (undefined as unknown as Faction),
@ -250,131 +234,70 @@ export function GameRoot({ player, engine, terminal }: IProps): React.ReactEleme
setTimeout(() => htmlLocation.reload(), 2000); setTimeout(() => htmlLocation.reload(), 2000);
} }
function setCurrentPage(page: Page, ...args: any): void {
const history = [
{ page, args: cloneDeep(args) },
...pageHistory
].slice(0, 20);
setPageHistory(history)
setPage(page)
}
function goBack(fallback: (...args: any[]) => void): void {
const [ , previousPage ] = pageHistory;
if (previousPage) {
const handler = pageToRouterMap[previousPage?.page];
handler(...previousPage.args);
} else {
if (fallback) fallback();
}
const [ , ...history] = pageHistory;
setPageHistory(cloneDeep(history));
}
const pageToRouterMap: { [key: number] : (...args: any[]) => void } = {
[Page.ActiveScripts]: Router.toActiveScripts,
[Page.Augmentations]: Router.toAugmentations,
[Page.Bladeburner]: Router.toBladeburner,
[Page.Stats]: Router.toStats,
[Page.Corporation]: Router.toCorporation,
[Page.CreateProgram]: Router.toCreateProgram,
[Page.DevMenu]: Router.toDevMenu,
[Page.Faction]: Router.toFaction,
[Page.Factions]: Router.toFactions,
[Page.Options]: Router.toGameOptions,
[Page.Gang]: Router.toGang,
[Page.Hacknet]: Router.toHacknetNodes,
[Page.Milestones]: Router.toMilestones,
[Page.Resleeves]: Router.toResleeves,
[Page.ScriptEditor]: Router.toScriptEditor,
[Page.Sleeves]: Router.toSleeves,
[Page.StockMarket]: Router.toStockMarket,
[Page.Terminal]: Router.toTerminal,
[Page.Tutorial]: Router.toTutorial,
[Page.Job]: Router.toJob,
[Page.City]: Router.toCity,
[Page.Travel]: Router.toTravel,
[Page.BitVerse]: Router.toBitVerse,
[Page.Infiltration]: Router.toInfiltration,
[Page.Work]: Router.toWork,
[Page.BladeburnerCinematic]: Router.toBladeburnerCinematic,
[Page.Location]: Router.toLocation,
[Page.StaneksGift]: Router.toStaneksGift,
[Page.Achievements]: Router.toAchievements,
}
Router = { Router = {
page: () => page, page: () => page,
previousPage: () => { toActiveScripts: () => setPage(Page.ActiveScripts),
const [ , previousPage] = pageHistory; toAugmentations: () => setPage(Page.Augmentations),
return previousPage?.page ?? -1; toBladeburner: () => setPage(Page.Bladeburner),
}, toStats: () => setPage(Page.Stats),
clearHistory: () => setPageHistory([]), toCorporation: () => setPage(Page.Corporation),
toPreviousPage: goBack, toCreateProgram: () => setPage(Page.CreateProgram),
toActiveScripts: () => setCurrentPage(Page.ActiveScripts), toDevMenu: () => setPage(Page.DevMenu),
toAugmentations: () => setCurrentPage(Page.Augmentations),
toBladeburner: () => setCurrentPage(Page.Bladeburner),
toStats: () => setCurrentPage(Page.Stats),
toCorporation: () => setCurrentPage(Page.Corporation),
toCreateProgram: () => setCurrentPage(Page.CreateProgram),
toDevMenu: () => setCurrentPage(Page.DevMenu),
toFaction: (faction?: Faction) => { toFaction: (faction?: Faction) => {
setCurrentPage(Page.Faction, faction); setPage(Page.Faction);
if (faction) setFaction(faction); if (faction) setFaction(faction);
}, },
toFactions: () => setCurrentPage(Page.Factions), toFactions: () => setPage(Page.Factions),
toGameOptions: () => setCurrentPage(Page.Options), toGameOptions: () => setPage(Page.Options),
toGang: () => setCurrentPage(Page.Gang), toGang: () => setPage(Page.Gang),
toHacknetNodes: () => setCurrentPage(Page.Hacknet), toHacknetNodes: () => setPage(Page.Hacknet),
toMilestones: () => setCurrentPage(Page.Milestones), toMilestones: () => setPage(Page.Milestones),
toResleeves: () => setCurrentPage(Page.Resleeves), toResleeves: () => setPage(Page.Resleeves),
toScriptEditor: (files: Record<string, string>, options?: ScriptEditorRouteOptions) => { toScriptEditor: (files: Record<string, string>, options?: ScriptEditorRouteOptions) => {
setEditorOptions({ setEditorOptions({
files, files,
vim: !!options?.vim, vim: !!options?.vim,
}); });
setCurrentPage(Page.ScriptEditor, files, options); setPage(Page.ScriptEditor);
}, },
toSleeves: () => setCurrentPage(Page.Sleeves), toSleeves: () => setPage(Page.Sleeves),
toStockMarket: () => setCurrentPage(Page.StockMarket), toStockMarket: () => setPage(Page.StockMarket),
toTerminal: () => setCurrentPage(Page.Terminal), toTerminal: () => setPage(Page.Terminal),
toTutorial: () => setCurrentPage(Page.Tutorial), toTutorial: () => setPage(Page.Tutorial),
toJob: () => { toJob: () => {
setLocation(Locations[player.companyName]); setLocation(Locations[player.companyName]);
setCurrentPage(Page.Job); setPage(Page.Job);
}, },
toCity: () => { toCity: () => {
setCurrentPage(Page.City); setPage(Page.City);
}, },
toTravel: () => { toTravel: () => {
player.gotoLocation(LocationName.TravelAgency); player.gotoLocation(LocationName.TravelAgency);
setCurrentPage(Page.Travel); setPage(Page.Travel);
}, },
toBitVerse: (flume: boolean, quick: boolean) => { toBitVerse: (flume: boolean, quick: boolean) => {
setFlume(flume); setFlume(flume);
setQuick(quick); setQuick(quick);
setCurrentPage(Page.BitVerse, flume, quick); setPage(Page.BitVerse);
}, },
toInfiltration: (location: Location) => { toInfiltration: (location: Location) => {
setLocation(location); setLocation(location);
setCurrentPage(Page.Infiltration, location); setPage(Page.Infiltration);
},
toWork: () => {
setCurrentPage(Page.Work);
}, },
toWork: () => setPage(Page.Work),
toBladeburnerCinematic: () => { toBladeburnerCinematic: () => {
setCurrentPage(Page.BladeburnerCinematic); setPage(Page.BladeburnerCinematic);
setCinematicText(cinematicText); setCinematicText(cinematicText);
}, },
toLocation: (location: Location) => { toLocation: (location: Location) => {
setLocation(location); setLocation(location);
setCurrentPage(Page.Location, location); setPage(Page.Location);
}, },
toStaneksGift: () => { toStaneksGift: () => {
setCurrentPage(Page.StaneksGift); setPage(Page.StaneksGift);
}, },
toAchievements: () => { toAchievements: () => {
setCurrentPage(Page.Achievements); setPage(Page.Achievements);
}, },
}; };
@ -577,11 +500,7 @@ export function GameRoot({ player, engine, terminal }: IProps): React.ReactEleme
<SnackbarProvider> <SnackbarProvider>
<Overview mode={ITutorial.isRunning ? "tutorial" : "overview"}> <Overview mode={ITutorial.isRunning ? "tutorial" : "overview"}>
{!ITutorial.isRunning ? ( {!ITutorial.isRunning ? (
<CharacterOverview <CharacterOverview save={() => saveObject.saveGame()} killScripts={killAllScripts} />
save={() => saveObject.saveGame()}
killScripts={killAllScripts}
router={Router}
allowBackButton={withSidebar} />
) : ( ) : (
<InteractiveTutorialRoot /> <InteractiveTutorialRoot />
)} )}

@ -17,22 +17,19 @@ import Typography from "@mui/material/Typography";
import Button from "@mui/material/Button"; import Button from "@mui/material/Button";
import IconButton from "@mui/material/IconButton"; import IconButton from "@mui/material/IconButton";
import SaveIcon from "@mui/icons-material/Save"; import SaveIcon from "@mui/icons-material/Save";
import ArrowBackIcon from "@mui/icons-material/ArrowBack";
import ClearAllIcon from "@mui/icons-material/ClearAll"; import ClearAllIcon from "@mui/icons-material/ClearAll";
import { Settings } from "../../Settings/Settings"; import { Settings } from "../../Settings/Settings";
import { use } from "../Context"; import { use } from "../Context";
import { StatsProgressOverviewCell } from "./StatsProgressBar"; import { StatsProgressOverviewCell } from "./StatsProgressBar";
import { BitNodeMultipliers } from "../../BitNode/BitNodeMultipliers"; import { BitNodeMultipliers } from "../../BitNode/BitNodeMultipliers";
import { IRouter, Page } from "../Router";
import { Box, Tooltip } from "@mui/material"; import { Box, Tooltip } from "@mui/material";
import { CONSTANTS } from "../../Constants"; import { CONSTANTS } from "../../Constants";
interface IProps { interface IProps {
save: () => void; save: () => void;
killScripts: () => void; killScripts: () => void;
router: IRouter;
allowBackButton: boolean;
} }
function Intelligence(): React.ReactElement { function Intelligence(): React.ReactElement {
@ -239,7 +236,7 @@ const useStyles = makeStyles((theme: Theme) =>
export { useStyles as characterOverviewStyles }; export { useStyles as characterOverviewStyles };
export function CharacterOverview({ save, killScripts, router, allowBackButton }: IProps): React.ReactElement { export function CharacterOverview({ save, killScripts }: IProps): React.ReactElement {
const [killOpen, setKillOpen] = useState(false); const [killOpen, setKillOpen] = useState(false);
const player = use.Player(); const player = use.Player();
@ -278,9 +275,6 @@ export function CharacterOverview({ save, killScripts, router, allowBackButton }
player.charisma_mult * BitNodeMultipliers.CharismaLevelMultiplier, player.charisma_mult * BitNodeMultipliers.CharismaLevelMultiplier,
); );
const previousPageName =
router.previousPage() < 0 ? "" : Page[router.previousPage() ?? 0].replace(/([a-z])([A-Z])/g, "$1 $2");
return ( return (
<> <>
<Table sx={{ display: "block", m: 1 }}> <Table sx={{ display: "block", m: 1 }}>
@ -464,13 +458,6 @@ export function CharacterOverview({ save, killScripts, router, allowBackButton }
<SaveIcon color={Settings.AutosaveInterval !== 0 ? "primary" : "error"} /> <SaveIcon color={Settings.AutosaveInterval !== 0 ? "primary" : "error"} />
</Tooltip> </Tooltip>
</IconButton> </IconButton>
{allowBackButton && (
<IconButton disabled={!previousPageName} onClick={() => router.toPreviousPage()}>
<Tooltip title={previousPageName ? `Go back to "${previousPageName}"` : ""}>
<ArrowBackIcon />
</Tooltip>
</IconButton>
)}
</Box> </Box>
<Box sx={{ display: "flex", flex: 1, justifyContent: "flex-end", alignItems: "center" }}> <Box sx={{ display: "flex", flex: 1, justifyContent: "flex-end", alignItems: "center" }}>
<IconButton onClick={() => setKillOpen(true)}> <IconButton onClick={() => setKillOpen(true)}>

@ -53,9 +53,6 @@ export interface IRouter {
// toRedPill(): void; // toRedPill(): void;
// toworkInProgress(): void; // toworkInProgress(): void;
page(): Page; page(): Page;
previousPage(): Page;
clearHistory(): void;
toPreviousPage(fallback?: (...args: any[]) => void): void;
toActiveScripts(): void; toActiveScripts(): void;
toAugmentations(): void; toAugmentations(): void;
toBitVerse(flume: boolean, quick: boolean): void; toBitVerse(flume: boolean, quick: boolean): void;

@ -36,12 +36,12 @@ export function WorkInProgressRoot(): React.ReactElement {
const faction = Factions[player.currentWorkFactionName]; const faction = Factions[player.currentWorkFactionName];
if (player.workType == CONSTANTS.WorkTypeFaction) { if (player.workType == CONSTANTS.WorkTypeFaction) {
function cancel(): void { function cancel(): void {
router.toFaction(faction);
player.finishFactionWork(true); player.finishFactionWork(true);
router.toPreviousPage(() => router.toFaction(faction));
} }
function unfocus(): void { function unfocus(): void {
router.toFaction(faction);
player.stopFocusing(); player.stopFocusing();
router.toPreviousPage(() => router.toFaction(faction));
} }
return ( return (
<Grid container direction="column" justifyContent="center" alignItems="center" style={{ minHeight: "100vh" }}> <Grid container direction="column" justifyContent="center" alignItems="center" style={{ minHeight: "100vh" }}>
@ -120,12 +120,13 @@ export function WorkInProgressRoot(): React.ReactElement {
if (player.className !== "") { if (player.className !== "") {
function cancel(): void { function cancel(): void {
player.finishClass(true); player.finishClass(true);
router.toPreviousPage(() => router.toCity()); router.toCity();
} }
function unfocus(): void { function unfocus(): void {
router.toFaction(faction);
router.toCity();
player.stopFocusing(); player.stopFocusing();
router.toPreviousPage(() => router.toCity());
} }
let stopText = ""; let stopText = "";
@ -211,11 +212,11 @@ export function WorkInProgressRoot(): React.ReactElement {
function cancel(): void { function cancel(): void {
player.finishWork(true); player.finishWork(true);
router.toPreviousPage(() => router.toJob()); router.toJob();
} }
function unfocus(): void { function unfocus(): void {
player.stopFocusing(); player.stopFocusing();
router.toPreviousPage(() => router.toJob()); router.toJob();
} }
const position = player.jobs[player.companyName]; const position = player.jobs[player.companyName];
@ -303,11 +304,11 @@ export function WorkInProgressRoot(): React.ReactElement {
if (player.workType == CONSTANTS.WorkTypeCompanyPartTime) { if (player.workType == CONSTANTS.WorkTypeCompanyPartTime) {
function cancel(): void { function cancel(): void {
player.finishWorkPartTime(true); player.finishWorkPartTime(true);
router.toPreviousPage(() => router.toJob()); router.toJob();
} }
function unfocus(): void { function unfocus(): void {
player.stopFocusing(); player.stopFocusing();
router.toPreviousPage(() => router.toJob()); router.toJob();
} }
const comp = Companies[player.companyName]; const comp = Companies[player.companyName];
let companyRep = 0; let companyRep = 0;
@ -439,11 +440,11 @@ export function WorkInProgressRoot(): React.ReactElement {
if (player.createProgramName !== "") { if (player.createProgramName !== "") {
function cancel(): void { function cancel(): void {
player.finishCreateProgramWork(true); player.finishCreateProgramWork(true);
router.toPreviousPage(() => router.toTerminal()); router.toTerminal();
} }
function unfocus(): void { function unfocus(): void {
router.toTerminal();
player.stopFocusing(); player.stopFocusing();
router.toPreviousPage(() => router.toTerminal());
} }
return ( return (
<Grid container direction="column" justifyContent="center" alignItems="center" style={{ minHeight: "100vh" }}> <Grid container direction="column" justifyContent="center" alignItems="center" style={{ minHeight: "100vh" }}>