Skip to content

Commit

Permalink
add examples of wiring up system actions
Browse files Browse the repository at this point in the history
  • Loading branch information
willemolding committed Dec 27, 2023
1 parent c9ef539 commit 629cf42
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
28 changes: 19 additions & 9 deletions client/src/dojo/createSystemCalls.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { SetupNetworkResult } from "./setupNetwork";
import { ClientComponents } from "./createClientComponents";

import { toast } from 'react-toastify';

import { Account, num } from "starknet";

//HERE
import { Account } from "starknet";

export type SystemCalls = ReturnType<typeof createSystemCalls>;

Expand Down Expand Up @@ -70,6 +65,21 @@ export function createSystemCalls(
}
};

const interact = async (account: Account, gameId: number, itemId: number) => {
try {
const tx = await execute(account, "spellcrafter_system", "interact", [gameId, itemId]);
const receipt = await account.waitForTransaction(
tx.transaction_hash,
{ retryInterval: 100 }
)
console.log(receipt)
notify('Successfully foraged!', true)
} catch (e) {
console.log(e)
notify(`Error foraging ${e}`, false)
}
};

const summon = async (account: Account, gameId: number, familiarType: number) => {
try {
const tx = await execute(account, "spellcrafter_system", "summon", [gameId, familiarType]);
Expand Down Expand Up @@ -117,7 +127,7 @@ export function createSystemCalls(

const reapAction = async (account: Account, gameId: number, entityId: number) => {
try {
const tx = await execute(account, "spellcrafter_system", "sacrifice", [gameId, entityId]);
const tx = await execute(account, "spellcrafter_system", "reap_action", [gameId, entityId]);
const receipt = await account.waitForTransaction(
tx.transaction_hash,
{ retryInterval: 100 }
Expand All @@ -133,7 +143,7 @@ export function createSystemCalls(

const wait = async (account: Account, gameId: number) => {
try {
const tx = await execute(account, "spellcrafter_system", "sacrifice", [gameId]);
const tx = await execute(account, "spellcrafter_system", "wait", [gameId]);
const receipt = await account.waitForTransaction(
tx.transaction_hash,
{ retryInterval: 100 }
Expand All @@ -146,10 +156,10 @@ export function createSystemCalls(
}
};


return {
createGame,
forage,
interact,
summon,
send,
reapAction,
Expand Down
10 changes: 8 additions & 2 deletions client/src/ui/pages/gamePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const GamePage: React.FC = () => {
const {
account: { account },
networkLayer: {
systemCalls: { forage },
systemCalls: { forage, interact },
network: { graphSdk }
},
} = useDojo();
Expand All @@ -28,6 +28,12 @@ export const GamePage: React.FC = () => {
await forage(account, parseInt(currentGameId), 0);
}

const doInteract = async (account: Account, cardId: number) => {
//create a new game by sending a transaction
if (!currentGameId) return;
await interact(account, parseInt(currentGameId), cardId);
}

useEffect(() => {
const fetchGameData = async () => {
if(!currentGameId) return;
Expand All @@ -51,7 +57,7 @@ export const GamePage: React.FC = () => {
Forage
</div>

<div className="global-button-style" style={{ fontSize: "2.4cqw", padding: "5px 10px", fontFamily: "OL", fontWeight: "100" }} onClick={() => { doForage(account)}}>
<div className="global-button-style" style={{ fontSize: "2.4cqw", padding: "5px 10px", fontFamily: "OL", fontWeight: "100" }} onClick={() => { doInteract(account, gameState.cards[0][0])}}>
Interact
</div>

Expand Down

0 comments on commit 629cf42

Please sign in to comment.