Skip to content

Commit

Permalink
Added 'EndGame' Feature for admin use
Browse files Browse the repository at this point in the history
  • Loading branch information
jaymar921 committed Mar 15, 2024
1 parent ec72f9d commit 2720c4b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,14 @@ public IActionResult RoomProceedNextRound(int id)
return Ok(new { Message = "Proceed Success"});
}

[QuizMasterAdminAuthorization]
[HttpGet("room/forceExit/{id}")]
public IActionResult RoomForceExit(int id)
{
SessionHandler.ForceExitRoom(id);
return Ok(new { Message = "Proceed Success" });
}

[QuizMasterAuthorization]
[HttpGet("room/getAllRooms")]
public async Task<IActionResult> GetAllRoomsAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ public async Task StartQuiz(SessionHub hub, SessionHandler handler, QuizRoomServ
handler.SetPauseRoom(roomId, true); // <- Pause the room when before proceeding to next round
handler.AddActiveRoom(room.QRoomPin, room); // register the room to be started | set to active
int setIndex = 0;
bool ForcedToExit = false;
foreach (var Qset in quizSets)
{
if (ForcedToExit) break;
// Get the Sets
var setRequest = new SetRequest() { Id = Qset.QSetId };
var setReply = await grpcClient.GetQuizAsync(setRequest);
Expand All @@ -82,6 +84,10 @@ public async Task StartQuiz(SessionHub hub, SessionHandler handler, QuizRoomServ
int currentQuestion = 1;
foreach (var questionSet in Setquestions)
{
// Exit Set
ForcedToExit = handler.IsRoomForcedToExit(roomId);
if(ForcedToExit) break;

roomPin = Qset.QRoom.QRoomPin + "";
// update the participant start-time if haven't, for late joiners
UpdateParticipantsStartTime(handler, roomPin);
Expand Down Expand Up @@ -166,6 +172,7 @@ public async Task StartQuiz(SessionHub hub, SessionHandler handler, QuizRoomServ
await SendParticipantsScoresAsync(hub, handler, roomPin, room, adminData, false); // send scores
}

// Handling the Pause
if(setIndex < quizSets.Count)
{
int limit = quizSettings.ForceNextRoundTimeout; // DEFAULT (300s) of 5mins, if not clicked `proceed` then continue
Expand Down Expand Up @@ -203,6 +210,7 @@ public async Task StartQuiz(SessionHub hub, SessionHandler handler, QuizRoomServ
// Clear
handler.ResetParticipantLinkedConnectionsInAGroup(roomPin);
handler.ClearEliminatedParticipants(Convert.ToInt32(roomPin));
handler.ClearForcedExitRoom();
//await hub.Clients.Group(roomPin).SendAsync("notif", "Quiz Data: "+JsonConvert.SerializeObject(await GetQuizRoomDatasAsync()));
//await hub.Clients.Group(roomPin).SendAsync("stop",true);
}
Expand Down
17 changes: 17 additions & 0 deletions WebApp/backend/QuizMaster.API.Gatewway/Services/SessionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class SessionHandler
private Dictionary<string, string> SessionId;
private readonly ReportServiceHandler ReportHandler;
private Dictionary<int, bool> RoomNextSetPaused;
private List<int> RoomForceExitIds;

public SessionHandler(ReportServiceHandler reportServiceHandler)
{
Expand All @@ -46,6 +47,7 @@ public SessionHandler(ReportServiceHandler reportServiceHandler)
SessionId = new();
RoomNextSetPaused = new();
ReportHandler = reportServiceHandler;
RoomForceExitIds = new();
}

public string GenerateSessionId(string roomPin)
Expand Down Expand Up @@ -73,6 +75,21 @@ public bool GetPausedRoom(int roomId)
return result;
}

public void ForceExitRoom(int roomId)
{
RoomForceExitIds.Add(roomId);
}

public bool IsRoomForcedToExit(int roomId)
{
return RoomForceExitIds.Contains(roomId);
}

public void ClearForcedExitRoom()
{
RoomForceExitIds.Clear();
}

public async Task AddToGroup(SessionHub hub, string group, string connectionId)
{
connectionGroupPair[connectionId] = group;
Expand Down
20 changes: 20 additions & 0 deletions WebApp/frontend/quiz_session/app/room/quiz/components/interval.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@ export default function Interval({ leaderBoard, handleCloseLeaderboard}) {

}
}

const handleForceExit = () => {

const input = prompt("Are you sure you want to end game? Yes or No", "No").toLocaleLowerCase();

if(input === "yes" || input === "y"){
const roomInformationJson = localStorage.getItem("_rI");
const roomInformation = JSON.parse(roomInformationJson);

if(roomInformation && roomInformationJson){
const token = localStorage.getItem("token")
fetch(BASE_URL+`/gateway/api/room/forceExit/${roomInformation.id}`, {headers:{"Authorization": `Bearer ${token}`}}, {credentials: "include"}).catch(e => {alert("Error: ",e)})
handleCloseLeaderboard()
}

// Always call
handleNextRound();
}
}
useEffect(() => {
// const timer = setInterval(() => {
// setSeconds((prevSeconds) => prevSeconds - 1);
Expand Down Expand Up @@ -62,6 +81,7 @@ export default function Interval({ leaderBoard, handleCloseLeaderboard}) {
(
<div className="w-full flex items-center b-[50px] h-[30%]">
<button onClick={handleNextRound} className="text-white bg-orange-600 m-auto px-4 py-2 rounded-md">Next Round</button>
<button onClick={handleForceExit} className="text-white bg-red-600 m-auto px-4 py-2 rounded-md">End Game</button>
</div>
)}
</>
Expand Down

0 comments on commit 2720c4b

Please sign in to comment.