Skip to content

Commit

Permalink
feat: allow reregistration (#2612)
Browse files Browse the repository at this point in the history
* Allow people to re-register

* Add registered points
  • Loading branch information
edisontim authored Dec 27, 2024
1 parent 5058962 commit de1e3eb
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 24 deletions.
3 changes: 2 additions & 1 deletion client/src/dojo/contractComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
},
},
Expand Down
10 changes: 7 additions & 3 deletions client/src/dojo/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
},
},
],
Expand Down Expand Up @@ -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",
],
},
},
Expand Down Expand Up @@ -232,7 +236,7 @@ export async function setup(config: DojoConfig & { state: AppStore }) {
},
false,
false,
)
);
// .finally(() => {
// setLoading(LoadingStateKey.Events, false);
// });
Expand Down
28 changes: 28 additions & 0 deletions client/src/hooks/helpers/useContributions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
38 changes: 38 additions & 0 deletions client/src/hooks/helpers/useHyperstructures.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<ClientComponents["LeaderboardRegisterShare"]["schema"]> => 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),
Expand Down
62 changes: 42 additions & 20 deletions client/src/ui/modules/rewards/Rewards.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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 },
},
Expand All @@ -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]);

Expand Down Expand Up @@ -147,6 +162,13 @@ export const Rewards = () => {
<div className="text-lg">{Number(formatEther(prizePool)).toFixed(2)} $LORDS</div>
</div>
</Compartment>
<Compartment>
<div className="text-center text-lg font-semibold self-center w-full">
<div className="text-sm font-bold uppercase">Your registered points</div>

<div className="text-lg">{Number(leaderboardEntry?.points ?? 0)}</div>
</div>
</Compartment>
</div>

<div className="grid grid-cols-2 gap-4">
Expand Down

0 comments on commit de1e3eb

Please sign in to comment.