diff --git a/packages/client/src/components/CanvasMeta.tsx b/packages/client/src/components/CanvasMeta.tsx index e8e9e737a4ee6edad248625b119ce569ff8a6e45..b9867aa0f30327668dc83a2870021bce6f0ce119 100644 --- a/packages/client/src/components/CanvasMeta.tsx +++ b/packages/client/src/components/CanvasMeta.tsx @@ -10,6 +10,7 @@ import { useAppContext } from "../contexts/AppContext"; import { Canvas } from "../lib/canvas"; import { useEffect, useState } from "react"; import { ClientConfig } from "@sc07-canvas/lib/src/net"; +import network from "../lib/network"; const getTimeLeft = (pixels: { available: number }, config: ClientConfig) => { // this implementation matches the server's implementation @@ -40,10 +41,33 @@ const PlaceCountdown = () => { }, [pixels]); return ( - <>{pixels.available + 1 < config.canvas.pixel.maxStack && timeLeft + "s"} + <> + {timeLeft + ? pixels.available + 1 < config.canvas.pixel.maxStack && timeLeft + "s" + : ""} + ); }; +const OnlineCount = () => { + const [online, setOnline] = useState(); + + useEffect(() => { + function handleOnline(count: number) { + setOnline(count); + } + + network.waitFor("online").then(([count]) => setOnline(count)); + network.on("online", handleOnline); + + return () => { + network.off("online", handleOnline); + }; + }, []); + + return <>{typeof online === "number" ? online : "???"}; +}; + export const CanvasMeta = () => { const { canvasPosition, cursorPosition, pixels, config } = useAppContext(); const { isOpen, onOpen, onOpenChange } = useDisclosure(); @@ -74,7 +98,10 @@ export const CanvasMeta = () => { - Users Online: 321 + Users Online:{" "} + + + diff --git a/packages/client/src/lib/network.ts b/packages/client/src/lib/network.ts index 25ac90002955205640ed6c5a87f3c108ae3c7172..8b7ffb5bafd57d8e7f8ad5ae5dcf59dc8ca9dc86 100644 --- a/packages/client/src/lib/network.ts +++ b/packages/client/src/lib/network.ts @@ -13,6 +13,7 @@ export interface INetworkEvents { canvas: (pixels: string[]) => void; pixels: (data: { available: number }) => void; pixelLastPlaced: (time: number) => void; + online: (count: number) => void; } type SentEventValue = EventEmitter.ArgumentMap< @@ -55,6 +56,10 @@ class Network extends EventEmitter { this._emit("pixelLastPlaced", time); }); + this.socket.on("online", ({ count }) => { + this._emit("online", count); + }); + // this.socket.on("config", (config) => { // Pallete.load(config.pallete); // Canvas.load(config.canvas); @@ -67,10 +72,6 @@ class Network extends EventEmitter { // this.socket.on("canvas", (data: SCanvasPacket) => { // Canvas.handleBatch(data); // }); - - // this.socket.on("online", (data: { count: number }) => { - // this.online_count = data.count; - // }); } private _emit: typeof this.emit = (event, ...args) => {