From f47bae3ba701183b2a0e6e6e61892354c30129d5 Mon Sep 17 00:00:00 2001 From: Noy <59097122+noyyyy@users.noreply.github.com> Date: Tue, 12 Sep 2023 22:46:14 +0800 Subject: [PATCH] fix: page crashes and place back error --- packages/client/src/hooks/useChessboard.ts | 47 ++++++++++--------- packages/client/src/ui/Chessboard.tsx | 3 ++ packages/client/src/ui/Synergy.tsx | 9 ++-- .../contracts/src/systems/PlaceSystem.sol | 2 +- 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/packages/client/src/hooks/useChessboard.ts b/packages/client/src/hooks/useChessboard.ts index f9ba8f1..c6230ac 100644 --- a/packages/client/src/hooks/useChessboard.ts +++ b/packages/client/src/hooks/useChessboard.ts @@ -130,28 +130,31 @@ const useChessboard = () => { }, [BoardList, PieceInBattleList, creatureMap]); const PiecesList = playerObj?.heroes.map((row, _index: any) => { - try { - const hero = getComponentValueStrict( - Hero, - encodeEntity({ id: "bytes32" }, { id: numberToHex(row, { size: 32 }) }) - ); - const creature = getComponentValue( - Creature, - encodeCreatureEntity(hero.creatureId) - ); - - const decodeHeroData = decodeHero(hero.creatureId); - - return { - ...hero, - ...creature, - key: row, - _index, - ...decodeHeroData, - image: getHeroImg(hero.creatureId), - maxHealth: creature?.health, - }; - } catch (error) {} + const hero = getComponentValue( + Hero, + encodeEntity({ id: "bytes32" }, { id: numberToHex(row, { size: 32 }) }) + ); + + if (!hero) { + return undefined; + } + + const creature = getComponentValue( + Creature, + encodeCreatureEntity(hero.creatureId) + ); + + const decodeHeroData = decodeHero(hero.creatureId); + + return { + ...hero, + ...creature, + key: row, + _index, + ...decodeHeroData, + image: getHeroImg(hero.creatureId), + maxHealth: creature?.health, + }; }); const playerListData = currentGame?.players?.map((_player: string) => { diff --git a/packages/client/src/ui/Chessboard.tsx b/packages/client/src/ui/Chessboard.tsx index 4140318..90e705e 100644 --- a/packages/client/src/ui/Chessboard.tsx +++ b/packages/client/src/ui/Chessboard.tsx @@ -102,6 +102,9 @@ const Chessboard = ({ setAcHeroFn }: { setAcHeroFn: (any) => void }) => { }); } else { PiecesList?.map((item) => { + if (!item) { + return; + } const position = convertToIndex(item.x, item.y); newSquares[position] = { ...item, diff --git a/packages/client/src/ui/Synergy.tsx b/packages/client/src/ui/Synergy.tsx index 741e7b6..93c86f4 100644 --- a/packages/client/src/ui/Synergy.tsx +++ b/packages/client/src/ui/Synergy.tsx @@ -53,10 +53,11 @@ export function useSynergyCount(uniqueCreatureIds: bigint[]) { }; uniqueCreatureIds.forEach((v) => { - const creatureValue = getComponentValueStrict( - Creature, - encodeCreatureEntity(v) - ); + const creatureValue = getComponentValue(Creature, encodeCreatureEntity(v)); + + if (!creatureValue) { + return; + } raceSynergy[creatureValue.race as HeroRace] += 1; classSynergy[creatureValue.class as HeroClass] += 1; diff --git a/packages/contracts/src/systems/PlaceSystem.sol b/packages/contracts/src/systems/PlaceSystem.sol index 1bbef8c..3e7b286 100644 --- a/packages/contracts/src/systems/PlaceSystem.sol +++ b/packages/contracts/src/systems/PlaceSystem.sol @@ -91,7 +91,7 @@ contract PlaceSystem is System { function swapInventory(uint256 fromIndex, uint256 toIndex) public onlyWhenGamePreparing { address player = _msgSender(); - uint8 maxIdx = GameConfig.getInventorySlotNum(0) - 1; + uint8 maxIdx = GameConfig.getInventorySlotNum(0); require(fromIndex < maxIdx, "index out of range"); require(toIndex < maxIdx, "index out of range");