Skip to content

Commit

Permalink
refactor: format codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
0xibs committed Sep 28, 2024
1 parent 45f360b commit 4c4e68e
Show file tree
Hide file tree
Showing 16 changed files with 745 additions and 757 deletions.
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@
"typescript": "^5.5.4",
"vite": "^4.3.9"
}
}
}
14 changes: 7 additions & 7 deletions client/src/dojo/createClientComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { ContractComponents } from "./generated/contractComponents";
export type ClientComponents = ReturnType<typeof createClientComponents>;

export function createClientComponents({
contractComponents,
contractComponents,
}: {
contractComponents: ContractComponents;
contractComponents: ContractComponents;
}) {
return {
...contractComponents,
Position: overridableComponent(contractComponents.Position),
Moves: overridableComponent(contractComponents.Moves),
};
return {
...contractComponents,
Position: overridableComponent(contractComponents.Position),
Moves: overridableComponent(contractComponents.Moves),
};
}
225 changes: 107 additions & 118 deletions client/src/dojo/createSystemCalls.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { AccountInterface } from "starknet";
import {
Entity,
Has,
HasValue,
World,
defineSystem,
getComponentValue,
Entity,
Has,
HasValue,
World,
defineSystem,
getComponentValue,
} from "@dojoengine/recs";
import { uuid } from "@latticexyz/utils";
import { ClientComponents } from "./createClientComponents";
Expand All @@ -16,127 +16,116 @@ import type { IWorld } from "./generated/generated";
export type SystemCalls = ReturnType<typeof createSystemCalls>;

export function createSystemCalls(
{ client }: { client: IWorld },
{ Position, Moves }: ClientComponents,
world: World
{ client }: { client: IWorld },
{ Position, Moves }: ClientComponents,
world: World,
) {
const spawn = async (account: AccountInterface) => {
const entityId = getEntityIdFromKeys([
BigInt(account.address),
]) as Entity;
const spawn = async (account: AccountInterface) => {
const entityId = getEntityIdFromKeys([BigInt(account.address)]) as Entity;

const movesId = uuid();
Moves.addOverride(movesId, {
entity: entityId,
value: {
player: BigInt(entityId),
remaining:
(getComponentValue(Moves, entityId)?.remaining || 0) + 100,
},
});
const movesId = uuid();
Moves.addOverride(movesId, {
entity: entityId,
value: {
player: BigInt(entityId),
remaining: (getComponentValue(Moves, entityId)?.remaining || 0) + 100,
},
});

const positionId = uuid();
Position.addOverride(positionId, {
entity: entityId,
value: {
player: BigInt(entityId),
vec: {
x: 10 + (getComponentValue(Position, entityId)?.vec.x || 0),
y: 10 + (getComponentValue(Position, entityId)?.vec.y || 0),
},
},
});
const positionId = uuid();
Position.addOverride(positionId, {
entity: entityId,
value: {
player: BigInt(entityId),
vec: {
x: 10 + (getComponentValue(Position, entityId)?.vec.x || 0),
y: 10 + (getComponentValue(Position, entityId)?.vec.y || 0),
},
},
});

try {
await client.actions.spawn({
account,
});
try {
await client.actions.spawn({
account,
});

// Wait for the indexer to update the entity
// By doing this we keep the optimistic UI in sync with the actual state
await new Promise<void>((resolve) => {
defineSystem(
world,
[
Has(Moves),
HasValue(Moves, { player: BigInt(account.address) }),
],
() => {
resolve();
}
);
});
} catch (e) {
console.log(e);
Position.removeOverride(positionId);
Moves.removeOverride(movesId);
} finally {
Position.removeOverride(positionId);
Moves.removeOverride(movesId);
}
};
// Wait for the indexer to update the entity
// By doing this we keep the optimistic UI in sync with the actual state
await new Promise<void>((resolve) => {
defineSystem(
world,
[Has(Moves), HasValue(Moves, { player: BigInt(account.address) })],
() => {
resolve();
},
);
});
} catch (e) {
console.log(e);
Position.removeOverride(positionId);
Moves.removeOverride(movesId);
} finally {
Position.removeOverride(positionId);
Moves.removeOverride(movesId);
}
};

const move = async (account: AccountInterface, direction: Direction) => {
const entityId = getEntityIdFromKeys([
BigInt(account.address),
]) as Entity;
const move = async (account: AccountInterface, direction: Direction) => {
const entityId = getEntityIdFromKeys([BigInt(account.address)]) as Entity;

// Update the state before the transaction
// const positionId = uuid();
// Position.addOverride(positionId, {
// entity: entityId,
// value: {
// player: BigInt(entityId),
// vec: updatePositionWithDirection(
// direction,
// getComponentValue(Position, entityId) as any
// ).vec,
// },
// });
// Update the state before the transaction
// const positionId = uuid();
// Position.addOverride(positionId, {
// entity: entityId,
// value: {
// player: BigInt(entityId),
// vec: updatePositionWithDirection(
// direction,
// getComponentValue(Position, entityId) as any
// ).vec,
// },
// });

// // Update the state before the transaction
// const movesId = uuid();
// Moves.addOverride(movesId, {
// entity: entityId,
// value: {
// player: BigInt(entityId),
// remaining:
// (getComponentValue(Moves, entityId)?.remaining || 0) - 1,
// },
// });
// // Update the state before the transaction
// const movesId = uuid();
// Moves.addOverride(movesId, {
// entity: entityId,
// value: {
// player: BigInt(entityId),
// remaining:
// (getComponentValue(Moves, entityId)?.remaining || 0) - 1,
// },
// });

try {
await client.actions.move({
account,
direction,
});
try {
await client.actions.move({
account,
direction,
});

// Wait for the indexer to update the entity
// By doing this we keep the optimistic UI in sync with the actual state
await new Promise<void>((resolve) => {
defineSystem(
world,
[
Has(Moves),
HasValue(Moves, { player: BigInt(account.address) }),
],
() => {
resolve();
}
);
});
} catch (e) {
console.log(e);
// Position.removeOverride(positionId);
// Moves.removeOverride(movesId);
} finally {
// Position.removeOverride(positionId);
// Moves.removeOverride(movesId);
}
};
// Wait for the indexer to update the entity
// By doing this we keep the optimistic UI in sync with the actual state
await new Promise<void>((resolve) => {
defineSystem(
world,
[Has(Moves), HasValue(Moves, { player: BigInt(account.address) })],
() => {
resolve();
},
);
});
} catch (e) {
console.log(e);
// Position.removeOverride(positionId);
// Moves.removeOverride(movesId);
} finally {
// Position.removeOverride(positionId);
// Moves.removeOverride(movesId);
}
};

return {
spawn,
move,
};
return {
spawn,
move,
};
}
100 changes: 50 additions & 50 deletions client/src/dojo/generated/contractComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,57 @@
import { defineComponent, Type as RecsType, World } from "@dojoengine/recs";

