Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache optimisations #2529

Merged
merged 7 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 41 additions & 24 deletions client/src/dojo/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,52 @@ export const syncEntitiesEternum = async <S extends Schema>(
export const addToSubscription = async <S extends Schema>(
client: ToriiClient,
components: Component<S, Metadata, undefined>[],
entityID: string,
position?: { x: number; y: number },
entityID: string[],
position?: { x: number; y: number }[],
) => {
const positionClause: EntityKeysClause = {
Keys: {
keys: [String(position?.x || 0), String(position?.y || 0), undefined, undefined],
pattern_matching: "FixedLen" as PatternMatching,
models: [],
},
};

position &&
(await getEntities(
client,
{
...positionClause,
},
components,
30_000,
false,
));
// position &&
// (await getEntities(
// client,
// {
// Composite: {
// operator: "Or",
// clauses: position.map((position) => ({
// Keys: {
// keys: [String(position?.x || 0), String(position?.y || 0), undefined, undefined],
// pattern_matching: "FixedLen" as PatternMatching,
// models: [],
// },
// })),
// },
// },
// components,
// 30_000,
// false,
// ));

await getEntities(
client,
{
Keys: {
keys: [entityID],
pattern_matching: "VariableLen",
models: [],
Composite: {
operator: "Or",
clauses: [
...entityID.map((id) => ({
Keys: {
keys: [id],
pattern_matching: "VariableLen" as PatternMatching,
models: [],
},
})),
...(position
? position.map((position) => ({
Keys: {
keys: [String(position?.x || 0), String(position?.y || 0), undefined, undefined],
pattern_matching: "FixedLen" as PatternMatching,
models: [],
},
}))
: []),
],
},
},
components,
Expand Down
6 changes: 5 additions & 1 deletion client/src/three/scenes/HexagonScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,11 @@ export abstract class HexagonScene {
this.updateLights();
this.updateHighlightPulse();
this.biomeModels.forEach((biome) => {
biome.updateAnimations(deltaTime);
try {
biome.updateAnimations(deltaTime);
} catch (error) {
console.error(`Error updating biome animations:`, error);
}
});
}

Expand Down
264 changes: 133 additions & 131 deletions client/src/ui/components/bank/ResourceBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,152 +5,154 @@ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@
import TextInput from "@/ui/elements/TextInput";
import { divideByPrecision, formatNumber } from "@/ui/utils/utils";
import { ID, Resources, ResourcesIds, findResourceById, findResourceIdByTrait } from "@bibliothecadao/eternum";
import { useEffect, useRef, useState } from "react";
import { memo, useEffect, useRef, useState } from "react";
import { HintSection } from "../hints/HintModal";

export const ResourceBar = ({
entityId,
lordsFee,
resources,
resourceId,
setResourceId,
amount,
setAmount,
disableInput = false,
onFocus,
onBlur,
max = Infinity,
}: {
entityId: ID;
lordsFee: number;
resources: Resources[];
resourceId: ResourcesIds;
setResourceId: (resourceId: ResourcesIds) => void;
amount: number;
setAmount: (amount: number) => void;
disableInput?: boolean;
onFocus?: () => void; // New prop
onBlur?: () => void; // New prop
max?: number;
}) => {
const { getBalance } = useResourceBalance();
export const ResourceBar = memo(
({
entityId,
lordsFee,
resources,
resourceId,
setResourceId,
amount,
setAmount,
disableInput = false,
onFocus,
onBlur,
max = Infinity,
}: {
entityId: ID;
lordsFee: number;
resources: Resources[];
resourceId: ResourcesIds;
setResourceId: (resourceId: ResourcesIds) => void;
amount: number;
setAmount: (amount: number) => void;
disableInput?: boolean;
onFocus?: () => void; // New prop
onBlur?: () => void; // New prop
max?: number;
}) => {
const { getBalance } = useResourceBalance();

const [selectedResourceBalance, setSelectedResourceBalance] = useState(0);
const [searchInput, setSearchInput] = useState("");
const [open, setOpen] = useState(false);
const [selectedResourceBalance, setSelectedResourceBalance] = useState(0);
const [searchInput, setSearchInput] = useState("");
const [open, setOpen] = useState(false);

const inputRef = useRef<HTMLInputElement>(null);
const inputRef = useRef<HTMLInputElement>(null);

useEffect(() => {
setSelectedResourceBalance(divideByPrecision(getBalance(entityId, Number(resourceId)).balance));
}, [resourceId, getBalance, entityId]);
useEffect(() => {
setSelectedResourceBalance(divideByPrecision(getBalance(entityId, Number(resourceId)).balance));
}, [resourceId, getBalance, entityId]);

const handleResourceChange = (trait: string) => {
const newResourceId = findResourceIdByTrait(trait);
setResourceId(newResourceId);
};
const handleResourceChange = (trait: string) => {
const newResourceId = findResourceIdByTrait(trait);
setResourceId(newResourceId);
};

const handleAmountChange = (amount: number) => {
setAmount(amount);
};
const handleAmountChange = (amount: number) => {
setAmount(amount);
};

const hasLordsFees = lordsFee > 0 && resourceId === ResourcesIds.Lords;
const finalResourceBalance = hasLordsFees ? selectedResourceBalance - lordsFee : selectedResourceBalance;
const hasLordsFees = lordsFee > 0 && resourceId === ResourcesIds.Lords;
const finalResourceBalance = hasLordsFees ? selectedResourceBalance - lordsFee : selectedResourceBalance;

const filteredResources = resources.filter(
(resource) => resource.trait.toLowerCase().startsWith(searchInput.toLowerCase()) || resource.id === resourceId,
);
const filteredResources = resources.filter(
(resource) => resource.trait.toLowerCase().startsWith(searchInput.toLowerCase()) || resource.id === resourceId,
);

const handleOpenChange = (newOpen: boolean) => {
setOpen(newOpen);
if (newOpen && inputRef.current) {
setResourceId(ResourcesIds.Wood);
setSearchInput("");
setTimeout(() => {
inputRef.current?.focus();
}, 0);
}
};
const handleOpenChange = (newOpen: boolean) => {
setOpen(newOpen);
if (newOpen && inputRef.current) {
setResourceId(ResourcesIds.Wood);
setSearchInput("");
setTimeout(() => {
inputRef.current?.focus();
}, 0);
}
};

const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === "Enter") {
if (filteredResources.length > 0) {
const selectedResource = filteredResources.find((resource) => resource.id !== resourceId);
if (selectedResource) {
setResourceId(selectedResource.id);
setOpen(false);
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === "Enter") {
if (filteredResources.length > 0) {
const selectedResource = filteredResources.find((resource) => resource.id !== resourceId);
if (selectedResource) {
setResourceId(selectedResource.id);
setOpen(false);
}
}
setSearchInput("");
} else {
e.stopPropagation();
}
setSearchInput("");
} else {
e.stopPropagation();
}
};
};

return (
<div className="resource-bar-selector w-full bg-gold/10 rounded-xl p-3 flex justify-between h-28 flex-wrap">
<div className="self-center">
<NumberInput
className="text-2xl border-transparent"
value={amount}
onChange={handleAmountChange}
max={max}
arrows={false}
allowDecimals
onFocus={onFocus}
onBlur={onBlur}
/>

return (
<div className="resource-bar-selector w-full bg-gold/10 rounded-xl p-3 flex justify-between h-28 flex-wrap">
<div className="self-center">
<NumberInput
className="text-2xl border-transparent"
value={amount}
onChange={handleAmountChange}
max={max}
arrows={false}
allowDecimals
onFocus={onFocus}
onBlur={onBlur}
/>
{!disableInput && (
<div
className="flex text-xs text-gold/70 mt-1 justify-center items-center relative text-center self-center mx-auto w-full cursor-pointer"
onClick={() => handleAmountChange(finalResourceBalance)}
>
Max: {isNaN(selectedResourceBalance) ? "0" : selectedResourceBalance.toLocaleString()}
{hasLordsFees && (
<div className="text-danger ml-2">
<div>{`[+${isNaN(lordsFee) ? "0" : formatNumber(lordsFee, 2)}]`}</div>
</div>
)}
</div>
)}
</div>

{!disableInput && (
<div
className="flex text-xs text-gold/70 mt-1 justify-center items-center relative text-center self-center mx-auto w-full cursor-pointer"
onClick={() => handleAmountChange(finalResourceBalance)}
>
Max: {isNaN(selectedResourceBalance) ? "0" : selectedResourceBalance.toLocaleString()}
{hasLordsFees && (
<div className="text-danger ml-2">
<div>{`[+${isNaN(lordsFee) ? "0" : formatNumber(lordsFee, 2)}]`}</div>
<Select
open={open}
onOpenChange={handleOpenChange}
value={findResourceById(Number(resourceId))?.trait || ""}
onValueChange={(trait) => {
handleResourceChange(trait);
setOpen(false);
setSearchInput("");
}}
>
<SelectTrigger className="w-[140px]">
<SelectValue placeholder={HintSection.Resources} />
</SelectTrigger>
<SelectContent>
{resources.length > 1 && (
<div className="p-2">
<TextInput
ref={inputRef}
onChange={setSearchInput}
placeholder="Filter resources..."
onKeyDown={handleKeyDown}
/>
</div>
)}
</div>
)}
{filteredResources.map((resource) => (
<SelectItem key={resource.id} value={resource.trait} disabled={resource.id === resourceId}>
<ResourceCost
resourceId={resource.id}
amount={divideByPrecision(getBalance(entityId, resource.id).balance)}
className="border-0 bg-transparent"
/>
</SelectItem>
))}
</SelectContent>
</Select>
</div>

<Select
open={open}
onOpenChange={handleOpenChange}
value={findResourceById(Number(resourceId))?.trait || ""}
onValueChange={(trait) => {
handleResourceChange(trait);
setOpen(false);
setSearchInput("");
}}
>
<SelectTrigger className="w-[140px]">
<SelectValue placeholder={HintSection.Resources} />
</SelectTrigger>
<SelectContent>
{resources.length > 1 && (
<div className="p-2">
<TextInput
ref={inputRef}
onChange={setSearchInput}
placeholder="Filter resources..."
onKeyDown={handleKeyDown}
/>
</div>
)}
{filteredResources.map((resource) => (
<SelectItem key={resource.id} value={resource.trait} disabled={resource.id === resourceId}>
<ResourceCost
resourceId={resource.id}
amount={divideByPrecision(getBalance(entityId, resource.id).balance)}
className="border-0 bg-transparent"
/>
</SelectItem>
))}
</SelectContent>
</Select>
</div>
);
};
);
},
);
Loading
Loading