Skip to content

Commit

Permalink
Merge pull request #185 from dojoengine/recs-enum
Browse files Browse the repository at this point in the history
feat: parses enums as string
  • Loading branch information
ponderingdemocritus authored Apr 9, 2024
2 parents 149feee + 95df756 commit 25aba4d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
4 changes: 4 additions & 0 deletions examples/react/react-app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ function App() {
const position = useComponentValue(Position, entityId);
const moves = useComponentValue(Moves, entityId);

console.log(moves);

const handleRestoreBurners = async () => {
try {
await account?.applyFromClipboard();
Expand Down Expand Up @@ -112,6 +114,8 @@ function App() {
? `${position.vec.x}, ${position.vec.y}`
: "Need to Spawn"}
</div>

<div>{moves && moves.last_direction}</div>
</div>

<div className="card">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function defineContractComponents(world: World) {
{
player: RecsType.BigInt,
remaining: RecsType.Number,
last_direction: RecsType.Number,
last_direction: RecsType.String,
},
{
metadata: {
Expand Down
1 change: 1 addition & 0 deletions packages/core/bin/generateComponents.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const cairoToRecsType = {
u256: "RecsType.BigInt",
felt252: "RecsType.BigInt",
contractaddress: "RecsType.BigInt",
enum: "RecsType.String",
};

const manifestStr = fs.readFileSync(manifestPath, "utf8");
Expand Down
6 changes: 3 additions & 3 deletions packages/state/src/recs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ export const syncEntities = async <S extends Schema>(
client: Client,
components: Component<S, Metadata, undefined>[]
) => {
client.onEntityUpdated([], (entities: any) =>
setEntities(entities, components)
);
client.onEntityUpdated([], (entities: any) => {
setEntities(entities, components);
});
};

export const setEntities = async <S extends Schema>(
Expand Down
9 changes: 8 additions & 1 deletion packages/state/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,28 @@ export function convertValues(schema: Schema, values: any) {
const schemaType = schema[key];
const value = values[key];

console.log(schemaType, key, values[key]);

if (value === null || value === undefined) {
acc[key] = value;
return acc;
}

if (
// Check if the schemaType is a string, if so, assign the value directly
if (schemaType === RecsType.String) {
acc[key] = value;
} else if (
typeof schemaType === "object" &&
value &&
typeof value === "object"
) {
acc[key] = convertValues(schemaType, value);
} else {
// Convert to BigInt or Number based on schemaType
acc[key] =
schemaType === RecsType.BigInt ? BigInt(value) : Number(value);
}

return acc;
}, {});
}

0 comments on commit 25aba4d

Please sign in to comment.