use a better db json layout
Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
parent
0c6fc5ae1f
commit
1acaa6c864
8
db.js
8
db.js
@ -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;
|
||||||
|
Loading…
Reference in New Issue
Block a user