Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Multiplayer with HF
#4
by
Jofthomas
HF staff
- opened
- patches/InteractButton.tsx +88 -87
- patches/world.ts +3 -1
patches/InteractButton.tsx
CHANGED
@@ -1,87 +1,88 @@
|
|
1 |
-
import Button from './Button';
|
2 |
-
import { toast } from 'react-toastify';
|
3 |
-
import interactImg from '../../../assets/interact.svg';
|
4 |
-
import { useConvex, useMutation, useQuery } from 'convex/react';
|
5 |
-
import { api } from '../../../convex/_generated/api';
|
6 |
-
// import { SignInButton } from '@clerk/clerk-react';
|
7 |
-
import { ConvexError } from 'convex/values';
|
8 |
-
import { Id } from '../../../convex/_generated/dataModel';
|
9 |
-
import { useCallback } from 'react';
|
10 |
-
import { waitForInput } from '../../hooks/sendInput';
|
11 |
-
import { useServerGame } from '../../hooks/serverGame';
|
12 |
-
|
13 |
-
export default function InteractButton() {
|
14 |
-
// const { isAuthenticated } = useConvexAuth();
|
15 |
-
const worldStatus = useQuery(api.world.defaultWorldStatus);
|
16 |
-
const worldId = worldStatus?.worldId;
|
17 |
-
const game = useServerGame(worldId);
|
18 |
-
const oauth = JSON.parse(localStorage.getItem('oauth'));
|
19 |
-
const oauthToken = oauth ? oauth.userInfo.fullname : undefined;
|
20 |
-
|
21 |
-
const
|
22 |
-
|
23 |
-
|
24 |
-
const
|
25 |
-
const
|
26 |
-
|
27 |
-
|
28 |
-
const
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
//
|
68 |
-
//
|
69 |
-
//
|
70 |
-
//
|
71 |
-
//
|
72 |
-
//
|
73 |
-
//
|
74 |
-
//
|
75 |
-
//
|
76 |
-
//
|
77 |
-
//
|
78 |
-
//
|
79 |
-
//
|
80 |
-
//
|
81 |
-
//
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
|
1 |
+
import Button from './Button';
|
2 |
+
import { toast } from 'react-toastify';
|
3 |
+
import interactImg from '../../../assets/interact.svg';
|
4 |
+
import { useConvex, useMutation, useQuery } from 'convex/react';
|
5 |
+
import { api } from '../../../convex/_generated/api';
|
6 |
+
// import { SignInButton } from '@clerk/clerk-react';
|
7 |
+
import { ConvexError } from 'convex/values';
|
8 |
+
import { Id } from '../../../convex/_generated/dataModel';
|
9 |
+
import { useCallback } from 'react';
|
10 |
+
import { waitForInput } from '../../hooks/sendInput';
|
11 |
+
import { useServerGame } from '../../hooks/serverGame';
|
12 |
+
|
13 |
+
export default function InteractButton() {
|
14 |
+
// const { isAuthenticated } = useConvexAuth();
|
15 |
+
const worldStatus = useQuery(api.world.defaultWorldStatus);
|
16 |
+
const worldId = worldStatus?.worldId;
|
17 |
+
const game = useServerGame(worldId);
|
18 |
+
const oauth = JSON.parse(localStorage.getItem('oauth'));
|
19 |
+
const oauthToken = oauth ? oauth.userInfo.fullname : undefined;
|
20 |
+
console.log(oauthToken)
|
21 |
+
const humanTokenIdentifier = useQuery(api.world.userStatus, worldId ? { worldId, oauthToken } : 'skip');
|
22 |
+
const userPlayerId =
|
23 |
+
game && [...game.world.players.values()].find((p) => p.human === humanTokenIdentifier)?.id;
|
24 |
+
const join = useMutation(api.world.joinWorld);
|
25 |
+
const leave = useMutation(api.world.leaveWorld);
|
26 |
+
const isPlaying = !!userPlayerId;
|
27 |
+
|
28 |
+
const convex = useConvex();
|
29 |
+
const joinInput = useCallback(
|
30 |
+
async (worldId: Id<'worlds'>) => {
|
31 |
+
let inputId;
|
32 |
+
try {
|
33 |
+
inputId = await join({ worldId, oauthToken });
|
34 |
+
} catch (e: any) {
|
35 |
+
if (e instanceof ConvexError) {
|
36 |
+
toast.error(e.data);
|
37 |
+
return;
|
38 |
+
}
|
39 |
+
throw e;
|
40 |
+
}
|
41 |
+
try {
|
42 |
+
await waitForInput(convex, inputId);
|
43 |
+
} catch (e: any) {
|
44 |
+
toast.error(e.message);
|
45 |
+
}
|
46 |
+
},
|
47 |
+
[convex, join, oauthToken],
|
48 |
+
);
|
49 |
+
|
50 |
+
|
51 |
+
const joinOrLeaveGame = () => {
|
52 |
+
if (
|
53 |
+
!worldId ||
|
54 |
+
// || !isAuthenticated
|
55 |
+
game === undefined
|
56 |
+
) {
|
57 |
+
return;
|
58 |
+
}
|
59 |
+
if (isPlaying) {
|
60 |
+
console.log(`Leaving game for player ${userPlayerId}`);
|
61 |
+
void leave({ worldId , oauthToken});
|
62 |
+
} else {
|
63 |
+
console.log(`Joining game`);
|
64 |
+
void joinInput(worldId);
|
65 |
+
}
|
66 |
+
};
|
67 |
+
// if (!isAuthenticated || game === undefined) {
|
68 |
+
// return (
|
69 |
+
// <SignInButton>
|
70 |
+
// <button className="button text-white shadow-solid text-2xl pointer-events-auto">
|
71 |
+
// <div className="inline-block bg-clay-700">
|
72 |
+
// <span>
|
73 |
+
// <div className="inline-flex h-full items-center gap-4">
|
74 |
+
// <img className="w-4 h-4 sm:w-[30px] sm:h-[30px]" src={interactImg} />
|
75 |
+
// Interact
|
76 |
+
// </div>
|
77 |
+
// </span>
|
78 |
+
// </div>
|
79 |
+
// </button>
|
80 |
+
// </SignInButton>
|
81 |
+
// );
|
82 |
+
// }
|
83 |
+
return (
|
84 |
+
<Button imgUrl={interactImg} onClick={joinOrLeaveGame}>
|
85 |
+
{isPlaying ? 'Leave' : 'Interact'}
|
86 |
+
</Button>
|
87 |
+
);
|
88 |
+
}
|
patches/world.ts
CHANGED
@@ -107,6 +107,7 @@ export const userStatus = query({
|
|
107 |
if (!oauthToken) {
|
108 |
return null;
|
109 |
}
|
|
|
110 |
return oauthToken;
|
111 |
},
|
112 |
});
|
@@ -143,7 +144,7 @@ export const joinWorld = mutation({
|
|
143 |
return await insertInput(ctx, world._id, 'join', {
|
144 |
name: oauthToken,
|
145 |
character: randomCharacter.character,
|
146 |
-
description:
|
147 |
tokenIdentifier: oauthToken,
|
148 |
});
|
149 |
},
|
@@ -159,6 +160,7 @@ export const leaveWorld = mutation({
|
|
159 |
const { worldId, oauthToken } = args;
|
160 |
|
161 |
|
|
|
162 |
if (!oauthToken) {
|
163 |
throw new ConvexError(`Not logged in`);
|
164 |
}
|
|
|
107 |
if (!oauthToken) {
|
108 |
return null;
|
109 |
}
|
110 |
+
console.log("oauthToken", oauthToken)
|
111 |
return oauthToken;
|
112 |
},
|
113 |
});
|
|
|
144 |
return await insertInput(ctx, world._id, 'join', {
|
145 |
name: oauthToken,
|
146 |
character: randomCharacter.character,
|
147 |
+
description: "This is you !",
|
148 |
tokenIdentifier: oauthToken,
|
149 |
});
|
150 |
},
|
|
|
160 |
const { worldId, oauthToken } = args;
|
161 |
|
162 |
|
163 |
+
console.log('OAuth Name:', oauthToken);
|
164 |
if (!oauthToken) {
|
165 |
throw new ConvexError(`Not logged in`);
|
166 |
}
|