Skip to content

Commit

Permalink
refactor: local piece idx system
Browse files Browse the repository at this point in the history
  • Loading branch information
noyyyy committed May 6, 2024
1 parent c15f5d2 commit 42b0045
Show file tree
Hide file tree
Showing 11 changed files with 311 additions and 202 deletions.
10 changes: 1 addition & 9 deletions packages/client/src/dojo/createSystemCalls.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import { ClientComponents } from "./createClientComponents";
import { IWorld } from "./generated/typescript/contracts.gen";
import { Account, RpcProvider } from "starknet";
import {
getComponentValue,
getComponentValueStrict,
updateComponent,
} from "@dojoengine/recs";
import { updateComponent } from "@dojoengine/recs";
import { zeroEntity } from "../utils";
import { getEntityIdFromKeys } from "@dojoengine/utils";
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";
import { opCommitPrepare } from "./opRender/opCommitPrepare";
Expand Down
20 changes: 2 additions & 18 deletions packages/client/src/dojo/opRender/opBuyHero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { uuid } from "@latticexyz/utils";

export const opBuyHero = async (
{ client }: { client: IWorld },
{ Player, Altar, Piece, PlayerInvPiece, PlayerProfile }: ClientComponents,
{ Player, Altar, Piece, PlayerProfile }: ClientComponents,
rpcProvider: RpcProvider,
account: Account,
altarSlot: number,
Expand All @@ -19,10 +19,6 @@ export const opBuyHero = async (
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) {
Expand Down Expand Up @@ -77,17 +73,6 @@ export const opBuyHero = async (
),
});

// 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, {
Expand All @@ -109,15 +94,14 @@ export const opBuyHero = async (
invSlot,
});
await rpcProvider.waitForTransaction(tx.transaction_hash, {
retryInterval: 1000,
retryInterval: 100,
});
} catch (e) {
console.error(e);
throw e;
} finally {
Piece.removeOverride(pieceOverUuid);
Altar.removeOverride(altarOverride);
PlayerInvPiece.removeOverride(playerInvOverride);
Player.removeOverride(playerOverride);
PlayerProfile.removeOverride(playerProfileOverride);
}
Expand Down
29 changes: 1 addition & 28 deletions packages/client/src/dojo/opRender/opSellHero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { uuid } from "@latticexyz/utils";

export const opSellHero = async (
{ client }: { client: IWorld },
{ Player, Piece, PlayerInvPiece }: ClientComponents,
{ Player, Piece }: ClientComponents,
rpcProvider: RpcProvider,
account: Account,
gid: number
Expand All @@ -16,14 +16,6 @@ export const opSellHero = async (
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;
Expand All @@ -39,23 +31,6 @@ export const opSellHero = async (
},
});

// 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,
Expand All @@ -69,7 +44,5 @@ export const opSellHero = async (
throw e;
} finally {
Piece.removeOverride(pieceOverUuid);
PlayerInvPiece.removeOverride(playerInvOverride);
Player.removeOverride(playerOverride);
}
};
244 changes: 244 additions & 0 deletions packages/client/src/phaser/systems/local/followIndexSystem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
import { PhaserLayer } from "../..";
import { defineSystemST } from "../../../utils";
import { world } from "../../../dojo/generated/world";
import {
Has,
getComponentValue,
getComponentValueStrict,
setComponent,
updateComponent,
} from "@dojoengine/recs";
import { logDebug, logPieceIdx } from "../../../ui/lib/utils";
import { getEntityIdFromKeys } from "@dojoengine/utils";

