From b13e6c42597ef268357998f1a7f145bd45ee2f17 Mon Sep 17 00:00:00 2001 From: Dhvani Patel Date: Tue, 14 May 2024 11:26:39 +0100 Subject: [PATCH] getters --- packages/hardhat/contracts/Game.sol | 80 ++++++++++++------- packages/nextjs/app/page.tsx | 3 +- .../nextjs/contracts/deployedContracts.ts | 26 ++++++ 3 files changed, 79 insertions(+), 30 deletions(-) diff --git a/packages/hardhat/contracts/Game.sol b/packages/hardhat/contracts/Game.sol index 4f8b4448..de7835a7 100644 --- a/packages/hardhat/contracts/Game.sol +++ b/packages/hardhat/contracts/Game.sol @@ -1,6 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity >=0.8.24; +import { Strings } from "@openzeppelin/contracts/utils/Strings.sol"; import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol"; import { ResourceId, WorldResourceIdLib, WorldResourceIdInstance } from "@latticexyz/world/src/WorldResourceId.sol"; import { Hook } from "@latticexyz/store/src/Hook.sol"; @@ -21,7 +22,7 @@ import { getObjectType, getEntityAtCoord, getPosition, getEntityFromPlayer, getO import { voxelCoordsAreEqual } from "@biomesaw/utils/src/VoxelCoordUtils.sol"; import { decodeCallData } from "../utils/HookUtils.sol"; -import { NamedBuild } from "../utils/GameUtils.sol"; +import { NamedBuild, getEmptyBlockOnGround } from "../utils/GameUtils.sol"; contract Game is ICustomUnregisterDelegation, IOptionalSystemHook { address public immutable biomeWorldAddress; @@ -166,29 +167,6 @@ contract Game is ICustomUnregisterDelegation, IOptionalSystemHook { allowedItemDrops.push(msgSender); } - function getEmptyBlockOnGround(VoxelCoord memory centerCoord) internal returns (VoxelCoord memory) { - for (int8 dx = -1; dx <= 1; dx++) { - for (int8 dy = -1; dy <= 1; dy++) { - for (int8 dz = -1; dz <= 1; dz++) { - VoxelCoord memory coord = VoxelCoord({ x: centerCoord.x + dx, y: centerCoord.y + dy, z: centerCoord.z + dz }); - VoxelCoord memory coordBelow = VoxelCoord({ - x: centerCoord.x + dx, - y: centerCoord.y + dy - 1, - z: centerCoord.z + dz - }); - - if ( - getObjectTypeAtCoord(biomeWorldAddress, coord) == AirObjectID && - getObjectTypeAtCoord(biomeWorldAddress, coordBelow) != AirObjectID - ) { - return coord; - } - } - } - } - revert("No empty block on ground"); - } - function dropItem(bytes32 toolEntityId) external { address msgSender = msg.sender; bool isAllowed = false; @@ -205,7 +183,7 @@ contract Game is ICustomUnregisterDelegation, IOptionalSystemHook { bytes32 playerEntityId = getEntityFromPlayer(delegatorAddress); require(playerEntityId != bytes32(0), "Player entity not found"); VoxelCoord memory playerPosition = getPosition(playerEntityId); - VoxelCoord memory dropCoord = getEmptyBlockOnGround(playerPosition); + VoxelCoord memory dropCoord = getEmptyBlockOnGround(biomeWorldAddress, playerPosition); bytes memory dropCallData = abi.encodeCall(IDropSystem.dropTool, (toolEntityId, dropCoord)); @@ -222,13 +200,57 @@ contract Game is ICustomUnregisterDelegation, IOptionalSystemHook { return allowedItemDrops; } - function getDisplayName() external view returns (string memory) { - return "Build For Drops"; - } - function getBuilds() external view returns (NamedBuild[] memory) { NamedBuild[] memory builds = new NamedBuild[](1); builds[0] = NamedBuild({ name: "Logo", build: build }); return builds; } + + function getDisplayName() external view returns (string memory) { + return "Build For Drops"; + } + + function getStatus() external view returns (string memory) { + if (msg.sender == delegatorAddress) { + if (build.objectTypeIds.length == 0) { + return "Build not set yet. Please set the build."; + } + + if (allowedItemDrops.length == 0) { + return "No players have been submitted matching builds. Please wait."; + } + + return string.concat("Build set. ", Strings.toString(allowedItemDrops.length), " players allowed to drop items."); + } else { + if (build.objectTypeIds.length == 0) { + return "Build not set yet. Please wait."; + } + + bool isAllowed = false; + for (uint i = 0; i < allowedItemDrops.length; i++) { + if (allowedItemDrops[i] == msg.sender) { + isAllowed = true; + break; + } + } + if (isAllowed) { + return "Allowed to drop items!"; + } else { + return "Not allowed to drop items. Build and submit to be allowed."; + } + } + } + + function getUnregisterMessage() external view returns (string memory) { + if (msg.sender == delegatorAddress && allowedItemDrops.length > 0) { + return + string.concat( + "You cannot unregister until all ", + Strings.toString(allowedItemDrops.length), + " players have used their allowed item drops." + ); + } + + return ""; + } } diff --git a/packages/nextjs/app/page.tsx b/packages/nextjs/app/page.tsx index 2649bb6d..a227533e 100644 --- a/packages/nextjs/app/page.tsx +++ b/packages/nextjs/app/page.tsx @@ -18,7 +18,8 @@ const Home: NextPage = () => { const isBiomesRegistered = useGlobalState(({ isBiomesRegistered }) => isBiomesRegistered); // const isGameRegistered = useGlobalState(({ isGameRegistered }) => isGameRegistered); const isGameRegistered = true; - const isBiomesClientSetup = useGlobalState(({ isBiomesClientSetup }) => isBiomesClientSetup); + // const isBiomesClientSetup = useGlobalState(({ isBiomesClientSetup }) => isBiomesClientSetup); + const isBiomesClientSetup = true; useEffect(() => { if (connectedAddress) { diff --git a/packages/nextjs/contracts/deployedContracts.ts b/packages/nextjs/contracts/deployedContracts.ts index 196f6dd1..05f5339a 100644 --- a/packages/nextjs/contracts/deployedContracts.ts +++ b/packages/nextjs/contracts/deployedContracts.ts @@ -264,6 +264,32 @@ const deployedContracts = { stateMutability: "view", type: "function", }, + { + inputs: [], + name: "getStatus", + outputs: [ + { + internalType: "string", + name: "", + type: "string", + }, + ], + stateMutability: "view", + type: "function", + }, + { + inputs: [], + name: "getUnregisterMessage", + outputs: [ + { + internalType: "string", + name: "", + type: "string", + }, + ], + stateMutability: "view", + type: "function", + }, { inputs: [ {