From 0dd53f9c1f2d8fb9721d876d3d0b071ce80a7edd Mon Sep 17 00:00:00 2001 From: Mitja Belak Date: Thu, 24 Oct 2024 12:59:49 +0200 Subject: [PATCH] Tweaked formatting fn a bit plus per second counter is also formatted now --- app/src/components/Counter.tsx | 15 ++++++++++----- app/src/components/Header.tsx | 6 ++++-- src/RaidGeld.sol | 1 + 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/app/src/components/Counter.tsx b/app/src/components/Counter.tsx index b33c814..2e1af64 100644 --- a/app/src/components/Counter.tsx +++ b/app/src/components/Counter.tsx @@ -12,8 +12,10 @@ const calculateBalance = (balance: bigint, perSecond: bigint, lastRaidedAt: bigi / BigInt(1000) /* deduct milliseconds*/)) } -export const toReadable = (value: bigint) => { - value = value / BigInt(10000); +export const toReadable = (value: bigint, applyTokenDivision?: boolean) => { + if (applyTokenDivision) { + value = value / BigInt(10000); + } const suffixes = [ { value: BigInt('1000'), suffix: 'thousand' }, { value: BigInt('1000000'), suffix: 'million' }, @@ -35,13 +37,16 @@ export const toReadable = (value: bigint) => { { value: BigInt('1000000000000000000000000000000000000000000000000000000'), suffix: 'septendecillion' }, ]; + console.log(value) + for (let i = 0; i < suffixes.length; i++) { if (value < suffixes[i].value) { if (i == 0) { return value.toString(); } else { const divided = value / suffixes[i - 1].value; - const remainder = value % suffixes[i - 1].value; + const numStr = (value % suffixes[i - 0].value).toString().slice(0, 3); + const remainder = parseInt(numStr.replace(/0+$/, ''), 10); return `${divided.toString()}.${remainder.toString()} ${suffixes[i - 1].suffix}`; } } @@ -61,8 +66,8 @@ const Counter = () => { balance, army?.profit_per_second ?? BigInt(0), player?.last_raided_at ?? BigInt(0) - )).toString(); - availableBalance.current = toReadable(balance); + ), true); + availableBalance.current = toReadable(balance, true); render(); }, 100); return () => clearInterval(tickInterval) diff --git a/app/src/components/Header.tsx b/app/src/components/Header.tsx index 8618248..09a407e 100644 --- a/app/src/components/Header.tsx +++ b/app/src/components/Header.tsx @@ -3,7 +3,7 @@ import styles from "../styles/Header.module.css" import { usePlayer } from "../providers/PlayerProvider"; import { useAccount } from 'wagmi'; import dynamic from "next/dynamic"; -import Counter from "./Counter"; +import Counter, { toReadable } from "./Counter"; const Header = () => { const { isConnected } = useAccount(); @@ -24,8 +24,10 @@ const Header = () => { }, [isRegistered]) const perSecondParagraph = useMemo(() => { + const perSecond = toReadable(army?.profit_per_second ?? BigInt(0)) + console.log(perSecond, army?.profit_per_second) return (isRegistered) ? -

per second: {army?.profit_per_second.toString()}

+

per second: {perSecond}

: null }, [isRegistered, army?.profit_per_second]) diff --git a/src/RaidGeld.sol b/src/RaidGeld.sol index c3d2afa..feb5422 100644 --- a/src/RaidGeld.sol +++ b/src/RaidGeld.sol @@ -79,6 +79,7 @@ contract RaidGeld is ERC20, Ownable { // Helper so we can use it when buying units too function performRaid(address player) private { uint256 time_past = block.timestamp - players[player].last_raided_at; + uint256 new_geld = armies[player].profit_per_second * time_past * 10 ** decimals(); // TODO: Pink noise, make it so sometimes its better than expected