From de1e3eb29a8e8d4d9843a1ac7ed267262f39bd42 Mon Sep 17 00:00:00 2001 From: tedison <76473430+edisontim@users.noreply.github.com> Date: Fri, 27 Dec 2024 12:14:40 -0500 Subject: [PATCH] feat: allow reregistration (#2612) * Allow people to re-register * Add registered points --- client/src/dojo/contractComponents.ts | 3 +- client/src/dojo/setup.ts | 10 ++- client/src/hooks/helpers/useContributions.tsx | 28 +++++++++ .../src/hooks/helpers/useHyperstructures.tsx | 38 ++++++++++++ client/src/ui/modules/rewards/Rewards.tsx | 62 +++++++++++++------ 5 files changed, 117 insertions(+), 24 deletions(-) diff --git a/client/src/dojo/contractComponents.ts b/client/src/dojo/contractComponents.ts index 1b9dd3d93..d173da9f4 100644 --- a/client/src/dojo/contractComponents.ts +++ b/client/src/dojo/contractComponents.ts @@ -649,13 +649,14 @@ export function defineContractComponents(world: World) { { address: RecsType.BigInt, hyperstructure_entity_id: RecsType.Number, + epoch: RecsType.Number, registered: RecsType.Boolean, }, { metadata: { namespace: "s0_eternum", name: "LeaderboardRegisterShare", - types: ["contractaddress", "u32", "bool"], + types: ["contractaddress", "u32", "u16", "bool"], customTypes: [], }, }, diff --git a/client/src/dojo/setup.ts b/client/src/dojo/setup.ts index 5e31740e2..33b3032e7 100644 --- a/client/src/dojo/setup.ts +++ b/client/src/dojo/setup.ts @@ -157,14 +157,14 @@ export async function setup(config: DojoConfig & { state: AppStore }) { Keys: { keys: [undefined, undefined], pattern_matching: "FixedLen", - models: ["s0_eternum-Epoch", "s0_eternum-Progress"], + models: ["s0_eternum-Epoch", "s0_eternum-Progress", "s0_eternum-LeaderboardRegisterContribution"], }, }, { Keys: { keys: [undefined, undefined, undefined], pattern_matching: "FixedLen", - models: ["s0_eternum-Contribution"], + models: ["s0_eternum-Contribution", "s0_eternum-LeaderboardRegisterShare"], }, }, ], @@ -199,6 +199,10 @@ export async function setup(config: DojoConfig & { state: AppStore }) { "s0_eternum-Structure", "s0_eternum-Battle", "s0_eternum-Guild", + "s0_eternum-LeaderboardRegistered", + "s0_eternum-Leaderboard", + "s0_eternum-LeaderboardRewardClaimed", + "s0_eternum-LeaderboardEntry", ], }, }, @@ -232,7 +236,7 @@ export async function setup(config: DojoConfig & { state: AppStore }) { }, false, false, - ) + ); // .finally(() => { // setLoading(LoadingStateKey.Events, false); // }); diff --git a/client/src/hooks/helpers/useContributions.tsx b/client/src/hooks/helpers/useContributions.tsx index ed3e2282f..9024c0ab9 100644 --- a/client/src/hooks/helpers/useContributions.tsx +++ b/client/src/hooks/helpers/useContributions.tsx @@ -69,3 +69,31 @@ export const useGetHyperstructuresWithContributionsFromPlayer = () => { return getContributions; }; + +export const useGetUnregisteredContributions = () => { + const { + account: { account }, + setup: { + components: { LeaderboardRegisterContribution }, + }, + } = useDojo(); + const getContributions = useGetHyperstructuresWithContributionsFromPlayer(); + + const getUnregisteredContributions = useCallback(() => { + const registeredContributionsEntities = runQuery([ + HasValue(LeaderboardRegisterContribution, { address: ContractAddress(account.address) }), + ]); + const registeredContributions = Array.from(registeredContributionsEntities) + .map((entityId) => getComponentValue(LeaderboardRegisterContribution, entityId)?.hyperstructure_entity_id) + .filter((x): x is number => x !== undefined); + console.log("registeredContributions", registeredContributions); + const hyperstructuresContributedTo = Array.from(getContributions()); + console.log("hyperstructuresContributedTo", hyperstructuresContributedTo); + return hyperstructuresContributedTo.filter( + (hyperstructureEntityId) => + !registeredContributions.some((contribution) => contribution === hyperstructureEntityId), + ); + }, [getContributions]); + + return getUnregisteredContributions; +}; diff --git a/client/src/hooks/helpers/useHyperstructures.tsx b/client/src/hooks/helpers/useHyperstructures.tsx index 6dc141526..955d2d105 100644 --- a/client/src/hooks/helpers/useHyperstructures.tsx +++ b/client/src/hooks/helpers/useHyperstructures.tsx @@ -172,6 +172,44 @@ export const useGetPlayerEpochs = () => { return getEpochs; }; +export const useGetUnregisteredEpochs = () => { + const { + account: { account }, + setup: { + components: { LeaderboardRegisterShare }, + }, + } = useDojo(); + + const getEpochs = useGetPlayerEpochs(); + + const getUnregisteredShares = useCallback(() => { + const epochs = getEpochs(); + console.log("epochs", epochs); + + const registeredSharesEntities = runQuery([ + Has(LeaderboardRegisterShare), + HasValue(LeaderboardRegisterShare, { address: ContractAddress(account.address) }), + ]); + const registeredShares = Array.from(registeredSharesEntities) + .map((shareEntityId) => { + return getComponentValue(LeaderboardRegisterShare, shareEntityId); + }) + .filter( + (share): share is ComponentValue => share !== undefined, + ); + console.log("registeredShares", registeredShares); + + return epochs.filter( + (epoch) => + !registeredShares.some( + (share) => share.epoch === epoch.epoch && share.hyperstructure_entity_id === epoch.hyperstructure_entity_id, + ), + ); + }, [getEpochs]); + + return getUnregisteredShares; +}; + const getContributions = (hyperstructureEntityId: ID, Contribution: Component) => { const contributions = runQuery([ Has(Contribution), diff --git a/client/src/ui/modules/rewards/Rewards.tsx b/client/src/ui/modules/rewards/Rewards.tsx index 435880949..697b310f4 100644 --- a/client/src/ui/modules/rewards/Rewards.tsx +++ b/client/src/ui/modules/rewards/Rewards.tsx @@ -1,14 +1,17 @@ import { useDojo } from "@/hooks/context/DojoContext"; import { usePrizePool } from "@/hooks/helpers/use-rewards"; -import { useGetHyperstructuresWithContributionsFromPlayer } from "@/hooks/helpers/useContributions"; -import { useGetPlayerEpochs } from "@/hooks/helpers/useHyperstructures"; +import { + useGetHyperstructuresWithContributionsFromPlayer, + useGetUnregisteredContributions, +} from "@/hooks/helpers/useContributions"; +import { useGetPlayerEpochs, useGetUnregisteredEpochs } from "@/hooks/helpers/useHyperstructures"; import useUIStore from "@/hooks/store/useUIStore"; import { HintSection } from "@/ui/components/hints/HintModal"; import { rewards } from "@/ui/components/navigation/Config"; import { OSWindow } from "@/ui/components/navigation/OSWindow"; import Button from "@/ui/elements/Button"; import { formatTime, getEntityIdFromKeys } from "@/ui/utils/utils"; -import { ContractAddress, WORLD_CONFIG_ID } from "@bibliothecadao/eternum"; +import { ContractAddress } from "@bibliothecadao/eternum"; import { useComponentValue, useEntityQuery } from "@dojoengine/react"; import { Has, getComponentValue, runQuery } from "@dojoengine/recs"; import { useCallback, useEffect, useMemo, useState } from "react"; @@ -22,11 +25,10 @@ const BRIDGE_OUT_DELAY = 60 * 60 * 24 * 2; // 2 days export const Rewards = () => { const { account: { account }, - network: { provider }, setup: { components: { AddressName, - Leaderboard, + LeaderboardEntry, LeaderboardRegistered, events: { GameEnded }, }, @@ -45,34 +47,47 @@ export const Rewards = () => { const getContributions = useGetHyperstructuresWithContributionsFromPlayer(); const getEpochs = useGetPlayerEpochs(); + const getUnregisteredContributions = useGetUnregisteredContributions(); + const getUnregisteredEpochs = useGetUnregisteredEpochs(); const gameEndedEntityId = useEntityQuery([Has(GameEnded)]); + const leaderboardEntry = useComponentValue(LeaderboardEntry, getEntityIdFromKeys([ContractAddress(account.address)])); + const gameEnded = useMemo(() => { return getComponentValue(GameEnded, gameEndedEntityId[0]); }, [gameEndedEntityId]); - const leaderboard = useComponentValue(Leaderboard, getEntityIdFromKeys([WORLD_CONFIG_ID])); - const registerToLeaderboard = useCallback(async () => { setIsLoading(true); - const contributions = Array.from(getContributions()); - const epochs = getEpochs(); - - await register_to_leaderboard({ - signer: account, - hyperstructure_contributed_to: contributions, - hyperstructure_shareholder_epochs: epochs, - }); - setIsLoading(false); + const epochs = getUnregisteredEpochs(); + const contributions = getUnregisteredContributions(); + + try { + await register_to_leaderboard({ + signer: account, + hyperstructure_contributed_to: contributions, + hyperstructure_shareholder_epochs: epochs, + }); + } catch (error) { + console.error("Error registering to leaderboard", error); + } finally { + setIsLoading(false); + } }, [getContributions, getEpochs]); const claimRewards = useCallback(async () => { setIsLoading(true); - await claim_leaderboard_rewards({ - signer: account, - token: env.VITE_LORDS_ADDRESS!, - }); + try { + await claim_leaderboard_rewards({ + signer: account, + token: env.VITE_LORDS_ADDRESS!, + }); + } catch (error) { + console.error("Error claiming rewards", error); + } finally { + setIsLoading(false); + } setIsLoading(false); }, [account]); @@ -147,6 +162,13 @@ export const Rewards = () => {
{Number(formatEther(prizePool)).toFixed(2)} $LORDS
+ +
+
Your registered points
+ +
{Number(leaderboardEntry?.points ?? 0)}
+
+