Skip to content

Commit

Permalink
Merge pull request #193 from sopra-fs24-group-09/dev
Browse files Browse the repository at this point in the history
Dev merge to main for deploy
  • Loading branch information
yixuan-zhou-uzh authored May 20, 2024
2 parents 36b3a85 + ab9ac24 commit 72dcf2f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 55 deletions.
125 changes: 72 additions & 53 deletions src/components/views/Gameroom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { throttle } from "lodash";
const DEFAULT_VOLUME = 0.5;
const THROTTLE_TIME = 1000;
const RESPONSE_TIME = 5000;
const TOAST_TIME_LONG = 6000;
const INDEX_NOT_FOUND = -1;

type SharedAudioURL = { [userId: string]: string };
Expand All @@ -46,6 +47,7 @@ const Gameroom = () => {
const [showReadyPopup, setShowReadyPopup] = useState(false);
const readyStatus = useRef(false);
const [gameOver, setGameOver] = useState(false);
const isMicReady = useRef(false);
const gameOverRef = useRef(false);
const [currentSpeakerID, setCurrentSpeakerID] = useState(null);
const [playerLists, setPlayerLists] = useState([]);
Expand Down Expand Up @@ -98,6 +100,18 @@ const Gameroom = () => {
return ffmpeg;
}, []);

function checkMic() {
navigator.mediaDevices.getUserMedia({ audio: true })
.then(function (stream) {
isMicReady.current = true;
console.log("Microphone is already accessible.");
})
.catch(function (err) {
isMicReady.current = false;
console.log("Microphone access is not granted.");
showToast("Microphone access is required to start the game; Please verify your settings.", "error", TOAST_TIME_LONG);
});
}
//("GameInfo", gameInfo);


Expand Down Expand Up @@ -391,35 +405,38 @@ const Gameroom = () => {

//ready
const getReady = useCallback(() => {
console.log("ready once - throttle")
const payload: Timestamped<PlayerAndRoomID> = {
// need to make sure the timestamp is UTC format
// and invariant to the time zone settings
timestamp: new Date().getTime(),
message: {
userID: user.id,
roomID: currentRoomID,
},
};
// get a random receipt uuid
const receiptId = uuidv4();
stompClientRef.current?.send(
`/app/message/users/ready/${currentRoomID}`,
{ receiptId: receiptId,
token: sessionStorage.getItem("token") },
JSON.stringify(payload)
);
requestLists.current.push({ type: "ready",receiptId: receiptId });
console.log(requestLists.current)
const timeoutId = setTimeout(() => {
const index = requestLists.current.findIndex(request => request.receiptId === receiptId);
if (index !== INDEX_NOT_FOUND) {
requestLists.current.splice(index, 1);
}
checkMic();
if(isMicReady.current){
console.log("ready once - throttle")
const payload: Timestamped<PlayerAndRoomID> = {
// need to make sure the timestamp is UTC format
// and invariant to the time zone settings
timestamp: new Date().getTime(),
message: {
userID: user.id,
roomID: currentRoomID,
},
};
// get a random receipt uuid
const receiptId = uuidv4();
stompClientRef.current?.send(
`/app/message/users/ready/${currentRoomID}`,
{ receiptId: receiptId,
token: sessionStorage.getItem("token") },
JSON.stringify(payload)
);
requestLists.current.push({ type: "ready",receiptId: receiptId });
console.log(requestLists.current)
}, RESPONSE_TIME);
const timeoutId = setTimeout(() => {
const index = requestLists.current.findIndex(request => request.receiptId === receiptId);
if (index !== INDEX_NOT_FOUND) {
requestLists.current.splice(index, 1);
}
console.log(requestLists.current)
}, RESPONSE_TIME);

return () => clearTimeout(timeoutId);
return () => clearTimeout(timeoutId);
}
},[user.id,currentRoomID]);
const throttledGetReady = useCallback(throttle(getReady, THROTTLE_TIME), [getReady, THROTTLE_TIME]);

Expand Down Expand Up @@ -458,34 +475,36 @@ const Gameroom = () => {

//start game
const startGame = useCallback(() => {
console.log("start button used once")
const payload: Timestamped<PlayerAndRoomID> = {
// TODO: need to make sure the timestamp is UTC format
// and invariant to the time zone settings
timestamp: new Date().getTime(),
message: {
userID: user.id,
roomID: currentRoomID,
},
};
const receiptId = uuidv4();
stompClientRef.current?.send(
`/app/message/games/start/${currentRoomID}`,
{ receiptId: receiptId,
token: sessionStorage.getItem("token") },
JSON.stringify(payload)
);
requestLists.current.push({ type: "start",receiptId: receiptId });
console.log(requestLists.current)
const timeoutId = setTimeout(() => {
const index = requestLists.current.findIndex(request => request.receiptId === receiptId);
if (index !== INDEX_NOT_FOUND) {
requestLists.current.splice(index, 1);
}
checkMic();
if (isMicReady){
console.log("start button used once")
const payload: Timestamped<PlayerAndRoomID> = {
// and invariant to the time zone settings
timestamp: new Date().getTime(),
message: {
userID: user.id,
roomID: currentRoomID,
},
};
const receiptId = uuidv4();
stompClientRef.current?.send(
`/app/message/games/start/${currentRoomID}`,
{ receiptId: receiptId,
token: sessionStorage.getItem("token") },
JSON.stringify(payload)
);
requestLists.current.push({ type: "start",receiptId: receiptId });
console.log(requestLists.current)
}, RESPONSE_TIME);
const timeoutId = setTimeout(() => {
const index = requestLists.current.findIndex(request => request.receiptId === receiptId);
if (index !== INDEX_NOT_FOUND) {
requestLists.current.splice(index, 1);
}
console.log(requestLists.current)
}, RESPONSE_TIME);

return () => clearTimeout(timeoutId);
return () => clearTimeout(timeoutId);
}
},[user.id,currentRoomID]);
const throttledStartGame = useCallback(throttle(startGame, THROTTLE_TIME),[startGame, THROTTLE_TIME]);

Expand Down
4 changes: 2 additions & 2 deletions src/components/views/Lobby.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function checkMic() {
})
.catch(function (err) {
console.log("Microphone access is not granted.");
showToast("Microphone access is required; please verify your settings.", "error", TOAST_TIME_LONG);
showToast("Microphone access is required; Please verify your settings.", "error", TOAST_TIME_LONG);
});
}

Expand Down Expand Up @@ -425,7 +425,7 @@ const Lobby = () => {

const handleRoomClick = useCallback((Room) => (e) => {
e.preventDefault();
checkMic();
//checkMic();
throttledClickHandler(Room, navigate, showToast);
}, [navigate, showToast]);

Expand Down

0 comments on commit 72dcf2f

Please sign in to comment.