notherbase-fs 3.2.21 → 3.3.0

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.
@@ -5,12 +5,11 @@ import fs from 'fs';
5
5
  * Creation is all the renedered pages in a base.
6
6
  */
7
7
  export default class Creation {
8
- constructor(siteTitle = "Base") {
9
- this.siteTitle = siteTitle;
8
+ constructor() {
10
9
  this.router = express.Router();
11
10
 
12
11
  //home
13
- this.router.get("/", this.front, this.explore);
12
+ this.router.get("/", function(req, res) { res.redirect("/the-front"); });
14
13
 
15
14
  //the-front
16
15
  this.router.get(`/the-front`, this.front, this.explore);
@@ -19,15 +18,13 @@ export default class Creation {
19
18
  //pages
20
19
  this.router.get(`/:page`, this.page, this.explore);
21
20
 
22
- //the-front optional shortcuts
23
- this.router.get(`/:frontDetail`, this.frontDetail, this.explore);
24
-
25
21
  //explorer
26
22
  this.router.get(`/:region/:area/:poi`, this.lock, this.poi, this.explore);
27
23
  this.router.get(`/:region/:area/:poi/:detail`, this.lock, this.detail, this.explore);
28
24
 
29
25
  //void
30
- this.router.use(function(req, res) {
26
+ this.router.use(function(req, res) {
27
+ console.log(req.path);
31
28
  res.redirect("/void");
32
29
  });
33
30
  }
@@ -91,7 +88,7 @@ export default class Creation {
91
88
  */
92
89
  front = async (req, res, next) => {
93
90
  req.main = req.contentPath + "/the-front/index";
94
- req.siteTitle = this.siteTitle;
91
+ req.siteTitle = "NotherBase - The Front";
95
92
  req.toRender = "explorer";
96
93
  next();
97
94
  }
@@ -104,7 +101,7 @@ export default class Creation {
104
101
  */
105
102
  frontDetail = async (req, res, next) => {
106
103
  req.main = `${req.contentPath}/the-front/${req.params.frontDetail}/index`;
107
- req.siteTitle = `${this.siteTitle} - ${req.params.frontDetail}`;
104
+ req.siteTitle = `NotherBase - ${req.params.frontDetail}`;
108
105
  req.toRender = "explorer";
109
106
  next();
110
107
  }
@@ -117,7 +114,7 @@ export default class Creation {
117
114
  */
118
115
  poi = async (req, res, next) => {
119
116
  req.main = `${req.contentPath}/${req.params.region}/${req.params.area}/${req.params.poi}/index`;
120
- req.siteTitle = `${this.siteTitle} - ${req.params.poi}`;
117
+ req.siteTitle = `NotherBase - ${req.params.poi}`;
121
118
  req.toRender = "explorer";
122
119
  next();
123
120
  }
@@ -130,7 +127,7 @@ export default class Creation {
130
127
  */
131
128
  detail = async (req, res, next) => {
132
129
  req.main = `${req.contentPath}/${req.params.region}/${req.params.area}/${req.params.poi}/${req.params.detail}/index`;
133
- req.siteTitle = `${this.siteTitle} - ${req.params.detail}`;
130
+ req.siteTitle = `NotherBase - ${req.params.detail}`;
134
131
  req.toRender = "explorer";
135
132
  next();
136
133
  }
@@ -85,26 +85,40 @@ export default class SpiritWorld {
85
85
  }
86
86
 
87
87
  /**
88
- * This API route requests a spirit from the database.
89
- * @param {Object} req
90
- * @param {Object} res
88
+ * This API route requests all spirits of a kind from the database.
89
+ * @param {Object} req
90
+ * @param {Object} res
91
+ * @returns {Object} The requested spirits.
91
92
  */
92
- load = async (req, res) => {
93
+ loadAll = async (req, res) => {
93
94
  try {
94
95
  let parent = null;
96
+ let data = req.body.data ? req.body.data : {};
97
+ let id = req.body.id ? req.body.id : null;
95
98
 
99
+ // if the scope is local, the parent is the user's id
96
100
  if (req.body.scope === "local") {
97
101
  let user = await req.db.User.recallOne(req.session.currentUser);
98
102
  if (user?.id) parent = user.id;
99
- else {
100
- fail(res, "User had no id on load()");
101
- return;
102
- }
103
+ else throw new Error("User had no id on load(): ", user);
103
104
  }
104
105
 
105
- let spirit = await req.db.Spirit.recallOne(req.body.service, parent);
106
+ // recall all spirits with the given service name and parent
107
+ let spirit = await req.db.Spirit.recallAll(req.body.service, parent, data, id);
106
108
 
107
- res.send(spirit.memory.data ? spirit.memory.data : {});
109
+ // if the spirit is not an array, it's a single spirit
110
+ if (!Array.isArray(spirit.memory)) {
111
+ let togo = spirit.memory;
112
+ res.send(togo);
113
+ }
114
+ // if the spirit is an array, it's multiple spirits
115
+ else {
116
+ let togo = [];
117
+ for (let i = 0; i < spirit.memory.length; i++) {
118
+ togo.push(spirit.memory[i]);
119
+ }
120
+ res.send(togo);
121
+ }
108
122
  } catch (error) {
109
123
  console.log(error);
110
124
  fail(res, "Server error");
@@ -20,8 +20,6 @@ export default class User {
20
20
  this.router.post("/getInventory", this.getInventory);
21
21
  this.router.post("/getAttributes", this.getAttributes);
22
22
  this.router.post("/getInfo", this.getInfo);
23
- this.router.post("/getView", this.getView);
24
- this.router.post("/setView", this.setView);
25
23
  }
26
24
 
27
25
  /**
@@ -124,6 +122,14 @@ export default class User {
124
122
  }
125
123
  }
126
124
 
125
+ /**
126
+ * Checks if a user is logged in.
127
+ */
128
+ loggedIn = () => {
129
+ if (this.id) return true;
130
+ else return false;
131
+ }
132
+
127
133
  /**
128
134
  * Changes a user's email address on file.
129
135
  * @param {Object} req An Express.js request.
@@ -238,33 +244,4 @@ export default class User {
238
244
  }
239
245
  }
240
246
  }
241
-
242
- /**
243
- * Gets a user's saved view state.
244
- */
245
- getView = async (req, res) => {
246
- if (loginCheck(req, res)) {
247
- let user = await req.db.User.recallOne(req.session.currentUser);
248
-
249
- if (check(res, user, "Account not found!")) {
250
- success(res, "View found", user.memory.data.view);
251
- }
252
- }
253
- }
254
-
255
- /**
256
- * Sets a user's view state.
257
- */
258
- setView = async (req, res) => {
259
- if (loginCheck(req, res)) {
260
- let user = await req.db.User.recallOne(req.session.currentUser);
261
-
262
- if (check(res, user, "Account not found!")) {
263
- user.memory.data.view = req.body.view;
264
- await user.commit();
265
-
266
- success(res, "View set");
267
- }
268
- }
269
- }
270
247
  }
package/models/index.js CHANGED
@@ -6,8 +6,6 @@ import Item from "./item.js";
6
6
  import User from "./user.js";
7
7
  import SendMail from "./send-mail.js";
8
8
 
9
- mongoose.set('strictQuery', true);
10
-
11
9
  mongoose.connection.on('connected', (err) => {
12
10
  console.log(`Mongoose connected to db`);
13
11
  });
package/models/spirit.js CHANGED
@@ -66,14 +66,32 @@ export default class Spirit {
66
66
  * @param {String} service The name of the spirit.
67
67
  * @returns All spirits of the given name.
68
68
  */
69
- static recallAll = async (service, extraQuery) => {
69
+ static recallAll = async (service, parent = null, data = {}, id = null) => {
70
70
  let spirit = new Spirit();
71
71
 
72
- let found = await Spirit.db.find({ service: service, ...extraQuery });
72
+ let query = Spirit.buildQuery(service, data, parent, id);
73
+
74
+ let found = await Spirit.db.find(query);
75
+
76
+ if (found) {
77
+ spirit.memory = found;
78
+ return spirit;
79
+ }
80
+ else return null;
81
+ }
82
+
83
+ /**
84
+ * Recalls any spirit from the database.
85
+ * @param {String} service The name of the spirit.
86
+ * @returns Any spirit found.
87
+ */
88
+ static recallAny = async (service) => {
89
+ let spirit = new Spirit();
90
+
91
+ let found = await Spirit.db.findOne({ service: service });
73
92
 
74
93
  if (found) {
75
94
  spirit.memory = found;
76
-
77
95
  return spirit;
78
96
  }
79
97
  else return null;
@@ -109,7 +127,7 @@ export default class Spirit {
109
127
  * @returns The number of documents deleted.
110
128
  */
111
129
  static delete = async (service, data = {}, parent = null, id = null) => {
112
- let found = await Spirit.db.deleteMany(Spirit.buildQuery(service, data, parent, id));
130
+ let found = await Spirit.db.deleteMany(Spirit.buildQuery(service, data, id));
113
131
 
114
132
  return found.deletedCount;
115
133
  }
@@ -125,26 +143,45 @@ export default class Spirit {
125
143
  * @param {Object} data Data to save.
126
144
  * @returns Updated
127
145
  */
128
- commit = async (data = this.memory.data, which = -1) => {
146
+ commit = async (data = this.memory.data) => {
129
147
  this.memory._lastUpdate = Date.now();
130
148
 
131
- if (!Array.isArray(this.memory)) {
132
- this.memory.data = data;
133
- this.memory.markModified("data");
134
- await this.memory.save();
149
+ this.memory.data = data;
150
+ this.memory.markModified("data");
151
+ await this.memory.save();
152
+
153
+ return "Updated";
154
+ }
155
+
156
+ /**
157
+ * Normalizes the data property to an array.
158
+ */
159
+ normalizeDataToArray = () => {
160
+ if (!Array.isArray(this.memory.data)) this.memory.data = [];
161
+ }
162
+
163
+ /**
164
+ * Adds a new backup to the spirit's data.
165
+ * If backups have not already been enabled, the old dat will be moved to a backup.
166
+ * @param {Object} data Data to add to the backup.
167
+ */
168
+ addBackup = async (data, max = 5) => {
169
+ if (!this.memory.data._backupsEnabled) {
170
+ let oldData = this.memory.data;
171
+ this.memory.data = {
172
+ _backupsEnabled: true,
173
+ backups: [ oldData ]
174
+ };
135
175
  }
136
176
  else {
137
- for (let i = 0; i < this.memory.length; i++) {
138
- if (data) {
139
- if (which < 0) this.memory[i].data = data;
140
- else if (which === i) this.memory[i].data = data;
141
- }
142
-
143
- this.memory[i].markModified("data");
144
- await this.memory[i].save();
177
+ this.memory.data.backups.unshift({
178
+ _lastUpdate: Date.now(),
179
+ data: data
180
+ });
181
+
182
+ if (max > 1) {
183
+ while (this.memory.data.backups.length > max) this.memory.data.backups.pop();
145
184
  }
146
185
  }
147
-
148
- return "Updated";
149
186
  }
150
187
  };
package/models/user.js CHANGED
@@ -101,5 +101,98 @@ export default class User extends Spirit {
101
101
  this.email = email;
102
102
  this.id = id;
103
103
  }
104
+
105
+ /**
106
+ * Attempts to offset the user's inventory of a certain item.
107
+ * @param {String} name The item's name.
108
+ * @param {Number} offset The amount to offset by.
109
+ * @returns True if successful.
110
+ */
111
+ offsetItem = async (name, offset) => {
112
+ let item = await Item.recallOne(name);
113
+
114
+ if (!item) return false;
115
+
116
+ let inv = this.memory.data.inventory;
117
+
118
+ for (let j = 0; j < inv.length; j++) {
119
+ if (inv[j].name === name) {
120
+ if (inv[j].amount >= -Math.floor(offset)) {
121
+ inv[j].amount += Math.floor(offset);
122
+
123
+ if (inv[j].amount === 0) {
124
+ let empty = inv[j];
125
+
126
+ inv.splice(j, 1);
127
+ }
128
+
129
+ this.memory._lastUpdate = Date.now();
130
+ await this.commit();
131
+ return true;
132
+ }
133
+ else return false;
134
+ }
135
+ }
136
+
137
+ if (offset > 0) {
138
+ inv.push({
139
+ name: name,
140
+ amount: offset
141
+ });
142
+
143
+ this.memory._lastUpdate = Date.now();
144
+
145
+ await this.commit();
146
+
147
+ return true;
148
+ }
149
+ else return false;
150
+ }
151
+
152
+ /**
153
+ * Tests if an attribute meets a requirement.
154
+ * @param {String} check The attribute to check.
155
+ * @param {Number} against The number to meet.
156
+ * @returns
157
+ */
158
+ checkAttribute = async (check, against) => {
159
+ let att = this.memory.data.attributes;
160
+
161
+ return att[check] >= against;
162
+ }
163
+
164
+ /**
165
+ * Sets a user's attribute score.
166
+ * @param {String} change The attribute to change.
167
+ * @param {Number} to The number to set it to.
168
+ * @returns Attributes set
169
+ */
170
+ setAttribute = async (change, to) => {
171
+ let att = this.memory.data.attributes;
172
+
173
+ att[change] = to;
174
+ this.memory._lastUpdate = Date.now();
175
+ await this.commit();
176
+
177
+ return "Attributes set.";
178
+ }
179
+
180
+ /**
181
+ * Increments a user's attribute by one.
182
+ * @param {String} change The attribute to increment.
183
+ * @param {Number} max The ceiling.
184
+ * @returns The resulting attribute score.
185
+ */
186
+ incrementAttribute = async (change, max) => {
187
+ let att = this.memory.data.attributes;
188
+
189
+ if (att[change] < max) {
190
+ att[change]++;
191
+ this.memory._lastUpdate = Date.now();
192
+ await this.commit();
193
+ }
194
+
195
+ return att[change];
196
+ }
104
197
  }
105
198
 
package/notherbase-fs.js CHANGED
@@ -17,16 +17,11 @@ import SpiritWorld from "./controllers/spirit-world.js";
17
17
  * The engine that runs a nother base.
18
18
  */
19
19
  class NotherBaseFS {
20
- constructor(contentPath, globals = null, settings = {}) {
21
- this.settings = {
22
- siteTitle: "NotherBase",
23
- favicon: null,
24
- ...settings
25
- }
20
+ constructor(contentPath) {
26
21
  this.app = express();
27
22
  this.server = http.createServer(this.app);
28
23
  this.io = new Server(this.server);
29
- this.creation = new Creation(this.settings.siteTitle);
24
+ this.creation = new Creation();
30
25
  this.spiritWorld = new SpiritWorld(this.io);
31
26
 
32
27
  //set views path
@@ -49,8 +44,7 @@ class NotherBaseFS {
49
44
  this.app.use(express.static(`${__dirname}/public`));
50
45
 
51
46
  // sets the favicon image
52
- if (this.settings.favicon) this.app.use(favicon(this.settings.favicon));
53
- else this.app.use(favicon(__dirname + '/public/img/logo.png'));
47
+ this.app.use(favicon(__dirname + '/public/img/logo.png'));
54
48
 
55
49
  //enable cookies
56
50
  this.app.use(session({
@@ -62,7 +56,6 @@ class NotherBaseFS {
62
56
 
63
57
  //provide database access and etc to use in routes
64
58
  this.app.use((req, res, next) => {
65
- req.globals = globals;
66
59
  req.db = Models;
67
60
  req.contentPath = contentPath;
68
61
  req.lock = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "notherbase-fs",
3
- "version": "3.2.21",
3
+ "version": "3.3.0",
4
4
  "description": "Functions to help make developing for NotherBase easier.",
5
5
  "exports": "./notherbase-fs.js",
6
6
  "scripts": {