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

116 lines
3.5 KiB
TypeScript
Raw Normal View History

2019-04-14 11:08:10 +02:00
/**
* React component for a donate option on the Faction UI
*/
import * as React from "react";
import { CONSTANTS } from "../../Constants";
import { Faction } from "../../Faction/Faction";
import { IPlayer } from "../../PersonObjects/IPlayer";
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";
import { numeralWrapper } from "../../ui/numeralFormat";
2019-04-14 11:08:10 +02:00
import { dialogBoxCreate } from "../../../utils/DialogBox";
type IProps = {
faction: Faction;
disabled: boolean;
favorToDonate: number;
2019-04-14 11:08:10 +02:00
p: IPlayer;
rerender: () => void;
}
type IState = {
donateAmt: number;
status: JSX.Element;
2019-04-14 11:08:10 +02:00
}
const inputStyleMarkup = {
margin: "5px",
}
export class DonateOption extends React.Component<IProps, IState> {
// Style markup for block elements. Stored as property
2021-05-01 09:17:31 +02:00
blockStyle: any = { display: "block" };
2019-04-14 11:08:10 +02:00
constructor(props: IProps) {
super(props);
2019-04-14 11:08:10 +02:00
this.state = {
donateAmt: 0,
status: props.disabled ? <>Unlocked at {props.favorToDonate} favor with {props.faction.name}</> : <></>,
2019-04-14 11:08:10 +02:00
}
this.calculateRepGain = this.calculateRepGain.bind(this);
this.donate = this.donate.bind(this);
this.handleChange = this.handleChange.bind(this);
}
// Returns rep gain for a given donation amount
calculateRepGain(amt: number): number {
return amt / CONSTANTS.DonateMoneyToRepDivisor * this.props.p.faction_rep_mult;
2019-04-14 11:08:10 +02:00
}
donate(): void {
const fac = this.props.faction;
const amt = this.state.donateAmt;
if (isNaN(amt) || amt <= 0) {
dialogBoxCreate(`Invalid amount entered!`);
} else if (!this.props.p.canAfford(amt)) {
dialogBoxCreate(`You cannot afford to donate this much money!`);
} else {
this.props.p.loseMoney(amt);
const repGain = this.calculateRepGain(amt);
2019-04-14 11:08:10 +02:00
this.props.faction.playerReputation += repGain;
dialogBoxCreate(<>
You just donated {Money(amt)} to {fac.name} to gain {Reputation(repGain)} reputation
</>);
2019-04-14 11:08:10 +02:00
this.props.rerender();
}
}
handleChange(e: React.ChangeEvent<HTMLInputElement>): void {
const amt = numeralWrapper.parse(e.target.value);
2019-04-14 11:08:10 +02:00
if (isNaN(amt)) {
this.setState({
donateAmt: 0,
status: <>Invalid donate amount entered!</>,
2019-04-14 11:08:10 +02:00
});
} else {
const repGain = this.calculateRepGain(amt);
2019-04-14 11:08:10 +02:00
this.setState({
donateAmt: amt,
status: <>This donation will result in {Reputation(repGain)} reputation gain</>,
});
2019-04-14 11:08:10 +02:00
}
}
2021-05-01 09:17:31 +02:00
render(): React.ReactNode {
2019-04-14 11:08:10 +02:00
return (
<div className={"faction-work-div"}>
<div className={"faction-work-div-wrapper"}>
<input
className="text-input"
onChange={this.handleChange}
placeholder={"Donation amount"}
style={inputStyleMarkup}
disabled={this.props.disabled}
/>
2019-04-14 11:08:10 +02:00
<StdButton
onClick={this.donate}
text={"Donate Money"}
disabled={this.props.disabled}
2019-04-14 11:08:10 +02:00
/>
<p style={this.blockStyle}>{this.state.status}</p>
2019-04-14 11:08:10 +02:00
</div>
</div>
)
}
}