Skip to content

Commit

Permalink
refactor(ri): refactor all Type components to use solidity enums (#93)
Browse files Browse the repository at this point in the history
* refactor(ri): remove specific Item components (Gold, EmberCrown)

* refactor(ri/contracts): use enum in contracts for ItemType

* refactor(ri/contracts): use solidity enums for all *Types
  • Loading branch information
Kooshaba committed Jul 14, 2022
1 parent a6cb9fd commit ca7fa1e
Show file tree
Hide file tree
Showing 22 changed files with 117 additions and 78 deletions.
14 changes: 1 addition & 13 deletions packages/ri/client/src/layers/Network/createNetworkLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export async function createNetworkLayer(config: NetworkLayerConfig) {
),
Factory: defineComponent(
world,
{ prototypeIds: Type.StringArray, costs: Type.NumberArray },
{ prototypeIds: Type.StringArray, costs: Type.NumberArray, costItemTypes: Type.NumberArray },
{ id: "Factory", metadata: { contractId: keccak256("ember.component.factoryComponent") } }
),
Capturable: defineComponent(
Expand All @@ -139,21 +139,11 @@ export async function createNetworkLayer(config: NetworkLayerConfig) {
{ value: Type.Number },
{ id: "Inventory", metadata: { contractId: keccak256("ember.component.inventoryComponent") } }
),
Gold: defineComponent(
world,
{ value: Type.Boolean },
{ id: "Gold", metadata: { contractId: keccak256("ember.component.goldComponent") } }
),
ResourceGenerator: defineComponent(
world,
{ value: Type.String },
{ id: "ResourceGenerator", metadata: { contractId: keccak256("ember.component.resourceGenerator") } }
),
EmberCrown: defineComponent(
world,
{ value: Type.Boolean },
{ id: "EmberCrown", metadata: { contractId: keccak256("ember.component.emberCrown") } }
),
EscapePortal: defineComponent(
world,
{ value: Type.Boolean },
Expand Down Expand Up @@ -190,9 +180,7 @@ export async function createNetworkLayer(config: NetworkLayerConfig) {
[keccak256("ember.component.capturable")]: "Capturable",
[keccak256("ember.component.spawnPoint")]: "SpawnPoint",
[keccak256("ember.component.inventoryComponent")]: "Inventory",
[keccak256("ember.component.goldComponent")]: "Gold",
[keccak256("ember.component.resourceGenerator")]: "ResourceGenerator",
[keccak256("ember.component.emberCrown")]: "EmberCrown",
[keccak256("ember.component.escapePortal")]: "EscapePortal",
[keccak256("ember.component.winner")]: "Winner",
};
Expand Down
2 changes: 0 additions & 2 deletions packages/ri/contracts/deploy.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
"ItemTypeComponent",
"AttackComponent",
"CapturableComponent",
"EmberCrownComponent",
"FactoryComponent",
"FromBlueprintComponent",
"GameConfigComponent",
"GoldComponent",
"HealthComponent",
"InventoryComponent",
"LastActionTurnComponent",
Expand Down
9 changes: 0 additions & 9 deletions packages/ri/contracts/src/components/EmberCrownComponent.sol

This file was deleted.

17 changes: 12 additions & 5 deletions packages/ri/contracts/src/components/FactoryComponent.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "solecs/Component.sol";
struct Factory {
uint256[] prototypeIds;
int32[] costs;
uint32[] costItemTypes;
}

uint256 constant ID = uint256(keccak256("ember.component.factoryComponent"));
Expand All @@ -13,30 +14,36 @@ contract FactoryComponent is Component {
constructor(address world) Component(world, ID) {}

function getSchema() public pure override returns (string[] memory keys, LibTypes.SchemaValue[] memory values) {
keys = new string[](2);
values = new LibTypes.SchemaValue[](2);
keys = new string[](3);
values = new LibTypes.SchemaValue[](3);

keys[0] = "prototypeIds";
values[0] = LibTypes.SchemaValue.UINT256_ARRAY;

keys[1] = "costs";
values[1] = LibTypes.SchemaValue.INT32_ARRAY;

keys[2] = "costItemTypes";
values[2] = LibTypes.SchemaValue.UINT32_ARRAY;
}

function set(uint256 entity, Factory calldata value) public {
set(entity, encodedValue(value));
}

function getValue(uint256 entity) public view returns (Factory memory) {
(uint256[] memory prototypeIds, int32[] memory costs) = abi.decode(getRawValue(entity), (uint256[], int32[]));
return Factory(prototypeIds, costs);
(uint256[] memory prototypeIds, int32[] memory costs, uint32[] memory costItemTypes) = abi.decode(
getRawValue(entity),
(uint256[], int32[], uint32[])
);
return Factory(prototypeIds, costs, costItemTypes);
}

function getEntitiesWithValue(Factory calldata factory) public view returns (uint256[] memory) {
return getEntitiesWithValue(encodedValue(factory));
}

function encodedValue(Factory calldata factory) private pure returns (bytes memory) {
return abi.encode(factory.prototypeIds, factory.costs);
return abi.encode(factory.prototypeIds, factory.costs, factory.costItemTypes);
}
}
9 changes: 0 additions & 9 deletions packages/ri/contracts/src/components/GoldComponent.sol

This file was deleted.

4 changes: 3 additions & 1 deletion packages/ri/contracts/src/prototypes/DonkeyPrototype.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import { HealthComponent, Health, ID as HealthComponentID } from "../components/
import { AttackComponent, Attack, ID as AttackComponentID } from "../components/AttackComponent.sol";
import { UntraversableComponent, ID as UntraversableComponentID } from "../components/UntraversableComponent.sol";

import { UnitTypes } from "../utils/Types.sol";

import { ID as InventoryID } from "./InventoryPrototype.sol";

uint256 constant ID = uint256(keccak256("ember.prototype.donkey"));

function DonkeyPrototype(IUint256Component components, IWorld world) {
UnitTypeComponent(getAddressById(components, UnitTypeComponentID)).set(ID, uint32(1));
UnitTypeComponent(getAddressById(components, UnitTypeComponentID)).set(ID, uint32(UnitTypes.Donkey));
StaminaComponent(getAddressById(components, StaminaComponentID)).set(
ID,
Stamina({ current: 0, max: 4, regeneration: 2 })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import { PrototypeComponent, ID as PrototypeComponentID } from "../components/Pr
import { ItemTypeComponent, ID as ItemTypeComponentID } from "../components/ItemTypeComponent.sol";
import { InventoryComponent, ID as InventoryComponentID } from "../components/InventoryComponent.sol";

import { ItemTypes } from "../utils/Types.sol";

import { ID as EmberCrownID } from "./EmberCrownPrototype.sol";

uint256 constant ID = uint256(keccak256("ember.prototype.emberCrownInventory"));

function EmberCrownInventoryPrototype(IUint256Component components, IWorld world) {
InventoryComponent(getAddressById(components, InventoryComponentID)).set(ID, int32(3));
ItemTypeComponent(getAddressById(components, ItemTypeComponentID)).set(ID, uint32(0));
ItemTypeComponent(getAddressById(components, ItemTypeComponentID)).set(ID, uint32(ItemTypes.Inventory));

uint256[] memory componentIds = new uint256[](2);
componentIds[0] = InventoryComponentID;
Expand Down
11 changes: 5 additions & 6 deletions packages/ri/contracts/src/prototypes/EmberCrownPrototype.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ import { getAddressById } from "solecs/utils.sol";

import { PrototypeComponent, ID as PrototypeComponentID } from "../components/PrototypeComponent.sol";
import { ItemTypeComponent, ID as ItemTypeComponentID } from "../components/ItemTypeComponent.sol";
import { EmberCrownComponent, ID as EmberCrownComponentID } from "../components/EmberCrownComponent.sol";

import { ItemTypes } from "../utils/Types.sol";

uint256 constant ID = uint256(keccak256("ember.prototype.emberCrown"));

function EmberCrownPrototype(IUint256Component components) {
EmberCrownComponent(getAddressById(components, EmberCrownComponentID)).set(ID);
ItemTypeComponent(getAddressById(components, ItemTypeComponentID)).set(ID, uint32(2));
ItemTypeComponent(getAddressById(components, ItemTypeComponentID)).set(ID, uint32(ItemTypes.EmberCrown));

uint256[] memory componentIds = new uint256[](2);
componentIds[0] = EmberCrownComponentID;
componentIds[1] = ItemTypeComponentID;
uint256[] memory componentIds = new uint256[](1);
componentIds[0] = ItemTypeComponentID;

PrototypeComponent(getAddressById(components, PrototypeComponentID)).set(ID, componentIds);
}
15 changes: 10 additions & 5 deletions packages/ri/contracts/src/prototypes/EmptySettlementPrototype.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ import { SpawnPointComponent, ID as SpawnPointComponentID } from "../components/
import { UntraversableComponent, ID as UntraversableComponentID } from "../components/UntraversableComponent.sol";
import { InventoryComponent, ID as InventoryComponentID } from "../components/InventoryComponent.sol";

import { StructureTypes } from "../utils/Types.sol";

import { ID as SoldierID } from "./SoldierPrototype.sol";
import { ID as InventoryID } from "./InventoryPrototype.sol";

uint256 constant ID = uint256(keccak256("ember.prototype.emptySettlement"));

function EmptySettlementPrototype(IUint256Component components, IWorld world) {
StructureTypeComponent(getAddressById(components, StructureTypeComponentID)).set(ID, uint32(0));
StructureTypeComponent(getAddressById(components, StructureTypeComponentID)).set(
ID,
uint32(StructureTypes.Settlement)
);
StaminaComponent(getAddressById(components, StaminaComponentID)).set(
ID,
Stamina({ current: 0, max: 20, regeneration: 1 })
Expand All @@ -40,10 +45,10 @@ function EmptySettlementPrototype(IUint256Component components, IWorld world) {
int32[] memory costs = new int32[](1);
costs[0] = 1;

FactoryComponent(getAddressById(components, FactoryComponentID)).set(
ID,
Factory({ prototypeIds: prototypeIds, costs: costs })
);
uint32[] memory costItemTypes = new uint32[](1);
costItemTypes[0] = 1;

FactoryComponent(getAddressById(components, FactoryComponentID)).set(ID, Factory(prototypeIds, costs, costItemTypes));

SpawnPointComponent(getAddressById(components, SpawnPointComponentID)).set(ID);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ import { PrototypeComponent, ID as PrototypeComponentID } from "../components/Pr
import { StructureTypeComponent, ID as StructureTypeComponentID } from "../components/StructureTypeComponent.sol";
import { EscapePortalComponent, ID as EscapePortalComponentID } from "../components/EscapePortalComponent.sol";

import { StructureTypes } from "../utils/Types.sol";

uint256 constant ID = uint256(keccak256("ember.prototype.escapePortal"));

function EscapePortalPrototype(IUint256Component components) {
EscapePortalComponent(getAddressById(components, EscapePortalComponentID)).set(ID);
StructureTypeComponent(getAddressById(components, StructureTypeComponentID)).set(ID, uint32(2));
StructureTypeComponent(getAddressById(components, StructureTypeComponentID)).set(
ID,
uint32(StructureTypes.EscapePortal)
);

uint256[] memory componentIds = new uint256[](2);
componentIds[0] = EscapePortalComponentID;
Expand Down
11 changes: 5 additions & 6 deletions packages/ri/contracts/src/prototypes/GoldPrototype.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ import { getAddressById } from "solecs/utils.sol";

import { PrototypeComponent, ID as PrototypeComponentID } from "../components/PrototypeComponent.sol";
import { ItemTypeComponent, ID as ItemTypeComponentID } from "../components/ItemTypeComponent.sol";
import { GoldComponent, ID as GoldComponentID } from "../components/GoldComponent.sol";

import { ItemTypes } from "../utils/Types.sol";

uint256 constant ID = uint256(keccak256("ember.prototype.gold"));

function GoldPrototype(IUint256Component components) {
GoldComponent(getAddressById(components, GoldComponentID)).set(ID);
ItemTypeComponent(getAddressById(components, ItemTypeComponentID)).set(ID, uint32(1));
ItemTypeComponent(getAddressById(components, ItemTypeComponentID)).set(ID, uint32(ItemTypes.Gold));

uint256[] memory componentIds = new uint256[](2);
componentIds[0] = GoldComponentID;
componentIds[1] = ItemTypeComponentID;
uint256[] memory componentIds = new uint256[](1);
componentIds[0] = ItemTypeComponentID;

PrototypeComponent(getAddressById(components, PrototypeComponentID)).set(ID, componentIds);
}
7 changes: 6 additions & 1 deletion packages/ri/contracts/src/prototypes/GoldShrinePrototype.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ import { ResourceGeneratorComponent, ID as ResourceGeneratorComponentID } from "
import { LastActionTurnComponent, ID as LastActionTurnComponentID } from "../components/LastActionTurnComponent.sol";
import { UntraversableComponent, ID as UntraversableComponentID } from "../components/UntraversableComponent.sol";

import { StructureTypes } from "../utils/Types.sol";

import { ID as GoldID } from "./GoldPrototype.sol";

uint256 constant ID = uint256(keccak256("ember.prototype.goldShrine"));

function GoldShrinePrototype(IUint256Component components) {
StructureTypeComponent(getAddressById(components, StructureTypeComponentID)).set(ID, uint32(1));
StructureTypeComponent(getAddressById(components, StructureTypeComponentID)).set(
ID,
uint32(StructureTypes.GoldShrine)
);
StaminaComponent(getAddressById(components, StaminaComponentID)).set(
ID,
Stamina({ current: 0, max: 6, regeneration: 1 })
Expand Down
4 changes: 3 additions & 1 deletion packages/ri/contracts/src/prototypes/GrassPrototype.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { getAddressById } from "solecs/utils.sol";
import { PrototypeComponent, ID as PrototypeComponentID } from "../components/PrototypeComponent.sol";
import { TerrainTypeComponent, ID as TerrainTypeComponentID } from "../components/TerrainTypeComponent.sol";

import { TerrainTypes } from "../utils/Types.sol";

uint256 constant ID = uint256(keccak256("ember.prototype.grass"));

function GrassPrototype(IUint256Component components) {
TerrainTypeComponent(getAddressById(components, TerrainTypeComponentID)).set(ID, uint32(0));
TerrainTypeComponent(getAddressById(components, TerrainTypeComponentID)).set(ID, uint32(TerrainTypes.Grass));

uint256[] memory componentIds = new uint256[](1);
componentIds[0] = TerrainTypeComponentID;
Expand Down
4 changes: 3 additions & 1 deletion packages/ri/contracts/src/prototypes/GuardPrototype.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import { HealthComponent, Health, ID as HealthComponentID } from "../components/
import { AttackComponent, Attack, ID as AttackComponentID } from "../components/AttackComponent.sol";
import { UntraversableComponent, ID as UntraversableComponentID } from "../components/UntraversableComponent.sol";

import { UnitTypes } from "../utils/Types.sol";

import { ID as InventoryID } from "./InventoryPrototype.sol";

uint256 constant ID = uint256(keccak256("ember.prototype.guard"));

function GuardPrototype(IUint256Component components, IWorld world) {
UnitTypeComponent(getAddressById(components, UnitTypeComponentID)).set(ID, uint32(0));
UnitTypeComponent(getAddressById(components, UnitTypeComponentID)).set(ID, uint32(UnitTypes.Hero));
StaminaComponent(getAddressById(components, StaminaComponentID)).set(
ID,
Stamina({ current: 0, max: 3, regeneration: 1 })
Expand Down
4 changes: 3 additions & 1 deletion packages/ri/contracts/src/prototypes/InventoryPrototype.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import { PrototypeComponent, ID as PrototypeComponentID } from "../components/Pr
import { ItemTypeComponent, ID as ItemTypeComponentID } from "../components/ItemTypeComponent.sol";
import { InventoryComponent, ID as InventoryComponentID } from "../components/InventoryComponent.sol";

import { ItemTypes } from "../utils/Types.sol";

uint256 constant ID = uint256(keccak256("ember.prototype.inventory"));

function InventoryPrototype(IUint256Component components) {
InventoryComponent(getAddressById(components, InventoryComponentID)).set(ID, int32(3));
ItemTypeComponent(getAddressById(components, ItemTypeComponentID)).set(ID, uint32(0));
ItemTypeComponent(getAddressById(components, ItemTypeComponentID)).set(ID, uint32(ItemTypes.Inventory));

uint256[] memory componentIds = new uint256[](2);
componentIds[0] = InventoryComponentID;
Expand Down
15 changes: 10 additions & 5 deletions packages/ri/contracts/src/prototypes/SettlementPrototype.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ import { InventoryComponent, ID as InventoryComponentID } from "../components/In
import { UntraversableComponent, ID as UntraversableComponentID } from "../components/UntraversableComponent.sol";
import { LastActionTurnComponent, ID as LastActionTurnComponentID } from "../components/LastActionTurnComponent.sol";

import { StructureTypes } from "../utils/Types.sol";

import { ID as SoldierID } from "./SoldierPrototype.sol";
import { ID as InventoryID } from "./InventoryPrototype.sol";

uint256 constant ID = uint256(keccak256("ember.prototype.settlement"));

function SettlementPrototype(IUint256Component components, IWorld world) {
StructureTypeComponent(getAddressById(components, StructureTypeComponentID)).set(ID, uint32(0));
StructureTypeComponent(getAddressById(components, StructureTypeComponentID)).set(
ID,
uint32(StructureTypes.Settlement)
);
StaminaComponent(getAddressById(components, StaminaComponentID)).set(
ID,
Stamina({ current: 0, max: 5, regeneration: 1 })
Expand All @@ -41,10 +46,10 @@ function SettlementPrototype(IUint256Component components, IWorld world) {
int32[] memory costs = new int32[](1);
costs[0] = 1;

FactoryComponent(getAddressById(components, FactoryComponentID)).set(
ID,
Factory({ prototypeIds: prototypeIds, costs: costs })
);
uint32[] memory costItemTypes = new uint32[](1);
costItemTypes[0] = 1;

FactoryComponent(getAddressById(components, FactoryComponentID)).set(ID, Factory(prototypeIds, costs, costItemTypes));

uint256[] memory componentIds = new uint256[](8);
componentIds[0] = StructureTypeComponentID;
Expand Down
4 changes: 3 additions & 1 deletion packages/ri/contracts/src/prototypes/SoldierPrototype.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import { HealthComponent, Health, ID as HealthComponentID } from "../components/
import { AttackComponent, Attack, ID as AttackComponentID } from "../components/AttackComponent.sol";
import { UntraversableComponent, ID as UntraversableComponentID } from "../components/UntraversableComponent.sol";

import { UnitTypes } from "../utils/Types.sol";

import { ID as InventoryID } from "./InventoryPrototype.sol";

uint256 constant ID = uint256(keccak256("ember.prototype.soldier"));

function SoldierPrototype(IUint256Component components, IWorld world) {
UnitTypeComponent(getAddressById(components, UnitTypeComponentID)).set(ID, uint32(0));
UnitTypeComponent(getAddressById(components, UnitTypeComponentID)).set(ID, uint32(UnitTypes.Hero));
StaminaComponent(getAddressById(components, StaminaComponentID)).set(
ID,
Stamina({ current: 0, max: 3, regeneration: 1 })
Expand Down
4 changes: 3 additions & 1 deletion packages/ri/contracts/src/prototypes/TreePrototype.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import { PrototypeComponent, ID as PrototypeComponentID } from "../components/Pr
import { TerrainTypeComponent, ID as TerrainTypeComponentID } from "../components/TerrainTypeComponent.sol";
import { UntraversableComponent, ID as UntraversableComponentID } from "../components/UntraversableComponent.sol";

import { TerrainTypes } from "../utils/Types.sol";

uint256 constant ID = uint256(keccak256("ember.prototype.tree"));

function TreePrototype(IUint256Component components) {
TerrainTypeComponent(getAddressById(components, TerrainTypeComponentID)).set(ID, uint32(4));
TerrainTypeComponent(getAddressById(components, TerrainTypeComponentID)).set(ID, uint32(TerrainTypes.Tree));
UntraversableComponent(getAddressById(components, UntraversableComponentID)).set(ID);

uint256[] memory componentIds = new uint256[](2);
Expand Down
4 changes: 3 additions & 1 deletion packages/ri/contracts/src/prototypes/WaterPrototype.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import { PrototypeComponent, ID as PrototypeComponentID } from "../components/Pr
import { TerrainTypeComponent, ID as TerrainTypeComponentID } from "../components/TerrainTypeComponent.sol";
import { UntraversableComponent, ID as UntraversableComponentID } from "../components/UntraversableComponent.sol";

import { TerrainTypes } from "../utils/Types.sol";

uint256 constant ID = uint256(keccak256("ember.prototype.water"));

function WaterPrototype(IUint256Component components) {
TerrainTypeComponent(getAddressById(components, TerrainTypeComponentID)).set(ID, uint32(2));
TerrainTypeComponent(getAddressById(components, TerrainTypeComponentID)).set(ID, uint32(TerrainTypes.Water));
UntraversableComponent(getAddressById(components, UntraversableComponentID)).set(ID);

uint256[] memory componentIds = new uint256[](2);
Expand Down

0 comments on commit ca7fa1e

Please sign in to comment.