-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
237 lines (166 loc) · 4.61 KB
/
utils.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
/**
* Miscellaneous utility functions
*
* @author Anthony Wilson
*/
"use strict";
//Filesystem
const fs = require("fs");
//Crypto
const webcrypto = require("crypto").webcrypto;
/**
* Generate a random number between 0 (inclusive) and 1 (exclusive)
* This function serves as an unpredictable version of Math.random()
* @returns {float}
*/
exports.rand = () => {
//Use the crypto API to generate a random 32 bit unsigned integer
//Divide the integer by 2^32 to get a random number between 0 and 1
return webcrypto.getRandomValues(new Uint32Array(1))[0] / 0x100000000; //0x100000000 = 2^32 = 4294967296
}
/**
* @async
* Print a message to the console and send a message in the #nas-logs thread in the Nomic guild.
* @param {string} message
*/
exports.logMessage = async (message) => {
console.log("["+(new Date()).getTime()+"]",message);
if(!devmode){
//Unarchive the NAS-Logs thread if necessary
client.channels.cache.get(SecureInfo.channels[6].ID).setArchived(false);
//Send a message to the NAS-Logs thread
await client.channels.cache.get(SecureInfo.channels[6].ID).send(message);
}
}
/**
* Return the matching player ID given one of: the player ID; the Discord user ID; the player name.
* @param {string} id A Discord user ID
* @returns {int} Player ID (PID)
*/
exports.identifyPlayer = (id) => {
if(id == undefined){
return undefined;
}
var type = 2;
if(isNaN(id)){
//Player name
type = 2;
}else if(id.length <= 3){
//Player ID
type = 0;
}else{
//User ID
type = 1;
}
if(type == 1){
for(var p = 0;p < SecureInfo.players.length;p ++){
if(SecureInfo.players[p].ID == id){
return SecureInfo.players[p].PID;
}
}
}else{
for(var p = 0;p < Players.length;p ++){
switch(type){
case 0:
if(p == id){
return p;
}
break;
case 2:
if(id.toLowerCase().includes(Players[p].name.toLowerCase())){
return p;
}
break;
}
}
}
return undefined;
}
/**
* @async
* Return the matching Discord User given their ID.
* @param {string} id A Discord user ID
* @returns {int} Player ID (PID)
*/
exports.getUser = async (id) => {
var user;
try{
user = await client.users.fetch(id);
}catch(error){
console.error("Failed to find user.", error);
}
//Report an error if the user was not found
if(typeof(user) != "object"){
return undefined;
}
return user;
}
/**
* Decode arbitrary data from a vault string.
* @param {string} encString String of encoded data.
* @returns {Object} Decoded data.
*/
exports.decodeVaultData = (encString) => {
return JSON.parse( Buffer.from(encString, "base64").toString("utf8") );
}
/**
* Encode arbitrary data to be stored in a vault string.
* @param {Object} data Data to be encoded (must be JSON-compatible).
* @returns {string} Encoded data.
*/
exports.encodeVaultData = (data) => {
return Buffer.from(JSON.stringify(data), "utf8").toString("base64");
}
/**
* Return the matching attributes of an array of objects
* @param {array} list An array of objects
* @param {string} attr A string specifying the name of the attribute
* @returns {array} An array of attributes of the input object
*/
exports.getAttrList = (list, attr) => {
var output = [];
for(var a = 0;a < list.length;a ++){
output.push(list[a][attr]);
}
return output;
}
/**
* Update a JSON file in the automation system (only specifically allows files work).
* @param {string} file A Discord user ID.
* @param {Object} data Optional data to be stored in the specified file.
* @returns {boolean} False if @param file does not match a known file to write to.
*/
exports.updateFile = (file, data) => {
var path;
var content;
switch(file){
case "propositions":
path = sitePath+"/Propositions/propositions.json";
content = JSON.stringify(Propositions, null, 2);
break;
//case "rules":
// path = sitePath+"/Rules/rules.json";
// content = JSON.stringify(Rules, null, 2);
// break;
case "players":
path = sitePath+"/Players/players.json";
content = JSON.stringify(Players, null, 2);
break;
case "localstorage":
path = "./localstorage.json";
content = JSON.stringify(data, null, 2);
break;
default:
return false;
}
fs.writeFile(
path,
content,
(error) => {
if(error){
console.error("Failed to write to "+path, error);
}
}
);
return true;
}