Skip to content
This repository has been archived by the owner on Sep 21, 2024. It is now read-only.

Commit

Permalink
chore: calculate pixels to reset on client
Browse files Browse the repository at this point in the history
  • Loading branch information
mariz-ov committed Jul 6, 2024
1 parent 536968f commit e0ef4ac
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 4 deletions.
10 changes: 10 additions & 0 deletions graphql/GetPixelsToReset.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
query GetResetPixels($color: u32, $xGTE: u32, $xLTE: u32, $yGTE: u32, $yLTE: u32, $limit: Int) {
pixelModels(where: { colorEQ: $color, xGTE: $xGTE, xLTE:$xLTE, yGTE: $yGTE, yLTE: $yLTE}, limit: $limit) {
edges {
node {
x
y
}
}
}
}
16 changes: 14 additions & 2 deletions src/components/ProposalList/ProposalItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { numRGBAToHex } from '@/webtools/utils.ts';
import { GAME_ID, NEEDED_YES_PX } from '@/global/constants.ts';
import { formatWalletAddress, toastContractError, formatTimeRemaining, formatTimeRemainingFotTitle } from '@/global/utils.ts';
import { type ProposalDataType } from '@/hooks/useProposals.ts';
import useGetPixelsToReset from "@/hooks/useGetPixelsToReset.ts";

export type StartVoteParam = {
id: number;
Expand Down Expand Up @@ -69,6 +70,8 @@ const ProposalItem: React.FC<PropsType> = ({ proposal, onStartVote, filter, sear
const start = Number(proposal?.start ?? 0);
const end = Number(proposal?.end ?? 0);

const getPixelsToReset = useGetPixelsToReset()

useEffect(() => {
if (proposalStatus === 'closed') return;

Expand Down Expand Up @@ -114,10 +117,19 @@ const ProposalItem: React.FC<PropsType> = ({ proposal, onStartVote, filter, sear
comments: '',
};

const handleActivateProposal = () => {
const handleActivateProposal = async () => {
if (!gameData?.account.account) return;
let clearData: undefined | {x: number, y: number}[] = undefined

// check if proposal is of type reset
if (proposal.proposal_type === 2) {
// get all colors to reset
clearData = await getPixelsToReset.mutateAsync({color: proposal.target_args_1})
}


gameData.setup.systemCalls
.activateProposal(gameData.account.account, GAME_ID, proposal.index)
.activateProposal(gameData.account.account, GAME_ID, proposal.index, clearData)
.then(() => console.log('activateProposal', proposal))
.catch((e) => {
console.error('handleActivateProposal error: ', e);
Expand Down
3 changes: 2 additions & 1 deletion src/dojo/createSystemCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,12 @@ export function createSystemCalls({ client }: { client: IWorld }) {
await new Promise((resolve) => setTimeout(resolve, 1000));
};

const activateProposal = async (account: AccountInterface, gameId: number, index: number) => {
const activateProposal = async (account: AccountInterface, gameId: number, index: number, clearData?: {x: number, y: number}[]) => {
const { transaction_hash } = await client.actions.activateProposal({
account,
gameId,
index,
clearData
});

await account.waitForTransaction(transaction_hash, {
Expand Down
20 changes: 19 additions & 1 deletion src/dojo/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,36 @@ export async function setupWorld(provider: DojoProvider) {
account,
gameId,
index,
clearData
}: {
account: AccountInterface;
gameId: number;
index: number;
clearData?: {x: number, y: number}[]
}) => {

const clearDataArgs: number[] = []
if (clearData) {
clearData.forEach(({x, y}) => {
clearDataArgs.push(x)
clearDataArgs.push(y)
})
}

if (clearData) console.log(clearDataArgs)

try {
return await provider.execute(
account,
{
contractAddress: PROPOSAL_CONTRACT_ADDRESS,
entrypoint: 'activate_proposal',
calldata: [gameId, index],
calldata: [
gameId,
index,
clearData?.length ?? 0,
...clearDataArgs
],
},
{
skipValidate: true,
Expand Down
50 changes: 50 additions & 0 deletions src/hooks/useGetPixelsToReset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {useMutation} from '@tanstack/react-query';
import { GraphQLClient } from 'graphql-request';
import GetResetPixels from '@/../graphql/GetPixelsToReset.graphql';
import { useSettingsStore } from '@/stores/SettingsStore.ts';
import useBoard from "@/hooks/useBoard.ts";
import {GAME_ID} from "@/global/constants.ts";

type Data = {
pixelModels: {
edges: {
node: {
x: number,
y: number
};
}[];
};
};

const useGetPixelsToReset = () => {
const settings = useSettingsStore();
const baseUrl = settings?.config?.toriiUrl ?? 'http://localhost:8080';
const gqlClient = new GraphQLClient(`${baseUrl}/graphql`);

const board = useBoard(GAME_ID)

return useMutation({
mutationKey: ['usePixelRecoveryRate'],
mutationFn: async ({color}: {color: number}) => {
if (!board.data) throw new Error('board data not yet loaded')
const result: Data = await gqlClient
.request(
GetResetPixels,
{
color,
xGTE: board.data.origin.x,
xLTE: board.data.origin.x + board.data.width - 1,
yGTE: board.data.origin.y,
yLTE: board.data.origin.y + board.data.height - 1,
limit: board.data.height * board.data.width
}
);
return result.pixelModels.edges.map(({ node: { x, y }}) => {
return { x, y }
})
},
retryDelay: (failureCount) => failureCount * 1_000,
});
};

export default useGetPixelsToReset;

0 comments on commit e0ef4ac

Please sign in to comment.