-
Notifications
You must be signed in to change notification settings - Fork 1
/
RuleGuide.tsx
375 lines (360 loc) · 10.7 KB
/
RuleGuide.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import React, { useState, useEffect, useRef, useMemo, useLayoutEffect } from "react";
import { useNavigate } from "react-router-dom";
import { PlayerList } from "./GameroomPlayerList";
import { Roundstatus } from "./GameroomRoundStatus";
import { ValidateAnswerForm } from "./GameroomAnswerForm";
import BaseContainer from "components/ui/BaseContainer";
import Header from "./Header";
import { FFmpeg } from "@ffmpeg/ffmpeg";
import "../../styles/views/RuleGuide.scss";
import { TourProvider, useTour } from "@reactour/tour"
import { showToast} from "../../helpers/toastService";
const mockGameTheme = "FOOD";
const mockCurrentPlayer = "id2";
const mockRoomName = "GuideRoom";
const mockGlobalVolume = 0.5;
const MS_PER_SEC = 1000;
const DEFAULT_ROUND_DURATION_S = 20;
const mockGameInfo = {
roomID: "1",
currentSpeaker: {
userID: "id2",
username: "myself",
avatar: "dog-face"
},
currentAnswer: "Banana",
roundStatus: "speak",
currentRoundNum: 1
};
const mockSharedAudioList = {
"id1": "fakeURL"
};
const mockPlayerLists = [
{
user: {
id: "id1",
name: "user1",
avatar: "clown-face"
},
score: {
total: 0
},
ifGuess: true,
roundFinished: false,
ready: true
},
{
user: {
id: "id2",
name: "myself",
avatar: "dog-face"
},
score: {
total: 0
},
ifGuess: false,
roundFinished: false,
ready: true
},
{
user: {
id: "id3",
name: "user3",
avatar: "cat-face"
},
score: {
total: 0
},
ifGuess: true,
roundFinished: false,
ready: true
},
{
user: {
id: "id4",
name: "user4",
avatar: "angry-face"
},
score: {
total: 0
},
ifGuess: true,
roundFinished: false,
ready: true
}
];
const gameOver = false;
const user = {
token: "mockToken",
id: "id2",
username: "myself",
};
type GuideContentDivProps = {
children: React.ReactNode;
}
const GuideContentDiv: React.FC<GuideContentDivProps> = (props:GuideContentDivProps) => {
return (
<div className="guide-content">
{props.children}
</div>
);
}
const RuleGuideContent = ({ffmpegObj}:any) => {
const { isOpen, currentStep, steps, setIsOpen, setCurrentStep, setSteps } = useTour()
// const currentSpeakerAudioURL = "fakeURL";
const [currentSpeakerAudioURL, setCurrentSpeakerAudioURL] = useState("fakeURL");
const [sharedAudioList, setSharedAudioList] = useState(mockSharedAudioList);
const isRecorded = useRef(false);
const roundStatusComponentRef = useRef(null);
const [gameInfo, setGameInfo] = useState(mockGameInfo);
const [playerInfo, setPlayerInfo] = useState(mockPlayerLists);
const endTime = useMemo(() => new Date(Date.now() + DEFAULT_ROUND_DURATION_S * MS_PER_SEC).toISOString(), [gameInfo.roundStatus]);
const step_speak_phase = [
{
selector: ".roundstatus",
// title: "Guide: Speak-Phase",
content: "Now you are the Speaker, and you need to record the word 'Banana' in the next 20 seconds",
stepInteraction: false,
padding: 0,
},
{
selector: ".remindermssg",
// title: "Guide: Speak-Phase",
content: "This is where you record your audio",
stepInteraction: false,
padding: 0,
},
{
selector: ".record-button",
// title: "Guide: Speak-Phase",
// content: "Click here to start a recording, click again to finish, if you are not satisfied with the recording, you can click again to re-record. The maximum recording time is 5 seconds, if you exceed the time limit, the recording will be automatically stopped.",
content: (
<GuideContentDiv>
<p>Click here to start a recording, click again to finish.</p>
<p style={{ color: "red" }}>(you must record something to continue the guide.)</p>
</GuideContentDiv>
),
// stepInteraction: false,
padding: 0,
disableActions: !isRecorded.current,
},
{
selector: ".toggle-reverse-button",
// title: "Guide: Speak-Phase",
content: "Click here to reverse the audio, and click again to restore to the original audio, you can see the sound wave changes.",
stepInteraction: isRecorded.current,
padding: 0,
},
{
selector: ".audio-recorder.play-button",
// title: "Guide: Speak-Phase",
content: "Click here to listen to your recording, you can also click the sound wave to listen.",
stepInteraction: isRecorded.current,
padding: 0,
},
{
// upload button
selector: ".readybutton", // it is weird that the button is using a class name of readybutton
// title: "Guide: Speak-Phase",
// placement: "top",
position: "top",
content: "Click here to submit your recording, and then all the other players will guess the word you recorded",
stepInteraction: false,
padding: 0,
},
{
selector: ".roundstatus",
// title: "Guide: Guess-Phase",
content: "Now, user3 is the Speaker, and you need to guess the word by simulating the reversed audio",
// placement: "left",
position: "left",
stepInteraction: false,
padding: 0,
action:() => {
if (gameInfo.roundStatus === "speak"){
setPlayerInfo(playerInfo.map((player) => {
if (player.user.id === user.id) {
return { ...player, roundFinished: false, ifGuess: true };
}
if (player.user.id === "id3") {
return { ...player, ifGuess: false, roundFinished: false };
}
return player;
}));
setGameInfo(
{
...gameInfo,
currentSpeaker: {
userID: "id3",
username: "user3",
avatar: "cat-face"
},
roundStatus: "guess"
}
);
}
}
},
{
selector: ".speakPlayerContainer",
// title: "Guide: Guess-Phase",
content: "Click here to listen to the speaker's audio",
stepInteraction: isRecorded.current,
padding: 0,
},
{
selector: ".remindermssg",
// title: "Guide: Guess-Phase",
content: "You shuold simulate the reversed audio and reverse it again to figure out the word",
stepInteraction: false,
padding: 0,
// placement: "top",
},
{
selector: ".inputarea",
// title: "Guide: Guess-Phase",
content: "After you figure out the word, you can submit your answer here, also you can share your audio with others (Optional)",
stepInteraction: false,
padding: 0,
// placement: "top",
},
{
selector: ".btn-player",
title: "Guide: Guess-Phase",
content: (
<GuideContentDiv>
<p>When someone shares their audio, you can click here to listen to their audio</p>
<p style={{ color: "red" }}>(You have now completed the guide, click anywhere out of this box to leave.)</p>
</GuideContentDiv>
),
// stepInteraction: false,
padding: 0,
actionAfter: () => {
showToast("Congratulations! You have successfully completed the Rule Guide!\nEnjoy the game!", "success");
}
},
];
useLayoutEffect(() => {
setSteps(step_speak_phase);
setIsOpen(true);
}, [isRecorded.current]);
return (
<BaseContainer className="gameroom basecontainer">
{/* <Header left="28vw" /> */}
<PlayerList
currentPlayerId={mockCurrentPlayer}
playerStatus={playerInfo}
sharedAudioList={sharedAudioList}
gameTheme={mockGameTheme}
currentRoomName={mockRoomName}
showReadyPopup={false}
gameOver={false}
globalVolume={mockGlobalVolume}
/>
<div className="gameroom right-area">
<Header
onChange={
e => {
}
}
onClickMute={
() => {
}
}
volume={mockGlobalVolume}
/>
{!gameOver && (
<Roundstatus
gameInfo={gameInfo}
currentSpeakerAudioURL={currentSpeakerAudioURL}
endTime={endTime}
ffmpegObj={ffmpegObj}
meId={user.id}
globalVolume={mockGlobalVolume}
handleAudioReversed={(audio) => {
// make url for the reversed audio
const TIME_DELAY = 200;
const url = URL.createObjectURL(audio);
isRecorded.current = true;
setSharedAudioList({["id1"]: url });
setCurrentSpeakerAudioURL(url);
setTimeout(() => {
setCurrentStep(prev => prev + 1);
}
, TIME_DELAY);
}}
ref={roundStatusComponentRef}
/>
)}
<div className="gameroom inputarea">
{gameInfo.roundStatus === "guess" && (
<div style={{ display: "flex", flexDirection: "row" }}>
<ValidateAnswerForm
submitAnswer={(answer) => { }}
roundFinished={false}
/>
</div>
)}
{gameInfo.roundStatus === "speak" && (
<button className="gameroom readybutton"
disabled={false}
onClick={
e => {
}
}>upload</button>
)}
{gameInfo.roundStatus === "guess" && (
<div style={{ marginTop: "1rem" }} className="gameroom readybutton" onClick={
() => {
//console.log("upload audio");
// throttledUploadAudio();
}
}>Share Audio</div>
)}
</div>
</div>
</BaseContainer>
);
}
const RuleGuide = () => {
const navigate = useNavigate();
const ffmpegRef = useRef(null);
const [ifFFmpegLoaded, setIfFFmpegLoaded] = useState(false);
useLayoutEffect(() => {
ffmpegRef.current = new FFmpeg();
const loadFFmpeg = async (ffmpeg: FFmpeg) => {
await ffmpeg.load();
}
try {
loadFFmpeg(ffmpegRef.current).then(() => {
console.log("FFmpeg loaded successfully", ffmpegRef.current);
setIfFFmpegLoaded(true);
});
} catch (e) {
console.error(e);
alert("Failed to load ffmpeg! Please use Chrome Browser.");
}
return () => {
ffmpegRef.current?.loaded && ffmpegRef.current.terminate();
}
}, []);
return (
<TourProvider
className="gameroom guide"
steps={[]}
defaultOpen={true}
startAt={0}
disableDotsNavigation={true}
showCloseButton={false}
beforeClose={() => {
showToast("You have left the guide page.", "info");
navigate("/lobby");
}}
>
{
ifFFmpegLoaded && <RuleGuideContent ffmpegObj={ffmpegRef.current} />
}
</TourProvider>
);
}
export default RuleGuide;