bitburner-src/src/Faction/ui/DonateOption.tsx

110 lines
3.4 KiB
TypeScript
Raw Normal View History

2019-04-14 11:08:10 +02:00
/**
* React component for a donate option on the Faction UI
*/
2021-09-04 07:39:34 +02:00
import React, { useState } from "react";
2019-04-14 11:08:10 +02:00
import { CONSTANTS } from "../../Constants";
import { Faction } from "../../Faction/Faction";
import { IPlayer } from "../../PersonObjects/IPlayer";
2021-09-04 07:39:34 +02:00
import { repFromDonation } from "../formulas/donation";
2021-09-21 02:42:13 +02:00
import { Favor } from "../../ui/React/Favor";
2019-04-14 11:08:10 +02:00
import { Money } from "../../ui/React/Money";
import { Reputation } from "../../ui/React/Reputation";
2019-04-14 11:08:10 +02:00
import { numeralWrapper } from "../../ui/numeralFormat";
2021-09-25 20:42:57 +02:00
import { dialogBoxCreate } from "../../ui/React/DialogBox";
2021-10-13 08:15:29 +02:00
import { MathJax, MathJaxContext } from "better-react-mathjax";
2019-04-14 11:08:10 +02:00
2021-09-21 02:42:13 +02:00
import Typography from "@mui/material/Typography";
import Paper from "@mui/material/Paper";
import Button from "@mui/material/Button";
import TextField from "@mui/material/TextField";
2019-04-14 11:08:10 +02:00
type IProps = {
2021-09-05 01:09:30 +02:00
faction: Faction;
disabled: boolean;
favorToDonate: number;
p: IPlayer;
rerender: () => void;
};
2019-04-14 11:08:10 +02:00
2021-09-04 07:39:34 +02:00
export function DonateOption(props: IProps): React.ReactElement {
2021-09-05 01:09:30 +02:00
const [donateAmt, setDonateAmt] = useState<number | null>(null);
const digits = (CONSTANTS.DonateMoneyToRepDivisor + "").length - 1;
function canDonate(): boolean {
if (donateAmt === null) return false;
if (isNaN(donateAmt) || donateAmt <= 0) return false;
if (props.p.money.lt(donateAmt)) return false;
return true;
}
function onChange(event: React.ChangeEvent<HTMLInputElement>): void {
const amt = numeralWrapper.parseMoney(event.target.value);
if (event.target.value === "" || isNaN(amt)) setDonateAmt(null);
else setDonateAmt(amt);
}
function donate(): void {
const fac = props.faction;
const amt = donateAmt;
if (amt === null) return;
if (!canDonate()) return;
props.p.loseMoney(amt);
const repGain = repFromDonation(amt, props.p);
props.faction.playerReputation += repGain;
dialogBoxCreate(
<>
2021-10-01 19:08:37 +02:00
You just donated <Money money={amt} /> to {fac.name} to gain <Reputation reputation={repGain} /> reputation.
2021-09-05 01:09:30 +02:00
</>,
);
props.rerender();
}
function Status(): React.ReactElement {
if (donateAmt === null) return <></>;
if (!canDonate()) {
2021-09-21 02:42:13 +02:00
if (props.p.money.lt(donateAmt)) return <Typography>Insufficient funds</Typography>;
return <Typography>Invalid donate amount entered!</Typography>;
2019-04-14 11:08:10 +02:00
}
2021-09-21 02:42:13 +02:00
return (
<Typography>
2021-10-01 19:08:37 +02:00
This donation will result in <Reputation reputation={repFromDonation(donateAmt, props.p)} /> reputation gain
2021-09-21 02:42:13 +02:00
</Typography>
);
2021-09-05 01:09:30 +02:00
}
return (
2021-09-21 02:42:13 +02:00
<Paper sx={{ my: 1, p: 1, width: "100%" }}>
<Status />
{props.disabled ? (
<Typography>
2021-10-01 19:08:37 +02:00
Unlock donations at <Favor favor={props.favorToDonate} /> favor with {props.faction.name}
2021-09-21 02:42:13 +02:00
</Typography>
) : (
<>
<TextField
onChange={onChange}
placeholder={"Donation amount"}
disabled={props.disabled}
InputProps={{
endAdornment: (
<Button onClick={donate} disabled={props.disabled || !canDonate()}>
donate
</Button>
),
}}
/>
<Typography>
2021-10-13 08:15:29 +02:00
<MathJaxContext>
<MathJax>{String.raw`reputation = \frac{\text{donation amount} \times \text{reputation multiplier}}{10^{${digits}}}`}</MathJax>
</MathJaxContext>
2021-09-21 02:42:13 +02:00
</Typography>
</>
)}
</Paper>
2021-09-05 01:09:30 +02:00
);
2019-04-14 11:08:10 +02:00
}