forked from tinyspeck/glitch-GameServerJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
486 lines (399 loc) · 11 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// Tiny Speck would like to offer a shoutout to Kevin van Zonneveld
// (http://kevin.vanzonneveld.net). We're using his number_format() function.
// Kevin, there weren't any licensing terms on this, so please let us know
// ([email protected]) if you'd like us to remove it.
//
//#include common.js
//#include utils/craftytasking.js
//#include utils/json.js
//stuff will go here!
function http_get(url, args){
// jdev/jstaging have callbacks disabled by unsetting these config vars
if (!config.web_api_url) return;
var args2 = [];
for (var i in args){
args2.push(encodeURIComponent(i)+'='+encodeURIComponent(args[i]));
}
var qs = args2.join('&');
var full = config.web_api_url + url + '?' + qs;
if (config.is_dev) log.info('HTTP: '+full);
//log.info('HTTP: '+full);
apiAsyncHttpCall(full, {});
}
function http_get_world(url, args){
// jdev/jstaging have callbacks disabled by unsetting these config vars
if (!config.world_api_url) return;
var args2 = [];
for (var i in args){
args2.push(encodeURIComponent(i)+'='+encodeURIComponent(args[i]));
}
var qs = args2.join('&');
apiAsyncHttpCall(config.world_api_url + url + '?' + qs, {});
}
// obj_tsid is optional, but should be the tsid of the calling object. Used for callback ordering.
function http_post(url, args, obj_tsid){
// jdev/jstaging have callbacks disabled by unsetting these config vars
if (!config.web_api_url) return;
var full = config.web_api_url + url;
if (config.is_dev) log.info('HTTP-POST: '+full);
apiAsyncHttpCall(full, {}, args, obj_tsid);
}
// Escape all xml characters
function escape(txt){
txt = "" + txt;
txt = txt.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""");
return txt;
}
function filter_chat(txt){
//
// we currently allow the following pieces of html to appear in chat:
//
// <i></i>
// <b></b>
//
// this is *really* annoying, since we need to balance them and filter out everything else.
//
// first step is to escape all xml characters
txt = "" + txt;
txt = txt.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""");
// now we can un-escape sequences we like
txt = txt.replace(/<b>/,'<b>').replace(/<\/b>/,'</b>');
txt = txt.replace(/<i>/,'<i>').replace(/<\/i>/,'</i>');
// break up the string by tag
var pieces = [];
while (txt.length){
// do we have a tag coming up?
var i = txt.indexOf('<');
if (i >= 0){
// find the end
var j = txt.indexOf('>');
pieces.push(txt.substr(0, i));
pieces.push(txt.substr(i, 1+j-i));
txt = txt.substr(j+1);
}else{
pieces.push(txt);
txt = '';
}
}
// balance tags
var states = {};
var out = '';
for (var i=0; i<pieces.length; i++){
if (pieces[i].substr(0,1) == '<'){
if (pieces[i] == '<b>' && !states.b){
out += pieces[i];
states.b = 1;
}
if (pieces[i] == '</b>' && states.b){
out += pieces[i];
states.b = 0;
}
if (pieces[i] == '<i>' && !states.i){
out += pieces[i];
states.i = 1;
}
if (pieces[i] == '</i>' && states.i){
out += pieces[i];
states.i = 0;
}
}else{
out += pieces[i];
}
}
if (states.b) out += '</b>';
if (states.i) out += '</i>';
return out;
}
function test_filter(){
var map = [
['hello', 'hello'],
['hi <b>there</b>', 'hi <b>there</b>'],
['hi <<b>>there</b>', 'hi <<b>>there</b>'],
['<foo>', '<foo>'],
['hi <b>there', 'hi <b>there</b>'],
['<i>' ,'<i></i>'],
['</i>' ,''],
['</i><i>' ,'<i></i>'],
];
for (var i=0; i<map.length; i++){
var txt_in = map[i][0];
var txt_out = map[i][1];
var txt_got = this.filter_chat(txt_in);
if (txt_got == txt_out){
log.info("PASS "+txt_in+" -> "+txt_out);
}else{
log.info("FAIL "+txt_in+" -> "+txt_out+" (got "+txt_got+")");
}
}
}
function trim(str){
str = ""+str;
return str.replace(/^\s+|\s+$/g, "");
}
function for_all_players(f){
var out = {};
for (var i=0; i<config.all_players.length; i++){
try {
var p = apiFindObject(config.all_players[i]);
var ret = f(p);
if (ret) out[k] = ret;
}catch (e){}
}
return out;
}
function for_all_locations(f){
var out = {};
for (var i in config.data_maps.streets){
for (var j in config.data_maps.streets[i]){
for (var k in config.data_maps.streets[i][j]){
try {
var l = apiFindObject(k);
var ret = l.run_on_location(f);
if (ret) out[k] = ret;
} catch(e) {}
}
}
}
return out;
}
function for_all_ground_items(f){
var out = {};
for (var i in config.data_maps.streets){
for (var j in config.data_maps.streets[i]){
for (var k in config.data_maps.streets[i][j]){
try {
var l = apiFindObject(k);
var temp = l.run_on_items(f);
for (var ii in temp){
if (temp[ii]) out[ii] = temp[ii];
}
} catch(e) {}
}
}
}
return out;
}
var roman_map = {
'M' : 1000,
'CM' : 900,
'D' : 500,
'CD' : 400,
'C' : 100,
'XC' : 90,
'L' : 50,
'XL' : 40,
'X' : 10,
'IX' : 9,
'V' : 5,
'IV' : 4,
'I' : 1
};
function to_roman_numerals(num){
var n = parseInt(num, 10); // intval is not available
var res = '';
for (var roman in this.roman_map){
var number = this.roman_map[roman];
var matches = parseInt(n / number, 10); // intval is not available
for (var i=0; i<matches; i++){
res += roman;
}
n = n % number;
}
return res;
}
function ago(ts){
if (!ts){
return 'a long time ago';
}
var s = time() - ts;
if (s > 60 * 60 * 36){
var d = intval(Math.round(s/(60*60*24)));
return d+" days ago";
}
if (s > 60 * 90){
var h = intval(Math.round(s/(60*60)));
return h+" hr ago";
}
if (s > 90){
var m = intval(Math.round(s/60));
return m+" min ago";
}
if (s < 2){
return "just now";
}
return s+" sec ago";
}
function copy_hash(hash){
return this.apiCopyHash(hash);
}
function copy_value(v){
if (typeof v == 'object'){
return this.copy_hash(v);
}
return v;
}
function copy_gs_object(src){
var out = {};
var keys = src.apiGetDynamicKeys();
for (var i=0; i<keys.length; i++){
out[keys[i]] = this.copy_value(src[keys[i]]);
}
return out;
}
function replace_gs_object(obj, new_dyn){
var old_keys = obj.apiGetDynamicKeys();
for (var i=0; i<old_keys.length; i++){
delete obj[old_keys[i]];
}
for (var k in new_dyn){
obj[k] = this.copy_value(new_dyn[k]);
}
}
function substitute(str, vars){
var var_keys = this.array_keys(vars);
var out = '';
var rx = /\$([a-z0-9_]+)/ig;
var m;
while (m = rx.exec(str)){
var var_name = m[1];
var pre = str.substr(0, rx.lastIndex - (1 + var_name.length));
str = str.substr(rx.lastIndex);
out += pre;
if (this.in_array(var_name, var_keys)){
out += vars[var_name];
}else{
out += "{$"+var_name+"}";
}
rx.lastIndex = 0;
}
out += str;
return out;
}
function in_array(needle, haystack){
for (var i in haystack){
if (haystack[i] == needle) return 1;
}
return 0;
}
function array_keys(a){
var out = [];
for (var i in a) out.push(i);
return out;
}
function shuffle(o){
for(var j, x, i = o.length; i; j = parseInt(Math.random() * i, 10), x = o[--i], o[i] = o[j], o[j] = x);
return o;
}
function sortObj(object, sortFunc){
var rv = [];
for (var k in object){
if (object[k]) rv.push({key: k, value: object[k]});
}
rv.sort(function(o1, o2){
return sortFunc(o1.key, o2.key);
});
return rv;
}
function pad(number, length) {
var str = '' + number;
while (str.length < length) {
str = '0' + str;
}
return str;
}
function get_pol_config(door_uid){
for (var i in config.pol_types){
if (door_uid == config.pol_types[i].uid) return config.pol_types[i];
}
return {};
}
function has_key(key, a){
for (var i in a){
if (i == key) return true;
}
return false;
}
function regexp_escape(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
function irc_inject(channel, msg){
var args = {
channel: channel,
msg: msg
};
http_get('callbacks/irc_inject.php', args);
}
function strip_html(txt){
var matchTag = /<(?:.|\s)*?>/g;
return txt.replace(matchTag, "");
}
function number_format(number, decimals, dec_point, thousands_sep){
// http://kevin.vanzonneveld.net
// + original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfix by: Michael White (http://getsprink.com)
// + bugfix by: Benjamin Lupton
// + bugfix by: Allan Jensen (http://www.winternet.no)
// + revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
// + bugfix by: Howard Yeend
// + revised by: Luke Smith (http://lucassmith.name)
// + bugfix by: Diogo Resende
// + bugfix by: Rival
// + input by: Kheang Hok Chin (http://www.distantia.ca/)
// + improved by: davook
// + improved by: Brett Zamir (http://brett-zamir.me)
// + input by: Jay Klehr
// + improved by: Brett Zamir (http://brett-zamir.me)
// + input by: Amir Habibi (http://www.residence-mixte.com/)
// + bugfix by: Brett Zamir (http://brett-zamir.me)
// + improved by: Theriault
// + input by: Amirouche
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: number_format(1234.56);
// * returns 1: '1,235'
// * example 2: number_format(1234.56, 2, ',', ' ');
// * returns 2: '1 234,56'
// * example 3: number_format(1234.5678, 2, '.', '');
// * returns 3: '1234.57'
// * example 4: number_format(67, 2, ',', '.');
// * returns 4: '67,00'
// * example 5: number_format(1000);
// * returns 5: '1,000'
// * example 6: number_format(67.311, 2);
// * returns 6: '67.31'
// * example 7: number_format(1000.55, 1);
// * returns 7: '1,000.6'
// * example 8: number_format(67000, 5, ',', '.');
// * returns 8: '67.000,00000'
// * example 9: number_format(0.9, 0);
// * returns 9: '1'
// * example 10: number_format('1.20', 2);
// * returns 10: '1.20'
// * example 11: number_format('1.20', 4);
// * returns 11: '1.2000'
// * example 12: number_format('1.2000', 3);
// * returns 12: '1.200'
// * example 13: number_format('1 000,50', 2, '.', ' ');
// * returns 13: '100 050.00'
// Strip all characters but numerical ones.
number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
var n = !isFinite(+number) ? 0 : +number,
prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
s = '',
toFixedFix = function (n, prec) {
var k = Math.pow(10, prec);
return '' + Math.round(n * k) / k;
};
// Fix for IE parseFloat(0.55).toFixed(0) = 0;
s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
if (s[0].length > 3){
s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
}
if ((s[1] || '').length < prec){
s[1] = s[1] || '';
s[1] += new Array(prec - s[1].length + 1).join('0');
}
return s.join(dec);
}