Skip to content

Commit ce1fe2e

Browse files
Merge branch 'master' into remove-rabbitmq
2 parents 2617037 + d1f7aa3 commit ce1fe2e

File tree

5 files changed

+69
-20
lines changed

5 files changed

+69
-20
lines changed

frontend/src/components/MatchMakeBtn/MatchMakeBtn.component.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {
1414
} from "@chakra-ui/react";
1515
import { useMatchmake } from "../../contexts/matchmake.context";
1616
import { AiOutlineDisconnect as DisconnectIcon } from "react-icons/ai";
17+
import { useSelector } from "react-redux";
18+
import { selectUser } from "../../reducers/authSlice";
1719

1820
const diffRange = [
1921
[0, 2.9],
@@ -24,6 +26,7 @@ const diffRange = [
2426
];
2527

2628
const MatchMakeBtn = () => {
29+
const user = useSelector(selectUser);
2730
const {
2831
findMatch,
2932
isMatching,
@@ -34,6 +37,8 @@ const MatchMakeBtn = () => {
3437
quitRoom,
3538
} = useMatchmake();
3639

40+
if (!user) return <></>;
41+
3742
return matchedRoom ? (
3843
<ButtonGroup isAttached variant="outline">
3944
<Tooltip label="Leave match" aria-label="collaborate">

frontend/src/contexts/matchmake.context.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ interface MatchmakeContextInterface {
3232
quitRoom: () => void;
3333
disconnectRoom: () => void;
3434
restoreRoom: () => void;
35+
reloadRoom: () => void;
3536
isMatching: boolean;
3637
timeLeft?: number;
3738
matchedRoom?: Match;
@@ -43,6 +44,7 @@ const MatchmakeContext = createContext<MatchmakeContextInterface>({
4344
quitRoom: () => {},
4445
disconnectRoom: () => {},
4546
restoreRoom: () => {},
47+
reloadRoom: () => {},
4648
isMatching: false,
4749
});
4850

@@ -230,18 +232,25 @@ export const MatchmakeProvider = ({
230232
const restoreRoom = () => {
231233
if (!socket || !user) return;
232234
if (!socket.connected) socket.connect();
233-
console.log("attempt to reconnect");
234235
socket.emit("restore", user.username);
235236
};
236237

238+
const reloadRoom = () => {
239+
if (!socket || !user || !socket.connected || !matchedRoom) return;
240+
console.log("attempting to reload");
241+
setMatchedRoom({
242+
...matchedRoom,
243+
});
244+
};
245+
237246
const memo = useMemo(() => {
238-
console.log("matchmake update");
239247
return {
240248
findMatch,
241249
cancelMatch,
242250
quitRoom,
243251
disconnectRoom,
244252
restoreRoom,
253+
reloadRoom,
245254
isMatching,
246255
matchedRoom,
247256
timeLeft,

frontend/src/contexts/sharededitor.context.tsx

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export const SharedEditorProvider = ({
122122
const user = useSelector(selectUser) as User; // null check should be done before this
123123

124124
// exposed variables
125-
const { matchedRoom, disconnectRoom } = useMatchmake();
125+
const { matchedRoom, disconnectRoom, reloadRoom } = useMatchmake();
126126
const [lang, setLang] = useState<language>();
127127
const [codeUndo, setCodeUndo] = useState<Y.UndoManager>();
128128
const [ycode, setycode] = useState<Y.Text>();
@@ -140,10 +140,12 @@ export const SharedEditorProvider = ({
140140
const lastSubmissionToastId = useRef<ToastId | undefined>();
141141
const lastLangSelected = useRef<language | undefined>();
142142
const lastCode = useRef<string | undefined>();
143+
const lastChat = useRef<chatRecord[]>([]);
143144
const cachedPastSubmissions = useRef<submissionRecord[]>([]);
144145
const _states = useRef<Y.Map<any> | undefined>();
145146
const _submissions = useRef<Y.Array<any> | undefined>();
146147
const _poll_interval = useRef<NodeJS.Timeout | undefined>();
148+
const _reloadTimeout = useRef<NodeJS.Timeout | undefined>();
147149

148150
const myAvatar = getProfilePicUrl(user.profilePic);
149151

@@ -155,7 +157,7 @@ export const SharedEditorProvider = ({
155157
source_code: submission.code,
156158
qn__id: submission.qn_id,
157159
uid: submission.user,
158-
})
160+
});
159161

160162
const token = res.data.token as string;
161163

@@ -254,6 +256,8 @@ export const SharedEditorProvider = ({
254256
useEffect(() => {
255257
const doc = new Y.Doc();
256258
const ychat = doc.getArray<chatRecord>(CHAT_KEY);
259+
if (!matchedRoom) lastChat.current = [];
260+
ychat.push(lastChat.current);
257261
_setChat(ychat);
258262
const ysubmissions = doc.getArray<submissionRecord>(SUBMISSION_HISTORY_KEY);
259263
_submissions.current = ysubmissions;
@@ -347,7 +351,6 @@ export const SharedEditorProvider = ({
347351
lastLangSelected.current = randLang;
348352
ystates.set(CURR_LANG_STATE, randLang);
349353
ycode.insert(0, lastCode.current ?? LangDataMap[randLang]?.default ?? "");
350-
ystates.set("SYNCEVENT", user.id + random.uint32().toString());
351354
};
352355

353356
const setCodeFromMap = () => {
@@ -382,8 +385,12 @@ export const SharedEditorProvider = ({
382385

383386
const waitForInit = (e: Y.YMapEvent<any>, t: Y.Transaction) => {
384387
// the initer have not initialized the code => wait for him to do so
385-
if (!ystates.has(CODE_STATE)) return;
388+
console.log("waiting for init");
389+
if (!ystates.get(CODE_STATE)) return;
386390
setCodeFromMap();
391+
console.log("Success");
392+
clearTimeout(_reloadTimeout.current);
393+
_reloadTimeout.current = undefined;
387394
ystates.unobserve(waitForInit); // remove this method from observer
388395
ystates.observe(stateEventObserver);
389396
};
@@ -405,6 +412,9 @@ export const SharedEditorProvider = ({
405412
initStates();
406413
} else {
407414
ystates.observe(waitForInit);
415+
_reloadTimeout.current = setTimeout(() => {
416+
reloadRoom();
417+
}, 2000); // reload if not connected after 2s
408418
}
409419

410420
(async () => {
@@ -446,12 +456,17 @@ export const SharedEditorProvider = ({
446456
});
447457

448458
return () => {
449-
console.log("destroying provider");
450459
lastCode.current = ycode?.toString();
460+
lastChat.current = ychat.toArray();
461+
console.log(lastChat.current, matchedRoom);
451462
_states.current = undefined;
452463
cachedPastSubmissions.current = cachedPastSubmissions.current.concat(
453464
ysubmissions.toArray()
454465
);
466+
clearTimeout(_reloadTimeout.current);
467+
_reloadTimeout.current = undefined;
468+
clearInterval(_poll_interval.current);
469+
_poll_interval.current = undefined;
455470
_provider.destroy();
456471
doc.destroy();
457472

frontend/src/pages/ViewQuestionPage/ViewQuestion.page.tsx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { redirect, LoaderFunction, useLoaderData } from "react-router-dom";
1+
import { redirect, LoaderFunction } from "react-router-dom";
22
import {
33
HStack,
44
VStack,
@@ -12,6 +12,7 @@ import {
1212
MenuButton,
1313
MenuList,
1414
MenuItem,
15+
Skeleton,
1516
} from "@chakra-ui/react";
1617
import { Question } from "../../models/Question.model";
1718
import { QnDrawer } from "../../components/QnDrawer/QnDrawer.component";
@@ -20,7 +21,6 @@ import "./ViewQuestion.page.css";
2021
import store from "../../reducers/store";
2122
import { loadQuestions } from "../../data/sampleqn";
2223
import { fetchQuestion } from "../../api/questions";
23-
import { useMatchmake } from "../../contexts/matchmake.context";
2424
import CollabEditor from "../../components/CollabEditor/CollabEditor.component";
2525
import { useContext } from "react";
2626
import {
@@ -54,22 +54,38 @@ export const qnLoader: LoaderFunction<Question> = async ({ params }) => {
5454
return qn ?? redirect("/");
5555
};
5656

57+
const loadingSkeleton = () => {
58+
return (
59+
<HStack className="fit-parent" padding={2.5}>
60+
<VStack className="fit-parent" gap="1">
61+
<Skeleton width="100%" height="5%" speed={0.9}></Skeleton>
62+
<Skeleton width="100%" height="95%" speed={1}></Skeleton>
63+
</VStack>
64+
<VStack h="100%" w="30%">
65+
<Skeleton className="fit-parent" speed={1.1}></Skeleton>
66+
<Skeleton className="fit-parent" speed={1.2}></Skeleton>
67+
<Skeleton height="10%" width="100%" speed={1.3}></Skeleton>
68+
</VStack>
69+
</HStack>
70+
);
71+
};
72+
5773
const InnerViewQuestion = () => {
5874
const {
5975
qn,
60-
chat,
6176
lang,
6277
replaceCode,
6378
changeLang,
6479
currSubmission,
6580
submitCode,
81+
ycode,
6682
} = useContext(SharedEditorContext);
6783

6884
return (
6985
<>
7086
{qn ? <QnDrawer question={qn} size="xl" /> : <></>}
71-
{!lang ? (
72-
<></>
87+
{!(lang && ycode) ? (
88+
loadingSkeleton()
7389
) : (
7490
<HStack className="fit-parent" padding={2.5}>
7591
<VStack className="fit-parent" gap="1">

matching_service/src/sockethandlers.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ const handleMatchRequest = async (
170170
const match = await createMatch(potMatch, userInterval.user);
171171

172172
detail.match = {
173+
// update for this socker user
173174
...match,
174175
isMaster: true,
175176
};
@@ -186,27 +187,30 @@ const handleMatchRequest = async (
186187
continue;
187188
}
188189
matchedUserDetail.match = {
190+
// update for matched user from interval tree
189191
...match,
190192
user: userInterval.user,
191193
isMaster: false,
192194
};
193195

196+
// update for this user from this socket
194197
io.to(userInterval.user)
195-
.except(socket.id)
198+
// .except(socket.id)
196199
.emit("matchFound", {
197-
...matchedUserDetail.match,
198-
init: false,
200+
...detail.match,
201+
init: true,
199202
} as Match);
200203

204+
// update for matched partner from this socket
201205
io.to(match.user).emit("matchFound", {
202-
...detail.match,
206+
...matchedUserDetail.match,
203207
init: false,
204208
} as Match);
205209

206-
socket.emit("matchFound", {
207-
...detail.match,
208-
init: true,
209-
} as Match);
210+
// socket.emit("matchFound", {
211+
// ...detail.match,
212+
// init: true,
213+
// } as Match);
210214

211215
return;
212216
}

0 commit comments

Comments
 (0)