diff --git a/packages/client/src/hooks/useMerkel.tsx b/packages/client/src/hooks/useMerkel.tsx index 96e48f91..6806fffa 100644 --- a/packages/client/src/hooks/useMerkel.tsx +++ b/packages/client/src/hooks/useMerkel.tsx @@ -8,22 +8,21 @@ const useMerkel = (mapData) => { const convertToLeafs = (mapData) => { const result = []; - for (let x = 0; x < mapData.length; x++) { - for (let y = 0; y < mapData[x].length; y++) { - result.push({ x, y, value: mapData[x][y] }); + for (let y = 0; y < mapData.length; y++) { + for (let x = 0; x < mapData[y].length; x++) { + result.push({ x, y, value: mapData[y][x] }); } } return result; } - const merkel = useRef({ - leafs: convertToLeafs(mapData), - merkleTree: null - }); + const leafs = useRef([]); + + const merkel = useRef(null); const getProof = (x, y) => { const leaf = generateLeaf(x, y, 1); - return merkel.current.merkleTree.getHexProof(leaf); + return merkel.current!.getHexProof(leaf); } // 通过本函数将地图初始化为默克尔树节点的字符串数组,每个字符串的格式为"x,y-value" @@ -53,11 +52,9 @@ const useMerkel = (mapData) => { if (mapData.length === 0) { return; } - merkel.current.leafs = convertToLeafs(mapData); - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - merkel.current.merkleTree = new MerkleTree( - merkel.current.leafs.map((info) => generateLeaf(info.x, info.y, info.value)), + leafs.current = convertToLeafs(mapData); + merkel.current = new MerkleTree( + leafs.current.map((info) => generateLeaf(info.x, info.y, info.value)), keccak256, { sortPairs: true } ); diff --git a/packages/client/src/pages/game/index.tsx b/packages/client/src/pages/game/index.tsx index 264285c5..25142480 100644 --- a/packages/client/src/pages/game/index.tsx +++ b/packages/client/src/pages/game/index.tsx @@ -49,7 +49,7 @@ const Game = () => { }; const movePlayer = (paths, merkelData) => { - let pathIndex = 1; + let pathIndex = 0; const curPlayerIndex = players.findIndex(item => item.id === curPlayer!.id); const interval = setInterval(() => { Object.assign(players[curPlayerIndex], paths[pathIndex]); diff --git a/packages/client/src/utils/map.ts b/packages/client/src/utils/map.ts index 45a42437..6f6482e8 100644 --- a/packages/client/src/utils/map.ts +++ b/packages/client/src/utils/map.ts @@ -242,7 +242,7 @@ export const isMovable = (type) => { export const bfs = (mapData: number[][], from: ICoordinate, to: ICoordinate) => { const data = mapData.map((row) => [...row]); - data[from.y][from.x] = 1; + data[from.y][from.x] = 0; let paths = [[from]]; const dirs = [[-1, 0], [1, 0], [0, -1], [0, 1]]; @@ -258,7 +258,7 @@ export const bfs = (mapData: number[][], from: ICoordinate, to: ICoordinate) => if (data[nextY][nextX] === 1) { newPaths.push([...path, { x: nextX, y: nextY}]); - data[nextY][nextX] = 1; + data[nextY][nextX] = 0; } return nextX === to.x && nextY === to.y; });