Reduce prop chaining

This commit is contained in:
Snarling 2022-08-20 18:52:18 -04:00
parent 9d5cf89f68
commit 97961d6896
4 changed files with 17 additions and 88 deletions

@ -7,31 +7,14 @@ import { InfoAndPurchases } from "./InfoAndPurchases";
import { StockTickers } from "./StockTickers"; import { StockTickers } from "./StockTickers";
import { IStockMarket } from "../IStockMarket"; import { IStockMarket } from "../IStockMarket";
import { Stock } from "../Stock";
import { OrderTypes } from "../data/OrderTypes";
import { PositionTypes } from "../data/PositionTypes";
import { IPlayer } from "../../PersonObjects/IPlayer"; import { IPlayer } from "../../PersonObjects/IPlayer";
import { EventEmitter } from "../../utils/EventEmitter"; import { EventEmitter } from "../../utils/EventEmitter";
type txFn = (stock: Stock, shares: number) => boolean;
type placeOrderFn = (
stock: Stock,
shares: number,
price: number,
ordType: OrderTypes,
posType: PositionTypes,
) => boolean;
type IProps = { type IProps = {
buyStockLong: txFn;
buyStockShort: txFn;
eventEmitterForReset?: EventEmitter<[]>; eventEmitterForReset?: EventEmitter<[]>;
initStockMarket: () => void; initStockMarket: () => void;
p: IPlayer; p: IPlayer;
placeOrder: placeOrderFn;
sellStockLong: txFn;
sellStockShort: txFn;
stockMarket: IStockMarket; stockMarket: IStockMarket;
}; };
@ -49,16 +32,7 @@ export function StockMarketRoot(props: IProps): React.ReactElement {
<> <>
<InfoAndPurchases initStockMarket={props.initStockMarket} p={props.p} rerender={rerender} /> <InfoAndPurchases initStockMarket={props.initStockMarket} p={props.p} rerender={rerender} />
{props.p.hasWseAccount && ( {props.p.hasWseAccount && (
<StockTickers <StockTickers eventEmitterForReset={props.eventEmitterForReset} p={props.p} stockMarket={props.stockMarket} />
buyStockLong={props.buyStockLong}
buyStockShort={props.buyStockShort}
eventEmitterForReset={props.eventEmitterForReset}
p={props.p}
placeOrder={props.placeOrder}
sellStockLong={props.sellStockLong}
sellStockShort={props.sellStockShort}
stockMarket={props.stockMarket}
/>
)} )}
</> </>
); );

