use a better db json layout

Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
Arthur Lu 2023-04-11 17:56:48 +00:00
parent 059c1fe0d2
commit 123431332f

8
db.js
View File

@ -25,10 +25,10 @@ function init () {
function requestResources (user, resources) { function requestResources (user, resources) {
let approved = true; let approved = true;
Object.keys(resources).forEach((element) => { Object.keys(resources).forEach((element) => {
if(!(element in db[user])) { // if the resource does not exist in the user's entry, assume the user is not allowed to use it if(!(element in db[user].avaliable)) { // if the resource does not exist in the user's entry, assume the user is not allowed to use it
approved = false; approved = false;
} }
else if (db[user][element] - resources[element] < 0) { else if (db[user].avaliable[element] - resources[element] < 0) {
approved = false; approved = false;
} }
}); });
@ -46,7 +46,7 @@ function allocateResources (user, resources) {
Object.assign(newdb, db); Object.assign(newdb, db);
Object.keys(resources).forEach((element) => { Object.keys(resources).forEach((element) => {
if(typeof(resources[element]) === "number" && isFinite(resources[element])) { if(typeof(resources[element]) === "number" && isFinite(resources[element])) {
newdb[user][element] -= resources[element]; newdb[user].avaliable[element] -= resources[element];
} }
else { else {
return false; return false;
@ -74,7 +74,7 @@ function releaseResources (user, resources) {
Object.assign(newdb, db); Object.assign(newdb, db);
Object.keys(resources).forEach((element) => { Object.keys(resources).forEach((element) => {
if(typeof(resources[element]) === "number" && isFinite(resources[element]) && resources[element]) { if(typeof(resources[element]) === "number" && isFinite(resources[element]) && resources[element]) {
newdb[user][element] += resources[element]; newdb[user].avaliable[element] += resources[element];
} }
else { else {
return false; return false;