-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathal_util.js
156 lines (141 loc) · 3.2 KB
/
al_util.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
'use strict';
globalThis.make_exposure = function make_exposure(container){
return function expose(func,name=func.name){
container[name] = func;
}
}
globalThis.al_util = (function(){
const ut = {};
const expose = make_exposure(ut);
const behaviours = {};
expose(
function register_behaviour(timeout,func,name = func.name)
{
behaviours[name] = [func,timeout]
});
expose(
function recommended_monitoring_interval()
{
return 1000 / 10;
});
expose(
function flatten(arr) {
return [].concat.apply([], arr);
});
expose(
function range(start, stop, step) {
if (typeof stop == 'undefined') {
// one param defined
stop = start;
start = 0;
}
if (typeof step == 'undefined') {
step = 1;
}
if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) {
return [];
}
const result = [];
for (var i = start; step > 0 ? i < stop : i > stop; i += step) {
result.push(i);
}
return result;
});
let run_loop = {};
//TODO maybe turn this into a case by case thing
expose(
function start_behaviours()
{
for(let k in behaviours)
{
if(run_loop[k])
continue;
const [func,timer] = behaviours[k];
run_loop[k] = setInterval(func,timer);
}
});
expose(
function sleep(milliseconds) {
return new Promise(resolve => setTimeout(resolve, milliseconds));
});
expose(
function old_date() {
return 1337;
});
expose(
class Timeout {
constructor(_period) {
this._timeout = _period;
this._next_op = ut.old_date();
}
unlocked() {
return this._next_op <= Date.now();
}
execute(operations, multiples = false)
{
const ops = multiples ? operations : [operations];
if(!this.unlocked())
return false;
let timeout = 0;
const result = [];
for(let i = 0; i < ops.length; i++)
{
result.push(ops[i]());
timeout += i == 0 ? this._timeout : this._timeout / 2;
}
this._next_op = Date.now() + timeout;
return ut.sleep(timeout).then(x=>result);
}
lock()
{
return this.execute(()=>false);
}
});
expose(
class Ring {
constructor(src_func) {
this._src_func = src_func;
this._index = 0;
}
next() {
const src = this._src_func();
if(src.length <= 0)
return null;
this._index = (this._index+1) % src.length;
return src[this._index];
}
});
const DEFAULT_GRACE_PERIOD = 220;
expose(
function recommended_grace_period() {
return DEFAULT_GRACE_PERIOD;
});
expose(
function log(msg,color){
return game_log(msg,color);
});
const sock_listeners = {};
expose(
function psock_on(name,func)
{
if(!sock_listeners[name])
sock_listeners[name] = [];
sock_listeners[name].push(func);
parent.socket.on(name,func);
});
const old_cleanup = globalThis.on_destroy;
globalThis.on_destroy = function()
{
for(let k in sock_listeners) {
for(let func of sock_listeners[k]) {
parent.socket.off(k,func);
}
}
return old_cleanup();
}
expose(
function is_hardcore() {
return "hardcore" == parent.gameplay;
});
return ut;
})();