@ -14,6 +14,8 @@ import { Stock } from "../Stock";
import { getBuyTransactionCost, getSellTransactionGain, calculateBuyMaxAmount } from "../StockMarketHelpers"; import { getBuyTransactionCost, getSellTransactionGain, calculateBuyMaxAmount } from "../StockMarketHelpers";
import { OrderTypes } from "../data/OrderTypes"; import { OrderTypes } from "../data/OrderTypes";
import { PositionTypes } from "../data/PositionTypes"; import { PositionTypes } from "../data/PositionTypes";
import { placeOrder } from "../StockMarket";
import { buyStock, shortStock, sellStock, sellShort } from "../BuyingAndSelling";
import { IPlayer } from "../../PersonObjects/IPlayer"; import { IPlayer } from "../../PersonObjects/IPlayer";
import { numeralWrapper } from "../../ui/numeralFormat"; import { numeralWrapper } from "../../ui/numeralFormat";
@ -38,24 +40,10 @@ enum SelectorOrderType {
Stop = "Stop Order", Stop = "Stop Order",
} }
type txFn = (stock: Stock, shares: number) => boolean;
type placeOrderFn = (
stock: Stock,
shares: number,
price: number,
ordType: OrderTypes,
posType: PositionTypes,
) => boolean;
type IProps = { type IProps = {
buyStockLong: txFn;
buyStockShort: txFn;
orders: Order[]; orders: Order[];
p: IPlayer; p: IPlayer;
placeOrder: placeOrderFn;
rerenderAllTickers: () => void; rerenderAllTickers: () => void;
sellStockLong: txFn;
sellStockShort: txFn;
stock: Stock; stock: Stock;
}; };
@ -138,9 +126,9 @@ export function StockTicker(props: IProps): React.ReactElement {
switch (orderType) { switch (orderType) {
case SelectorOrderType.Market: { case SelectorOrderType.Market: {
if (position === PositionTypes.Short) { if (position === PositionTypes.Short) {
props.buyStockShort(props.stock, shares); shortStock(props.stock, shares);
} else { } else {
props.buyStockLong(props.stock, shares); buyStock(props.stock, shares);
} }
props.rerenderAllTickers(); props.rerenderAllTickers();
break; break;
@ -150,7 +138,7 @@ export function StockTicker(props: IProps): React.ReactElement {
setModalProps({ setModalProps({
text: "Enter the price for your Limit Order", text: "Enter the price for your Limit Order",
placeText: "Place Buy Limit Order", placeText: "Place Buy Limit Order",
place: (price: number) => props.placeOrder(props.stock, shares, price, OrderTypes.LimitBuy, position), place: (price: number) => placeOrder(props.stock, shares, price, OrderTypes.LimitBuy, position),
}); });
break; break;
} }
@ -159,7 +147,7 @@ export function StockTicker(props: IProps): React.ReactElement {
setModalProps({ setModalProps({
text: "Enter the price for your Stop Order", text: "Enter the price for your Stop Order",
placeText: "Place Buy Stop Order", placeText: "Place Buy Stop Order",
place: (price: number) => props.placeOrder(props.stock, shares, price, OrderTypes.StopBuy, position), place: (price: number) => placeOrder(props.stock, shares, price, OrderTypes.StopBuy, position),
}); });
break; break;
} }
@ -178,9 +166,9 @@ export function StockTicker(props: IProps): React.ReactElement {
switch (orderType) { switch (orderType) {
case SelectorOrderType.Market: { case SelectorOrderType.Market: {
if (position === PositionTypes.Short) { if (position === PositionTypes.Short) {
props.buyStockShort(stock, maxShares); shortStock(stock, maxShares);
} else { } else {
props.buyStockLong(stock, maxShares); buyStock(stock, maxShares);
} }
props.rerenderAllTickers(); props.rerenderAllTickers();
break; break;
@ -234,9 +222,9 @@ export function StockTicker(props: IProps): React.ReactElement {
switch (orderType) { switch (orderType) {
case SelectorOrderType.Market: { case SelectorOrderType.Market: {
if (position === PositionTypes.Short) { if (position === PositionTypes.Short) {
props.sellStockShort(props.stock, shares); sellShort(props.stock, shares);
} else { } else {
props.sellStockLong(props.stock, shares); sellStock(props.stock, shares);
} }
props.rerenderAllTickers(); props.rerenderAllTickers();
break; break;
@ -246,7 +234,7 @@ export function StockTicker(props: IProps): React.ReactElement {
setModalProps({ setModalProps({
text: "Enter the price for your Limit Order", text: "Enter the price for your Limit Order",
placeText: "Place Sell Limit Order", placeText: "Place Sell Limit Order",
place: (price: number) => props.placeOrder(props.stock, shares, price, OrderTypes.LimitSell, position), place: (price: number) => placeOrder(props.stock, shares, price, OrderTypes.LimitSell, position),
}); });
break; break;
} }
@ -255,7 +243,7 @@ export function StockTicker(props: IProps): React.ReactElement {
setModalProps({ setModalProps({
text: "Enter the price for your Stop Order", text: "Enter the price for your Stop Order",
placeText: "Place Sell Stop Order", placeText: "Place Sell Stop Order",
place: (price: number) => props.placeOrder(props.stock, shares, price, OrderTypes.StopSell, position), place: (price: number) => placeOrder(props.stock, shares, price, OrderTypes.StopSell, position),
}); });
break; break;
} }
@ -270,9 +258,9 @@ export function StockTicker(props: IProps): React.ReactElement {
switch (orderType) { switch (orderType) {
case SelectorOrderType.Market: { case SelectorOrderType.Market: {
if (position === PositionTypes.Short) { if (position === PositionTypes.Short) {
props.sellStockShort(stock, stock.playerShortShares); sellShort(stock, stock.playerShortShares);
} else { } else {
props.sellStockLong(stock, stock.playerShares); sellStock(stock, stock.playerShares);
} }
props.rerenderAllTickers(); props.rerenderAllTickers();
break; break;

@ -10,29 +10,13 @@ import { StockTickersConfig, TickerDisplayMode } from "./StockTickersConfig";
import { IStockMarket } from "../IStockMarket"; import { IStockMarket } from "../IStockMarket";
import { Stock } from "../Stock"; import { Stock } from "../Stock";
import { OrderTypes } from "../data/OrderTypes";
import { PositionTypes } from "../data/PositionTypes";
import { IPlayer } from "../../PersonObjects/IPlayer"; import { IPlayer } from "../../PersonObjects/IPlayer";
import { EventEmitter } from "../../utils/EventEmitter"; import { EventEmitter } from "../../utils/EventEmitter";
type txFn = (stock: Stock, shares: number) => boolean;
type placeOrderFn = (
stock: Stock,
shares: number,
price: number,
ordType: OrderTypes,
posType: PositionTypes,
) => boolean;
type IProps = { type IProps = {
buyStockLong: txFn;
buyStockShort: txFn;
eventEmitterForReset?: EventEmitter<[]>; eventEmitterForReset?: EventEmitter<[]>;
p: IPlayer; p: IPlayer;
placeOrder: placeOrderFn;
sellStockLong: txFn;
sellStockShort: txFn;
stockMarket: IStockMarket; stockMarket: IStockMarket;
}; };
@ -86,18 +70,7 @@ export function StockTickers(props: IProps): React.ReactElement {
} }
tickers.push( tickers.push(
<StockTicker <StockTicker key={val.symbol} orders={orders} p={props.p} rerenderAllTickers={rerender} stock={val} />,
buyStockLong={props.buyStockLong}
buyStockShort={props.buyStockShort}
key={val.symbol}
orders={orders}
p={props.p}
placeOrder={props.placeOrder}
rerenderAllTickers={rerender}
sellStockLong={props.sellStockLong}
sellStockShort={props.sellStockShort}
stock={val}
/>,
); );
} }
} }

@ -17,8 +17,7 @@ import { prestigeAugmentation } from "../Prestige";
import { dialogBoxCreate } from "./React/DialogBox"; import { dialogBoxCreate } from "./React/DialogBox";
import { GetAllServers } from "../Server/AllServers"; import { GetAllServers } from "../Server/AllServers";
import { Factions } from "../Faction/Factions"; import { Factions } from "../Faction/Factions";
import { buyStock, sellStock, shortStock, sellShort } from "../StockMarket/BuyingAndSelling"; import { eventEmitterForUiReset, initStockMarketFn, StockMarket } from "../StockMarket/StockMarket";
import { eventEmitterForUiReset, initStockMarketFn, placeOrder, StockMarket } from "../StockMarket/StockMarket";
import { Theme } from "@mui/material/styles"; import { Theme } from "@mui/material/styles";
import makeStyles from "@mui/styles/makeStyles"; import makeStyles from "@mui/styles/makeStyles";
@ -438,14 +437,9 @@ export function GameRoot({ player, engine, terminal }: IProps): React.ReactEleme
case Page.StockMarket: { case Page.StockMarket: {
mainPage = ( mainPage = (
<StockMarketRoot <StockMarketRoot
buyStockLong={buyStock}
buyStockShort={shortStock}
eventEmitterForReset={eventEmitterForUiReset} eventEmitterForReset={eventEmitterForUiReset}
initStockMarket={initStockMarketFn} initStockMarket={initStockMarketFn}
p={player} p={player}
placeOrder={placeOrder}
sellStockLong={sellStock}
sellStockShort={sellShort}
stockMarket={StockMarket} stockMarket={StockMarket}
/> />
); );