Skip to content

Commit be80a81

Browse files
committed
Add Emotes and Fix Deployment Errors
1 parent 3d2d936 commit be80a81

File tree

8 files changed

+53
-11
lines changed

8 files changed

+53
-11
lines changed

src/app/create/page.tsx

+8-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ import { api } from "~/trpc/react";
1616
import { IoReload } from "react-icons/io5";
1717
import { DataTable } from "./data-table";
1818
import { cards } from "./columns";
19-
import { type PlayerJoinLeaveEvent } from "~/types/pusherEvents";
19+
import {
20+
PlayerEmoteEvent,
21+
type PlayerJoinLeaveEvent,
22+
} from "~/types/pusherEvents";
2023
import { Badge } from "~/components/ui/badge";
2124

2225
export default function Create() {
@@ -96,6 +99,10 @@ export default function Create() {
9699
toast(`Player ${data.username} has left.`);
97100
setPlayers([...newState]);
98101
});
102+
103+
channel.bind("emote", (data: PlayerEmoteEvent) => {
104+
toast(`${data.username}: ${data.emote}`);
105+
});
99106
}
100107
}, [gameId, pusher]);
101108

src/app/layout.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const inter = Inter({
1111
});
1212

1313
export const metadata = {
14-
title: "Create T3 App",
14+
title: "Quizoo",
1515
description: "Generated by create-t3-app",
1616
icons: [{ rel: "icon", url: "/favicon.svg" }],
1717
};

src/app/page.tsx

+14-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import RoomIdInput from "~/components/RoomIdInput";
1010
import { api } from "~/trpc/react";
1111
import { getPusherEnvVars } from "~/utils/pusherClient";
1212
import { useSearchParams } from "next/navigation";
13+
import { Button } from "~/components/ui/button";
1314

1415
export default function Home() {
16+
const [username, setUsername] = useState<string>("");
1517
const router = useRouter();
1618
const query = useSearchParams();
1719
const roomId = query.get("roomId");
@@ -20,6 +22,7 @@ export default function Home() {
2022

2123
const leaveMutation = api.sockets.playerLeave.useMutation();
2224
const joinMutation = api.sockets.playerJoin.useMutation();
25+
const emoteMutation = api.sockets.playerEmote.useMutation();
2326

2427
useEffect(() => {
2528
// Initialise Client Pusher
@@ -35,7 +38,7 @@ export default function Home() {
3538
// Trigger Leave
3639
if (initializedPusher) {
3740
leaveMutation.mutate({
38-
username: localStorage.getItem("username")!,
41+
username,
3942
gameId: roomId!,
4043
});
4144

@@ -87,6 +90,15 @@ export default function Home() {
8790

8891
setIsSubscribed(true);
8992
});
93+
setUsername(username);
94+
};
95+
96+
const handleEmote = (emote: string) => {
97+
emoteMutation.mutate({
98+
gameId: roomId!,
99+
username,
100+
emote,
101+
});
90102
};
91103

92104
if (!isSubscribed) {
@@ -103,6 +115,7 @@ export default function Home() {
103115
return (
104116
<PageLayout className="justify-center" isPusherActive={pusher !== null}>
105117
<p>Waiting...</p>
118+
<Button onClick={() => handleEmote("😄")}>Send Emote 😄</Button>
106119
</PageLayout>
107120
);
108121
}

src/components/RoomIdInput.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export default function RoomIdInput({ handleSubmit }: RoomIdInputInterface) {
6666
<FormItem>
6767
<FormLabel>😇 Username</FormLabel>
6868
<FormControl>
69-
<Input placeholder="eg. Jane Chen" {...field} />
69+
<Input placeholder="eg. Jane Cheah" {...field} />
7070
</FormControl>
7171
</FormItem>
7272
)}

src/components/ui/input.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import * as React from "react"
22

33
import { cn } from "~/lib/utils"
44

5-
export interface InputProps
6-
extends React.InputHTMLAttributes<HTMLInputElement> {}
5+
export type InputProps = React.InputHTMLAttributes<HTMLInputElement>
76

87
const Input = React.forwardRef<HTMLInputElement, InputProps>(
98
({ className, type, ...props }, ref) => {

src/components/ui/textarea.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import * as React from "react";
22

33
import { cn } from "~/lib/utils";
44

5-
export interface TextareaProps
6-
extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {}
5+
export type TextareaProps = React.TextareaHTMLAttributes<HTMLTextAreaElement>
76

87
const Textarea = React.forwardRef<HTMLTextAreaElement, TextareaProps>(
98
({ className, ...props }, ref) => {

src/server/api/routers/sockets.ts

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { createTRPCRouter, publicProcedure } from "../trpc";
22
import { z } from "zod";
3-
import { type PlayerJoinLeaveEvent as PlayerJoinLeftEvent } from "~/types/pusherEvents";
3+
import {
4+
type PlayerEmoteEvent,
5+
type PlayerJoinLeaveEvent as PlayerJoinLeftEvent,
6+
} from "~/types/pusherEvents";
47
import { pusherServer } from "~/utils/pusherServer";
58
import axios from "axios";
69
import { env } from "~/env";
@@ -16,7 +19,7 @@ export const socketsRouter = createTRPCRouter({
1619
.input(
1720
z.object({
1821
username: z.string().min(1),
19-
gameId: z.string().min(1),
22+
gameId: z.string().length(6),
2023
}),
2124
)
2225
.mutation((opts) => {
@@ -36,7 +39,7 @@ export const socketsRouter = createTRPCRouter({
3639
.input(
3740
z.object({
3841
username: z.string().min(1),
39-
gameId: z.string().min(1),
42+
gameId: z.string().length(6),
4043
}),
4144
)
4245
.mutation((opts) => {
@@ -47,6 +50,22 @@ export const socketsRouter = createTRPCRouter({
4750
};
4851
return pusherServer.trigger(gameId, "player-left", leftEvent);
4952
}),
53+
playerEmote: publicProcedure
54+
.input(
55+
z.object({
56+
emote: z.string().min(1),
57+
username: z.string().min(1),
58+
gameId: z.string().length(6),
59+
}),
60+
)
61+
.mutation((opts) => {
62+
const { emote, username, gameId } = opts.input;
63+
const emoteEvent: PlayerEmoteEvent = {
64+
username,
65+
emote,
66+
};
67+
return pusherServer.trigger(gameId, "emote", emoteEvent);
68+
}),
5069
getChannelInfo: publicProcedure
5170
.input(
5271
z.object({

src/types/pusherEvents.ts

+5
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ export interface PlayerJoinLeaveEvent {
22
username: string;
33
time: Date;
44
}
5+
6+
export interface PlayerEmoteEvent {
7+
username: string;
8+
emote: string;
9+
}

0 commit comments

Comments
 (0)