bitburner-src/src/PersonObjects/Sleeve/ui/CovenantSleeveMemoryUpgrade.tsx

97 lines
2.6 KiB
TypeScript
Raw Normal View History

/**
* React component for a panel that lets you purchase upgrades for a Duplicate
* Sleeve's Memory (through The Covenant)
*/
2021-09-27 02:55:38 +02:00
import React, { useState } from "react";
import { Sleeve } from "../Sleeve";
import { IPlayer } from "../../IPlayer";
import { numeralWrapper } from "../../../ui/numeralFormat";
import { Money } from "../../../ui/React/Money";
2021-09-27 02:55:38 +02:00
import Typography from "@mui/material/Typography";
import TextField from "@mui/material/TextField";
import Button from "@mui/material/Button";
import Box from "@mui/material/Box";
import Paper from "@mui/material/Paper";
interface IProps {
2021-09-05 01:09:30 +02:00
index: number;
p: IPlayer;
rerender: () => void;
sleeve: Sleeve;
}
2021-09-27 02:55:38 +02:00
export function CovenantSleeveMemoryUpgrade(props: IProps): React.ReactElement {
const [amt, setAmt] = useState(1);
2021-09-05 01:09:30 +02:00
2021-09-27 02:55:38 +02:00
function changePurchaseAmount(e: React.ChangeEvent<HTMLInputElement>): void {
2021-09-05 01:09:30 +02:00
let n: number = parseInt(e.target.value);
2021-09-05 01:09:30 +02:00
if (isNaN(n)) n = 1;
2021-09-27 02:55:38 +02:00
const maxMemory = 100 - props.sleeve.memory;
2021-09-05 01:09:30 +02:00
if (n > maxMemory) n = maxMemory;
2021-09-27 02:55:38 +02:00
setAmt(n);
2021-09-05 01:09:30 +02:00
}
2021-09-27 02:55:38 +02:00
function getPurchaseCost(): number {
if (isNaN(amt)) {
2021-09-05 01:09:30 +02:00
return Infinity;
}
2021-09-27 02:55:38 +02:00
const maxMemory = 100 - props.sleeve.memory;
if (amt > maxMemory) {
2021-09-05 01:09:30 +02:00
return Infinity;
}
2021-09-27 02:55:38 +02:00
return props.sleeve.getMemoryUpgradeCost(amt);
2021-09-05 01:09:30 +02:00
}
2021-09-27 02:55:38 +02:00
function purchaseMemory(): void {
const cost = getPurchaseCost();
if (props.p.canAfford(cost)) {
props.sleeve.upgradeMemory(amt);
2021-10-27 20:18:33 +02:00
props.p.loseMoney(cost, "sleeves");
2021-09-27 02:55:38 +02:00
props.rerender();
}
2021-09-05 01:09:30 +02:00
}
2021-09-27 02:55:38 +02:00
// Purchase button props
const cost = getPurchaseCost();
const purchaseBtnDisabled = !props.p.canAfford(cost);
let purchaseBtnContent = <></>;
if (isNaN(amt)) {
purchaseBtnContent = <>Invalid value</>;
} else {
purchaseBtnContent = (
<>
Purchase {amt} memory&nbsp;-&nbsp;
<Money money={cost} player={props.p} />
</>
2021-09-05 01:09:30 +02:00
);
}
2021-09-27 02:55:38 +02:00
return (
<Paper sx={{ my: 1, p: 1 }}>
<Typography variant="h6" color="primary">
Upgrade Memory of Sleeve {props.index}
</Typography>
<Typography>
Purchase a memory upgrade for your sleeve. Note that a sleeve's max memory is 100 (current:{" "}
{numeralWrapper.formatSleeveMemory(props.sleeve.memory)})
</Typography>
<Box display="flex" flexDirection="row" alignItems="center">
<Typography>Amount of memory to purchase (must be an integer):&nbsp;</Typography>
2021-09-29 07:49:22 +02:00
<TextField onChange={changePurchaseAmount} type={"number"} value={amt} />
2021-09-27 02:55:38 +02:00
</Box>
<br />
<Button disabled={purchaseBtnDisabled} onClick={purchaseMemory}>
{purchaseBtnContent}
</Button>
</Paper>
);
}