This repository has been archived by the owner on Mar 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdb.js
251 lines (225 loc) · 7.16 KB
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
var fs = require('fs');
var config = require('config');
var driver = require('./lib/driver/mongodb');
var promptly = require('promptly');
var helper = require('./lib/cli/userHelper');
var crypto = require('crypto');
var uuid = require('node-uuid');
var argv = require('yargs')
.usage('Usage: $0 <command> [options]')
.command('show', 'Shows the data of an existing user.')
.command('create', 'Creates a new user.')
.command('update', 'Updates an existing user.')
.command('delete', 'Removed a user from database.')
.demand(1)
.help('h')
.alias('h', 'help')
.argv;
switch (argv._[0]) {
case 'show':
show();
break;
case 'create':
create();
break;
case 'update':
update();
break;
case 'delete':
del();
break;
default:
console.log('Invalid command! Use --help for more information.');
process.exit(1);
break;
}
function show() {
helper.getUser(driver)
.then(function (user) {
if (!user) {
return console.log('User cannot be found!');
}
helper.dumpUser(user);
})
.catch(function () {
})
.then(function () {
driver.close();
});
}
function create() {
var user = {};
helper.askReqField('Username (required)')
.then(function (username) {
user.username = username;
return helper.askPassword('Password (required)');
})
.then(function (password) {
user.password = crypto.createHash(config.get('hashAlgorithm')).update(password).digest("hex");
return helper.askReqField('Player name (required)');
})
.then(function (playerName) {
user.playerName = playerName;
return helper.askOptField('Skin (optional)');
})
.then(function (skin) {
user.skinUrl = skin ? skin : 'steve.png';
try {
fs.accessSync('./images/skin/' + user.skinUrl);
} catch (err) {
console.log('Skin does not exist!');
throw err;
}
return helper.askOptField('Cape (optional)');
})
.then(function (cape) {
user.capeUrl = cape;
if (user.capeUrl) {
try {
fs.accessSync('./images/cape/' + user.skinUrl);
} catch (err) {
console.log('Cape does not exist!');
throw err;
}
}
})
.then(function () {
return driver.findUserBy('username', user.username)
})
.then(function (user) {
if (user) {
console.log('Username already exists!');
throw Error('Username already exists!');
}
})
.then(function () {
return driver.findUserBy('playerName', user.playerName);
})
.then(function (user) {
if (user) {
console.log('Player name already exists!');
throw Error('Player name already exists!');
}
})
.then(function () {
helper.dumpUser(user);
return helper.askConfirm('Are you sure you want to create this user?');
})
.then(function () {
user.id = uuid.v4();
user.playerNameIndex = user.playerName.toLowerCase();
return driver.addUser(user);
})
.then(function () {
console.log('User is added to the database!');
})
.catch(function (err) {
})
.then(function () {
driver.close();
});
}
function update() {
var userDoc;
helper.getUser(driver)
.then(function (user) {
if (!user) {
console.log('User cannot be found!');
throw Error('User cannot be found!');
}
helper.dumpUser(userDoc = user);
return helper.askOptField('Username (leave blank for skip)');
})
.then(function (username) {
if (username) {
userDoc.username = username;
}
return driver.findUserBy('username', username);
})
.then(function (user) {
if (user && user.id !== userDoc.id) {
console.log('Username already exists!');
throw Error('Username already exists!');
}
return helper.askPassword('Password (leave blank for skip)', true);
})
.then(function (password) {
if (password) {
userDoc.password = crypto.createHash(config.get('hashAlgorithm')).update(password).digest("hex");
}
return helper.askOptField('Player name (leave blank for skip)');
})
.then(function (playerName) {
if (playerName) {
userDoc.playerName = playerName;
userDoc.playerNameIndex = playerName.toLowerCase();
}
return driver.findUserBy('playerName', playerName);
})
.then(function (user) {
if (user && user.id !== userDoc.id) {
console.log('Player name already exists!');
throw Error('Player name already exists!');
}
return helper.askOptField('Skin (leave blank for skip)');
})
.then(function (skin) {
if (skin) {
try {
fs.accessSync('./images/skin/' + skin);
} catch (err) {
console.log('Skin does not exist!');
throw err;
}
userDoc.skinUrl = skin;
}
return helper.askOptField('Cape (leave blank for skip)');
})
.then(function (cape) {
if (cape) {
try {
fs.accessSync('./images/cape/' + cape);
} catch (err) {
console.log('Cape does not exist!');
throw err;
}
userDoc.capeUrl = cape;
}
helper.dumpUser(userDoc);
return helper.askConfirm('Are you sure you want to update this user as above?');
})
.then(function () {
return driver.saveUser(userDoc);
})
.then(function () {
console.log('User has been updated!');
})
.catch(function (err) {
})
.then(function () {
driver.close();
});
}
function del() {
var userDoc;
helper.getUser(driver)
.then(function (user) {
if (!user) {
console.log('User cannot be found!');
throw Error('User cannot be found!');
}
helper.dumpUser(userDoc = user);
return helper.askConfirm('Are you sure you want to delete this user?');
})
.then(function () {
return driver.removeUser(userDoc);
})
.then(function () {
console.log('User has been removed!');
})
.catch(function (err) {
})
.then(function () {
driver.close();
});
}