export function followIndexSystem(layer: PhaserLayer) {
const {
networkLayer: {
clientComponents: {
LocalPiece,
LocalPlayerInvPiece,
LocalPlayerPiece,
LocalPlayer,
},
account: { address },
playerEntity,
},
} = layer;

// local player piece and local player inv piece should follow local piece change
defineSystemST<typeof LocalPiece.schema>(
world,
[Has(LocalPiece)],
({ entity, type, value: [v, preV] }) => {
if (!v) {
return;
}

// don't care piece which doesn't belong to player
if (v.owner !== BigInt(address)) {
return;
}

const player = getComponentValueStrict(LocalPlayer, playerEntity);

const localInvPieceEntity = getEntityIdFromKeys([
v.owner,
BigInt(v.slot),
]);

const localPlayerPieceEntity = getEntityIdFromKeys([
v.owner,
BigInt(v.idx),
]);

// if no previous value, it could be buy event or new sync
if (!preV) {
if (v.slot === 0 && v.gid === 0) {
throw Error("sync local piece logic error");
}

if (v.slot !== 0) {
setComponent(LocalPlayerInvPiece, localInvPieceEntity, {
owner: v.owner,
slot: v.slot,
gid: v.gid,
});
} else if (v.idx !== 0) {
setComponent(LocalPlayerPiece, localPlayerPieceEntity, {
owner: v.owner,
idx: v.idx,
gid: v.gid,
});
}

return;
}

const preLocalInvPieceEntity = getEntityIdFromKeys([
v.owner,
BigInt(preV.slot),
]);

const preLocalPlayerPieceEntity = getEntityIdFromKeys([
v.owner,
BigInt(preV.idx),
]);

if (
preV.slot !== 0 &&
v.slot !== 0 &&
preV.idx === 0 &&
preV.idx === 0
) {
/**
* @dev piece move between two different inventory slots
*/

logPieceIdx(
`piece ${v.gid} move from slot ${preV.slot} to slot ${v.slot}`
);

// update prev slot
updateComponent(LocalPlayerInvPiece, preLocalInvPieceEntity, {
gid: 0,
});
// update new slot
if (
!getComponentValue(LocalPlayerInvPiece, localInvPieceEntity)
) {
setComponent(LocalPlayerInvPiece, localInvPieceEntity, {
owner: v.owner,
slot: v.slot,
gid: v.gid,
});
} else {
updateComponent(LocalPlayerInvPiece, localInvPieceEntity, {
gid: v.gid,
});
}
} else if (
preV.slot !== 0 &&
v.slot === 0 &&
preV.idx === 0 &&
v.idx !== 0
) {
/**
* @dev piece move from inventory to board
*/
logPieceIdx(
`piece ${v.gid} move from ${preV.slot} to ${v.idx}`
);

// update local player's hero count and inv count
updateComponent(LocalPlayer, playerEntity, {
heroesCount: player.heroesCount + 1,
inventoryCount: player.inventoryCount - 1,
});

// update prev slot
updateComponent(LocalPlayerInvPiece, preLocalInvPieceEntity, {
gid: 0,
});
// update new board index
const preVPlayerPiece = getComponentValue(
LocalPlayerPiece,
localPlayerPieceEntity
);
if (!preVPlayerPiece) {
logDebug(
`set local player piece ${localPlayerPieceEntity}`
);
setComponent(LocalPlayerPiece, localPlayerPieceEntity, {
owner: v.owner,
idx: v.idx,
gid: v.gid,
});
} else {
logDebug(
`set local player piece ${localPlayerPieceEntity} ${v.gid}`
);
updateComponent(LocalPlayerPiece, localPlayerPieceEntity, {
gid: v.gid,
});
}
} else if (
preV.idx !== 0 &&
v.idx === 0 &&
preV.slot === 0 &&
v.slot !== 0
) {
/**
* @dev piece move from board to inventory
*/
logPieceIdx(
`piece ${v.gid} move from ${preV.idx} to ${v.slot}`
);

// update local player's hero count and inv count
updateComponent(LocalPlayer, playerEntity, {
heroesCount: player.heroesCount - 1,
inventoryCount: player.inventoryCount + 1,
});

// update prev board index
updateComponent(LocalPlayerPiece, preLocalPlayerPieceEntity, {
gid: 0,
});

// update new inventory slot
if (
!getComponentValue(LocalPlayerInvPiece, localInvPieceEntity)
) {
setComponent(LocalPlayerInvPiece, localInvPieceEntity, {
owner: v.owner,
slot: v.slot,
gid: v.gid,
});
} else {
updateComponent(LocalPlayerInvPiece, localInvPieceEntity, {
gid: v.gid,
});
}
} else if (
preV.idx === 0 &&
preV.slot !== 0 &&
v.idx === 0 &&
v.slot === 0
) {
/**
* @dev piece sold
*/
// update prev inventory index
updateComponent(LocalPlayerInvPiece, localInvPieceEntity, {
gid: 0,
});

// update local player's hero count and inv count
updateComponent(LocalPlayer, playerEntity, {
heroesCount: player.heroesCount - 1,
inventoryCount: player.inventoryCount + 1,
});
} else if (
preV.idx !== 0 &&
v.idx !== 0 &&
preV.slot === 0 &&
v.slot === 0
) {
/**
* @dev player piece index change
*/
logDebug(
`player piece ${v.gid} change from ${preV.idx} to ${v.idx}`
);
// just update player piece
updateComponent(LocalPlayerPiece, preLocalPlayerPieceEntity, {
gid: 0,
});

updateComponent(LocalPlayerPiece, localPlayerPieceEntity, {
gid: v.gid,
});
}
}
);
}
Loading

0 comments on commit 42b0045

Please sign in to comment.