notherbase-fs 3.0.10 → 3.1.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.
package/README.md CHANGED
@@ -1,2 +1,4 @@
1
1
  # notherbase-fs
2
- Functions to help make developing for NotherBase easier.
2
+ Functions to help one develope a base.
3
+
4
+ Visit notherbase.com/doc to for documentation on notherbase-fs and notherbase.
@@ -29,29 +29,42 @@ export default class Creation {
29
29
 
30
30
  explore = async (req, res, next) => {
31
31
  let main = `${req.contentPath}`;
32
+ let route = "";
32
33
  let siteTitle = `NotherBase - `;
33
34
 
34
35
  if (req.params.frontDetail) {
35
- main += `/the-front/${req.params.frontDetail}/index`;
36
+ route = `/the-front/${req.params.frontDetail}/index`;
36
37
  siteTitle += req.params.frontDetail;
37
38
  }
38
39
  else if (req.params.detail) {
39
- main += `/${req.params.region}/${req.params.area}/${req.params.poi}/${req.params.detail}/index`;
40
+ route = `/${req.params.region}/${req.params.area}/${req.params.poi}/${req.params.detail}/index`;
40
41
  siteTitle += req.params.detail;
41
42
  }
42
43
  else if (req.params.poi) {
43
- main += `/${req.params.region}/${req.params.area}/${req.params.poi}/index`;
44
+ route = `/${req.params.region}/${req.params.area}/${req.params.poi}/index`;
44
45
  siteTitle += req.params.poi;
45
46
  }
46
47
  else {
47
- main += `/the-front/index`;
48
+ route = `/the-front/index`;
48
49
  siteTitle += "the-front";
49
50
  }
51
+ main += route;
50
52
 
51
53
  try {
52
54
  if (fs.existsSync(main + ".ejs")) {
53
55
  let user = await req.db.User.recallOne(req.session.currentUser);
54
56
 
57
+ let stats = await req.db.Spirit.recallOne("stats");
58
+ if (stats.memory.data[route]) {
59
+ stats.memory.data[route].visits++;
60
+ }
61
+ else {
62
+ stats.memory.data[route] = {
63
+ visits: 1
64
+ }
65
+ }
66
+ await stats.commit();
67
+
55
68
  let userStuff = {
56
69
  userID: null,
57
70
  user: null,
@@ -67,7 +80,7 @@ export default class Creation {
67
80
  siteTitle: siteTitle,
68
81
  main: main,
69
82
  query: req.query,
70
- dir: req.frontDir,
83
+ // dir: req.frontDir,
71
84
  route: req.path,
72
85
  requireUser: req.lock
73
86
  }
@@ -91,6 +104,17 @@ export default class Creation {
91
104
  if (fs.existsSync(main)) {
92
105
  let user = await req.db.User.recallOne(req.session.currentUser);
93
106
 
107
+ let stats = await req.db.Spirit.recallOne("stats");
108
+ if (stats.memory.data[main]) {
109
+ stats.memory.data[main].visits++;
110
+ }
111
+ else {
112
+ stats.memory.data[main] = {
113
+ visits: 1
114
+ }
115
+ }
116
+ await stats.commit();
117
+
94
118
  let userStuff = {
95
119
  userID: null,
96
120
  user: null
@@ -1,8 +1,7 @@
1
1
  import express from "express";
2
2
  import { stripHtml } from "string-strip-html";
3
- import User from "./spirits/user.js";
4
- import Contact from "./spirits/contact.js";
5
3
  import { success, fail } from "./spirits/util.js";
4
+ import User from "./spirits/user.js";
6
5
  import fs from 'fs';
7
6
 
8
7
  export default class SpiritWorld {
@@ -36,12 +35,10 @@ export default class SpiritWorld {
36
35
  this.io = io;
37
36
  this.router = express.Router();
38
37
  this.user = new User();
39
- this.contact = new Contact();
40
38
 
39
+ this.router.post("/load", this.load);
41
40
  this.router.post("/serve", this.serve);
42
- this.router.get("/load", this.load);
43
41
  this.router.use("/user", this.user.router);
44
- this.router.use("/contact-nother", this.contact.router);
45
42
 
46
43
  this.io.on('connection', this.#setupChat);
47
44
  }
@@ -49,12 +46,12 @@ export default class SpiritWorld {
49
46
  load = async (req, res) => {
50
47
  let parent = null;
51
48
 
52
- if (req.query.scope === "local") {
49
+ if (req.body.scope === "local") {
53
50
  let user = await req.db.User.recallOne(req.session.currentUser);
54
51
  parent = user.id;
55
52
  }
56
53
 
57
- let spirit = await req.db.Spirit.recallOne(req.query.service, parent);
54
+ let spirit = await req.db.Spirit.recallOne(req.body.service, parent);
58
55
 
59
56
  if (!spirit.memory.data) spirit.memory.data = {};
60
57
 
@@ -63,7 +60,7 @@ export default class SpiritWorld {
63
60
 
64
61
  serve = async (req, res) => {
65
62
  try {
66
- let scriptPath = `${req.contentPath}${req.body.data.route}/${req.body.data.script}.js`;
63
+ let scriptPath = `${req.contentPath}${req.body.route}/${req.body.script}.js`;
67
64
 
68
65
  let script, result = null;
69
66
 
@@ -74,7 +71,7 @@ export default class SpiritWorld {
74
71
  result = await script.default(req, user);
75
72
  success(res, "Served.", result);
76
73
  }
77
- else fail(res, `Script not found: ${req.body.data.script} at ${scriptPath}`);
74
+ else fail(res, `Script not found: ${req.body.script} at ${scriptPath}`);
78
75
  } catch (error) {
79
76
  console.log(error);
80
77
  fail(res, "Server error");
@@ -25,18 +25,18 @@ export default class User {
25
25
  }
26
26
 
27
27
  sendPasswordReset = async (req, res) => {
28
- let spirit = await req.db.User.recallOne(req.body.data.email);
28
+ let spirit = await req.db.User.recallOne(req.body.email);
29
29
 
30
30
  if (spirit) {
31
31
  let token = Math.floor(Math.random() * 9999);
32
32
 
33
- if (req.body.data.test) console.log("token: " + token);
33
+ if (req.body.test) console.log("token: " + token);
34
34
 
35
35
  spirit.memory.data.resetToken = token;
36
36
  spirit.memory.data.resetExp = Date.now() + (1000 * 60 * 10);
37
37
  await spirit.commit();
38
38
 
39
- req.db.SendMail.passwordReset(req.body.data.email, token);
39
+ req.db.SendMail.passwordReset(req.body.email, token);
40
40
 
41
41
  success(res, "Password reset token sent.");
42
42
  }
@@ -44,18 +44,18 @@ export default class User {
44
44
  }
45
45
 
46
46
  changePassword = async (req, res) => {
47
- if (check(res, req.body.data.token, "No token provided!")){
48
- let spirit = await req.db.User.recallOne(req.body.data.email);
47
+ if (check(res, req.body.token, "No token provided!")){
48
+ let spirit = await req.db.User.recallOne(req.body.email);
49
49
 
50
50
  if (check(res, spirit, "User not found!") &&
51
- check(res, spirit.memory.data.resetToken == req.body.data.token, "Reset token not valid!") &&
51
+ check(res, spirit.memory.data.resetToken == req.body.token, "Reset token not valid!") &&
52
52
  check(res, spirit.memory.data.resetExp > Date.now(), "Reset token expired!") &&
53
- check(res, req.body.data.password === req.body.data.confirmation, "Passwords must match!"))
53
+ check(res, req.body.password === req.body.confirmation, "Passwords must match!"))
54
54
  {
55
55
  spirit.memory.data.resetExp = -1;
56
56
 
57
57
  const salt = await bcrypt.genSalt(10);
58
- const hash = await bcrypt.hash(req.body.data.password, salt);
58
+ const hash = await bcrypt.hash(req.body.password, salt);
59
59
 
60
60
  spirit.memory.data.password = hash;
61
61
  await spirit.commit();
@@ -66,14 +66,14 @@ export default class User {
66
66
  }
67
67
 
68
68
  register = async (req, res) => {
69
- if (check(res, req.body.data.password.length > 7, "Password too short.") &&
70
- check(res, req.body.data.email.length > 7, "Email too short.") &&
71
- check(res, req.body.data.username.length > 2, "Username too short."))
69
+ if (check(res, req.body.password.length > 7, "Password too short.") &&
70
+ check(res, req.body.email.length > 7, "Email too short.") &&
71
+ check(res, req.body.username.length > 2, "Username too short."))
72
72
  {
73
- let spirit = await req.db.User.recallOne(req.body.data.email);
73
+ let spirit = await req.db.User.recallOne(req.body.email);
74
74
 
75
75
  if (check(res, !spirit, "Email already in use!")) {
76
- spirit = await req.db.User.create(req.body.data.username, req.body.data.password, req.body.data.email);
76
+ spirit = await req.db.User.create(req.body.username, req.body.password, req.body.email);
77
77
 
78
78
  success(res, "Registration successful!");
79
79
  }
@@ -81,12 +81,13 @@ export default class User {
81
81
  }
82
82
 
83
83
  login = async (req, res) => {
84
- let spirit = await req.db.User.recallOne(req.body.data.email);
84
+ let spirit = await req.db.User.recallOne(req.body.email);
85
+
85
86
  if (check(res, spirit, "User not found.")) {
86
- let passResult = await bcrypt.compare(req.body.data.password, spirit.memory.data.password);
87
+ let passResult = await bcrypt.compare(req.body.password, spirit.memory.data.password);
87
88
 
88
89
  if (check(res, passResult, "Password doesn't match the email.")) {
89
- req.session.currentUser = req.body.data.email;
90
+ req.session.currentUser = req.body.email;
90
91
 
91
92
  success(res, "Logged in.", spirit.memory.data.username);
92
93
  }
@@ -95,15 +96,15 @@ export default class User {
95
96
 
96
97
  changeEmail = async (req, res) => {
97
98
  if (loginCheck(req, res)) {
98
- let spirit = await req.db.User.recallOne(req.body.data.email);
99
+ let spirit = await req.db.User.recallOne(req.body.email);
99
100
 
100
101
  if (check(res, !spirit, "Email already in use!")) {
101
102
  spirit = await req.db.User.recallOne(req.session.currentUser);
102
103
 
103
- spirit.memory.data.email = req.body.data.email;
104
+ spirit.memory.data.email = req.body.email;
104
105
  await spirit.commit();
105
106
 
106
- req.session.currentUser = req.body.data.email;
107
+ req.session.currentUser = req.body.email;
107
108
 
108
109
  success(res, "Email changed.");
109
110
  }
@@ -112,12 +113,12 @@ export default class User {
112
113
 
113
114
  changeUsername = async (req, res) => {
114
115
  if (loginCheck(req, res)) {
115
- let spirit = await req.db.User.recallOne(null, req.body.data.username);
116
+ let spirit = await req.db.User.recallOne(null, req.body.username);
116
117
 
117
118
  if (check(res, !spirit, "Username already in use!")) {
118
119
  spirit = await req.db.User.recallOne(req.session.currentUser);
119
120
 
120
- spirit.memory.data.username = req.body.data.username;
121
+ spirit.memory.data.username = req.body.username;
121
122
  await spirit.commit();
122
123
 
123
124
  success(res, "Username changed");
package/models/spirit.js CHANGED
@@ -77,8 +77,8 @@ export default class Spirit {
77
77
  }
78
78
  }
79
79
 
80
- static delete = async (options = {}, data = {}, id = null) => {
81
- let found = await Spirit.db.deleteMany(Spirit.buildQuery(options, data, id));
80
+ static delete = async (service, data = {}, id = null) => {
81
+ let found = await Spirit.db.deleteMany(Spirit.buildQuery(service, data, id));
82
82
 
83
83
  return found.deletedCount;
84
84
  }
package/models/user.js CHANGED
@@ -66,13 +66,11 @@ export default class User extends Spirit {
66
66
  return spirit;
67
67
  }
68
68
 
69
- static delete = async (target) => {
69
+ static delete = async (email) => {
70
70
  let found = await Spirit.db.findAndDelete(Spirit.buildQuery({
71
- route: "/",
72
71
  service: "user",
73
- scope: "global",
74
72
  parent: null
75
- }, { email: target }));
73
+ }, { email: email }));
76
74
 
77
75
  return found.deletedCount;
78
76
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "notherbase-fs",
3
- "version": "3.0.10",
3
+ "version": "3.1.0",
4
4
  "description": "Functions to help make developing for NotherBase easier.",
5
5
  "exports": "./notherbase-fs.js",
6
6
  "scripts": {
@@ -47,6 +47,7 @@
47
47
  width: 64px;
48
48
  height: 64px;
49
49
  border-radius: 5px;
50
+ z-index: 100;
50
51
  }
51
52
 
52
53
  .menu button#close {
@@ -0,0 +1,16 @@
1
+ export default async (req, user) => {
2
+ await user.offsetItem("Gold Coin", 3);
3
+
4
+ let spirit = await req.db.Spirit.recallOne("test");
5
+ if (!spirit.memory.data.amount) spirit.memory.data.amount = 0;
6
+ spirit.memory.data.amount += 3;
7
+ await spirit.commit();
8
+
9
+ let stats = await req.db.Spirit.recallOne("stats");
10
+ let render = [];
11
+ let keys = Object.keys(stats.memory.data);
12
+ for (let i = 0; i < keys.length; i++) {
13
+ render.push(`${keys[i]} - ${stats.memory.data[keys[i]].visits}`)
14
+ }
15
+ console.log(render);
16
+ }
@@ -40,4 +40,8 @@
40
40
  setInterval(moveNono, 2000);
41
41
 
42
42
  <%- include("./nono.js"); %>
43
+
44
+ base.load("test", "global").then((res) => {
45
+ console.log(res);
46
+ });
43
47
  </script>
@@ -185,7 +185,7 @@ class NonoGame {
185
185
 
186
186
  tryFinishGame() {
187
187
  if (this.checkForSolve()) {
188
- playerInventory.change(this.goldItem, this.level + this.difficulty);
188
+ base.do("add-gold");
189
189
 
190
190
  let $tiles = $(".nono-tile");
191
191
  $tiles.off("click");
@@ -7,27 +7,15 @@ export default async (req, user) => {
7
7
 
8
8
  await user.offsetItem("Gold Coin", 15);
9
9
 
10
- let local = await req.db.Spirit.recallOrCreate({
11
- route: req.body.route,
12
- scope: "local",
13
- parent: user.id,
14
- service: "gold"
15
- }, {}, {
16
- gold: 0
17
- });
10
+ // await req.db.Spirit.delete("gold");
11
+
12
+ let local = await req.db.Spirit.recallOne("gold", user.id);
18
13
 
19
14
  if (!local.memory.data.gold) local.memory.data.gold = 0;
20
15
  local.memory.data.gold += 15;
21
16
  await local.commit();
22
17
 
23
- let global = await req.db.Spirit.recallOrCreate({
24
- route: req.body.route,
25
- scope: "global",
26
- parent: null,
27
- service: "gold"
28
- }, {}, {
29
- gold: 0
30
- });
18
+ let global = await req.db.Spirit.recallOne("gold");
31
19
 
32
20
  if (!global.memory.data.gold) global.memory.data.gold = 0;
33
21
  global.memory.data.gold += 15;
@@ -1,5 +1,2 @@
1
1
  <div class="content invisible" id="more">
2
- <h4>Message Nother</h4>
3
- <textarea id="content"></textarea>
4
- <button id="send" onclick="sendMessageToNother()">Send Message</button>
5
2
  </div>
@@ -1,13 +1,8 @@
1
1
  class Base {
2
- static commune = async (route, data = null, options = null) => {
3
- let body = { data, ...options };
4
-
5
- let response = null;
2
+ static commune = async (route, data = {}) => {
3
+ let response = await $.post("/s/user/" + route, JSON.stringify(data));
6
4
 
7
- await $.post(route, JSON.stringify(body), (res) => {
8
- response = res;
9
- if (res.status != "success") console.log(`${route} - ${res.message}`);
10
- });
5
+ if (response.status != "success") console.log(`${"/s/user/" + route} - ${response.message}`);
11
6
 
12
7
  return response;
13
8
  }
@@ -25,7 +20,7 @@ class Base {
25
20
  async refresh() {
26
21
  let $list = $(".inventory .item-list");
27
22
 
28
- let response = await Base.commune("/s/user/getInventory", {}, { _lastUpdate: this.lastUpdate });
23
+ let response = await Base.commune("getInventory", { _lastUpdate: this.lastUpdate });
29
24
 
30
25
  if (response.status === "success") {
31
26
  this.items = response.data;
@@ -74,7 +69,7 @@ class Base {
74
69
  }
75
70
 
76
71
  async refresh() {
77
- let response = await Base.commune("/s/user/getAttributes", {}, { _lastUpdate: this.lastUpdate });
72
+ let response = await Base.commune("getAttributes", { _lastUpdate: this.lastUpdate });
78
73
  if (response.status === "success") {
79
74
  this.lastUpdate = response.lastUpdate;
80
75
  this.attributes = response.data;
@@ -148,7 +143,7 @@ class Base {
148
143
  let $email = $(".content#account .setting#email p");
149
144
  let $emailInput = $(".content#account .edit#email input");
150
145
 
151
- let response = await Base.commune("/s/user/changeEmail", { email: $emailInput.val() });
146
+ let response = await Base.commune("changeEmail", { email: $emailInput.val() });
152
147
 
153
148
  if (response.status === "success") {
154
149
  $email.text($emailInput.val());
@@ -184,7 +179,7 @@ class Base {
184
179
  let $username = $(".content#account .setting#username p");
185
180
  let $usernameInput = $(".content#account .edit#username input");
186
181
 
187
- let response = await Base.commune("/s/user/changeUsername", { username: $usernameInput.val() });
182
+ let response = await Base.commune("changeUsername", { username: $usernameInput.val() });
188
183
 
189
184
  if (response.status === "success") {
190
185
  $username.text($usernameInput.val());
@@ -237,22 +232,13 @@ class Base {
237
232
  }
238
233
 
239
234
  logout = async () => {
240
- let response = await Base.commune("/s/user/logout");
235
+ let response = await Base.commune("logout");
241
236
 
242
237
  return response;
243
238
  }
244
-
245
- sendMessageToNother = async () => {
246
- await Base.commune("contactNother", {
247
- content: $(".menu .content#more #content").val(),
248
- route: currentRoute
249
- });
250
-
251
- $(".menu .content#more #content").val("");
252
- }
253
239
 
254
240
  attemptRegister = async (email, username, password) => {
255
- let response = await Base.commune("/s/user/register", {
241
+ let response = await Base.commune("register", {
256
242
  email, username, password
257
243
  });
258
244
 
@@ -260,7 +246,7 @@ class Base {
260
246
  }
261
247
 
262
248
  attemptLogin = async (email, password) => {
263
- let response = await Base.commune("/s/user/login", {
249
+ let response = await Base.commune("login", {
264
250
  email: email,
265
251
  password: password
266
252
  });
@@ -277,24 +263,25 @@ class Base {
277
263
  };
278
264
 
279
265
  resetPassword = async (email, test = false) => {
280
- let response = await Base.commune("/s/user/sendPasswordReset", { email, test });
266
+ let response = await Base.commune("sendPasswordReset", { email, test });
281
267
 
282
268
  return response;
283
269
  }
284
270
 
285
271
  changePassword = async (token, email, password, confirmation) => {
286
- let response = await Base.commune("/s/user/changePassword", { token, email, password, confirmation });
272
+ let response = await Base.commune("changePassword", { token, email, password, confirmation });
287
273
 
288
274
  return response;
289
275
  }
290
276
 
291
277
  do = async (what, data = null) => {
292
- console.log(window.location.pathname);
293
- let response = await Base.commune("/s/serve", {
278
+ let response = await $.post("/s/serve", JSON.stringify({
294
279
  script: what,
295
280
  route: window.location.pathname,
296
281
  ...data
297
- });
282
+ }));
283
+
284
+ if (response.status != "success") console.log(`${window.location.pathname} - ${response.message}`);
298
285
 
299
286
  this.playerInventory.refresh();
300
287
  this.playerAttributes.refresh();
@@ -303,7 +290,7 @@ class Base {
303
290
  }
304
291
 
305
292
  load = async (service, scope = "local") => {
306
- let response = await $.get("/s/load", { service, scope });
293
+ let response = await $.post("/s/load", JSON.stringify({ service, scope }));
307
294
 
308
295
  return response;
309
296
  }
@@ -1,23 +0,0 @@
1
- import express from "express";
2
- import { success } from "./util.js";
3
-
4
- export default class Contact {
5
- constructor() {
6
- this.router = express.Router();
7
-
8
- this.router.post(`/`, this.contactNother);
9
- }
10
-
11
- contactNother = async function(req, res) {
12
- let user = await User.recallOne(req.session.currentUser);
13
-
14
- let spirit = await req.db.Spirit.create({
15
- route: "/",
16
- service: "nother-contacts",
17
- scope: "local",
18
- parent: user.id
19
- }, req.body);
20
-
21
- success(res, "Message sent.");
22
- }
23
- }