This repository has been archived by the owner on Sep 21, 2024. It is now read-only.
forked from pixelaw/web
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: calculate pixels to reset on client
- Loading branch information
Showing
5 changed files
with
95 additions
and
4 deletions.
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 |
---|---|---|
@@ -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 | ||
} | ||
} | ||
} | ||
} |
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
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; |