From 717b32b0b4111ffd9b2d9df42a74e40e70fbb13d Mon Sep 17 00:00:00 2001 From: Olivier Gagnon Date: Sat, 28 Aug 2021 14:03:07 -0400 Subject: [PATCH] more conversion --- src/Corporation/ui/BuybackSharesPopup.tsx | 87 +++++++++++++++++++ .../ui/CorporationUIEventHandler.js | 76 ---------------- src/Corporation/ui/Overview.tsx | 12 ++- 3 files changed, 98 insertions(+), 77 deletions(-) create mode 100644 src/Corporation/ui/BuybackSharesPopup.tsx diff --git a/src/Corporation/ui/BuybackSharesPopup.tsx b/src/Corporation/ui/BuybackSharesPopup.tsx new file mode 100644 index 000000000..45af985b2 --- /dev/null +++ b/src/Corporation/ui/BuybackSharesPopup.tsx @@ -0,0 +1,87 @@ +import React, { useState } from 'react'; +import { IPlayer } from "../../PersonObjects/IPlayer"; +import { removePopup } from "../../ui/React/createPopup"; +import { numeralWrapper } from "../../ui/numeralFormat"; +import { dialogBoxCreate } from "../../../utils/DialogBox"; +import { createElement } from "../../../utils/uiHelpers/createElement"; + +interface IProps { + player: IPlayer, + popupId: string, + corp: any, +} + +// Create a popup that lets the player buyback shares +// This is created when the player clicks the "Buyback Shares" button in the overview panel +export function BuybackSharesPopup(props: IProps): React.ReactElement { + const [shares, setShares] = useState(null); + + function changeShares(event: React.ChangeEvent): void { + if(event.target.value === "") setShares(null); + else setShares(Math.round(parseFloat(event.target.value))); + } + + const currentStockPrice = props.corp.sharePrice; + const buybackPrice = currentStockPrice * 1.1; + + function buy() { + if(shares === null) return; + const tempStockPrice = props.corp.sharePrice; + const buybackPrice = tempStockPrice * 1.1; + if (isNaN(shares) || shares <= 0) { + dialogBoxCreate("ERROR: Invalid value for number of shares"); + } else if (shares > props.corp.issuedShares) { + dialogBoxCreate("ERROR: There are not this many oustanding shares to buy back"); + } else if (shares * buybackPrice > props.player.money) { + dialogBoxCreate("ERROR: You do not have enough money to purchase this many shares (you need " + + numeralWrapper.format(shares * buybackPrice, "$0.000a") + ")"); + } else { + props.corp.numShares += shares; + if (isNaN(props.corp.issuedShares)) { + console.warn("Corporation issuedShares is NaN: " + props.corp.issuedShares); + console.warn("Converting to number now"); + const res = parseInt(props.corp.issuedShares); + if (isNaN(res)) { + props.corp.issuedShares = 0; + } else { + props.corp.issuedShares = res; + } + } + props.corp.issuedShares -= shares; + props.player.loseMoney(shares * buybackPrice); + removePopup(props.popupId); + props.corp.rerender(); + } + } + + function CostIndicator(): React.ReactElement { + if(shares === null) return (<>); + if (isNaN(shares) || shares <= 0) { + return (<>ERROR: Invalid value entered for number of shares to buyback); + } else if (shares > props.corp.issuedShares) { + return (<>There are not this many shares available to buy back. + There are only {numeralWrapper.formatBigNumber(props.corp.issuedShares)} outstanding shares.); + } else { + return (<>Purchase {shares} shares for a total of {numeralWrapper.formatMoney(shares * buybackPrice)}); + } + } + + function onKeyDown(event: React.KeyboardEvent) { + if (event.keyCode === 13) buy(); + } + + return (<> +

+Enter the number of outstanding shares you would like to buy back. +These shares must be bought at a 10% premium. However, +repurchasing shares from the market tends to lead to an increase in stock price.

+To purchase these shares, you must use your own money (NOT your Corporation's funds).

+The current buyback price of your company's stock is {numeralWrapper.formatMoney(buybackPrice)}. +Your company currently has {numeralWrapper.formatBigNumber(props.corp.issuedShares)} outstanding stock shares. +

+ +
+ + + ); +} \ No newline at end of file diff --git a/src/Corporation/ui/CorporationUIEventHandler.js b/src/Corporation/ui/CorporationUIEventHandler.js index f56b80a34..3b3013d09 100644 --- a/src/Corporation/ui/CorporationUIEventHandler.js +++ b/src/Corporation/ui/CorporationUIEventHandler.js @@ -56,82 +56,6 @@ export class CorporationEventHandler { this.routing = routing; } - // Create a popup that lets the player buyback shares - // This is created when the player clicks the "Buyback Shares" button in the overview panel - createBuybackSharesPopup() { - const popupId = "cmpy-mgmt-buyback-shares-popup"; - const currentStockPrice = this.corp.sharePrice; - const buybackPrice = currentStockPrice * 1.1; - const txt = createElement("p", { - innerHTML: "Enter the number of outstanding shares you would like to buy back. " + - "These shares must be bought at a 10% premium. However, " + - "repurchasing shares from the market tends to lead to an increase in stock price.

