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";
|
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
|
|
|
|
|
|
|
import { StdButton } from "../../ui/React/StdButton";
|
|
|
|
|
2021-05-04 19:00:38 +02:00
|
|
|
import { numeralWrapper } from "../../ui/numeralFormat";
|
|
|
|
|
2019-04-14 11:08:10 +02:00
|
|
|
import { dialogBoxCreate } from "../../../utils/DialogBox";
|
2021-09-05 01:09:30 +02:00
|
|
|
import { MathComponent } from "mathjax-react";
|
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
|
|
|
|
|
|
|
const inputStyleMarkup = {
|
2021-09-05 01:09:30 +02:00
|
|
|
margin: "5px",
|
|
|
|
height: "26px",
|
|
|
|
};
|
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(
|
|
|
|
<>
|
|
|
|
You just donated <Money money={amt} /> to {fac.name} to gain{" "}
|
|
|
|
{Reputation(repGain)} reputation.
|
|
|
|
</>,
|
|
|
|
);
|
|
|
|
props.rerender();
|
|
|
|
}
|
|
|
|
|
|
|
|
function Status(): React.ReactElement {
|
|
|
|
if (donateAmt === null) return <></>;
|
|
|
|
if (!canDonate()) {
|
|
|
|
if (props.p.money.lt(donateAmt)) return <p>Insufficient funds</p>;
|
|
|
|
return <p>Invalid donate amount entered!</p>;
|
2019-04-14 11:08:10 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
return (
|
|
|
|
<p>
|
|
|
|
This donation will result in{" "}
|
|
|
|
{Reputation(repFromDonation(donateAmt, props.p))} reputation gain
|
|
|
|
</p>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={"faction-work-div"}>
|
|
|
|
<div className={"faction-work-div-wrapper"}>
|
|
|
|
<input
|
|
|
|
className="text-input"
|
|
|
|
onChange={onChange}
|
|
|
|
placeholder={"Donation amount"}
|
|
|
|
style={inputStyleMarkup}
|
|
|
|
disabled={props.disabled}
|
|
|
|
/>
|
|
|
|
<StdButton
|
|
|
|
onClick={donate}
|
|
|
|
text={"Donate Money"}
|
|
|
|
disabled={props.disabled || !canDonate()}
|
|
|
|
/>
|
|
|
|
<Status />
|
|
|
|
{props.disabled ? (
|
|
|
|
<p>
|
|
|
|
Unlocked at {props.favorToDonate} favor with {props.faction.name}
|
|
|
|
</p>
|
|
|
|
) : (
|
|
|
|
<div className="text">
|
|
|
|
<MathComponent
|
|
|
|
tex={String.raw`reputation = \frac{\text{donation amount} \times \text{reputation multiplier}}{10^{${digits}}}`}
|
2021-09-04 07:39:34 +02:00
|
|
|
/>
|
2021-09-05 01:09:30 +02:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2019-04-14 11:08:10 +02:00
|
|
|
}
|