Skip to content

Commit

Permalink
Merge pull request #235 from UoaWDCC/VPS-60/reset-button-fix
Browse files Browse the repository at this point in the history
VPS-60/reset button fix
  • Loading branch information
codecreator127 authored Sep 17, 2024
2 parents ef45396 + 66ccd5a commit b01b0f0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
42 changes: 37 additions & 5 deletions backend/src/routes/api/navigate/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,31 @@ const deleteAllNotes = async (groupData) => {
return true;
};

const deleteAllFlags = async (groupData) => {
const groupId = groupData._id;
const group = await Group.findById(groupId).lean();
if (!group) {
throw new HttpError("Group not found", STATUS.NOT_FOUND);
}

await Group.updateOne(
{ _id: groupId },
{ $set: { currentFlags: [] } }
).exec();
return true;
};

const deleteAllPaths = async (groupData) => {
const groupId = groupData._id;
const group = await Group.findById(groupId).lean();
if (!group) {
throw new HttpError("Group not found", STATUS.NOT_FOUND);
}

await Group.updateOne({ _id: groupId }, { $set: { path: [] } }).exec();
return true;
};

export const getScenarioFirstScene = async (scenarioId) => {
const scenario = await Scenario.findById(scenarioId, {
scenes: { $slice: 1 },
Expand Down Expand Up @@ -189,6 +214,9 @@ export const groupNavigate = async (req) => {
export const groupReset = async (req) => {
const { uid, currentScene } = req.body;
const group = await getGroupByIdAndUser(req.params.groupId, uid);
if (!group) {
throw new HttpError("Group not found", STATUS.NOT_FOUND);
}
const { role } = group.users[0];

if (!(await deleteAllNotes(group)))
Expand All @@ -198,14 +226,18 @@ export const groupReset = async (req) => {
throw new HttpError("Scene mismatch has occured", STATUS.CONFLICT);

const scene = await getSceneConsideringRole(currentScene, role);
if (!scene || !scene.components) {
throw new HttpError("Scene not found or invalid", STATUS.NOT_FOUND);
}

const hasReset = scene.components.some((c) => c.type === "RESET_BUTTON");
if (!hasReset) throw new HttpError("Invalid reset", STATUS.FORBIDDEN);

await Group.findOneAndUpdate(
{ _id: group._id },
{ $set: { path: [], currentFlags: [] } },
{ new: true }
);
if (!(await deleteAllFlags(group)))
throw new HttpError("Failed to delete flags", STATUS.INTERNAL_SERVER_ERROR);

if (!(await deleteAllPaths(group)))
throw new HttpError("Failed to delete paths", STATUS.INTERNAL_SERVER_ERROR);

return { status: STATUS.OK };
};
Expand Down
8 changes: 6 additions & 2 deletions frontend/src/features/playScenario/PlayScenarioCanvas.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ export default function PlayScenarioCanvas({
setIsModalOpen(true);
};

const handleConfirmReset = () => {
const handleConfirmReset = async () => {
setIsModalOpen(false);
reset();
try {
await reset();
} catch (error) {
console.error("Error during reset confirmation:", error);
}
};

const handleCancelReset = () => {
Expand Down
23 changes: 11 additions & 12 deletions frontend/src/features/playScenario/PlayScenarioPageMulti.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,18 @@ export default function PlayScenarioPageMulti({ group }) {
}, [sceneId]);

const reset = async () => {
const res = await usePost(
`api/navigate/group/reset/${group._id}`,
{ currentScene: sceneId },
user.getIdToken.bind(user)
);
if (res.status) {
handleError(res);
return;
try {
const res = await usePost(
`api/navigate/group/reset/${group._id}`,
{ currentScene: sceneId },
user.getIdToken.bind(user)
);

setPrevious(null);
history.replace(`/play/${scenarioId}/multiplayer`);
} catch (error) {
console.error("Error during reset:", error);
}

console.log("reset");
setPrevious(null);
history.replace(`/play/${scenarioId}/multiplayer`);
};

if (loading) return <LoadingPage text="Loading Scene..." />;
Expand Down

0 comments on commit b01b0f0

Please sign in to comment.