Skip to content

Commit dc17f44

Browse files
committed
Update session startTime and duration
1 parent 9ff2805 commit dc17f44

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

Backend/CollabService/utils/roomManager.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ function manageRoom(ws, roomId, userId, type) {
1515
rooms[roomId] = { sockets: [], userIds: [], matchedUserIds: [], startTime: new Date().toISOString()};
1616
}
1717

18+
// Broadcast start time to the newly joined user
19+
broadcastRoomStartTime(ws, roomId);
20+
1821
console.log(`BEFORE room Info: userIds[${rooms[roomId].userIds}] || matchedusers[${rooms[roomId].matchedUserIds}]`)
1922

2023
const numOfMatchedUsers = rooms[roomId].matchedUserIds.length

Frontend/src/components/collab/CollaborationSpace.jsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const CollaborationSpace = () => {
2929
const [output, setOutput] = useState("")
3030
const [messages, setMessages] = useState([])
3131
const [outputLoading, setOutputLoading] = useState(false)
32+
const [roomStartTime, setRoomStartTime] = useState(null); // Initialize state for room start time
3233

3334
// use https://emkc.org/api/v2/piston/runtimes to GET other languages
3435
const LANGUAGEVERSIONS = {
@@ -136,6 +137,10 @@ const CollaborationSpace = () => {
136137
case 'userLeft':
137138
addNotif(`User ${data.user} has left`)
138139
break;
140+
// Handle room start time message
141+
case 'roomStartTime':
142+
setRoomStartTime(data.startTime);
143+
break;
139144
default:
140145
console.log("No messages received from room management server");
141146
break;
@@ -169,6 +174,8 @@ const CollaborationSpace = () => {
169174
try {
170175
// Filter out the current user from the usersSave array to find the matched user
171176
const matchedUser = usersSave.filter(user => user !== username)[0]; // Assuming usersSave contains objects with userId property
177+
const startTime = new Date(roomStartTime); // Convert start time from ISO to Date object
178+
const duration = new Date().getTime() - startTime.getTime();
172179

173180
if (!matchedUser) {
174181
throw new Error("No matched user found");
@@ -179,9 +186,11 @@ const CollaborationSpace = () => {
179186
questionTitle: 'BFS', // This ID should be available in context or passed down
180187
// questionTitle: questionTitle, // This ID should be available in context or passed down
181188
// startTime: roomCreationTime,
182-
startTime: new Date(),
189+
// startTime: new Date(),
183190
// duration: new Date().getTime() - new Date(roomCreationTime).getTime(),
184-
duration: 10,
191+
// duration: 10,
192+
startTime: startTime.toISOString(),
193+
duration: Math.floor(duration / 1000),
185194
code: '1'
186195
}
187196

Frontend/src/components/user/HistoryAttempt.jsx

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,44 @@ import React from 'react';
44
const HistoryAttempt = ({ attempt }) => {
55
const { matchedUsername, questionTitle, startTime, duration, code } = attempt;
66

7+
// Function to format duration based on requirements
8+
const formatDuration = (seconds) => {
9+
if (seconds >= 3600) {
10+
// Format as H:MM:SS
11+
const hours = Math.floor(seconds / 3600);
12+
const minutes = Math.floor((seconds % 3600) / 60);
13+
const remainingSeconds = seconds % 60;
14+
return `${hours}:${String(minutes).padStart(2, '0')}:${String(remainingSeconds).padStart(2, '0')}`;
15+
} else if (seconds >= 60) {
16+
// Format as MM:SS
17+
const minutes = Math.floor(seconds / 60);
18+
const remainingSeconds = seconds % 60;
19+
return `${String(minutes).padStart(2, '0')}:${String(remainingSeconds).padStart(2, '0')}`;
20+
} else {
21+
// Format as 00:SS
22+
return `00:${String(seconds).padStart(2, '0')}`;
23+
}
24+
};
25+
726
return (
827
<tr>
928
<td>{matchedUsername}</td>
1029
<td>{questionTitle}</td>
1130
<td>{new Date(startTime).toLocaleString()}</td>
12-
<td>{duration} minutes</td>
31+
<td>{formatDuration(duration)}</td>
32+
{/* Uncomment the below section if you want to enable code download */}
1333
{/* <td>
14-
{/* <button onClick={() => downloadCode(codeFile)} className="btn btn-sm btn-primary">
34+
<button onClick={() => downloadCode(code)} className="btn btn-sm btn-primary">
1535
Download Code
16-
</button> }
17-
</td>*/}
36+
</button>
37+
</td> */}
1838
<td>{code} dummy file content</td>
19-
2039
</tr>
2140
);
2241
};
2342

43+
// Function to download the binary code file
2444
const downloadCode = (file) => {
25-
// Logic for downloading the binary code file
2645
const blob = new Blob([file], { type: 'application/octet-stream' });
2746
const link = document.createElement('a');
2847
link.href = URL.createObjectURL(blob);

0 commit comments

Comments
 (0)