Skip to content

Commit

Permalink
feat: update x y map
Browse files Browse the repository at this point in the history
  • Loading branch information
Vivomo committed Oct 4, 2023
1 parent 79e3f7c commit 8b13c75
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
23 changes: 10 additions & 13 deletions packages/client/src/hooks/useMerkel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<MerkleTree | null>(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"
Expand Down Expand Up @@ -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 }
);
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/pages/game/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/utils/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]];
Expand All @@ -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;
});
Expand Down

0 comments on commit 8b13c75

Please sign in to comment.