Skip to content

Commit

Permalink
enh: make chat more optimized (#1706)
Browse files Browse the repository at this point in the history
* make chat more optimized

* closes #1675
  • Loading branch information
edisontim committed Sep 23, 2024
1 parent 87c0098 commit c37b55e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
11 changes: 6 additions & 5 deletions client/src/dojo/modelManager/StaminaManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ID, ResourcesIds, WORLD_CONFIG_ID } from "@bibliothecadao/eternum";
import { getComponentValue } from "@dojoengine/recs";
import { ComponentValue, getComponentValue } from "@dojoengine/recs";
import { getEntityIdFromKeys } from "@dojoengine/utils";
import { ClientComponents } from "../createClientComponents";
import { SetupResult } from "../setup";

export class StaminaManager {
Expand Down Expand Up @@ -58,16 +59,16 @@ export class StaminaManager {
return newStamina;
}

public getMaxStamina = (troops: any): number => {
public getMaxStamina = (troops: ComponentValue<ClientComponents["Army"]["schema"]["troops"]> | undefined): number => {
let maxStaminas: number[] = [];
const staminaConfig = this.getStaminaConfig();
if (troops.knight_count > 0) {
if ((troops?.knight_count ?? 0) > 0) {
maxStaminas.push(staminaConfig.knightConfig);
}
if (troops.crossbowman_count > 0) {
if ((troops?.crossbowman_count ?? 0) > 0) {
maxStaminas.push(staminaConfig.crossbowmanConfig);
}
if (troops.paladin_count > 0) {
if ((troops?.paladin_count ?? 0) > 0) {
maxStaminas.push(staminaConfig.paladinConfig);
}

Expand Down
34 changes: 20 additions & 14 deletions client/src/ui/modules/chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useDojo } from "@/hooks/context/DojoContext";
import { useGetAllPlayers } from "@/hooks/helpers/useEntities";
import TextInput from "@/ui/elements/TextInput";
import { useEntityQuery } from "@dojoengine/react";
import { getComponentValue, Has, HasValue } from "@dojoengine/recs";
import { getComponentValue, Has, HasValue, runQuery } from "@dojoengine/recs";
import { getEntityIdFromKeys } from "@dojoengine/utils";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { shortString, TypedData } from "starknet";
Expand Down Expand Up @@ -102,17 +102,11 @@ export const Chat = () => {

const allMessageEntities = useEntityQuery([Has(Message)]);

const selfMessageEntities = useEntityQuery([Has(Message), HasValue(Message, { identity: BigInt(account.address) })]);
const receivedMessageEntities = useEntityQuery([
Has(Message),
HasValue(Message, { channel: BigInt(account.address) }),
]);

const recipientEntities = useEntityQuery([Has(AddressName), HasValue(AddressName, { name: BigInt("0x0") })]);

useEffect(() => {
scrollToElement(bottomChatRef);
}, [allMessageEntities, receivedMessageEntities]);
}, [allMessageEntities]);

const setNewTabs = (newTabs: Tab[]) => {
setTabs(newTabs);
Expand Down Expand Up @@ -193,7 +187,7 @@ export const Chat = () => {
});

return messageMap;
}, [allMessageEntities, receivedMessageEntities]);
}, [allMessageEntities]);

const messagesToDisplay = useMemo(() => {
if (currentTab.name === "Global") {
Expand All @@ -206,13 +200,14 @@ export const Chat = () => {
}, [messages, currentTab.address]);

useEffect(() => {
const salts = selfMessageEntities.map((entity) => {
const selfMessageEntities = runQuery([Has(Message), HasValue(Message, { identity: BigInt(account.address) })]);
const salts = Array.from(selfMessageEntities).map((entity) => {
const message = getComponentValue(Message, entity);
return message?.salt || 0n;
});
if (!salts.length) return;
setSalt(salts.sort((a, b) => Number(b) - Number(a))[0] + 1n);
}, [selfMessageEntities, salt]);
}, [salt]);

const publish = useCallback(
async (message: string) => {
Expand Down Expand Up @@ -327,7 +322,7 @@ export const Chat = () => {
/>
))}
</div>
<div className="flex flex-col gap-2 w-[28vw] border bg-black/40 p-1 border-gold/40 bg-hex-bg bottom-0 rounded-b max-h-80">
<div className="flex flex-col gap-2 w-[28vw] max-w-[28vw] border bg-black/40 p-1 border-gold/40 bg-hex-bg bottom-0 rounded-b max-h-80">
<div className="border p-2 border-gold/40 rounded text-xs h-60 overflow-y-auto min-h-40">
{messagesToDisplay?.map((message, index) => (
<div
Expand All @@ -339,7 +334,12 @@ export const Chat = () => {
<span className="font-bold mr-1 inline" onClick={() => changeTabs(message.name, message.address)}>
[{message.fromSelf ? "you" : message.name}]:
</span>
<span className="font-bold mr-2 inline">{`${message.content}`}</span>
<span
className="font-bold mr-2 inline text-wrap max-w-full"
style={{ wordBreak: "break-word", overflowWrap: "break-word", whiteSpace: "pre-wrap" }}
>
{`${message.content}`}
</span>
</div>
</div>
))}
Expand Down Expand Up @@ -379,7 +379,13 @@ export const Chat = () => {
);
};

const scrollToElement = (ref: React.RefObject<HTMLDivElement>) => {};
const scrollToElement = (ref: React.RefObject<HTMLDivElement>) => {
setTimeout(() => {
if (ref.current) {
ref.current.scrollIntoView({ behavior: "smooth", block: "nearest" });
}
}, 1);
};

class ContentParser {
isCommand(content: string): boolean {
Expand Down

0 comments on commit c37b55e

Please sign in to comment.