notherbase-fs 3.3.15 → 3.4.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/controllers/creation.js +6 -8
- package/controllers/spirit-world.js +4 -16
- package/controllers/user.js +36 -54
- package/models/index.js +1 -82
- package/models/spirit.js +85 -70
- package/package.json +1 -1
- package/public/js/base.js +14 -57
- package/test/the-front/add-gold.js +8 -0
- package/test/the-front/index.ejs +7 -6
- package/views/explorer.ejs +1 -1
- package/views/footer.ejs +5 -2
- package/models/item.js +0 -83
- package/models/user.js +0 -120
package/controllers/creation.js
CHANGED
|
@@ -52,9 +52,9 @@ export default class Creation {
|
|
|
52
52
|
explore = async (req, res, next) => {
|
|
53
53
|
try {
|
|
54
54
|
if (fs.existsSync(req.main + ".ejs")) {
|
|
55
|
-
let stats = await req.db.Spirit.
|
|
56
|
-
if (stats.memory.data
|
|
57
|
-
else stats.memory.data
|
|
55
|
+
let stats = await req.db.Spirit.recallOrCreateOne("stats", null, { route: req.path });
|
|
56
|
+
if (stats.memory.data.visits) stats.memory.data.visits++;
|
|
57
|
+
else stats.memory.data.visits = 1;
|
|
58
58
|
await stats.commit();
|
|
59
59
|
|
|
60
60
|
let context = {
|
|
@@ -67,11 +67,9 @@ export default class Creation {
|
|
|
67
67
|
requireUser: req.lock
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
context.user = user.data;
|
|
74
|
-
};
|
|
70
|
+
if (req.session.currentUser) {
|
|
71
|
+
context.user = await req.db.Spirit.recallOne("user", null, { email: req.session.currentUser });
|
|
72
|
+
}
|
|
75
73
|
|
|
76
74
|
res.render(req.toRender, context);
|
|
77
75
|
}
|
|
@@ -98,7 +98,7 @@ export default class SpiritWorld {
|
|
|
98
98
|
|
|
99
99
|
// if the scope is local, the parent is the user's id
|
|
100
100
|
if (req.body.scope === "local") {
|
|
101
|
-
let user = await req.db.
|
|
101
|
+
let user = await req.db.Spirit.recallOne("user", null, { email: req.session.currentUser });
|
|
102
102
|
if (user?.id) parent = user.id;
|
|
103
103
|
else {
|
|
104
104
|
fail(res, "User had no id on load()");
|
|
@@ -107,21 +107,9 @@ export default class SpiritWorld {
|
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
// recall all spirits with the given service name and parent
|
|
110
|
-
let
|
|
110
|
+
let spirits = await req.db.Spirit.recallAll(req.body.service, parent, data, id);
|
|
111
111
|
|
|
112
|
-
|
|
113
|
-
if (!Array.isArray(spirit.memory)) {
|
|
114
|
-
let togo = spirit.memory;
|
|
115
|
-
res.send(togo);
|
|
116
|
-
}
|
|
117
|
-
// if the spirit is an array, it's multiple spirits
|
|
118
|
-
else {
|
|
119
|
-
let togo = [];
|
|
120
|
-
for (let i = 0; i < spirit.memory.length; i++) {
|
|
121
|
-
togo.push(spirit.memory[i]);
|
|
122
|
-
}
|
|
123
|
-
res.send(togo);
|
|
124
|
-
}
|
|
112
|
+
res.send(spirits);
|
|
125
113
|
} catch (error) {
|
|
126
114
|
console.log(error);
|
|
127
115
|
fail(res, "Server error");
|
|
@@ -140,7 +128,7 @@ export default class SpiritWorld {
|
|
|
140
128
|
let script, result = null;
|
|
141
129
|
|
|
142
130
|
if (fs.existsSync(scriptPath)) {
|
|
143
|
-
let user = await req.db.
|
|
131
|
+
let user = await req.db.Spirit.recallOne("user", null, { email: req.session.currentUser });
|
|
144
132
|
|
|
145
133
|
script = await import(scriptPath);
|
|
146
134
|
result = await script.default(req, user, this.io);
|
package/controllers/user.js
CHANGED
|
@@ -17,8 +17,6 @@ export default class User {
|
|
|
17
17
|
this.router.post("/changeEmail", this.changeEmail);
|
|
18
18
|
this.router.post("/changeUsername", this.changeUsername);
|
|
19
19
|
this.router.post("/deletePermanently", this.deletePermanently);
|
|
20
|
-
this.router.post("/getInventory", this.getInventory);
|
|
21
|
-
this.router.post("/getAttributes", this.getAttributes);
|
|
22
20
|
this.router.post("/getInfo", this.getInfo);
|
|
23
21
|
this.router.post("/getView", this.getView);
|
|
24
22
|
this.router.post("/setView", this.setView);
|
|
@@ -41,7 +39,7 @@ export default class User {
|
|
|
41
39
|
* @param {Object} res An Express.js response.
|
|
42
40
|
*/
|
|
43
41
|
sendPasswordReset = async (req, res) => {
|
|
44
|
-
let spirit = await req.db.
|
|
42
|
+
let spirit = await req.db.Spirit.recallOne("user", null, { email: req.body.email });
|
|
45
43
|
|
|
46
44
|
if (spirit) {
|
|
47
45
|
let token = Math.floor(Math.random() * 9999);
|
|
@@ -65,7 +63,7 @@ export default class User {
|
|
|
65
63
|
*/
|
|
66
64
|
changePassword = async (req, res) => {
|
|
67
65
|
if (check(res, req.body.token, "No token provided!")){
|
|
68
|
-
let spirit = await req.db.
|
|
66
|
+
let spirit = await req.db.Spirit.recallOne("user", null, { email: req.body.email });
|
|
69
67
|
|
|
70
68
|
if (check(res, spirit, "User not found!") &&
|
|
71
69
|
check(res, spirit.memory.data.resetToken == req.body.token, "Reset token not valid!") &&
|
|
@@ -79,7 +77,7 @@ export default class User {
|
|
|
79
77
|
|
|
80
78
|
spirit.memory.data.password = hash;
|
|
81
79
|
spirit.addBackup({
|
|
82
|
-
...spirit.memory.data
|
|
80
|
+
...spirit.memory.data,
|
|
83
81
|
password: hash
|
|
84
82
|
});
|
|
85
83
|
|
|
@@ -100,10 +98,30 @@ export default class User {
|
|
|
100
98
|
check(res, req.body.email.length > 7, "Email too short.") &&
|
|
101
99
|
check(res, req.body.username.length > 2, "Username too short."))
|
|
102
100
|
{
|
|
103
|
-
let spirit = await req.db.
|
|
101
|
+
let spirit = await req.db.Spirit.recallOne("user", null, { email: req.body.email });
|
|
104
102
|
|
|
105
103
|
if (check(res, !spirit, "Email already in use!")) {
|
|
106
|
-
|
|
104
|
+
const salt = await bcrypt.genSalt(10);
|
|
105
|
+
const hash = await bcrypt.hash(req.body.password, salt);
|
|
106
|
+
|
|
107
|
+
spirit = await req.db.Spirit.create("user", {
|
|
108
|
+
username: req.body.username,
|
|
109
|
+
email: req.body.email,
|
|
110
|
+
password: hash,
|
|
111
|
+
resetToken: null,
|
|
112
|
+
resetExp: null,
|
|
113
|
+
coin: 0,
|
|
114
|
+
home: "/",
|
|
115
|
+
authLevels: [ "Basic" ],
|
|
116
|
+
location: "/the-front",
|
|
117
|
+
attributes: {
|
|
118
|
+
translation: 0,
|
|
119
|
+
strength: 0,
|
|
120
|
+
agility: 0,
|
|
121
|
+
defense: 0
|
|
122
|
+
},
|
|
123
|
+
inventory: []
|
|
124
|
+
});
|
|
107
125
|
|
|
108
126
|
success(res, "Registration successful!");
|
|
109
127
|
}
|
|
@@ -116,15 +134,15 @@ export default class User {
|
|
|
116
134
|
* @param {Object} res An Express.js response.
|
|
117
135
|
*/
|
|
118
136
|
login = async (req, res) => {
|
|
119
|
-
let spirit = await req.db.
|
|
137
|
+
let spirit = await req.db.Spirit.recallOne("user", null, { email: req.body.email });
|
|
120
138
|
|
|
121
139
|
if (check(res, spirit, "User not found.")) {
|
|
122
|
-
let passResult = await bcrypt.compare(req.body.password, spirit.data.password);
|
|
140
|
+
let passResult = await bcrypt.compare(req.body.password, spirit.memory.data.password);
|
|
123
141
|
|
|
124
142
|
if (check(res, passResult, "Password doesn't match the email.")) {
|
|
125
143
|
req.session.currentUser = req.body.email;
|
|
126
144
|
|
|
127
|
-
success(res, "Logged in.", spirit.data.username);
|
|
145
|
+
success(res, "Logged in.", spirit.memory.data.username);
|
|
128
146
|
}
|
|
129
147
|
}
|
|
130
148
|
}
|
|
@@ -136,10 +154,10 @@ export default class User {
|
|
|
136
154
|
*/
|
|
137
155
|
changeEmail = async (req, res) => {
|
|
138
156
|
if (loginCheck(req, res)) {
|
|
139
|
-
let spirit = await req.db.
|
|
157
|
+
let spirit = await req.db.Spirit.recallOne("user", null, { email: req.body.email });
|
|
140
158
|
|
|
141
159
|
if (check(res, !spirit, "Email already in use!")) {
|
|
142
|
-
spirit = await req.db.
|
|
160
|
+
spirit = await req.db.Spirit.recallOne("user", null, { email: req.session.currentUser });
|
|
143
161
|
|
|
144
162
|
spirit.memory.data.email = req.body.email;
|
|
145
163
|
await spirit.commit();
|
|
@@ -158,10 +176,10 @@ export default class User {
|
|
|
158
176
|
*/
|
|
159
177
|
changeUsername = async (req, res) => {
|
|
160
178
|
if (loginCheck(req, res)) {
|
|
161
|
-
let spirit = await req.db.
|
|
179
|
+
let spirit = await req.db.Spirit.recallOne("user", null, { username: req.body.username });
|
|
162
180
|
|
|
163
181
|
if (check(res, !spirit, "Username already in use!")) {
|
|
164
|
-
spirit = await req.db.
|
|
182
|
+
spirit = await req.db.Spirit.recallOne("user", null, { email: req.session.currentUser });
|
|
165
183
|
|
|
166
184
|
spirit.memory.data.username = req.body.username;
|
|
167
185
|
await spirit.commit();
|
|
@@ -178,7 +196,7 @@ export default class User {
|
|
|
178
196
|
*/
|
|
179
197
|
deletePermanently = async (req, res) => {
|
|
180
198
|
if (loginCheck(req, res)) {
|
|
181
|
-
let deleted = await req.db.
|
|
199
|
+
let deleted = await req.db.Spirit.delete("user", null, { email: req.session.currentUser });
|
|
182
200
|
|
|
183
201
|
if (check(res, deleted > 0, "No account deleted")) {
|
|
184
202
|
await req.session.destroy();
|
|
@@ -188,42 +206,6 @@ export default class User {
|
|
|
188
206
|
}
|
|
189
207
|
}
|
|
190
208
|
|
|
191
|
-
/**
|
|
192
|
-
* Gets a user's inventory.
|
|
193
|
-
* @param {Object} req An Express.js request.
|
|
194
|
-
* @param {Object} res An Express.js response.
|
|
195
|
-
*/
|
|
196
|
-
getInventory = async (req, res) => {
|
|
197
|
-
if (loginCheck(req, res)) {
|
|
198
|
-
let spirit = await req.db.User.recallOne(req.session.currentUser);
|
|
199
|
-
|
|
200
|
-
if (check(res, spirit, "Account not found!")) {
|
|
201
|
-
if (check(res, spirit.memory._lastUpdate > req.body._lastUpdate, "Inventory up to date.")) {
|
|
202
|
-
let inv = spirit.memory.data.inventory;
|
|
203
|
-
|
|
204
|
-
success(res, "Inventory found", inv, spirit.memory._lastUpdate);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* Gets a user attributes.
|
|
212
|
-
* @param {Object} req An Express.js request.
|
|
213
|
-
* @param {Object} res An Express.js response.
|
|
214
|
-
*/
|
|
215
|
-
getAttributes = async (req, res) => {
|
|
216
|
-
if (loginCheck(req, res)) {
|
|
217
|
-
let user = await req.db.User.recallOne(req.session.currentUser);
|
|
218
|
-
|
|
219
|
-
if(check(res, user, "Account not found!")) {
|
|
220
|
-
if (check(res, user.memory._lastUpdate > req.body._lastUpdate, "Attributes up to date.")) {
|
|
221
|
-
success(res, "Attributes found", user.memory.data.attributes);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
209
|
/**
|
|
228
210
|
* Gets basic account information.
|
|
229
211
|
* @param {Object} req An Express.js request.
|
|
@@ -231,7 +213,7 @@ export default class User {
|
|
|
231
213
|
*/
|
|
232
214
|
getInfo = async (req, res) => {
|
|
233
215
|
if (loginCheck(req, res)) {
|
|
234
|
-
let user = await req.db.
|
|
216
|
+
let user = await req.db.Spirit.recallOne("user", null, { email: req.session.currentUser });
|
|
235
217
|
|
|
236
218
|
if (check(res, user, "Account not found!")) {
|
|
237
219
|
if (check(res, user.memory._lastUpdate > req.body._lastUpdate, "Info up to date.")) {
|
|
@@ -249,7 +231,7 @@ export default class User {
|
|
|
249
231
|
*/
|
|
250
232
|
getView = async (req, res) => {
|
|
251
233
|
if (loginCheck(req, res)) {
|
|
252
|
-
let user = await req.db.
|
|
234
|
+
let user = await req.db.Spirit.recallOne("user", null, { email: req.session.currentUser });
|
|
253
235
|
|
|
254
236
|
if (check(res, user, "Account not found!")) {
|
|
255
237
|
success(res, "View found", user.memory.data.view);
|
|
@@ -262,7 +244,7 @@ export default class User {
|
|
|
262
244
|
*/
|
|
263
245
|
setView = async (req, res) => {
|
|
264
246
|
if (loginCheck(req, res)) {
|
|
265
|
-
let user = await req.db.
|
|
247
|
+
let user = await req.db.Spirit.recallOne("user", null, { email: req.session.currentUser });
|
|
266
248
|
|
|
267
249
|
if (check(res, user, "Account not found!")) {
|
|
268
250
|
user.memory.data.view = req.body.view;
|
package/models/index.js
CHANGED
|
@@ -2,8 +2,6 @@ import mongoose from "mongoose";
|
|
|
2
2
|
import dotenv from "dotenv";
|
|
3
3
|
dotenv.config();
|
|
4
4
|
import Spirit from "./spirit.js";
|
|
5
|
-
import Item from "./item.js";
|
|
6
|
-
import User from "./user.js";
|
|
7
5
|
import SendMail from "./send-mail.js";
|
|
8
6
|
|
|
9
7
|
mongoose.set('strictQuery', true);
|
|
@@ -30,86 +28,7 @@ catch (err) {
|
|
|
30
28
|
console.log(`Mongoose on connect: ${err}`);
|
|
31
29
|
}
|
|
32
30
|
|
|
33
|
-
// let migrate = function () {
|
|
34
|
-
// // migrate
|
|
35
|
-
|
|
36
|
-
// const user = mongoose.model('users', new mongoose.Schema({
|
|
37
|
-
// username: String,
|
|
38
|
-
// password: String,
|
|
39
|
-
// email: String,
|
|
40
|
-
// coin: Number,
|
|
41
|
-
// home: String,
|
|
42
|
-
// authLevels: [ String ],
|
|
43
|
-
// location: String,
|
|
44
|
-
// attributes: {
|
|
45
|
-
// translation: Number,
|
|
46
|
-
// strength: Number,
|
|
47
|
-
// agility: Number,
|
|
48
|
-
// defense: Number
|
|
49
|
-
// },
|
|
50
|
-
// reset: {
|
|
51
|
-
// token: Number,
|
|
52
|
-
// exp: Number
|
|
53
|
-
// }
|
|
54
|
-
// }));
|
|
55
|
-
// const page = mongoose.model('pages', new mongoose.Schema({
|
|
56
|
-
// name: String,
|
|
57
|
-
// type: String,
|
|
58
|
-
// user: {
|
|
59
|
-
// type: mongoose.Schema.Types.ObjectId,
|
|
60
|
-
// ref: "users",
|
|
61
|
-
// required: false
|
|
62
|
-
// },
|
|
63
|
-
// data: {}
|
|
64
|
-
// }));
|
|
65
|
-
|
|
66
|
-
// user.find({}, async (err, users) => {
|
|
67
|
-
// for (let i = 0; i < users.length; i++) {
|
|
68
|
-
// let userSpirit = await Spirit.create({
|
|
69
|
-
// route: "/",
|
|
70
|
-
// service: "user",
|
|
71
|
-
// scope: "global",
|
|
72
|
-
// parent: null,
|
|
73
|
-
// _lastUpdate: Date.now()
|
|
74
|
-
// }, {
|
|
75
|
-
// username: users[i].username,
|
|
76
|
-
// password: users[i].password,
|
|
77
|
-
// email: users[i].email,
|
|
78
|
-
// resetToken: null,
|
|
79
|
-
// resetExp: null,
|
|
80
|
-
// coin: 0,
|
|
81
|
-
// home: "/",
|
|
82
|
-
// authLevels: [ "Basic" ],
|
|
83
|
-
// location: "/the-front",
|
|
84
|
-
// attributes: {
|
|
85
|
-
// translation: 0,
|
|
86
|
-
// strength: 0,
|
|
87
|
-
// agility: 0,
|
|
88
|
-
// defense: 0
|
|
89
|
-
// },
|
|
90
|
-
// inventory: []
|
|
91
|
-
// });
|
|
92
|
-
|
|
93
|
-
// let foundPages = await page.find({ user: users[i]._id });
|
|
94
|
-
|
|
95
|
-
// for (let i = 0; i < foundPages.length; i++) {
|
|
96
|
-
// let spirit = await Spirit.create({
|
|
97
|
-
// route: `/${foundPages[i].name}`,
|
|
98
|
-
// service: foundPages[i].name,
|
|
99
|
-
// scope: foundPages[i].type,
|
|
100
|
-
// parent: userSpirit.memory._id,
|
|
101
|
-
// _lastUpdate: 0
|
|
102
|
-
// }, foundPages[i].data.tickets);
|
|
103
|
-
// }
|
|
104
|
-
// }
|
|
105
|
-
// });
|
|
106
|
-
// }
|
|
107
|
-
|
|
108
|
-
// migrate();
|
|
109
|
-
|
|
110
31
|
export default {
|
|
111
32
|
SendMail: SendMail,
|
|
112
|
-
Spirit: Spirit
|
|
113
|
-
User: User,
|
|
114
|
-
Item: Item
|
|
33
|
+
Spirit: Spirit
|
|
115
34
|
}
|
package/models/spirit.js
CHANGED
|
@@ -13,7 +13,11 @@ export default class Spirit {
|
|
|
13
13
|
ref: "spirits",
|
|
14
14
|
required: false
|
|
15
15
|
},
|
|
16
|
-
data: {}
|
|
16
|
+
data: {},
|
|
17
|
+
backups: [{
|
|
18
|
+
_lastUpdate: Number,
|
|
19
|
+
data: {}
|
|
20
|
+
}]
|
|
17
21
|
}));
|
|
18
22
|
|
|
19
23
|
/**
|
|
@@ -41,30 +45,30 @@ export default class Spirit {
|
|
|
41
45
|
return query;
|
|
42
46
|
}
|
|
43
47
|
|
|
44
|
-
static buildBackupQuery = (service, data = null, parent = null, id = null) => {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
48
|
+
// static buildBackupQuery = (service, data = null, parent = null, id = null) => {
|
|
49
|
+
// let query = {
|
|
50
|
+
// service: service,
|
|
51
|
+
// parent: parent
|
|
52
|
+
// };
|
|
49
53
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
+
// if (id) query._id = id;
|
|
55
|
+
// else if (data){
|
|
56
|
+
// let keys = Object.keys(data);
|
|
57
|
+
// for (let i = 0; i < keys.length; i++) {
|
|
54
58
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
59
|
+
// query[`data.backups.0.data.${keys[i]}`] = data[keys[i]];
|
|
60
|
+
// }
|
|
61
|
+
// }
|
|
58
62
|
|
|
59
|
-
|
|
60
|
-
}
|
|
63
|
+
// return query;
|
|
64
|
+
// }
|
|
61
65
|
|
|
62
66
|
/**
|
|
63
67
|
* Creates a spirit in the database.
|
|
64
68
|
* @param {String} service The name of the spirit.
|
|
65
69
|
* @param {Object} data An object with data to create the spirit with.
|
|
66
70
|
* @param {ObjectID} parent The MongoDB id of the parent of the spirit to be created.
|
|
67
|
-
* @returns A new
|
|
71
|
+
* @returns A new spirit.
|
|
68
72
|
*/
|
|
69
73
|
static create = async (service, data = {}, parent = null) => {
|
|
70
74
|
let spirit = new Spirit();
|
|
@@ -73,7 +77,8 @@ export default class Spirit {
|
|
|
73
77
|
service,
|
|
74
78
|
parent,
|
|
75
79
|
_lastUpdate: Date.now(),
|
|
76
|
-
data: data
|
|
80
|
+
data: data,
|
|
81
|
+
backups: []
|
|
77
82
|
});
|
|
78
83
|
|
|
79
84
|
return spirit;
|
|
@@ -82,56 +87,86 @@ export default class Spirit {
|
|
|
82
87
|
/**
|
|
83
88
|
* Recalls all spirits in the database with a given name.
|
|
84
89
|
* @param {String} service The name of the spirit.
|
|
85
|
-
* @
|
|
90
|
+
* @param {ObjectID} parent The MongoDB id of the parent spirit to search by.
|
|
91
|
+
* @param {Object} data An object with data to query the spirit by.
|
|
92
|
+
* @param {ObjectID} id The exact id of the MongoDB document.
|
|
93
|
+
* @returns All spirits found.
|
|
86
94
|
*/
|
|
87
95
|
static recallAll = async (service, parent = null, data = {}, id = null) => {
|
|
88
|
-
let
|
|
96
|
+
let spirits = [];
|
|
89
97
|
|
|
90
98
|
let query = Spirit.buildQuery(service, data, parent, id);
|
|
91
99
|
|
|
92
100
|
let found = await Spirit.db.find(query);
|
|
93
101
|
|
|
94
102
|
if (found) {
|
|
95
|
-
|
|
96
|
-
|
|
103
|
+
for (let i = 0; i < found.length; i++) {
|
|
104
|
+
let spirit = new Spirit(found[i]);
|
|
105
|
+
spirits.push(spirit);
|
|
106
|
+
// convert old backups to new format
|
|
107
|
+
if (spirit.memory.data?._backupsEnabled) {
|
|
108
|
+
spirit.memory.backups = spirit.memory.data.backups;
|
|
109
|
+
spirit.memory.data = spirit.memory.backups[0].data;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return spirits;
|
|
97
114
|
}
|
|
98
115
|
else return null;
|
|
99
116
|
}
|
|
100
117
|
|
|
101
118
|
/**
|
|
102
|
-
* Recalls
|
|
119
|
+
* Recalls one spirit from the database or creates a new one.
|
|
103
120
|
* @param {String} service The name of the spirit.
|
|
104
|
-
* @
|
|
121
|
+
* @param {ObjectID} parent The MongoDB id of the parent spirit to search by.
|
|
122
|
+
* @param {Object} data An object with data to query the spirit by.
|
|
123
|
+
* @param {ObjectID} id The exact id of the MongoDB document.
|
|
124
|
+
* @returns The spirit found or a new one created in the database.
|
|
105
125
|
*/
|
|
106
|
-
static
|
|
107
|
-
let spirit =
|
|
126
|
+
static recallOne = async (service, parent = null, data = {}, id = null) => {
|
|
127
|
+
let spirit = null;
|
|
108
128
|
|
|
109
|
-
let
|
|
129
|
+
let query = Spirit.buildQuery(service, data, parent, id);
|
|
110
130
|
|
|
131
|
+
let found = await Spirit.db.findOne(query);
|
|
132
|
+
|
|
111
133
|
if (found) {
|
|
112
|
-
spirit
|
|
134
|
+
spirit = new Spirit(found);
|
|
135
|
+
|
|
136
|
+
// convert old backups to new format
|
|
137
|
+
if (spirit.memory.data?._backupsEnabled) {
|
|
138
|
+
spirit.memory.backups = spirit.memory.data.backups;
|
|
139
|
+
spirit.memory.data = spirit.memory.backups[0].data;
|
|
140
|
+
}
|
|
141
|
+
|
|
113
142
|
return spirit;
|
|
114
143
|
}
|
|
115
144
|
else return null;
|
|
116
145
|
}
|
|
117
146
|
|
|
118
147
|
/**
|
|
119
|
-
* Recalls
|
|
148
|
+
* Recalls or creates a spirit in the database.
|
|
120
149
|
* @param {String} service The name of the spirit.
|
|
121
150
|
* @param {ObjectID} parent The MongoDB id of the parent spirit to search by.
|
|
122
151
|
* @param {Object} data An object with data to query the spirit by.
|
|
123
152
|
* @param {ObjectID} id The exact id of the MongoDB document.
|
|
124
|
-
* @returns The spirit found or a new one created in the database
|
|
153
|
+
* @returns The spirit found or a new one created in the database.
|
|
125
154
|
*/
|
|
126
|
-
static
|
|
127
|
-
let spirit =
|
|
155
|
+
static recallOrCreateOne = async (service, parent = null, data = {}, id = null) => {
|
|
156
|
+
let spirit = null;
|
|
128
157
|
|
|
129
158
|
let query = Spirit.buildQuery(service, data, parent, id);
|
|
130
159
|
|
|
131
160
|
let found = await Spirit.db.findOne(query);
|
|
132
|
-
|
|
133
|
-
if (found) spirit
|
|
134
|
-
else spirit = await Spirit.create(service,
|
|
161
|
+
|
|
162
|
+
if (found) spirit = new Spirit(found);
|
|
163
|
+
else spirit = await Spirit.create(service, data, parent);
|
|
164
|
+
|
|
165
|
+
// convert old backups to new format
|
|
166
|
+
if (spirit.memory.data?._backupsEnabled) {
|
|
167
|
+
spirit.memory.backups = spirit.memory.data.backups;
|
|
168
|
+
spirit.memory.data = spirit.memory.backups[0].data;
|
|
169
|
+
}
|
|
135
170
|
|
|
136
171
|
return spirit;
|
|
137
172
|
}
|
|
@@ -139,21 +174,19 @@ export default class Spirit {
|
|
|
139
174
|
/**
|
|
140
175
|
* Deletes all of the specified spirit.
|
|
141
176
|
* @param {String} service The name of the spirit.
|
|
142
|
-
* @param {Object} data An object with data to query the spirit by.
|
|
143
177
|
* @param {ObjectID} parent The MongoDB id of the parent spirit to search with.
|
|
178
|
+
* @param {Object} data An object with data to query the spirit by.
|
|
144
179
|
* @param {ObjectID} id The exact id of the MongoDB document.
|
|
145
180
|
* @returns The number of documents deleted.
|
|
146
181
|
*/
|
|
147
|
-
static delete = async (service,
|
|
182
|
+
static delete = async (service, parent = null, data = {}, id = null) => {
|
|
148
183
|
let found = await Spirit.db.deleteMany(Spirit.buildQuery(service, data, parent, id));
|
|
149
184
|
|
|
150
185
|
return found.deletedCount;
|
|
151
186
|
}
|
|
152
187
|
|
|
153
|
-
constructor() {
|
|
154
|
-
this.memory =
|
|
155
|
-
data: {}
|
|
156
|
-
};
|
|
188
|
+
constructor(memory = {}) {
|
|
189
|
+
this.memory = memory;
|
|
157
190
|
}
|
|
158
191
|
|
|
159
192
|
/**
|
|
@@ -161,25 +194,12 @@ export default class Spirit {
|
|
|
161
194
|
* @param {Object} data Data to save.
|
|
162
195
|
* @returns Updated
|
|
163
196
|
*/
|
|
164
|
-
commit = async (data = this.memory.data
|
|
197
|
+
commit = async (data = this.memory.data) => {
|
|
165
198
|
this.memory._lastUpdate = Date.now();
|
|
166
199
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
await this.memory.save();
|
|
171
|
-
}
|
|
172
|
-
else {
|
|
173
|
-
for (let i = 0; i < this.memory.length; i++) {
|
|
174
|
-
if (data) {
|
|
175
|
-
if (which < 0) this.memory[i].data = data;
|
|
176
|
-
else if (which === i) this.memory[i].data = data;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
this.memory[i].markModified("data");
|
|
180
|
-
await this.memory[i].save();
|
|
181
|
-
}
|
|
182
|
-
}
|
|
200
|
+
this.memory.data = data;
|
|
201
|
+
this.memory.markModified("data");
|
|
202
|
+
await this.memory.save();
|
|
183
203
|
|
|
184
204
|
return "Updated";
|
|
185
205
|
}
|
|
@@ -193,25 +213,20 @@ export default class Spirit {
|
|
|
193
213
|
|
|
194
214
|
/**
|
|
195
215
|
* Adds a new backup to the spirit's data.
|
|
196
|
-
* If backups have not already been enabled, the old dat will be moved to a backup.
|
|
197
216
|
* @param {Object} data Data to add to the backup.
|
|
198
217
|
*/
|
|
199
|
-
addBackup =
|
|
200
|
-
|
|
201
|
-
let oldData = this.memory.data;
|
|
202
|
-
this.memory.data = {
|
|
203
|
-
_backupsEnabled: true,
|
|
204
|
-
backups: [{ _lastUpdate: Date.now(), data: oldData }]
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
this.memory.data.backups.unshift({
|
|
218
|
+
addBackup = (data, max = 5) => {
|
|
219
|
+
this.memory.backups.unshift({
|
|
209
220
|
_lastUpdate: Date.now(),
|
|
210
|
-
data: data
|
|
221
|
+
data: this.memory.data
|
|
211
222
|
});
|
|
212
223
|
|
|
224
|
+
this.memory.data = data;
|
|
225
|
+
|
|
213
226
|
if (max > 1) {
|
|
214
|
-
while (this.memory.
|
|
227
|
+
while (this.memory.backups.length > max) this.memory.backups.pop();
|
|
215
228
|
}
|
|
229
|
+
|
|
230
|
+
this.memory.markModified("backups");
|
|
216
231
|
}
|
|
217
232
|
};
|
package/package.json
CHANGED
package/public/js/base.js
CHANGED
|
@@ -12,73 +12,23 @@ class Base {
|
|
|
12
12
|
|
|
13
13
|
return response;
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Services for the player's account.
|
|
18
|
-
*/
|
|
19
|
-
#AccountServices = class AccountServices {
|
|
20
|
-
constructor() {
|
|
21
|
-
this.username = "";
|
|
22
|
-
this.email = "";
|
|
23
|
-
this.lastUpdate = 0;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Confirms and submits an email edit.
|
|
28
|
-
*/
|
|
29
|
-
async updateEmail() {
|
|
30
|
-
let $info = $(".content#account #info");
|
|
31
|
-
let $email = $(".content#account .setting#email p");
|
|
32
|
-
let $emailInput = $(".content#account .edit#email input");
|
|
33
|
-
|
|
34
|
-
let response = await Base.commune("changeEmail", { email: $emailInput.val() });
|
|
35
|
-
|
|
36
|
-
if (response.status === "success") {
|
|
37
|
-
this.email = $emailInput.val();
|
|
38
|
-
$email.text($emailInput.val());
|
|
39
|
-
$info.text("Email Updated.");
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
$info.text("Email Not Updated!");
|
|
43
|
-
}
|
|
44
|
-
this.cancelEmail();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Confirms and submits a username edit.
|
|
49
|
-
*/
|
|
50
|
-
async updateUsername() {
|
|
51
|
-
let $info = $(".content#account #info");
|
|
52
|
-
let $username = $(".content#account .setting#username p");
|
|
53
|
-
let $usernameInput = $(".content#account .edit#username input");
|
|
54
|
-
|
|
55
|
-
let response = await Base.commune("changeUsername", { username: $usernameInput.val() });
|
|
56
|
-
|
|
57
|
-
if (response.status === "success") {
|
|
58
|
-
this.username = $usernameInput.val();
|
|
59
|
-
$username.text($usernameInput.val());
|
|
60
|
-
$info.text("Username Updated.");
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
$info.text("Username Not Updated!");
|
|
64
|
-
}
|
|
65
|
-
this.cancelUsername();
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
15
|
|
|
69
16
|
constructor() {
|
|
70
|
-
this.playerAccount =
|
|
17
|
+
this.playerAccount = {
|
|
18
|
+
username: null,
|
|
19
|
+
email: null
|
|
20
|
+
};
|
|
71
21
|
}
|
|
72
22
|
|
|
73
23
|
/**
|
|
74
24
|
* Communes to logout.
|
|
75
25
|
* @returns Communion response.
|
|
76
26
|
*/
|
|
77
|
-
logout = async () => {
|
|
27
|
+
logout = async (test = false) => {
|
|
78
28
|
let response = await Base.commune("logout");
|
|
79
29
|
|
|
80
|
-
location.reload();
|
|
81
|
-
|
|
30
|
+
if (!test) location.reload();
|
|
31
|
+
else return response;
|
|
82
32
|
}
|
|
83
33
|
|
|
84
34
|
/**
|
|
@@ -172,6 +122,9 @@ class Base {
|
|
|
172
122
|
return response;
|
|
173
123
|
}
|
|
174
124
|
|
|
125
|
+
/**
|
|
126
|
+
* Creates the toggle view button.
|
|
127
|
+
*/
|
|
175
128
|
createToggleViewButton = async () => {
|
|
176
129
|
Base.commune("getView").then((res) => {
|
|
177
130
|
// add a button to the footer for toggling between compact and full view
|
|
@@ -185,6 +138,10 @@ class Base {
|
|
|
185
138
|
});
|
|
186
139
|
}
|
|
187
140
|
|
|
141
|
+
/**
|
|
142
|
+
* Toggles the view between compact and full.
|
|
143
|
+
* @param {Boolean} save Whether or not to save the view state.
|
|
144
|
+
*/
|
|
188
145
|
toggleView = async (save = true) => {
|
|
189
146
|
if (this.$viewToggle.text() === ">") {
|
|
190
147
|
this.$viewToggle.text("<");
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export default async (req, user, io) => {
|
|
2
|
+
// let deleted = await req.db.Spirit.delete("gold");
|
|
3
|
+
let spirit = await req.db.Spirit.recallOrCreateOne("gold");
|
|
4
|
+
spirit.addBackup({
|
|
5
|
+
amount: spirit.memory?.data?.amount != null ? spirit.memory.data.amount + 1 : 0
|
|
6
|
+
});
|
|
7
|
+
await spirit.commit();
|
|
8
|
+
}
|
package/test/the-front/index.ejs
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
|
|
16
16
|
<input type="number" id="token">
|
|
17
17
|
<button onclick="test.continueTest()">Continue Test</button>
|
|
18
|
-
<button onclick="base.do('
|
|
18
|
+
<button onclick="base.do('add-gold', { route: '/the-front' })">add gold</button>
|
|
19
19
|
|
|
20
20
|
<hr>
|
|
21
21
|
|
|
@@ -57,8 +57,9 @@
|
|
|
57
57
|
this.$info.append(this.newEmail + "<br>");
|
|
58
58
|
this.$info.append(this.newPassword + "<br>");
|
|
59
59
|
|
|
60
|
-
let loaded = await base.
|
|
61
|
-
|
|
60
|
+
let loaded = await base.loadAll("gold", "global");
|
|
61
|
+
console.log(loaded);
|
|
62
|
+
this.$gold.text(`?/${loaded[0]?.memory?.data?.amount !== null ? loaded[0].memory.data.amount : 0} Gold Added`);
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
runTest = async () => {
|
|
@@ -72,7 +73,7 @@
|
|
|
72
73
|
response = await base.attemptLogin(this.oldEmail, this.oldPassword);
|
|
73
74
|
this.$info.append(response.message + "<br>");
|
|
74
75
|
|
|
75
|
-
|
|
76
|
+
|
|
76
77
|
// this.$info.append(response.message + "<br>");
|
|
77
78
|
|
|
78
79
|
// let amountLocal = await base.load("gold");
|
|
@@ -84,7 +85,7 @@
|
|
|
84
85
|
}
|
|
85
86
|
|
|
86
87
|
continueTest = async () => {
|
|
87
|
-
let response = await base.logout();
|
|
88
|
+
let response = await base.logout(true);
|
|
88
89
|
this.$info.append(response.message + "<br>");
|
|
89
90
|
|
|
90
91
|
response = await base.changePassword(
|
|
@@ -98,7 +99,7 @@
|
|
|
98
99
|
response = await base.attemptLogin(this.oldEmail, this.newPassword);
|
|
99
100
|
this.$info.append(response.message + "<br>");
|
|
100
101
|
|
|
101
|
-
response = await base.logout();
|
|
102
|
+
response = await base.logout(true);
|
|
102
103
|
this.$info.append(response.message + "<br>");
|
|
103
104
|
}
|
|
104
105
|
}
|
package/views/explorer.ejs
CHANGED
package/views/footer.ejs
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
|
|
2
2
|
<footer>
|
|
3
|
-
|
|
3
|
+
<% if (user) { %>
|
|
4
|
+
<p class="login-status">Logged In: <%= user.memory.data.username %></p><button onclick="base.logout()">Logout</button>
|
|
5
|
+
<% } else { %>
|
|
6
|
+
<p class="login-status">Not Logged In</p>
|
|
7
|
+
<% } %>
|
|
4
8
|
</footer>
|
|
5
|
-
|
|
6
9
|
</body>
|
|
7
10
|
</html>
|
package/models/item.js
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import Spirit from "./spirit.js";
|
|
2
|
-
|
|
3
|
-
export default class Item extends Spirit {
|
|
4
|
-
/**
|
|
5
|
-
* Creates an item in the database.
|
|
6
|
-
* @param {String} name The item name.
|
|
7
|
-
* @param {String} short A short descrption.
|
|
8
|
-
* @param {String} long A long description.
|
|
9
|
-
* @returns The item created.
|
|
10
|
-
*/
|
|
11
|
-
static create = async (name, short, long) => {
|
|
12
|
-
let spirit = new Item(name);
|
|
13
|
-
|
|
14
|
-
spirit.memory = await Spirit.db.create({
|
|
15
|
-
service: "items",
|
|
16
|
-
parent: null,
|
|
17
|
-
_lastUpdate: Date.now(),
|
|
18
|
-
data: {
|
|
19
|
-
name,
|
|
20
|
-
short,
|
|
21
|
-
long
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
return spirit;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Recalls all items.
|
|
30
|
-
* @returns The items found or null.
|
|
31
|
-
*/
|
|
32
|
-
static recallAll = async () => {
|
|
33
|
-
let items = new Item();
|
|
34
|
-
|
|
35
|
-
let found = await Spirit.db.find({
|
|
36
|
-
service: "items",
|
|
37
|
-
parent: null
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
if (found) {
|
|
41
|
-
items.memory = found;
|
|
42
|
-
|
|
43
|
-
return items;
|
|
44
|
-
}
|
|
45
|
-
else return null;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Recalls one item by name.
|
|
50
|
-
* @param {String} name The item name.
|
|
51
|
-
* @returns The item found or null.
|
|
52
|
-
*/
|
|
53
|
-
static recallOne = async (name) => {
|
|
54
|
-
let spirit = new Item(name);
|
|
55
|
-
|
|
56
|
-
let query = Spirit.buildQuery("items", { name });
|
|
57
|
-
|
|
58
|
-
let found = await Spirit.db.findOne(query);
|
|
59
|
-
|
|
60
|
-
if (found) {
|
|
61
|
-
spirit.memory = found;
|
|
62
|
-
|
|
63
|
-
return spirit;
|
|
64
|
-
}
|
|
65
|
-
else return null;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Deletes items by name.
|
|
70
|
-
* @param {String} name The item name.
|
|
71
|
-
* @returns Number of documents deleted.
|
|
72
|
-
*/
|
|
73
|
-
static delete = async (name) => {
|
|
74
|
-
let found = await Spirit.db.findAndDelete(Spirit.buildQuery("items", { name }));
|
|
75
|
-
|
|
76
|
-
return found.deletedCount;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
constructor(name = "") {
|
|
80
|
-
super();
|
|
81
|
-
this.name = name;
|
|
82
|
-
}
|
|
83
|
-
}
|
package/models/user.js
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
import Spirit from "./spirit.js";
|
|
2
|
-
import Item from "./item.js";
|
|
3
|
-
import bcrypt from "bcrypt";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* A user spirit.
|
|
7
|
-
* @extends Spirit
|
|
8
|
-
*/
|
|
9
|
-
export default class User extends Spirit {
|
|
10
|
-
/**
|
|
11
|
-
* Recalls one user from the database.
|
|
12
|
-
* @param {String} email Email address of the user.
|
|
13
|
-
* @param {String} username The user's name.
|
|
14
|
-
* @param {ObjectID} id The MongoDB id of the user.
|
|
15
|
-
* @returns A user found or null.
|
|
16
|
-
*/
|
|
17
|
-
static recallOne = async (email = null, username = null, id = null) => {
|
|
18
|
-
let user = new User(email, id);
|
|
19
|
-
|
|
20
|
-
let query = null;
|
|
21
|
-
|
|
22
|
-
if (id) {
|
|
23
|
-
query = Spirit.buildBackupQuery("user", null, null, id);
|
|
24
|
-
}
|
|
25
|
-
else if (username) {
|
|
26
|
-
query = Spirit.buildBackupQuery("user", { username: username });
|
|
27
|
-
}
|
|
28
|
-
else if (email) {
|
|
29
|
-
query = Spirit.buildBackupQuery("user", { email: email });
|
|
30
|
-
}
|
|
31
|
-
else return null;
|
|
32
|
-
|
|
33
|
-
let found = await Spirit.db.findOne(query);
|
|
34
|
-
|
|
35
|
-
if (found) {
|
|
36
|
-
user.memory = found;
|
|
37
|
-
user.id = found._id;
|
|
38
|
-
user.data = found.data.backups[0].data;
|
|
39
|
-
user.email = found.data.backups[0].data.email;
|
|
40
|
-
user.username = found.data.backups[0].data.username;
|
|
41
|
-
|
|
42
|
-
return user;
|
|
43
|
-
}
|
|
44
|
-
else return null;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Creates one user in the database.
|
|
49
|
-
* @param {String} email Email address of the user.
|
|
50
|
-
* @param {String} password The user's ****.
|
|
51
|
-
* @param {String} username The user's name.
|
|
52
|
-
* @returns The user created.
|
|
53
|
-
*/
|
|
54
|
-
static create = async (username, password, email) => {
|
|
55
|
-
let user = new User(email);
|
|
56
|
-
|
|
57
|
-
const salt = await bcrypt.genSalt(10);
|
|
58
|
-
const hash = await bcrypt.hash(password, salt);
|
|
59
|
-
|
|
60
|
-
user.memory = await Spirit.db.create({
|
|
61
|
-
service: "user",
|
|
62
|
-
parent: null,
|
|
63
|
-
_lastUpdate: Date.now(),
|
|
64
|
-
data: {}
|
|
65
|
-
});
|
|
66
|
-
user.addBackup({
|
|
67
|
-
username: username,
|
|
68
|
-
password: hash,
|
|
69
|
-
email: email,
|
|
70
|
-
resetToken: null,
|
|
71
|
-
resetExp: null,
|
|
72
|
-
coin: 0,
|
|
73
|
-
home: "/",
|
|
74
|
-
authLevels: [ "Basic" ],
|
|
75
|
-
location: "/the-front",
|
|
76
|
-
attributes: {
|
|
77
|
-
translation: 0,
|
|
78
|
-
strength: 0,
|
|
79
|
-
agility: 0,
|
|
80
|
-
defense: 0
|
|
81
|
-
},
|
|
82
|
-
inventory: []
|
|
83
|
-
});
|
|
84
|
-
user.id = user.memory._id;
|
|
85
|
-
user.data = user.memory.data.backups[0].data;
|
|
86
|
-
user.email = user.data.email;
|
|
87
|
-
await user.commit();
|
|
88
|
-
|
|
89
|
-
return user;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Delete a user account.
|
|
94
|
-
* @param {String} email Email address of the account to delete.
|
|
95
|
-
* @returns
|
|
96
|
-
*/
|
|
97
|
-
static delete = async (email) => {
|
|
98
|
-
let found = await Spirit.db.findAndDelete(Spirit.buildQuery({
|
|
99
|
-
service: "user",
|
|
100
|
-
parent: null
|
|
101
|
-
}, { email: email }));
|
|
102
|
-
|
|
103
|
-
return found.deletedCount;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
constructor(email, id = null) {
|
|
107
|
-
super();
|
|
108
|
-
this.email = email;
|
|
109
|
-
this.id = id;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Checks if a user is logged in.
|
|
114
|
-
*/
|
|
115
|
-
loggedIn = () => {
|
|
116
|
-
if (this.id) return true;
|
|
117
|
-
else return false;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|