Skip to content

Commit

Permalink
feat: op buy and sell
Browse files Browse the repository at this point in the history
  • Loading branch information
noyyyy committed Apr 8, 2024
1 parent b01f947 commit 4ae3e6e
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 103 deletions.
1 change: 1 addition & 0 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"graphql-ws": "^5.15.0",
"loadash": "^1.0.0",
"lucide-react": "^0.292.0",
"micro-starknet": "^0.2.3",
"mobx": "^6.9.0",
"pathfinding": "^0.4.18",
"phaser": "3.60.0-beta.14",
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/dojo/createClientComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export function createClientComponents({
...contractComponents,
// create overridable component for optimistic rendering
Player: overridableComponent(contractComponents.Player),
PlayerProfile: overridableComponent(contractComponents.PlayerProfile),
Piece: overridableComponent(contractComponents.Piece),
PlayerInvPiece: overridableComponent(contractComponents.PlayerInvPiece),
Altar: overridableComponent(contractComponents.Altar),
Expand Down
35 changes: 21 additions & 14 deletions packages/client/src/dojo/createSystemCalls.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { ClientComponents } from "./createClientComponents";
import { IWorld } from "./generated/typescript/contracts.gen";
import { Account } from "starknet";
import { Account, RpcProvider } from "starknet";
import {
getComponentValue,
getComponentValueStrict,
updateComponent,
} from "@dojoengine/recs";
import { zeroEntity } from "../utils";
import { getEntityIdFromKeys } from "@dojoengine/utils";
import { isBoolean, isEqual, isNull, isUndefined } from "lodash";
import { isEqual } from "lodash";
import { PieceChange } from "./types";
import { processBattle } from "../phaser/systems/utils/processBattleLogs";
import { opBuyHero } from "./opRender/opBuyHero";
import { opSellHero } from "./opRender/opSellHero";

export type SystemCalls = ReturnType<typeof createSystemCalls>;

export function createSystemCalls(
{ client }: { client: IWorld },
clientComponents: ClientComponents,
{ GameStatus, LocalPiecesChangeTrack, Piece, LocalPiece }: ClientComponents
{ GameStatus, LocalPiecesChangeTrack, Piece, LocalPiece }: ClientComponents,
rpcProvider: RpcProvider
) {
const spawn = async (account: Account) => {
try {
Expand Down Expand Up @@ -126,16 +129,14 @@ export function createSystemCalls(
altarSlot: number,
invSlot: number
) => {
try {
return await client.home.buyHero({
account,
altarSlot,
invSlot,
});
} catch (e) {
console.error(e);
throw e;
}
await opBuyHero(
{ client },
clientComponents,
rpcProvider,
account,
altarSlot,
invSlot
);
};

const buyExp = async (account: Account) => {
Expand All @@ -151,7 +152,13 @@ export function createSystemCalls(

const sellHero = async (account: Account, gid: number) => {
try {
return await client.home.sellHero({ account, gid });
await opSellHero(
{ client },
clientComponents,
rpcProvider,
account,
gid
);
} catch (e) {
console.error(e);
throw e;
Expand Down
8 changes: 7 additions & 1 deletion packages/client/src/dojo/generated/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { createClient } from "graphql-ws";
import { GraphQLWsLink } from "@apollo/client/link/subscriptions";
import { getMainDefinition } from "@apollo/client/utilities";
import { RpcProvider } from "starknet";
export type SetupResult = Awaited<ReturnType<typeof setup>>;

export async function setup({
Expand All @@ -29,6 +30,10 @@ export async function setup({
worldAddress: config.worldAddress,
});

const rpcProvider = new RpcProvider({
nodeUrl: config.rpcUrl,
});

// ws link
const wsLink = new GraphQLWsLink(
createClient({
Expand Down Expand Up @@ -99,7 +104,8 @@ export async function setup({
systemCalls: createSystemCalls(
{ client },
clientComponents,
clientComponents
clientComponents,
rpcProvider
),
config,
graphqlClient,
Expand Down
105 changes: 102 additions & 3 deletions packages/client/src/dojo/opRender/opBuyHero.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,121 @@
import { Account, AccountInterface } from "starknet";
import { Account, RpcProvider } from "starknet";
import { IWorld } from "../generated/typescript/contracts.gen";
import { ClientComponents } from "../createClientComponents";
import { getComponentValueStrict } from "@dojoengine/recs";
import { getEntityIdFromKeys } from "@dojoengine/utils";
import { poseidonHashMany } from "micro-starknet";
import { logDebug } from "../../ui/lib/utils";
import { uuid } from "@latticexyz/utils";

export const opBuyHero = async (
{ client }: { client: IWorld },
{ Player, Altar, PlayerInvPiece }: ClientComponents,
{ Player, Altar, Piece, PlayerInvPiece, PlayerProfile }: ClientComponents,
rpcProvider: RpcProvider,
account: Account,
altarSlot: number,
invSlot: number
) => {
const playerEntity = getEntityIdFromKeys([BigInt(account.address)]);
const playerProfile = getComponentValueStrict(PlayerProfile, playerEntity);
const player = getComponentValueStrict(Player, playerEntity);
const altar = getComponentValueStrict(Altar, playerEntity);
const playerInvEntity = getEntityIdFromKeys([
BigInt(account.address),
BigInt(invSlot),
]);

// check
if (player.coin <= 0 || player.inventoryCount >= 6) {
alert("cannot buy");
return;
}

playerProfile.pieceCounter += 1;

const pieceGid = Number(
poseidonHashMany([
BigInt(account.address),
BigInt(playerProfile.pieceCounter),
]) & BigInt(0xffffffff)
);
const pieceEntity = getEntityIdFromKeys([BigInt(pieceGid)]);

const creatureId = Number(Object.entries(altar)[altarSlot][1]);

player.coin -= 1;
player.inventoryCount += 1;

logDebug(`generate gid ${pieceGid}`);

const pieceOverUuid = uuid();
Piece.addOverride(pieceOverUuid, {
entity: pieceEntity,
value: {
gid: pieceGid,
owner: BigInt(account.address),
idx: 0,
slot: invSlot,
level: 1,
creature_index: creatureId,
x: 0,
y: 0,
},
});

// altar override
const altarOverride = uuid();
const altarArray = Object.entries(altar);
altarArray[altarSlot][1] = 0;
Altar.addOverride(altarOverride, {
entity: playerEntity,
value: altarArray.reduce((accumulator, [key, value]) => {
accumulator[key] = value;
return accumulator;
}, {}),
});

// player inv piece override
const playerInvOverride = uuid();
PlayerInvPiece.addOverride(playerInvOverride, {
entity: playerInvEntity,
value: {
owner: BigInt(account.address),
slot: invSlot,
gid: pieceGid,
},
});

// player override
const playerOverride = uuid();
Player.addOverride(playerOverride, {
entity: playerEntity,
value: player,
});

// player profile override
const playerProfileOverride = uuid();
PlayerProfile.addOverride(playerProfileOverride, {
entity: playerEntity,
value: playerProfile,
});

try {
return await client.home.buyHero({
const tx = await client.home.buyHero({
account,
altarSlot,
invSlot,
});
await rpcProvider.waitForTransaction(tx.transaction_hash, {
retryInterval: 1000,
});
} catch (e) {
console.error(e);
throw e;
} finally {
Piece.removeOverride(pieceOverUuid);
Altar.removeOverride(altarOverride);
PlayerInvPiece.removeOverride(playerInvOverride);
Player.removeOverride(playerOverride);
PlayerProfile.removeOverride(playerProfileOverride);
}
};
75 changes: 75 additions & 0 deletions packages/client/src/dojo/opRender/opSellHero.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Account, RpcProvider } from "starknet";
import { IWorld } from "../generated/typescript/contracts.gen";
import { ClientComponents } from "../createClientComponents";
import { getComponentValueStrict } from "@dojoengine/recs";
import { getEntityIdFromKeys } from "@dojoengine/utils";
import { uuid } from "@latticexyz/utils";

export const opSellHero = async (
{ client }: { client: IWorld },
{ Player, Piece, PlayerInvPiece }: ClientComponents,
rpcProvider: RpcProvider,
account: Account,
gid: number
) => {
const playerEntity = getEntityIdFromKeys([BigInt(account.address)]);
const player = getComponentValueStrict(Player, playerEntity);
const pieceEntity = getEntityIdFromKeys([BigInt(gid)]);
const piece = getComponentValueStrict(Piece, pieceEntity);
const playerInvEntity = getEntityIdFromKeys([
BigInt(account.address),
BigInt(piece.slot),
]);
const playerInvPiece = getComponentValueStrict(
PlayerInvPiece,
playerInvEntity
);

// check
player.coin += 1;
player.inventoryCount -= 1;

const pieceOverUuid = uuid();
Piece.addOverride(pieceOverUuid, {
entity: pieceEntity,
value: {
...piece,
owner: 0n,
slot: 0,
},
});

// player inv piece override
const playerInvOverride = uuid();
PlayerInvPiece.addOverride(playerInvOverride, {
entity: playerInvEntity,
value: {
...playerInvPiece,
gid: 0,
},
});

// player override
const playerOverride = uuid();
Player.addOverride(playerOverride, {
entity: playerEntity,
value: player,
});

try {
const tx = await client.home.sellHero({
account,
gid,
});
await rpcProvider.waitForTransaction(tx.transaction_hash, {
retryInterval: 1000,
});
} catch (e) {
console.error(e);
throw e;
} finally {
Piece.removeOverride(pieceOverUuid);
PlayerInvPiece.removeOverride(playerInvOverride);
Player.removeOverride(playerOverride);
}
};
74 changes: 0 additions & 74 deletions packages/client/src/dojo/setupNetwork.ts

This file was deleted.

Loading

0 comments on commit 4ae3e6e

Please sign in to comment.