Skip to content

Commit

Permalink
🛍️ Cast shop buy
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdodo committed Nov 28, 2024
1 parent 15ea1f2 commit 3b91f58
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
6 changes: 1 addition & 5 deletions core/types/interaction.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import type { PieceSlot } from "./piece-slot";

interface shopBuy {
productId: string;
}

export interface Interaction {
grabPiece?: PieceSlot;
ungrabPiece?: PieceSlot;
shopBuy?: shopBuy;
shopBuy?: number;
reroll?: true;
levelUp?: true;
}
30 changes: 30 additions & 0 deletions core/utils/cast.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { test } from "vitest";
import { cast } from "./cast";
import { withTwoPlayerGameStarted } from "../fixtures/with-two-player-game-started";
import { firstValueFrom, map, filter } from "rxjs";
import { observeGame } from "../api/observe-game";

test("cast a reroll", async () => {
const testContext = await withTwoPlayerGameStarted();

const frontContext = testContext.frontContexts[0];

if (!frontContext) {
throw new Error(`Player ${0} not found !`);
}

const moneyBefore = await firstValueFrom(
observeGame(frontContext).pipe(
map(({ game }) => game.playerMoney[frontContext.publicKey]),
),
);

await cast(frontContext, { reroll: true });

await firstValueFrom(
observeGame(frontContext).pipe(
map(({ game }) => game.playerMoney[frontContext.publicKey]),
filter((money) => money !== moneyBefore),
),
);
});
5 changes: 5 additions & 0 deletions core/utils/cast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { reroll } from "../api/reroll";
import type { Interaction } from "../types/interaction";
import type { FrontContext } from "../types/front-context";
import { levelUp } from "../api/level-up";
import { shopBuy } from "../api/shop-buy";

export async function cast(
frontContext: FrontContext,
Expand All @@ -14,4 +15,8 @@ export async function cast(
if (interaction.levelUp) {
await levelUp(frontContext);
}

if (interaction.shopBuy !== undefined) {
await shopBuy(frontContext, interaction.shopBuy);
}
}
8 changes: 5 additions & 3 deletions interface/utils/observe-interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ export function observeInteractions(
}

if (grabbing) {
for (const [productId, shopProductBackgroundMesh] of Object.entries(
threeContext.shopProductBackgroundMeshes,
for (const [_index, shopProductBackgroundMesh] of Object.entries(
Object.values(threeContext.shopProductBackgroundMeshes),
)) {
const index = Number.parseInt(_index);

if (raycaster.intersectObject(shopProductBackgroundMesh).length > 0) {
interaction.shopBuy = { productId };
interaction.shopBuy = index;
}
}
}
Expand Down

0 comments on commit 3b91f58

Please sign in to comment.