Skip to content

Commit

Permalink
fix: inventory, hasresources, optimistic lapu
Browse files Browse the repository at this point in the history
  • Loading branch information
Cygnusfear committed Oct 24, 2023
1 parent f58d413 commit b5538e1
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 13 deletions.
10 changes: 8 additions & 2 deletions packages/client/src/components/ui/inventory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "./inventory.css";
import { getState, useStore } from "@/game/store";
import { ResourceIcons, ResourceType } from "@/game/data/resources";
import { cn } from "@/lib/utils";
import { useEffect, useMemo, useState } from "react";
import { useEffect, useState } from "react";
import {
animated,
config,
Expand Down Expand Up @@ -86,9 +86,15 @@ function InventoryItem(
const { name, blurb, image, produces, costs, style } = props;
const {
input: { building },
player: {
playerData: { resources, LAPUtoBeConsolidated },
},
} = useStore();
const [tooExpensive, setTooExpensive] = useState(!canAffordBuilding(props));

const tooExpensive = useMemo(() => !canAffordBuilding(props), [props]);
useEffect(() => {
setTooExpensive(!canAffordBuilding(props));
}, [props, resources, LAPUtoBeConsolidated]);

const hideCursor = () => {
getState().input.cursor.setCursor({ cursorState: "hidden" });
Expand Down
6 changes: 4 additions & 2 deletions packages/client/src/components/ui/resourcePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ function ResourcePanel() {
</div>
)}
{Object.entries(playerData.resources)
.filter(([type]) => type !== "water" && type !== "food")
.filter(
([type]) => type !== "water" && type !== "food" && type !== "crystal"
)
.map(([type, amount], idx) => {
const calcAmount = type === "LAPU" ? amount + LAPUtemp : amount;
return (
Expand All @@ -96,7 +98,7 @@ function ResourcePanel() {
/>
);
})}
<ResourceItem resourceType="LAPU" amount={LAPUtemp} />
{/* <ResourceItem resourceType="LAPU" amount={LAPUtemp} /> */}
</div>
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/game/data/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const EntityData = {
image: "gravity.webp",
costs: [
["LAPU", 400],
["crystal", 1],
// ["crystal", 1],
],
produces: [["gravity", 10, 1]],
variants: ModelData.well,
Expand Down
8 changes: 8 additions & 0 deletions packages/client/src/game/data/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ export const doOptimisticLapuDelta = async (
getState().player.setPlayerData({
...getState().player.playerData,
LAPUtoBeConsolidated: consolidatePrev - amount,
resources: {
...getState().player.playerData.resources,
LAPU: getState().player.playerData.resources.LAPU + amount,
},
});
})
.catch((e: unknown) => {
Expand All @@ -126,6 +130,10 @@ export const doOptimisticLapuDelta = async (
getState().player.setPlayerData({
...getState().player.playerData,
LAPUtoBeConsolidated: consolidatePrev - amount,
resources: {
...getState().player.playerData.resources,
LAPU: getState().player.playerData.resources.LAPU + amount,
},
});
});
});
Expand Down
6 changes: 5 additions & 1 deletion packages/client/src/game/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ const useStore = create<IState>((set, get) => ({
resources: { resource: ResourceType; amount: number }[]
): boolean => {
return resources.every(({ resource, amount }) => {
return get().player.playerData.resources[resource] >= amount;
let consolidated = get().player.playerData.resources[resource];
if (resource === "LAPU") {
consolidated += get().player.playerData.LAPUtoBeConsolidated || 0;
}
return consolidated >= amount;
});
},
spendResouces: (
Expand Down
8 changes: 2 additions & 6 deletions packages/client/src/game/systems/constructionSystem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { floodFill } from "../utils/floodFill";
import { FacilityDataType } from "../data/entities";
import { doOptimisticLapuDelta } from "../data/player";
import { getMUD } from "@/mud/setup";
import { queueAsyncCall } from "../utils/asyncQueue";

// TODO: Extract build conditions, can't build 1 tile below gravity well, only gravity well can build at y==1, etc
const canBuildAtPosition = (position: Vector3) => {
Expand Down Expand Up @@ -135,11 +134,8 @@ const buildFacility = async ({
variant,
owner,
];
queueAsyncCall(async () => {
console.trace("buildFacility hook", position, build);
await doOptimisticLapuDelta(-LAPUCost, async () => {
await mudBuildFacility(...build);
});
doOptimisticLapuDelta(-LAPUCost, async () => {
await mudBuildFacility(...build);
});
}
addEntity(newFacility);
Expand Down
10 changes: 10 additions & 0 deletions packages/client/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,13 @@ export const faceDirections: Vector3[] = [
export const degreesToRadians = (degrees: number) => {
return degrees * (Math.PI / 180);
};

export function getTimestamp(): string {
const date = new Date();
const hours = date.getHours().toString().padStart(2, "0");
const minutes = date.getMinutes().toString().padStart(2, "0");
const seconds = date.getSeconds().toString().padStart(2, "0");
const milliseconds = date.getMilliseconds().toString().padStart(3, "0");

return `${hours}:${minutes}:${seconds}:${milliseconds}`;
}
7 changes: 6 additions & 1 deletion packages/client/src/mudExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useMUD } from "./useMUD";

import { getState } from "./game/store";
import { useState, useEffect } from "react";
import { getTimestamp } from "./lib/utils";

export const MudExample = () => {
const {
Expand Down Expand Up @@ -55,7 +56,11 @@ export const MudExample = () => {
getState().player?.playerData?.address
)) as number;
setPlayerLapuBalance(playerLapuBalance_);
console.log("[contractRes] LAPU -> ", playerLapuBalance_);
console.log(
getTimestamp() + " [contractRes] LAPU -> ",
playerLapuBalance_
);
console.log(getTimestamp() + " [playerRes] LAPU -> ", playerLapuBalance_);
getState().player?.setPlayerData({
...getState().player?.playerData,
resources: {
Expand Down

0 comments on commit b5538e1

Please sign in to comment.