Multiplayer with HF

#4
by Jofthomas HF staff - opened
Files changed (2) hide show
  1. patches/InteractButton.tsx +88 -87
  2. 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
- const humanTokenIdentifier = useQuery(api.world.userStatus, worldId ? { worldId, oauthToken } : 'skip');
21
- const userPlayerId =
22
- game && [...game.world.players.values()].find((p) => p.human === humanTokenIdentifier)?.id;
23
- const join = useMutation(api.world.joinWorld);
24
- const leave = useMutation(api.world.leaveWorld);
25
- const isPlaying = !!userPlayerId;
26
-
27
- const convex = useConvex();
28
- const joinInput = useCallback(
29
- async (worldId: Id<'worlds'>) => {
30
- let inputId;
31
- try {
32
- inputId = await join({ worldId, oauthToken });
33
- } catch (e: any) {
34
- if (e instanceof ConvexError) {
35
- toast.error(e.data);
36
- return;
37
- }
38
- throw e;
39
- }
40
- try {
41
- await waitForInput(convex, inputId);
42
- } catch (e: any) {
43
- toast.error(e.message);
44
- }
45
- },
46
- [convex, join, oauthToken],
47
- );
48
-
49
-
50
- const joinOrLeaveGame = () => {
51
- if (
52
- !worldId ||
53
- // || !isAuthenticated
54
- game === undefined
55
- ) {
56
- return;
57
- }
58
- if (isPlaying) {
59
- console.log(`Leaving game for player ${userPlayerId}`);
60
- void leave({ worldId , oauthToken});
61
- } else {
62
- console.log(`Joining game`);
63
- void joinInput(worldId);
64
- }
65
- };
66
- // if (!isAuthenticated || game === undefined) {
67
- // return (
68
- // <SignInButton>
69
- // <button className="button text-white shadow-solid text-2xl pointer-events-auto">
70
- // <div className="inline-block bg-clay-700">
71
- // <span>
72
- // <div className="inline-flex h-full items-center gap-4">
73
- // <img className="w-4 h-4 sm:w-[30px] sm:h-[30px]" src={interactImg} />
74
- // Interact
75
- // </div>
76
- // </span>
77
- // </div>
78
- // </button>
79
- // </SignInButton>
80
- // );
81
- // }
82
- return (
83
- <Button imgUrl={interactImg} onClick={joinOrLeaveGame}>
84
- {isPlaying ? 'Leave' : 'Interact'}
85
- </Button>
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: `${oauthToken} is a Human player !`,
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
  }