Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
feat: set username
Browse files Browse the repository at this point in the history
  • Loading branch information
Pixselve committed Jan 24, 2024
1 parent cd3a172 commit 866ca18
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/client/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import logo from "../assets/logo.png";

export default function HomePage() {
const [serverUrl, setServerUrl] = useState<string>("http://localhost:4000");
const api = useApi(serverUrl);
const [isCentered, setIsCentered] = useState<boolean>(false);
const [username, setUsername] = useState("");
const api = useApi(username, serverUrl);

return (
<main className="dustBackground flex h-full flex-col items-center justify-center">
Expand Down
5 changes: 3 additions & 2 deletions packages/client/src/hooks/useApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const URL =
process.env.NODE_ENV === "production" ? undefined : "http://localhost:4000";
const socket = io(URL!, { autoConnect: false });

export function useApi(serverUrl?: string) {
export function useApi(username: string, serverUrl?: string) {
const {
sharedState: { isConnected, me, scene },
updateSharedState,
Expand All @@ -36,6 +36,7 @@ export function useApi(serverUrl?: string) {

useEffect(() => {
function handleConnect() {
socket.emit(SOCKET_EVENTS.JOIN, username);
updateSharedState({ isConnected: true });
}
function handleDisconnect() {
Expand All @@ -52,7 +53,7 @@ export function useApi(serverUrl?: string) {
socket.off("disconnect", handleDisconnect);
socket.off(SOCKET_EVENTS.FRAME, handleFrame);
};
}, [updateSharedState]);
}, [updateSharedState, username]);

return {
connect: () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ io.on(SOCKET_EVENTS.CONNECT, (socket) => {

const player: Player = {
id: socket.id,
name: "Player 1",
name: "TEMP USERNAME",
color: randomDarkColor(),
body: [{ x: 0, y: 0 }],
isSprinting: false,
Expand All @@ -56,6 +56,10 @@ io.on(SOCKET_EVENTS.CONNECT, (socket) => {
player.isSprinting = move.isSprinting;
player.desiredAngle = move.angle;
});

socket.on(SOCKET_EVENTS.JOIN, (name) => {
player.name = name;
});
});

setInterval(() => {
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const MIN_SCORE_TO_SPRINT = SCORE_PER_BODY_PART * 4;
*/
export enum SOCKET_EVENTS {
CONNECT = "connect",
JOIN = "join",
DISCONNECT = "disconnect",
MOVE = "move",
FRAME = "frame",
Expand Down

0 comments on commit 866ca18

Please sign in to comment.