Skip to content

Commit

Permalink
Merge pull request #225 from UoaWDCC/VPS-46/linking-resources-to-flags
Browse files Browse the repository at this point in the history
Vps 46/linking resources to flags
  • Loading branch information
Kot6603 authored Aug 30, 2024
2 parents 31ad5d4 + aed516e commit e075b3c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
23 changes: 22 additions & 1 deletion backend/src/routes/api/navigate/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,28 @@ export const groupReset = async (req) => {
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: [] } });
await Group.findOneAndUpdate(
{ _id: group._id },
{ $set: { path: [], currentFlags: [] } },
{ new: true }
);

return { status: STATUS.OK };
};

// Fetches groups flags and returns resources
export const groupGetResources = async (req) => {
const group = await Group.findById(req.params.groupId);

if (!group) {
throw new HttpError("Group not found", STATUS.NOT_FOUND);
}

const flags = group.currentFlags || [];
let resources = [];
if (flags) {
// TODO: add logic to map certain flags to groups here
resources = [];
}
return { status: STATUS.OK, json: resources };
};
10 changes: 9 additions & 1 deletion backend/src/routes/api/navigate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Router } from "express";
import auth from "../../../middleware/firebaseAuth";

import handle from "../../../error/handle";
import { groupNavigate, groupReset } from "./group";
import { groupNavigate, groupReset, groupGetResources } from "./group";
import { userNavigate, userReset } from "./user";

const router = Router();
Expand All @@ -25,6 +25,14 @@ router.post(
})
);

router.get(
"/group/resources/:groupId",
handle(async (req, res) => {
const response = await groupGetResources(req);
return res.status(response.status).json(response.json);
})
);

router.post(
"/user/reset/:scenarioId",
handle(async (req, res) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ const navigate = async (
return res.data.active;
};

// returns resources in the scene
const getResources = async (user, groupId) => {
const token = await user.getIdToken();
const config = {
method: "get",
url: `/api/navigate/group/resources/${groupId}`,
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
};
console.log(config);
const res = await axios.request(config);
console.log(res);
return res.data.active;
};

/**
* This page allows users to play a multiplayer scenario.
*
Expand All @@ -53,6 +70,7 @@ export default function PlayScenarioPageMulti({ group }) {
const [previous, setPrevious] = useState(null);
const [addFlags, setAddFlags] = useState([]);
const [removeFlags, setRemoveFlags] = useState([]);
const [resources, setResources] = useState([]);

const handleError = (error) => {
if (!error) return;
Expand All @@ -79,6 +97,9 @@ export default function PlayScenarioPageMulti({ group }) {
addFlags,
removeFlags
);
const newResources = await getResources(user, group._id);
setResources(newResources);
console.log(resources);
if (!sceneId)
history.replace(`/play/${scenarioId}/multiplayer/${newSceneId}`);
} catch (e) {
Expand Down

0 comments on commit e075b3c

Please sign in to comment.