Skip to content

Commit

Permalink
Merge branch 'next' of https://github.com/BibliothecaDAO/eternum into…
Browse files Browse the repository at this point in the history
… add-resources
  • Loading branch information
aymericdelab committed Dec 28, 2024
2 parents 4c7d532 + 2185bcb commit 5b67fe4
Show file tree
Hide file tree
Showing 26 changed files with 1,074 additions and 262 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
7 changes: 4 additions & 3 deletions client/src/dojo/modelManager/LeaderboardManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ export class LeaderboardManager {
getEntityIdFromKeys([BigInt(hyperstructure.entity_id), BigInt(i + 1)]),
);

const epochEndTimestamp = season.is_over
? season.ended_at
: nextEpoch?.start_timestamp ?? BigInt(currentTimestamp);
const epochEndTimestamp =
season.is_over && nextEpoch === undefined
? season.ended_at
: nextEpoch?.start_timestamp ?? BigInt(currentTimestamp);
const epochDuration = epochEndTimestamp - epoch.start_timestamp;

const nbOfCycles = Number(epochDuration) / ClientConfigManager.instance().getTick(TickIds.Default);
Expand Down
61 changes: 59 additions & 2 deletions client/src/dojo/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,38 @@ export async function setup(config: DojoConfig & { state: AppStore }) {
}

// fetch all existing entities from torii
setLoading(LoadingStateKey.Hyperstructure, true);
await getEntities(
network.toriiClient,
{
Composite: {
operator: "Or",
clauses: [
{
Keys: {
keys: [undefined, undefined],
pattern_matching: "FixedLen",
models: ["s0_eternum-Epoch", "s0_eternum-Progress", "s0_eternum-LeaderboardRegisterContribution"],
},
},
{
Keys: {
keys: [undefined, undefined, undefined],
pattern_matching: "FixedLen",
models: ["s0_eternum-Contribution", "s0_eternum-LeaderboardRegisterShare"],
},
},
],
},
},
network.contractComponents as any,
[],
[],
40_000,
false,
).finally(() => {
setLoading(LoadingStateKey.Hyperstructure, false);
});

setLoading(LoadingStateKey.SingleKey, true);
await getEntities(
Expand All @@ -167,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 All @@ -183,7 +219,28 @@ export async function setup(config: DojoConfig & { state: AppStore }) {

configManager.setDojo(components);

setLoading(LoadingStateKey.Events, true);
// setLoading(LoadingStateKey.Events, true);

await getEvents(
network.toriiClient,
network.contractComponents.events as any,
[],
[],
20000,
{
Keys: {
keys: [undefined],
pattern_matching: "VariableLen",
models: ["s0_eternum-GameEnded"],
},
},
false,
false,
);
// .finally(() => {
// setLoading(LoadingStateKey.Events, false);
// });

const eventSync = getEvents(
network.toriiClient,
network.contractComponents.events as any,
Expand All @@ -195,7 +252,7 @@ export async function setup(config: DojoConfig & { state: AppStore }) {
keys: [undefined],
pattern_matching: "VariableLen",
models: [
"s0_eternum-GameEnded",
// "s0_eternum-GameEnded",
"s0_eternum-HyperstructureFinished",
"s0_eternum-BattleClaimData",
"s0_eternum-BattleJoinData",
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
42 changes: 24 additions & 18 deletions client/src/ui/layouts/World.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { ADMIN_BANK_ENTITY_ID } from "@bibliothecadao/eternum";
import { getComponentValue } from "@dojoengine/recs";
import { getEntityIdFromKeys } from "@dojoengine/utils";
import { env } from "../../../env";
import { rewards } from "../components/navigation/Config";
import { IS_MOBILE } from "../config";
import { LoadingOroborus } from "../modules/loading-oroborus";
import { LoadingScreen } from "../modules/LoadingScreen";
Expand Down Expand Up @@ -230,25 +231,30 @@ export const World = ({ backgroundImage }: { backgroundImage: string }) => {
fetch();
}, []);

// useEffect(() => {
// const fetch = async () => {
// try {
// setLoading(LoadingStateKey.Hyperstructure, true);
// console.log("AddToSubscriptionStart - 4");
// await Promise.all([
// debouncedAddHyperstructureSubscription(dojo.network.toriiClient, dojo.network.contractComponents as any, () =>
// setLoading(LoadingStateKey.Hyperstructure, false),
// ),
// ]);
// } catch (error) {
// console.error("Fetch failed", error);
// } finally {
// // Ensure loading states are reset even if there's an error
// setLoading(LoadingStateKey.Hyperstructure, false);
// }
// };

// fetch();
// }, []);

const openPopup = useUIStore((state) => state.openPopup);
useEffect(() => {
const fetch = async () => {
try {
setLoading(LoadingStateKey.Hyperstructure, true);
console.log("AddToSubscriptionStart - 4");
await Promise.all([
debouncedAddHyperstructureSubscription(dojo.network.toriiClient, dojo.network.contractComponents as any, () =>
setLoading(LoadingStateKey.Hyperstructure, false),
),
]);
} catch (error) {
console.error("Fetch failed", error);
} finally {
// Ensure loading states are reset even if there's an error
setLoading(LoadingStateKey.Hyperstructure, false);
}
};

fetch();
openPopup(rewards);
}, []);

const battleViewContent = useMemo(
Expand Down
Loading

0 comments on commit 5b67fe4

Please sign in to comment.