-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor hooks #2642
Merged
+1,275
−1,643
Merged
refactor hooks #2642
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2a119cf
clean battles
aymericdelab 99bbb18
refactor use-armies
aymericdelab ff9a460
Merge branch 'next' of https://github.com/BibliothecaDAO/eternum into…
aymericdelab debe5b9
refactor use bank
aymericdelab 7647c7c
refactor hooks (battle evenst, buildings)
aymericdelab e2b67fe
refactor contributions
aymericdelab e2750e3
Merge branch 'next' of https://github.com/BibliothecaDAO/eternum into…
aymericdelab 4c50c46
refactor use-entities
aymericdelab 30527bd
refactor use players
aymericdelab dc588a7
refactor quests
aymericdelab 2a5acdf
refactor use realm
aymericdelab 047254c
refactor use-realm
aymericdelab ef8fc2b
refactor resource arrivals
aymericdelab f4842ce
refactor use resources
aymericdelab 38ba118
fix knip
aymericdelab 2f8524c
fix player
aymericdelab 16c5a7d
fix
aymericdelab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
refactor contributions
commit e2b67fe5ad8180b967a4113789741efc5702e650
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,20 @@ | ||
import { configManager } from "@/dojo/setup"; | ||
import { useDojo } from "@/hooks/context/dojo-context"; | ||
import { divideByPrecision } from "@/ui/utils/utils"; | ||
import { ClientComponents, ContractAddress, ID, Resource } from "@bibliothecadao/eternum"; | ||
import { ClientComponents, ContractAddress, ID } from "@bibliothecadao/eternum"; | ||
import { useEntityQuery } from "@dojoengine/react"; | ||
import { ComponentValue, HasValue, getComponentValue, runQuery } from "@dojoengine/recs"; | ||
import { useCallback } from "react"; | ||
import { ComponentValue, HasValue, getComponentValue } from "@dojoengine/recs"; | ||
|
||
export const useContributions = () => { | ||
export const usePlayerContributions = (playerAddress: ContractAddress, hyperstructureEntityId: ID) => { | ||
const { | ||
setup: { | ||
components: { Contribution }, | ||
}, | ||
} = useDojo(); | ||
|
||
const getContributions = (hyperstructureEntityId: ID) => { | ||
const contributionsToHyperstructure = Array.from( | ||
runQuery([HasValue(Contribution, { hyperstructure_entity_id: hyperstructureEntityId })]), | ||
).map((id) => getComponentValue(Contribution, id)); | ||
const contributionsToHyperstructure = useEntityQuery([ | ||
HasValue(Contribution, { hyperstructure_entity_id: hyperstructureEntityId, player_address: playerAddress }), | ||
]) | ||
.map((id) => getComponentValue(Contribution, id)) | ||
.filter((x): x is ComponentValue<ClientComponents["Contribution"]["schema"]> => x !== undefined); | ||
|
||
return contributionsToHyperstructure as ComponentValue<ClientComponents["Contribution"]["schema"]>[]; | ||
}; | ||
|
||
const useContributionsByPlayerAddress = (playerAddress: ContractAddress, hyperstructureEntityId: ID) => { | ||
const contributionsToHyperstructure = useEntityQuery([ | ||
HasValue(Contribution, { hyperstructure_entity_id: hyperstructureEntityId, player_address: playerAddress }), | ||
]) | ||
.map((id) => getComponentValue(Contribution, id)) | ||
.filter((x): x is ComponentValue<ClientComponents["Contribution"]["schema"]> => x !== undefined); | ||
|
||
return contributionsToHyperstructure; | ||
}; | ||
|
||
const getContributionsTotalPercentage = (hyperstructureId: number, contributions: Resource[]) => { | ||
const totalPlayerContribution = divideByPrecision( | ||
contributions.reduce((acc, { amount, resourceId }) => { | ||
return acc + amount * configManager.getResourceRarity(resourceId); | ||
}, 0), | ||
); | ||
|
||
const totalHyperstructureContribution = configManager.getHyperstructureTotalContributableAmount(hyperstructureId); | ||
|
||
return totalPlayerContribution / totalHyperstructureContribution; | ||
}; | ||
|
||
return { | ||
getContributions, | ||
useContributionsByPlayerAddress, | ||
getContributionsTotalPercentage, | ||
}; | ||
}; | ||
|
||
export const useGetHyperstructuresWithContributionsFromPlayer = () => { | ||
const { | ||
account: { account }, | ||
setup: { | ||
components: { Contribution }, | ||
}, | ||
} = useDojo(); | ||
|
||
const getContributions = useCallback(() => { | ||
const entityIds = runQuery([HasValue(Contribution, { player_address: ContractAddress(account.address) })]); | ||
const hyperstructureEntityIds = Array.from(entityIds).map( | ||
(entityId) => getComponentValue(Contribution, entityId)?.hyperstructure_entity_id ?? 0, | ||
); | ||
return new Set(hyperstructureEntityIds); | ||
}, [account.address]); | ||
|
||
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; | ||
return contributionsToHyperstructure; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codebase verification
Add null check to prevent NaN in percentage calculation
The current implementation could return NaN if
totalHyperstructureContribution
is zero. The suggested fix using|| 0
is appropriate as it will handle this edge case and any other potential undefined/NaN scenarios, while also improving code readability.🔗 Analysis chain
Handle potential undefined values in percentage calculation.
Ensure that
getContributionsTotalPercentage
returns a valid number to prevent potential issues when multiplying by 100.Apply this diff to add a default value:
🏁 Scripts executed
The following scripts were executed for the analysis:
Error: No shell scripts found
Script:
Length of output: 195
Script:
Length of output: 4613
Script:
Length of output: 1522