Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
ponderingdemocritus committed Dec 27, 2024
2 parents 8fd6d08 + 4fb9631 commit 908b27d
Show file tree
Hide file tree
Showing 17 changed files with 859 additions and 124 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
4 changes: 2 additions & 2 deletions landing/src/components/modules/app-sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
SidebarMenuItem,
} from "@/components/ui/sidebar";
import { Link } from "@tanstack/react-router";
import { Castle, Gamepad2, Home, PlayCircle, Scale, Sheet, Ship, Twitter } from "lucide-react";
import { Castle, Coins, Gamepad2, Home, PlayCircle, Scale, Sheet, Ship, Twitter } from "lucide-react";
import { TypeH2 } from "../typography/type-h2";

import { ReactComponent as Discord } from "@/assets/icons/discord.svg";
Expand All @@ -20,12 +20,12 @@ const items = [
url: "/",
icon: Home,
},
{ title: "Claim", url: "/claim", icon: Coins },
{
title: "Bridge",
url: "/trade",
icon: Ship,
},

{
title: "Realms",
url: "/mint",
Expand Down
45 changes: 45 additions & 0 deletions landing/src/components/modules/season-registration-timer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { useLeaderboardStatus } from "@/hooks/usePrizeClaim";
import { useEffect, useState } from "react";

export const SeasonRegistrationTimer = () => {
const [timeLeft, setTimeLeft] = useState({ hours: "00", minutes: "00", seconds: "00" });

const { leaderboard } = useLeaderboardStatus();

const registrationEnd = leaderboard?.registration_end_timestamp;

useEffect(() => {
if (!registrationEnd) return;

const timer = setInterval(() => {
const now = Math.floor(Date.now() / 1000);
const end = Number(registrationEnd);
if (now >= end) {
setTimeLeft({ hours: "00", minutes: "00", seconds: "00" });
clearInterval(timer);
return;
}

const diff = end - now;
const hours = Math.floor(diff / 3600);
const minutes = Math.floor((diff % 3600) / 60);
const seconds = diff % 60;
setTimeLeft({
hours: String(hours).padStart(2, "0"),
minutes: String(minutes).padStart(2, "0"),
seconds: String(seconds).padStart(2, "0"),
});
}, 1000);

return () => clearInterval(timer);
}, [registrationEnd]);

return (
<div className="bg-white/5 backdrop-blur-sm rounded-xl p-8 text-center flex flex-row gap-2 items-baseline">
<h2 className="text-2xl font-bold text-primary mb-4 font-">Registration Countdown: </h2>
<div className="text-3xl text-primary font-semibold">
{timeLeft.hours}:{timeLeft.minutes}:{timeLeft.seconds}
</div>
</div>
);
};
2 changes: 2 additions & 0 deletions landing/src/components/modules/top-navigation-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Button } from "../ui/button";
import { ResourceIcon } from "../ui/elements/ResourceIcon";
import { SidebarTrigger } from "../ui/sidebar";
import { ModeToggle } from "./mode-toggle";
import { SeasonRegistrationTimer } from "./season-registration-timer";
import { SeasonStartTimer } from "./season-start-timer";

interface TopNavigationViewProps {
Expand Down Expand Up @@ -63,6 +64,7 @@ export const TopNavigationView = ({
</Button>
</div>
<SeasonStartTimer />
<SeasonRegistrationTimer />
<div className="flex gap-2 justify-between">
{!isConnected ? (
<>
Expand Down
15 changes: 15 additions & 0 deletions landing/src/dojo/createSystemCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ export function createSystemCalls({ provider }: SetupNetworkResult) {
return await provider.bridge_finish_withdraw_from_realm(props);
};

const register_to_leaderboard = async (props: SystemProps.RegisterToLeaderboardProps) => {
await provider.register_to_leaderboard(props);
};

const end_game = async (props: SystemProps.EndGameProps) => {
await provider.end_game(props);
};

const claim_leaderboard_rewards = async (props: SystemProps.ClaimLeaderboardRewardsProps) => {
await provider.claim_leaderboard_rewards(props);
};

const isLive = async () => {
try {
await provider.uuid();
Expand All @@ -169,6 +181,9 @@ export function createSystemCalls({ provider }: SetupNetworkResult) {
bridge_resources_into_realm: withQueueing(withErrorHandling(bridge_resources_into_realm)),
bridge_start_withdraw_from_realm: withQueueing(withErrorHandling(bridge_start_withdraw_from_realm)),
bridge_finish_withdraw_from_realm: withQueueing(withErrorHandling(bridge_finish_withdraw_from_realm)),
register_to_leaderboard: withQueueing(withErrorHandling(register_to_leaderboard)),
end_game: withQueueing(withErrorHandling(end_game)),
claim_leaderboard_rewards: withQueueing(withErrorHandling(claim_leaderboard_rewards)),
};

// TODO: Fix Type
Expand Down
Loading

0 comments on commit 908b27d

Please sign in to comment.