" + - "To purchase these shares, you must use your own money (NOT your Corporation's funds).

" + - "The current buyback price of your company's stock is " + - numeralWrapper.format(buybackPrice, "$0.000a") + - ". Your company currently has " + numeralWrapper.formatBigNumber(this.corp.issuedShares) + " outstanding stock shares", - }); - var costIndicator = createElement("p", {}); - var input = createElement("input", { - type:"number", placeholder:"Shares to buyback", margin:"5px", - inputListener: ()=> { - var numShares = Math.round(input.value); - if (isNaN(numShares) || numShares <= 0) { - costIndicator.innerText = "ERROR: Invalid value entered for number of shares to buyback" - } else if (numShares > this.corp.issuedShares) { - costIndicator.innerText = "There are not this many shares available to buy back. " + - "There are only " + numeralWrapper.formatBigNumber(this.corp.issuedShares) + - " outstanding shares."; - } else { - costIndicator.innerText = "Purchase " + numShares + " shares for a total of " + - numeralWrapper.format(numShares * buybackPrice, '$0.000a'); - } - }, - }); - var confirmBtn = createElement("button", { - class:"a-link-button", innerText:"Buy shares", display:"inline-block", - clickListener: () => { - var shares = Math.round(input.value); - const tempStockPrice = this.corp.sharePrice; - const buybackPrice = tempStockPrice * 1.1; - if (isNaN(shares) || shares <= 0) { - dialogBoxCreate("ERROR: Invalid value for number of shares"); - } else if (shares > this.corp.issuedShares) { - dialogBoxCreate("ERROR: There are not this many oustanding shares to buy back"); - } else if (shares * buybackPrice > Player.money) { - dialogBoxCreate("ERROR: You do not have enough money to purchase this many shares (you need " + - numeralWrapper.format(shares * buybackPrice, "$0.000a") + ")"); - } else { - this.corp.numShares += shares; - if (isNaN(this.corp.issuedShares)) { - console.warn("Corporation issuedShares is NaN: " + this.corp.issuedShares); - console.warn("Converting to number now"); - const res = parseInt(this.corp.issuedShares); - if (isNaN(res)) { - this.corp.issuedShares = 0; - } else { - this.corp.issuedShares = res; - } - } - this.corp.issuedShares -= shares; - Player.loseMoney(shares * buybackPrice); - removeElementById(popupId); - this.rerender(); - } - return false; - - }, - }); - var cancelBtn = createPopupCloseButton(popupId, { - class: "std-button", - display: "inline-block", - innerText: "Cancel", - }); - - createPopup(popupId, [txt, costIndicator, input, confirmBtn, cancelBtn]); - input.focus(); - } - // Create a popup that lets the player discontinue a product createDiscontinueProductPopup(product, industry) { const popupId = "cmpy-mgmt-discontinue-product-popup"; diff --git a/src/Corporation/ui/Overview.tsx b/src/Corporation/ui/Overview.tsx index 58b48bc4e..7305d97e8 100644 --- a/src/Corporation/ui/Overview.tsx +++ b/src/Corporation/ui/Overview.tsx @@ -4,6 +4,7 @@ import { LevelableUpgrade } from "./LevelableUpgrade"; import { UnlockUpgrade } from "./UnlockUpgrade"; import { BribeFactionPopup } from "./BribeFactionPopup"; import { SellSharesPopup } from "./SellSharesPopup"; +import { BuybackSharesPopup } from "./BuybackSharesPopup"; import { CorporationConstants } from "../data/Constants"; import { CorporationUnlockUpgrades } from "../data/CorporationUnlockUpgrades"; @@ -227,10 +228,19 @@ export function Overview(props: IProps): React.ReactElement { tooltip: sellSharesTooltip, }); + function openBuybackSharesPopup() { + const popupId = "corp-buyback-shares-popup"; + createPopup(popupId, BuybackSharesPopup, { + player: props.player, + popupId: popupId, + corp: props.corp, + }); + } + const buybackSharesBtn = createButton({ class: "std-button", display: "inline-block", - onClick: props.eventHandler.createBuybackSharesPopup, + onClick: openBuybackSharesPopup, text: "Buyback shares", tooltip: "Buy back shares you that previously issued or sold at market price.", });