Skip to content

Commit

Permalink
Merge pull request #23 from Cygnusfear/sing/feat-fetchallentitieskey-01
Browse files Browse the repository at this point in the history
Sing/feat fetchallentitieskey 01
  • Loading branch information
Cygnusfear authored Oct 11, 2023
2 parents b16c825 + 691c1e5 commit fa2ba54
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
packages/client/dist/*
9 changes: 6 additions & 3 deletions packages/client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const App = () => {
const {
components: { Counter },
systemCalls: {
mudIsPositionEmpty,
mudGetAllFacilityEntityMetadatas,
mudBuildFacility,
mudGetEntityMetadataAtPosition,
},
Expand Down Expand Up @@ -39,10 +39,13 @@ export const App = () => {
className="px-100 py-100 rounded-md border border-gray-300 bg-white text-base font-medium text-gray-700 shadow-sm hover:bg-gray-50"
onClick={async (event) => {
event.preventDefault();
console.log("mudIsPositionEmpty:", await mudIsPositionEmpty());
console.log(
"mudGetAllFacilityEntityMetadatas:",
await mudGetAllFacilityEntityMetadatas()
);
}}
>
mudIsPositionEmpty
mudGetAllFacilityEntityMetadatas
</button>
<button
type="button"
Expand Down
56 changes: 56 additions & 0 deletions packages/client/src/mud/createSystemCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,61 @@ export function createSystemCalls(
};
};

/*
example output
[
{
"entityKey": "0x5c6090c0461491a2941743bda5c3658bf1ea53bbd3edcde54e16205e18b45792",
"position": {
"x": 1,
"y": 0,
"z": 1,
"__staticData": "0x000000010000000000000001",
"__encodedLengths": "0x0000000000000000000000000000000000000000000000000000000000000000",
"__dynamicData": "0x"
},
"entityTypeId": {
"typeId": 10,
"__staticData": "0x0000000a"
},
"orientation": {
"yaw": 0,
"__staticData": "0x00000000"
},
"ownedBy": {
"owner": "0x70997970C51812dc3A010C7d01b50e0d17dc79C8",
"__staticData": "0x70997970c51812dc3a010c7d01b50e0d17dc79c8"
}
}
]
*/
const mudGetAllFacilityEntityMetadatas = async () => {
const allEntityKeys = await worldContract.read.getAllFacilityEntityKeys();

const allEntityMetadatas = [];

for (const entityKeyArray of allEntityKeys) {
for (const entityKey of entityKeyArray) {
const entityPos = await mudGetPosition(entityKey);
const entityTypeId = await mudGetEntityType(entityKey);
const orientation = await mudGetOrientation(entityKey);
const ownedBy = await mudGetOwnedBy(entityKey);

const entityMetadata = {
entityKey,
position: entityPos,
entityTypeId,
orientation,
ownedBy,
};

allEntityMetadatas.push(entityMetadata);
}
}

return allEntityMetadatas;
};

return {
increment,
mudGetEntityType,
Expand All @@ -126,5 +181,6 @@ export function createSystemCalls(
mudIsPositionEmpty,
mudBuildFacility,
mudGetEntityMetadataAtPosition,
mudGetAllFacilityEntityMetadatas,
};
}
5 changes: 5 additions & 0 deletions packages/contracts/src/systems/FacilitySystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity >=0.8.21;
import { System } from "@latticexyz/world/src/System.sol";
//import { getUniqueEntity } from "@latticexyz/world-modules/src/modules/uniqueentity/getUniqueEntity.sol";
import { getKeysWithValue } from "@latticexyz/world-modules/src/modules/keyswithvalue/getKeysWithValue.sol";
import { getKeysInTable } from "@latticexyz/world-modules/src/modules/keysintable/getKeysInTable.sol";
import { PackedCounter } from "@latticexyz/store/src/PackedCounter.sol";

import { Counter, Position, PositionTableId, Orientation, EntityType, OwnedBy } from "../codegen/index.sol";
Expand Down Expand Up @@ -117,4 +118,8 @@ contract FacilitySystem is System {
Orientation.deleteRecord(entityKey);
Position.deleteRecord(entityKey);
}

function getAllFacilityEntityKeys() public view returns (bytes32[][] memory) {
return getKeysInTable(PositionTableId);
}
}
4 changes: 4 additions & 0 deletions packages/contracts/test/FacilitySystemTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ contract FacilitySystemTest is MudTest {
bytes32 entityKey03 = world.buildFacility(entityTypeIdGroundLevel, 2, 0, 1, 0);
PositionData memory posData03 = Position.get(entityKey03);
assertEq(posData03.x, 2);

//should return all entityKeys
bytes32[][] memory allEntityKeys = world.getAllFacilityEntityKeys();
assertEq(allEntityKeys.length, 2);
}

function testDestroyFacility() public {
Expand Down

0 comments on commit fa2ba54

Please sign in to comment.