notherbase-fs 1.2.13 → 1.2.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -29,73 +29,76 @@ router.get("/", async function(req, res) {
29
29
  router.post("/", async function(req, res) {
30
30
  if (connectionSuccess) {
31
31
  try {
32
- if (req.session.currentUser) {
33
- let foundInventory = await inventory.findOne({user: req.session.currentUser}).populate("items.item");
34
-
35
- let holding = false;
36
-
37
- for (let j = 0; j < foundInventory.items.length; j++) {
38
- if (foundInventory.items[j].item._id.equals(req.body.change.item)) {
39
- holding = true;
40
-
41
- if (foundInventory.items[j].amount >= -Math.floor(req.body.change.amount)) {
42
- foundInventory.items[j].amount += Math.floor(req.body.change.amount);
43
-
44
- if (foundInventory.items[j].amount === 0) {
45
- let itemToEmpty = foundInventory.items[j].item._id;
46
-
47
- foundInventory.items.splice(j, 1);
48
- await foundInventory.save();
32
+ if (req.body.item && req.body.amount) {
33
+ if (req.session.currentUser) {
34
+ let foundInventory = await inventory.findOne({user: req.session.currentUser}).populate("items.item");
49
35
 
50
- res.status(200).send({
51
- item: {
52
- _id: itemToEmpty
53
- },
54
- amount: 0
55
- });
36
+ let holding = false;
37
+
38
+ for (let j = 0; j < foundInventory.items.length; j++) {
39
+ if (foundInventory.items[j].item._id.equals(req.body.item)) {
40
+ holding = true;
41
+
42
+ if (foundInventory.items[j].amount >= -Math.floor(req.body.amount)) {
43
+ foundInventory.items[j].amount += Math.floor(req.body.amount);
44
+
45
+ if (foundInventory.items[j].amount === 0) {
46
+ let itemToEmpty = foundInventory.items[j].item._id;
47
+
48
+ foundInventory.items.splice(j, 1);
49
+ await foundInventory.save();
50
+
51
+ res.status(200).send({
52
+ item: itemToEmpty,
53
+ amount: 0
54
+ });
55
+ }
56
+ else {
57
+ await foundInventory.save();
58
+
59
+ res.status(200).send(foundInventory.items[j]);
60
+ }
56
61
  }
57
62
  else {
58
- await foundInventory.save();
59
-
60
- res.status(200).send(foundInventory.items[j]);
63
+ console.log("subtract from too few", req.change);
64
+ res.status(304).send(
65
+ `Unable to remove ${req.body.amount} ${req.body.item}
66
+ from inventory because the inventory has only ${foundInventory.items[j].amount}.`
67
+ );
61
68
  }
69
+
70
+ break;
71
+ }
72
+ }
73
+
74
+ if (!holding) {
75
+ if (req.body.amount > 0) {
76
+ foundInventory.items.push({
77
+ item: req.body.item,
78
+ amount: req.body.amount
79
+ });
80
+
81
+ await foundInventory.save();
82
+
83
+ await inventory.populate(foundInventory, "items.item");
84
+
85
+ res.status(200).send(foundInventory.items[foundInventory.items.length - 1]);
62
86
  }
63
87
  else {
64
- console.log("subtract from too few", req.body.change);
88
+ console.log("subtract from none", req.body);
65
89
  res.status(304).send(
66
- `Unable to remove ${req.body.change.amount} ${req.body.change.item}
67
- from inventory because the inventory has only ${foundInventory.items[j].amount}.`
90
+ `Unable to remove ${req.body.amount} ${req.body.item}
91
+ from inventory because the inventory has none.`
68
92
  );
69
93
  }
70
-
71
- break;
72
- }
94
+ };
95
+ }
96
+ else {
97
+ res.status(401).send("User not logged in!");
73
98
  }
74
-
75
- if (!holding) {
76
- if (req.body.change.amount > 0) {
77
- foundInventory.items.push({
78
- item: req.body.change.item,
79
- amount: req.body.change.amount
80
- });
81
-
82
- await foundInventory.save();
83
-
84
- await inventory.populate(foundInventory, "items.item");
85
-
86
- res.status(200).send(foundInventory.items[foundInventory.items.length - 1]);
87
- }
88
- else {
89
- console.log("subtract from none", req.body.change);
90
- res.status(304).send(
91
- `Unable to remove ${req.body.change.amount} ${req.body.change.item}
92
- from inventory because the inventory has none.`
93
- );
94
- }
95
- };
96
99
  }
97
100
  else {
98
- res.status(401).send("User not logged in!");
101
+ res.status(400).send("Check input!");
99
102
  }
100
103
  }
101
104
  catch(err) {
@@ -8,7 +8,6 @@ module.exports = function name(path)
8
8
 
9
9
  files.forEach(file => {
10
10
  file = file.slice(0, -4);
11
- console.log(file);
12
11
 
13
12
  router.get(`/${file}`, function(req, res) {
14
13
  res.render(`${path}/${file}.ejs`);
@@ -1,4 +1,4 @@
1
- const { user, inventory, connectionSuccess } = require("../models");
1
+ const { user, inventory, connectionSuccess, item } = require("../models");
2
2
  const path = require('path');
3
3
 
4
4
  let router = require("express").Router();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "notherbase-fs",
3
- "version": "1.2.13",
3
+ "version": "1.2.14",
4
4
  "description": "Functions to help make developing for NotherBase easier.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -55,7 +55,9 @@ const world = {
55
55
  details: [
56
56
  {
57
57
  name: "",
58
- options: {}
58
+ options: {
59
+ requiredItems: ["Gold Coin"]
60
+ }
59
61
  }
60
62
  ]
61
63
  }
@@ -2,6 +2,8 @@
2
2
  Welcome to The Front.
3
3
  </p>
4
4
 
5
+ <button onclick="addGold()">Gold</button>
6
+
5
7
  <hr>
6
8
 
7
9
  <h3>Register Account</h3>
@@ -144,4 +146,8 @@
144
146
  }
145
147
  }
146
148
  });
149
+
150
+ let addGold = function addGold(params) {
151
+ playerInventory.change(itemIDs[0], 2);
152
+ }
147
153
  </script>
package/views/head.ejs CHANGED
@@ -5,7 +5,6 @@
5
5
  <title><%= siteTitle %></title>
6
6
  <meta name="viewport" content="width=device-width,initial-scale=1.0">
7
7
  <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
8
-
9
8
  <link rel="preconnect" href="https://fonts.googleapis.com">
10
9
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
11
10
  <!-- 'Roboto Condensed', sans-serif; -->
@@ -38,8 +38,13 @@
38
38
  setInterval(this.update, this.updateCooldown);
39
39
  }
40
40
 
41
- async change(change) {
42
- await $.post("/inventory", { change: change }, (data, status) => {
41
+ async change(itemID, amount) {
42
+ let change = {
43
+ item: itemID,
44
+ amount: amount
45
+ }
46
+
47
+ await $.post("/inventory", change, (data, status) => {
43
48
  if (status === "success") {
44
49
  let holding = false;
45
50
 
@@ -52,7 +57,7 @@
52
57
  }
53
58
  }
54
59
 
55
- if (!holding && data.amount > 0) {
60
+ if (!holding && data.amount > 0 && data.item) {
56
61
  this.items.push(data);
57
62
  }
58
63