Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit and Market Order Localization #413

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/game/interface/details/marketplace/DepthChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import useOrderList from '~/hooks/useOrderList';
import useScreenSize from '~/hooks/useScreenSize';
import useStore from '~/hooks/useStore';
import formatters from '~/lib/formatters';
import { formatFixed, formatPrice, getCrewAbilityBonuses, nativeBool } from '~/lib/utils';
import { formatFixed, formatPrice, fromLocal, getCrewAbilityBonuses, nativeBool } from '~/lib/utils';
import theme, { hexToRGB } from '~/theme';
import { formatResourceAmount } from '../../hud/actionDialogs/components';

Expand Down Expand Up @@ -547,11 +547,8 @@ const MarketplaceDepthChart = ({ lot, marketplace, marketplaceOwner, resource })
}, [baseMarketplaceFee, feeReductionBonus, feeEnforcementBonus]);

const [quantity, setQuantity] = useState();
const [limitPrice, setLimitPrice] = useState();
const [limitPrice, setLimitPrice] = useState(0);

useEffect(() => {
setLimitPrice(0);
}, [mode, type]);

const createOrder = useCallback(() => {
onSetAction('MARKETPLACE_ORDER', {
Expand All @@ -576,7 +573,9 @@ const MarketplaceDepthChart = ({ lot, marketplace, marketplaceOwner, resource })

const handleChangeLimitPrice = useCallback((price, blur = false) => {
if (blur) {
if ((mode === 'buy' && price >= ask) || (mode === 'sell' && price <= bid)) {
// convert local formatted price to standard value
const _price = fromLocal(price);
if ((mode === 'buy' && _price >= ask) || (mode === 'sell' && _price <= bid)) {
setType('market');
return;
}
Expand All @@ -602,7 +601,7 @@ const MarketplaceDepthChart = ({ lot, marketplace, marketplaceOwner, resource })
}, [buyOrders, mode, quantity, sellOrders]);

const totalLimitPrice = useMemo(() => {
return (limitPrice || 0) * quantity;
return (fromLocal(limitPrice) || 0) * quantity;
}, [limitPrice, quantity]);

const fee = useMemo(() => {
Expand Down Expand Up @@ -671,6 +670,8 @@ const MarketplaceDepthChart = ({ lot, marketplace, marketplaceOwner, resource })
history.push(`/marketplace/${asteroidId}/${lotIndex}`);
}, [lot]);

console.log('avgMarketPrice: ', avgMarketPrice);

return (
<Wrapper>
<Main>
Expand Down Expand Up @@ -871,7 +872,8 @@ const MarketplaceDepthChart = ({ lot, marketplace, marketplaceOwner, resource })
onChange={(e) => handleChangeLimitPrice(e.currentTarget.value)}
onBlur={(e) => handleChangeLimitPrice(e.currentTarget.value, true)}
placeholder="Specify Price"
value={limitPrice || ''} />
type="string"
value={limitPrice} />
</TextInputWrapper>
</FormSection>
)}
Expand Down
32 changes: 15 additions & 17 deletions src/game/interface/hud/actionDialogs/MarketplaceOrder.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import useOrderList from '~/hooks/useOrderList';
import useSwayBalance from '~/hooks/useSwayBalance';
import formatters from '~/lib/formatters';
import actionStages from '~/lib/actionStages';
import { reactBool, formatFixed, formatTimer, getCrewAbilityBonuses, locationsArrToObj, formatPrice } from '~/lib/utils';
import { reactBool, formatFixed, toLocale, fromLocal, formatTimer, getCrewAbilityBonuses, locationsArrToObj, formatPrice } from '~/lib/utils';
import theme, { hexToRGB } from '~/theme';
import { ActionDialogInner, useAsteroidAndLot } from '../ActionDialog';
import {
Expand Down Expand Up @@ -198,6 +198,7 @@ const MarketplaceOrder = ({
preselect,
...props
}) => {
console.log('MarketplaceOrder, preselect: ', preselect);
const resource = Product.TYPES[resourceId] || {};
const resourceByMass = !resource?.isAtomic;
const { data: exchangeController } = useHydratedCrew(exchange.Control?.controller?.id);
Expand Down Expand Up @@ -336,7 +337,7 @@ const MarketplaceOrder = ({
}, [buyOrders, exchange, feeEnforcementBonus, feeReductionBonus, mode, quantity, sellOrders, type]);

const totalLimitPrice = useMemo(() => {
return (limitPrice || 0) * quantity;
return (fromLocal(limitPrice) || 0) * quantity;
}, [limitPrice, quantity]);

// maker = limit order
Expand All @@ -363,7 +364,7 @@ const MarketplaceOrder = ({
? undefined
: {
label: type === 'limit' ? 'Maker Fee' : 'Taker Fee',
value: <><SwayIcon /> {feeTotal.toLocaleString()} ({formatFixed(100 * feeRate, 1)}%)</>,
value: <><SwayIcon /> {feeTotal.toLocaleString()} ({toLocale(100 * feeRate, 1)}%)</>,
direction: feeRate === baseFeeRate ? 0 : (feeRate > baseFeeRate ? -1 : 1),
isTimeStat: true, // (to reverse direction of good/bad)
tooltip: feeRate !== baseFeeRate && (
Expand Down Expand Up @@ -446,7 +447,7 @@ const MarketplaceOrder = ({
cancelBuyOrder({
amount: quantityToUnits(quantity),
buyer: { id: crew?.id, label: crew?.label },
price: limitPrice,
price: fromLocal(limitPrice),
product: resourceId,
destination: { id: storage?.id, label: storage?.label },
destinationSlot: storageInventory?.slot,
Expand All @@ -458,7 +459,7 @@ const MarketplaceOrder = ({
amount: quantityToUnits(quantity),
seller: { id: crew?.id, label: crew?.label },
product: resourceId,
price: limitPrice,
price: fromLocal(limitPrice),
origin: { id: storage?.id, label: storage?.label },
originSlot: storageInventory?.slot,
})
Expand All @@ -485,7 +486,7 @@ const MarketplaceOrder = ({
const vars = {
product: resourceId,
amount: quantityToUnits(quantity),
price: limitPrice
price: fromLocal(limitPrice)
};
if (mode === 'buy') {
createBuyOrder({
Expand Down Expand Up @@ -524,15 +525,11 @@ const MarketplaceOrder = ({
setQuantity(input);
}, [mode, totalForSale, totalForBuy, type]);

const handleChangeLimitPrice = useCallback((e) => {
setLimitPrice(Number(e.currentTarget.value));
}, []);

const matchBestLimitOrder = useCallback((e) => {
if (mode === 'buy') {
setLimitPrice(buyOrders[0]?.price || sellOrders[sellOrders.length - 1].price);
setLimitPrice(toLocale(buyOrders[0]?.price || sellOrders[sellOrders.length - 1].price));
} else {
setLimitPrice(sellOrders[sellOrders.length - 1].price);
setLimitPrice(toLocale(sellOrders[sellOrders.length - 1].price));
}
}, [mode, buyOrders, sellOrders]);

Expand Down Expand Up @@ -677,7 +674,7 @@ const MarketplaceOrder = ({
<InputLabel>
<label>{type === 'market' ? '' : 'Max'} Quantity</label>
{type === 'market' && (
<span>Max <b>{((mode === 'buy' ? totalForSale : totalForBuy) || 0).toLocaleString()}{resourceByMass ? ' kg' : ''}</b></span>
<span>Max <b>{(toLocale(mode === 'buy' ? totalForSale : totalForBuy) || 0)}{resourceByMass ? ' kg' : ''}</b></span>
)}
{type === 'limit' && mode === 'sell' && (
<span>In Inventory <b>{formatResourceAmount(amountInInventory || 0, resourceId)}</b></span>
Expand Down Expand Up @@ -705,9 +702,10 @@ const MarketplaceOrder = ({
disabled={isCancellation || type === 'market' || stage !== actionStages.NOT_STARTED}
style={type === 'market' ? { backgroundColor: `rgba(${hexToRGB(theme.colors.disabledBackground)}, 0.2)` } : {}}
min={0}
onChange={handleChangeLimitPrice}
type="number"
value={type === 'market' ? avgMarketPrice : limitPrice} />
onChange={(e) => setLimitPrice(e.currentTarget.value)}
onBlur={(e) => setLimitPrice(e.currentTarget.value, true)}
type="string"
value={type === 'market' ? toLocale(avgMarketPrice, 4) : limitPrice} />
</TextInputWrapper>
</FormSection>

Expand Down Expand Up @@ -806,7 +804,7 @@ const MarketplaceOrder = ({
</b>
{!(type === 'limit' && mode === 'sell' && isCancellation) && (
<span style={{ display: 'inline-flex', fontSize: '36px', lineHeight: '36px' }}>
<SwayIcon /><TotalSway>{type === 'market' && (mode === 'buy' ? '-' : '+')}{formatFixed(total || 0)}</TotalSway>
<SwayIcon /><TotalSway>{type === 'market' && (mode === 'buy' ? '-' : '+')}{toLocale(total || 0)}</TotalSway>
</span>
)}
</div>
Expand Down
20 changes: 19 additions & 1 deletion src/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import esb from 'elastic-builder';
import { Crew, Entity, Lot, Permission, Processor, Time } from '@influenceth/sdk';
import trim from 'lodash/trim';
import numeral from 'numeral';
import * as locales from 'numeral/locales';

import { BioreactorBuildingIcon, ManufactureIcon, RefineIcon } from '~/components/Icons';

const getLocale = () => {
return ((navigator && navigator.language) || 'en').split('-')[0];
};

const timerIncrements = { d: 86400, h: 3600, m: 60, s: 1 };
export const formatTimer = (secondsRemaining, maxPrecision = null) => {
if (isNaN(secondsRemaining)) return '';
Expand All @@ -25,6 +31,18 @@ export const formatFixed = (value, maximumFractionDigits = 0) => {
return (Math.round((value || 0) * div) / div).toLocaleString();
};

// Get the numeric value from the specified locallized value
export const fromLocal = (value) => {
numeral.locale(getLocale());
return numeral(value).value();
}

export const toLocale = (value, minimumFractionDigits = 0) => {
if (!value) return 0;
const _value = (typeof value === 'string') ? fromLocal(value) : value;
return _value.toLocaleString(getLocale(), { minimumFractionDigits });
};

export const formatPrecision = (value, maximumPrecision = 0) => {
const allowedDecimals = Math.max(0, maximumPrecision - Math.ceil(Math.log10(Math.abs(value))));
if (isNaN(allowedDecimals)) { console.log('is nan', value, maximumPrecision); }
Expand All @@ -47,7 +65,7 @@ export const formatPrice = (inputSway, { minPrecision = 3, fixedPrecision = 4, f
scale = 1;
unitLabel = '';
} else {
return Number(sway || 0).toLocaleString(undefined, { minimumFractionDigits: minPrecision, maximumFractionDigits: fixedPrecision });
return Number(sway || 0).toLocaleString(getLocale(), { minimumFractionDigits: minPrecision, maximumFractionDigits: fixedPrecision });
}

const workingUnits = (sway / scale);
Expand Down