UI: Memoize character overview (#247)

This commit is contained in:
David Walker 2022-12-21 15:27:51 -08:00 committed by GitHub
parent addcee73fc
commit 149d687fd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 258 additions and 276 deletions

@ -432,11 +432,17 @@ export function GameRoot(): React.ReactElement {
<BypassWrapper content={bypassGame ? mainPage : null}> <BypassWrapper content={bypassGame ? mainPage : null}>
<SnackbarProvider> <SnackbarProvider>
<Overview mode={ITutorial.isRunning ? "tutorial" : "overview"}> <Overview mode={ITutorial.isRunning ? "tutorial" : "overview"}>
{!ITutorial.isRunning ? ( {(parentOpen) =>
<CharacterOverview save={() => saveObject.saveGame()} killScripts={killAllScripts} /> !ITutorial.isRunning ? (
<CharacterOverview
parentOpen={parentOpen}
save={() => saveObject.saveGame()}
killScripts={killAllScripts}
/>
) : ( ) : (
<InteractiveTutorialRoot /> <InteractiveTutorialRoot />
)} )
}
</Overview> </Overview>
{withSidebar ? ( {withSidebar ? (
<Box display="flex" flexDirection="row" width="100%"> <Box display="flex" flexDirection="row" width="100%">

@ -1,5 +1,5 @@
// Root React Component for the Corporation UI // Root React Component for the Corporation UI
import React, { useState, useEffect } from "react"; import React, { useMemo, useState, useEffect } from "react";
import { Theme, useTheme } from "@mui/material/styles"; import { Theme, useTheme } from "@mui/material/styles";
import makeStyles from "@mui/styles/makeStyles"; import makeStyles from "@mui/styles/makeStyles";
@ -38,41 +38,11 @@ import { isCompanyWork } from "../../Work/CompanyWork";
import { isCrimeWork } from "../../Work/CrimeWork"; import { isCrimeWork } from "../../Work/CrimeWork";
interface IProps { interface IProps {
parentOpen: boolean;
save: () => void; save: () => void;
killScripts: () => void; killScripts: () => void;
} }
function Intelligence(): React.ReactElement {
const theme = useTheme();
const classes = useStyles();
if (Player.skills.intelligence === 0) return <></>;
const progress = Player.calculateSkillProgress(Player.exp.intelligence);
return (
<>
<TableRow>
<TableCell component="th" scope="row" classes={{ root: classes.cell }}>
<Typography classes={{ root: classes.int }}>Int&nbsp;</Typography>
</TableCell>
<TableCell align="right" classes={{ root: classes.cell }}>
<Typography classes={{ root: classes.int }}>{formatNumber(Player.skills.intelligence, 0)}</Typography>
</TableCell>
<TableCell align="right" classes={{ root: classes.cell }}>
<Typography id="overview-int-hook" classes={{ root: classes.int }}>
{/*Hook for player scripts*/}
</Typography>
</TableCell>
</TableRow>
<TableRow>
{!Settings.DisableOverviewProgressBars && (
<StatsProgressOverviewCell progress={progress} color={theme.colors.int} />
)}
</TableRow>
</>
);
}
function Bladeburner(): React.ReactElement { function Bladeburner(): React.ReactElement {
const classes = useStyles(); const classes = useStyles();
const bladeburner = Player.bladeburner; const bladeburner = Player.bladeburner;
@ -81,11 +51,18 @@ function Bladeburner(): React.ReactElement {
if (action.type === "Idle") return <></>; if (action.type === "Idle") return <></>;
return ( return (
<> <>
{useMemo(
() => (
<TableRow> <TableRow>
<TableCell component="th" scope="row" colSpan={2} classes={{ root: classes.cellNone }}> <TableCell component="th" scope="row" colSpan={2} classes={{ root: classes.cellNone }}>
<Typography>Bladeburner:</Typography> <Typography>Bladeburner:</Typography>
</TableCell> </TableCell>
</TableRow> </TableRow>
),
[classes.cellNone],
)}
{useMemo(
() => (
<TableRow> <TableRow>
<TableCell component="th" scope="row" colSpan={2} classes={{ root: classes.cellNone }}> <TableCell component="th" scope="row" colSpan={2} classes={{ root: classes.cellNone }}>
<Typography> <Typography>
@ -93,6 +70,9 @@ function Bladeburner(): React.ReactElement {
</Typography> </Typography>
</TableCell> </TableCell>
</TableRow> </TableRow>
),
[classes.cellNone, action.type, action.name],
)}
</> </>
); );
} }
@ -101,15 +81,13 @@ interface WorkInProgressOverviewProps {
tooltip: React.ReactNode; tooltip: React.ReactNode;
header: React.ReactNode; header: React.ReactNode;
children: React.ReactNode; children: React.ReactNode;
onClickFocus: () => void;
} }
function WorkInProgressOverview({ const onClickFocusWork = (): void => {
tooltip, Player.startFocusing();
children, Router.toPage(Page.Work);
onClickFocus, };
header, function WorkInProgressOverview({ tooltip, children, header }: WorkInProgressOverviewProps): React.ReactElement {
}: WorkInProgressOverviewProps): React.ReactElement {
const classes = useStyles(); const classes = useStyles();
return ( return (
<> <>
@ -127,22 +105,23 @@ function WorkInProgressOverview({
<Typography className={classes.workSubtitles}>{children}</Typography> <Typography className={classes.workSubtitles}>{children}</Typography>
</TableCell> </TableCell>
</TableRow> </TableRow>
{useMemo(
() => (
<TableRow> <TableRow>
<TableCell component="th" scope="row" align="center" colSpan={2} classes={{ root: classes.cellNone }}> <TableCell component="th" scope="row" align="center" colSpan={2} classes={{ root: classes.cellNone }}>
<Button sx={{ mt: 1 }} onClick={onClickFocus}> <Button sx={{ mt: 1 }} onClick={onClickFocusWork}>
Focus Focus
</Button> </Button>
</TableCell> </TableCell>
</TableRow> </TableRow>
),
[classes.cellNone],
)}
</> </>
); );
} }
function Work(): React.ReactElement { function Work(): React.ReactElement {
const onClickFocus = (): void => {
Player.startFocusing();
Router.toPage(Page.Work);
};
if (Player.currentWork === null || Player.focus) return <></>; if (Player.currentWork === null || Player.focus) return <></>;
let details = <></>; let details = <></>;
@ -219,7 +198,7 @@ function Work(): React.ReactElement {
} }
return ( return (
<WorkInProgressOverview tooltip={details} header={header} onClickFocus={onClickFocus}> <WorkInProgressOverview tooltip={details} header={header}>
{innerText} {innerText}
</WorkInProgressOverview> </WorkInProgressOverview>
); );
@ -275,13 +254,63 @@ const useStyles = makeStyles((theme: Theme) =>
export { useStyles as characterOverviewStyles }; export { useStyles as characterOverviewStyles };
export function CharacterOverview({ save, killScripts }: IProps): React.ReactElement { function rowWithHook(name: string, value: string, className: string, cellNone: string): React.ReactElement {
return useMemo(
() => (
<TableRow>
<TableCell component="th" scope="row" classes={{ root: cellNone }}>
<Typography classes={{ root: className }}>{name}&nbsp;</Typography>
</TableCell>
<TableCell align="right" classes={{ root: cellNone }}>
<Typography classes={{ root: className }}>{value}</Typography>
</TableCell>
<TableCell align="right" classes={{ root: cellNone }}>
<Typography id={"overview-" + name.toLowerCase() + "-str-hook"} classes={{ root: className }}>
{/*Hook for player scripts*/}
</Typography>
</TableCell>
</TableRow>
),
[name, value, className, cellNone],
);
}
function statItem(
name: string,
value: number,
className: string,
cellNone: string,
themeColor: React.CSSProperties["color"],
exp: number,
mult: number,
bitNodeMult: number,
): React.ReactElement[] {
return [
rowWithHook(name, formatNumber(value, 0), className, cellNone),
useMemo(() => {
const progress = Player.calculateSkillProgress(exp, mult * bitNodeMult);
return (
<TableRow>
{!Settings.DisableOverviewProgressBars && (
<StatsProgressOverviewCell progress={progress} color={themeColor} />
)}
</TableRow>
);
}, [Settings.DisableOverviewProgressBars, themeColor, exp, mult, bitNodeMult]),
];
}
export function CharacterOverview({ parentOpen, save, killScripts }: IProps): React.ReactElement {
const [killOpen, setKillOpen] = useState(false); const [killOpen, setKillOpen] = useState(false);
const setRerender = useState(false)[1]; const setRerender = useState(false)[1];
// Don't rerender while the overview is closed.
useEffect(() => { useEffect(() => {
if (parentOpen) {
const id = setInterval(() => setRerender((old) => !old), 600); const id = setInterval(() => setRerender((old) => !old), 600);
return () => clearInterval(id); return () => clearInterval(id);
}, []); }
return () => null;
}, [parentOpen]);
const classes = useStyles(); const classes = useStyles();
const theme = useTheme(); const theme = useTheme();
@ -289,61 +318,21 @@ export function CharacterOverview({ save, killScripts }: IProps): React.ReactEle
Player.exp.hacking, Player.exp.hacking,
Player.mults.hacking * BitNodeMultipliers.HackingLevelMultiplier, Player.mults.hacking * BitNodeMultipliers.HackingLevelMultiplier,
); );
const strengthProgress = Player.calculateSkillProgress(
Player.exp.strength,
Player.mults.strength * BitNodeMultipliers.StrengthLevelMultiplier,
);
const defenseProgress = Player.calculateSkillProgress(
Player.exp.defense,
Player.mults.defense * BitNodeMultipliers.DefenseLevelMultiplier,
);
const dexterityProgress = Player.calculateSkillProgress(
Player.exp.dexterity,
Player.mults.dexterity * BitNodeMultipliers.DexterityLevelMultiplier,
);
const agilityProgress = Player.calculateSkillProgress(
Player.exp.agility,
Player.mults.agility * BitNodeMultipliers.AgilityLevelMultiplier,
);
const charismaProgress = Player.calculateSkillProgress(
Player.exp.charisma,
Player.mults.charisma * BitNodeMultipliers.CharismaLevelMultiplier,
);
return ( return (
<> <>
<Table sx={{ display: "block", m: 1 }}> <Table sx={{ display: "block", m: 1 }}>
<TableBody> <TableBody>
<TableRow> {rowWithHook(
<TableCell component="th" scope="row" classes={{ root: classes.cellNone }}> "HP",
<Typography classes={{ root: classes.hp }}>HP&nbsp;</Typography> numeralWrapper.formatHp(Player.hp.current) + "\u00a0/\u00a0" + numeralWrapper.formatHp(Player.hp.max),
</TableCell> classes.hp,
<TableCell align="right" classes={{ root: classes.cellNone }}> classes.cellNone,
<Typography classes={{ root: classes.hp }}> )}
{numeralWrapper.formatHp(Player.hp.current)}&nbsp;/&nbsp;{numeralWrapper.formatHp(Player.hp.max)} {rowWithHook("Money", numeralWrapper.formatMoney(Player.money), classes.money, classes.cellNone)}
</Typography>
</TableCell>
<TableCell align="right" classes={{ root: classes.cellNone }}>
<Typography id="overview-hp-hook" classes={{ root: classes.hp }}>
{/*Hook for player scripts*/}
</Typography>
</TableCell>
</TableRow>
<TableRow>
<TableCell component="th" scope="row" classes={{ root: classes.cellNone }}>
<Typography classes={{ root: classes.money }}>Money&nbsp;</Typography>
</TableCell>
<TableCell align="right" classes={{ root: classes.cellNone }}>
<Typography classes={{ root: classes.money }}>{numeralWrapper.formatMoney(Player.money)}</Typography>
</TableCell>
<TableCell align="right" classes={{ root: classes.cellNone }}>
<Typography id="overview-money-hook" classes={{ root: classes.money }}>
{/*Hook for player scripts*/}
</Typography>
</TableCell>
</TableRow>
{useMemo(
// Hack is a special-case, because of its overview-hack-hook placement
() => (
<TableRow> <TableRow>
<TableCell component="th" scope="row" classes={{ root: classes.cellNone }}> <TableCell component="th" scope="row" classes={{ root: classes.cellNone }}>
<Typography classes={{ root: classes.hack }}>Hack&nbsp;</Typography> <Typography classes={{ root: classes.hack }}>Hack&nbsp;</Typography>
@ -352,11 +341,21 @@ export function CharacterOverview({ save, killScripts }: IProps): React.ReactEle
<Typography classes={{ root: classes.hack }}>{formatNumber(Player.skills.hacking, 0)}</Typography> <Typography classes={{ root: classes.hack }}>{formatNumber(Player.skills.hacking, 0)}</Typography>
</TableCell> </TableCell>
</TableRow> </TableRow>
),
[Player.skills.hacking, classes.hack, classes.cellNone],
)}
{useMemo(
() => (
<TableRow> <TableRow>
{!Settings.DisableOverviewProgressBars && ( {!Settings.DisableOverviewProgressBars && (
<StatsProgressOverviewCell progress={hackingProgress} color={theme.colors.hack} /> <StatsProgressOverviewCell progress={hackingProgress} color={theme.colors.hack} />
)} )}
</TableRow> </TableRow>
),
[Settings.DisableOverviewProgressBars, Player.exp.hacking, Player.mults.hacking, theme.colors.hack],
)}
{useMemo(
() => (
<TableRow> <TableRow>
<TableCell component="th" scope="row" classes={{ root: classes.cell }}> <TableCell component="th" scope="row" classes={{ root: classes.cell }}>
<Typography classes={{ root: classes.hack }}></Typography> <Typography classes={{ root: classes.hack }}></Typography>
@ -367,104 +366,73 @@ export function CharacterOverview({ save, killScripts }: IProps): React.ReactEle
</Typography> </Typography>
</TableCell> </TableCell>
</TableRow> </TableRow>
),
<TableRow> [classes.cell, classes.hack],
<TableCell component="th" scope="row" classes={{ root: classes.cellNone }}>
<Typography classes={{ root: classes.combat }}>Str&nbsp;</Typography>
</TableCell>
<TableCell align="right" classes={{ root: classes.cellNone }}>
<Typography classes={{ root: classes.combat }}>{formatNumber(Player.skills.strength, 0)}</Typography>
</TableCell>
<TableCell align="right" classes={{ root: classes.cellNone }}>
<Typography id="overview-str-hook" classes={{ root: classes.combat }}>
{/*Hook for player scripts*/}
</Typography>
</TableCell>
</TableRow>
<TableRow>
{!Settings.DisableOverviewProgressBars && (
<StatsProgressOverviewCell progress={strengthProgress} color={theme.colors.combat} />
)} )}
</TableRow>
<TableRow> {statItem(
<TableCell component="th" scope="row" classes={{ root: classes.cellNone }}> "Str",
<Typography classes={{ root: classes.combat }}>Def&nbsp;</Typography> Player.skills.strength,
</TableCell> classes.combat,
<TableCell align="right" classes={{ root: classes.cellNone }}> classes.cellNone,
<Typography classes={{ root: classes.combat }}>{formatNumber(Player.skills.defense, 0)}</Typography> theme.colors.combat,
</TableCell> Player.exp.strength,
<TableCell align="right" classes={{ root: classes.cellNone }}> Player.mults.strength,
<Typography id="overview-def-hook" classes={{ root: classes.combat }}> BitNodeMultipliers.StrengthLevelMultiplier,
{/*Hook for player scripts*/}
</Typography>
</TableCell>
</TableRow>
<TableRow>
{!Settings.DisableOverviewProgressBars && (
<StatsProgressOverviewCell progress={defenseProgress} color={theme.colors.combat} />
)} )}
</TableRow> {statItem(
"Def",
<TableRow> Player.skills.defense,
<TableCell component="th" scope="row" classes={{ root: classes.cellNone }}> classes.combat,
<Typography classes={{ root: classes.combat }}>Dex&nbsp;</Typography> classes.cellNone,
</TableCell> theme.colors.combat,
<TableCell align="right" classes={{ root: classes.cellNone }}> Player.exp.defense,
<Typography classes={{ root: classes.combat }}>{formatNumber(Player.skills.dexterity, 0)}</Typography> Player.mults.defense,
</TableCell> BitNodeMultipliers.DefenseLevelMultiplier,
<TableCell align="right" classes={{ root: classes.cellNone }}>
<Typography id="overview-dex-hook" classes={{ root: classes.combat }}>
{/*Hook for player scripts*/}
</Typography>
</TableCell>
</TableRow>
<TableRow>
{!Settings.DisableOverviewProgressBars && (
<StatsProgressOverviewCell progress={dexterityProgress} color={theme.colors.combat} />
)} )}
</TableRow> {statItem(
"Dex",
<TableRow> Player.skills.dexterity,
<TableCell component="th" scope="row" classes={{ root: classes.cell }}> classes.combat,
<Typography classes={{ root: classes.combat }}>Agi&nbsp;</Typography> classes.cellNone,
</TableCell> theme.colors.combat,
<TableCell align="right" classes={{ root: classes.cell }}> Player.exp.dexterity,
<Typography classes={{ root: classes.combat }}>{formatNumber(Player.skills.agility, 0)}</Typography> Player.mults.dexterity,
</TableCell> BitNodeMultipliers.DexterityLevelMultiplier,
<TableCell align="right" classes={{ root: classes.cell }}>
<Typography id="overview-agi-hook" classes={{ root: classes.combat }}>
{/*Hook for player scripts*/}
</Typography>
</TableCell>
</TableRow>
<TableRow>
{!Settings.DisableOverviewProgressBars && (
<StatsProgressOverviewCell progress={agilityProgress} color={theme.colors.combat} />
)} )}
</TableRow> {statItem(
"Agi",
<TableRow> Player.skills.agility,
<TableCell component="th" scope="row" classes={{ root: classes.cellNone }}> classes.combat,
<Typography classes={{ root: classes.cha }}>Cha&nbsp;</Typography> classes.cellNone,
</TableCell> theme.colors.combat,
<TableCell align="right" classes={{ root: classes.cellNone }}> Player.exp.agility,
<Typography classes={{ root: classes.cha }}>{formatNumber(Player.skills.charisma, 0)}</Typography> Player.mults.agility,
</TableCell> BitNodeMultipliers.AgilityLevelMultiplier,
<TableCell align="right" classes={{ root: classes.cellNone }}>
<Typography id="overview-cha-hook" classes={{ root: classes.cha }}>
{/*Hook for player scripts*/}
</Typography>
</TableCell>
</TableRow>
<TableRow>
{!Settings.DisableOverviewProgressBars && (
<StatsProgressOverviewCell progress={charismaProgress} color={theme.colors.cha} />
)} )}
</TableRow> {statItem(
"Cha",
<Intelligence /> Player.skills.charisma,
classes.cha,
classes.cellNone,
theme.colors.cha,
Player.exp.charisma,
Player.mults.charisma,
BitNodeMultipliers.CharismaLevelMultiplier,
)}
{Player.skills.intelligence !== 0 &&
statItem(
"Int",
Player.skills.intelligence,
classes.int,
classes.cellNone,
theme.colors.int,
Player.exp.intelligence,
1,
1,
)}
{useMemo(
() => (
<TableRow> <TableRow>
<TableCell component="th" scope="row" classes={{ root: classes.cell }}> <TableCell component="th" scope="row" classes={{ root: classes.cell }}>
<Typography id="overview-extra-hook-0" classes={{ root: classes.hack }}> <Typography id="overview-extra-hook-0" classes={{ root: classes.hack }}>
@ -482,10 +450,15 @@ export function CharacterOverview({ save, killScripts }: IProps): React.ReactEle
</Typography> </Typography>
</TableCell> </TableCell>
</TableRow> </TableRow>
),
[classes.cell, classes.hack],
)}
<Work /> <Work />
<Bladeburner /> <Bladeburner />
</TableBody> </TableBody>
</Table> </Table>
{useMemo(
() => (
<Box sx={{ display: "flex", borderTop: `1px solid ${Settings.theme.welllight}` }}> <Box sx={{ display: "flex", borderTop: `1px solid ${Settings.theme.welllight}` }}>
<Box sx={{ display: "flex", flex: 1, justifyContent: "flex-start", alignItems: "center" }}> <Box sx={{ display: "flex", flex: 1, justifyContent: "flex-start", alignItems: "center" }}>
<IconButton aria-label="save game" onClick={save}> <IconButton aria-label="save game" onClick={save}>
@ -502,6 +475,9 @@ export function CharacterOverview({ save, killScripts }: IProps): React.ReactEle
</IconButton> </IconButton>
</Box> </Box>
</Box> </Box>
),
[Settings.theme.welllight, save, Settings.AutosaveInterval],
)}
<KillScriptsModal open={killOpen} onClose={() => setKillOpen(false)} killScripts={killScripts} /> <KillScriptsModal open={killOpen} onClose={() => setKillOpen(false)} killScripts={killScripts} />
</> </>
); );

@ -53,7 +53,7 @@ const useStyles = makeStyles({
}); });
interface IProps { interface IProps {
children: JSX.Element[] | JSX.Element | React.ReactElement[] | React.ReactElement; children: (parentOpen: boolean) => JSX.Element[] | JSX.Element | React.ReactElement[] | React.ReactElement;
mode: "tutorial" | "overview"; mode: "tutorial" | "overview";
} }
@ -141,7 +141,7 @@ export function Overview({ children, mode }: IProps): React.ReactElement {
</Box> </Box>
</Box> </Box>
<Collapse in={open} className={classes.collapse}> <Collapse in={open} className={classes.collapse}>
{children} {children(open)}
</Collapse> </Collapse>
</Paper> </Paper>
</Draggable> </Draggable>