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";
|
2022-03-06 05:05:55 +01:00
|
|
|
import { Faction } from "../Faction";
|
2022-10-10 00:42:14 +02:00
|
|
|
import { Player } from "@player";
|
2023-06-19 09:55:03 +02:00
|
|
|
import { canDonate, donate, 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
|
|
|
|
2021-03-31 06:45:21 +02:00
|
|
|
import { Money } from "../../ui/React/Money";
|
|
|
|
import { Reputation } from "../../ui/React/Reputation";
|
2019-04-14 11:08:10 +02:00
|
|
|
|
2021-09-25 20:42:57 +02:00
|
|
|
import { dialogBoxCreate } from "../../ui/React/DialogBox";
|
2023-01-03 13:38:14 +01:00
|
|
|
import { MathJax } 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";
|
2022-05-28 02:41:14 +02:00
|
|
|
import { NumberInput } from "../../ui/React/NumberInput";
|
2021-09-21 02:42:13 +02:00
|
|
|
|
2023-06-19 09:55:03 +02:00
|
|
|
type DonateOptionProps = {
|
2021-09-05 01:09:30 +02:00
|
|
|
faction: Faction;
|
|
|
|
disabled: boolean;
|
|
|
|
favorToDonate: number;
|
|
|
|
rerender: () => void;
|
2023-06-19 09:55:03 +02:00
|
|
|
};
|
2019-04-14 11:08:10 +02:00
|
|
|
|
2022-10-04 12:40:10 +02:00
|
|
|
/** React component for a donate option on the Faction UI */
|
2023-06-19 09:55:03 +02:00
|
|
|
export function DonateOption({ faction, favorToDonate, disabled, rerender }: DonateOptionProps): React.ReactElement {
|
2022-05-28 02:41:14 +02:00
|
|
|
const [donateAmt, setDonateAmt] = useState<number>(NaN);
|
2021-09-05 01:09:30 +02:00
|
|
|
const digits = (CONSTANTS.DonateMoneyToRepDivisor + "").length - 1;
|
|
|
|
|
2023-06-19 09:55:03 +02:00
|
|
|
function onDonate(): void {
|
|
|
|
const repGain = donate(donateAmt, faction);
|
|
|
|
if (repGain > 0) {
|
|
|
|
dialogBoxCreate(
|
|
|
|
<>
|
|
|
|
You just donated <Money money={donateAmt} /> to {faction.name} to gain <Reputation reputation={repGain} />{" "}
|
|
|
|
reputation.
|
|
|
|
</>,
|
|
|
|
);
|
|
|
|
rerender();
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function Status(): React.ReactElement {
|
2022-07-21 21:32:54 +02:00
|
|
|
if (isNaN(donateAmt)) return <></>;
|
2023-06-19 09:55:03 +02:00
|
|
|
if (!canDonate(donateAmt)) {
|
2022-09-06 15:07:12 +02:00
|
|
|
if (Player.money < donateAmt) return <Typography>Insufficient funds</Typography>;
|
2021-09-21 02:42:13 +02:00
|
|
|
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>
|
2022-09-06 15:07:12 +02:00
|
|
|
This donation will result in <Reputation reputation={repFromDonation(donateAmt, Player)} /> reputation gain
|
2021-09-21 02:42:13 +02:00
|
|
|
</Typography>
|
|
|
|
);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-01-21 23:15:41 +01:00
|
|
|
<Paper sx={{ my: 1, p: 1 }}>
|
2021-09-21 02:42:13 +02:00
|
|
|
<Status />
|
2023-06-19 09:55:03 +02:00
|
|
|
{disabled ? (
|
2021-09-21 02:42:13 +02:00
|
|
|
<Typography>
|
2023-06-19 09:55:03 +02:00
|
|
|
Unlock donations at <Favor favor={favorToDonate} /> favor with {faction.name}
|
2021-09-21 02:42:13 +02:00
|
|
|
</Typography>
|
|
|
|
) : (
|
|
|
|
<>
|
2022-05-28 02:41:14 +02:00
|
|
|
<NumberInput
|
|
|
|
onChange={setDonateAmt}
|
2021-09-21 02:42:13 +02:00
|
|
|
placeholder={"Donation amount"}
|
2023-06-19 09:55:03 +02:00
|
|
|
disabled={disabled}
|
2021-09-21 02:42:13 +02:00
|
|
|
InputProps={{
|
|
|
|
endAdornment: (
|
2023-06-19 09:55:03 +02:00
|
|
|
<Button onClick={onDonate} disabled={disabled || !canDonate(donateAmt)}>
|
2021-09-21 02:42:13 +02:00
|
|
|
donate
|
|
|
|
</Button>
|
|
|
|
),
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Typography>
|
2023-01-03 13:38:14 +01:00
|
|
|
<MathJax>{`\\(reputation = \\frac{\\text{donation amount} \\cdot \\text{reputation multiplier}}{10^{${digits}}}\\)`}</MathJax>
|
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
|
|
|
}
|