export type ContractComponents = Awaited<
ReturnType<typeof defineContractComponents>
ReturnType<typeof defineContractComponents>
>;

export function defineContractComponents(world: World) {
return {
DirectionsAvailable: (() => {
return defineComponent(
world,
{ player: RecsType.BigInt, directions: RecsType.StringArray },
{
metadata: {
name: "dojo_starter-DirectionsAvailable",
types: ["contractaddress"],
customTypes: ["Direction"],
},
}
);
})(),
Moves: (() => {
return defineComponent(
world,
{
player: RecsType.BigInt,
remaining: RecsType.Number,
last_direction: RecsType.Number,
can_move: RecsType.Boolean,
},
{
metadata: {
name: "dojo_starter-Moves",
types: ["contractaddress", "u8", "enum", "bool"],
customTypes: ["Direction"],
},
}
);
})(),
Position: (() => {
return defineComponent(
world,
{
player: RecsType.BigInt,
vec: { x: RecsType.Number, y: RecsType.Number },
},
{
metadata: {
name: "dojo_starter-Position",
types: ["contractaddress", "u32", "u32"],
customTypes: ["Vec2"],
},
}
);
})(),
};
return {
DirectionsAvailable: (() => {
return defineComponent(
world,
{ player: RecsType.BigInt, directions: RecsType.StringArray },
{
metadata: {
name: "dojo_starter-DirectionsAvailable",
types: ["contractaddress"],
customTypes: ["Direction"],
},
},
);
})(),
Moves: (() => {
return defineComponent(
world,
{
player: RecsType.BigInt,
remaining: RecsType.Number,
last_direction: RecsType.Number,
can_move: RecsType.Boolean,
},
{
metadata: {
name: "dojo_starter-Moves",
types: ["contractaddress", "u8", "enum", "bool"],
customTypes: ["Direction"],
},
},
);
})(),
Position: (() => {
return defineComponent(
world,
{
player: RecsType.BigInt,
vec: { x: RecsType.Number, y: RecsType.Number },
},
{
metadata: {
name: "dojo_starter-Position",
types: ["contractaddress", "u32", "u32"],
customTypes: ["Vec2"],
},
},
);
})(),
};
}
Loading

0 comments on commit 4c4e68e

Please sign in to comment.