Enables the counter to display sub zero amounts per second + prices and profits adjusted to match the contract

This commit is contained in:
mic0 2024-10-26 15:03:27 +02:00
parent 0ea4ba320c
commit 5bb2b5f97b
Signed by: mico
GPG Key ID: A3F8023524CF1C8D
2 changed files with 24 additions and 18 deletions

View File

@ -4,14 +4,24 @@ import styles from '../styles/Army.module.css';
import { calculateBalance, toReadable } from './Counter'; import { calculateBalance, toReadable } from './Counter';
const PRECISION = BigInt(10000); const PRECISION = BigInt(10000);
const BASE_PRICE = BigInt(380000);
const PRICE_FACTOR = BigInt(11500); const PRICE_FACTOR = BigInt(11500);
const APPRENTICE_PROFIT = BigInt(61000);
const ANOINTED_PROFIT = BigInt(6 * 64000); const base_cost: Record<UnitType, bigint> = {
const CHAMPION_PROFIT = BigInt(BigInt(67000 * 61000 * 64000) / PRECISION / PRECISION); 0: BigInt(380000),
1: BigInt(3420000),
2: BigInt(30096000),
3: BigInt(255816000),
}
const profits: Record<UnitType, bigint> = {
0: BigInt(2533),
1: BigInt(27863),
2: BigInt(306493),
3: BigInt(3371423),
}
function calculateUnitPrice(unit: UnitType, currentLevel: number, units: number) { function calculateUnitPrice(unit: UnitType, currentLevel: number, units: number) {
let rollingPriceCalculation = BigInt(unit + 1) * BASE_PRICE; let rollingPriceCalculation = base_cost[unit];
let price = BigInt(0); let price = BigInt(0);
// Each level costs 15% more than previous // Each level costs 15% more than previous
@ -26,12 +36,7 @@ function calculateUnitPrice(unit: UnitType, currentLevel: number, units: number)
function calculateProfitPerSecond(unit: UnitType, level: number) { function calculateProfitPerSecond(unit: UnitType, level: number) {
// Each next unit scales progressivelly better // Each next unit scales progressivelly better
switch (unit) { return BigInt(level) * profits[unit]
case 0: return BigInt(level) * PRECISION
case 1: return BigInt(level) * APPRENTICE_PROFIT
case 2: return BigInt(level) * ANOINTED_PROFIT
case 3: return BigInt(level) * CHAMPION_PROFIT
}
} }
const unitTypeToCss: Record<UnitType, string> = { const unitTypeToCss: Record<UnitType, string> = {
@ -79,8 +84,8 @@ interface UnitProps {
const Unit = ({ addUnit, unitType, canPurchase, isShrouded, n_units }: UnitProps) => { const Unit = ({ addUnit, unitType, canPurchase, isShrouded, n_units }: UnitProps) => {
const [unitPrice, unitProfit] = useMemo(() => { const [unitPrice, unitProfit] = useMemo(() => {
return [ return [
toReadable(calculateUnitPrice(unitType, n_units, 1)) + " geld", toReadable(calculateUnitPrice(unitType, n_units, 1)),
toReadable(calculateProfitPerSecond(unitType, n_units)) + " geld / sec" toReadable(calculateProfitPerSecond(unitType, n_units))
] ]
}, [n_units, unitType]); }, [n_units, unitType]);
return <div onClick={() => addUnit(unitType)} className={`${styles.armyUnit} ${canPurchase ? "" : styles.isUnavailable}`}> return <div onClick={() => addUnit(unitType)} className={`${styles.armyUnit} ${canPurchase ? "" : styles.isUnavailable}`}>
@ -91,9 +96,9 @@ const Unit = ({ addUnit, unitType, canPurchase, isShrouded, n_units }: UnitProps
${isShrouded ? styles.isShrouded : ""} ${isShrouded ? styles.isShrouded : ""}
`} /> `} />
<span className={`${styles.unitName} ${styles.uiElement}`}>{isShrouded ? "???????" : unitTypeToName[unitType]}</span> <span className={`${styles.unitName} ${styles.uiElement}`}>{isShrouded ? "???????" : unitTypeToName[unitType]}</span>
{isShrouded ? null : <span className={`${styles.unitPrice} ${styles.uiElement}`}>{unitPrice}</span>} {isShrouded ? null : <span className={`${styles.unitPrice} ${styles.uiElement}`}>{unitPrice} <small>GELD</small></span>}
{n_units > 0 ? <span className={`${styles.unitSupply} ${styles.uiElement}`}>{n_units}</span> : null} {n_units > 0 ? <span className={`${styles.unitSupply} ${styles.uiElement}`}>{n_units}</span> : null}
{n_units > 0 ? <span className={`${styles.unitProfit} ${styles.uiElement}`}>{unitProfit}</span> : null} {n_units > 0 ? <span className={`${styles.unitProfit} ${styles.uiElement}`}>{unitProfit} <small>per sec</small></span> : null}
</div> </div>
} }

View File

@ -1,6 +1,7 @@
import { useEffect, useReducer, useRef } from "react"; import { useEffect, useReducer, useRef } from "react";
import { usePlayer } from "../providers/PlayerProvider" import { usePlayer } from "../providers/PlayerProvider"
import styles from "../styles/Header.module.css" import styles from "../styles/Header.module.css"
import { formatUnits } from "viem";
export const calculateBalance = (balance: bigint, perSecond: bigint, lastRaidedAt: bigint) => { export const calculateBalance = (balance: bigint, perSecond: bigint, lastRaidedAt: bigint) => {
// convert to milliseconds trick so we get a more smooth counter // convert to milliseconds trick so we get a more smooth counter
@ -12,8 +13,8 @@ export const calculateBalance = (balance: bigint, perSecond: bigint, lastRaidedA
/ BigInt(1000) /* deduct milliseconds*/)) / BigInt(1000) /* deduct milliseconds*/))
} }
export const toReadable = (value: bigint) => { export const toReadable = (rawValue: bigint) => {
value = value / BigInt(10000); const value = rawValue / BigInt(10000);
const suffixes = [ const suffixes = [
{ value: BigInt('1000'), suffix: 'thousand' }, { value: BigInt('1000'), suffix: 'thousand' },
{ value: BigInt('1000000'), suffix: 'million' }, { value: BigInt('1000000'), suffix: 'million' },
@ -38,7 +39,7 @@ export const toReadable = (value: bigint) => {
for (let i = 0; i < suffixes.length; i++) { for (let i = 0; i < suffixes.length; i++) {
if (value < suffixes[i].value) { if (value < suffixes[i].value) {
if (i == 0) { if (i == 0) {
return value.toString(); return formatUnits(rawValue, 4);
} else { } else {
const divided = value / suffixes[i - 1].value; const divided = value / suffixes[i - 1].value;
const numStr = (value % suffixes[i - 1].value).toString().slice(0, 3); const numStr = (value % suffixes[i - 1].value).toString().slice(0, 3);