-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathal_storage.js
152 lines (131 loc) · 3.22 KB
/
al_storage.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
'use strict';
globalThis.al_storage = (function(){
const sto = {};
const expose = make_exposure(sto);
const slot_no = 73;
const slot_name = "al_storage";
const variables = {};
if(parent.caracAL) {
globalThis.load_code = function(name_or_slot,onerror)
{//its a shitty shitty workaround.
fetch("https://adventure.land/code.js?name="+encodeURIComponent(name_or_slot)+"×tamp="+(new Date().getTime()), {
headers: {"Cookie": "auth=" + parent.user_id+"-"+parent.user_auth}
}).then(x=>x.text()).then(x=>(1,eval)(x));
}
}
function write()
{
return upload_code(slot_no,slot_name,
"al_storage.update_all(\n"
+ JSON.stringify(variables,null,2)
+"\n,true);al_util.log(\"loaded al_storage from CODE\");");
}
expose(
function update_all(object, silently=false)
{
if(!object) return;
for(let key in object)
variables[key] = object[key];
if(!silently)
{
write();
let cnt = 0;
parent.X.characters.forEach(char => {
if(char && char.server && char.name != character.name)
if(char.server == parent.server_region+parent.server_identifier)
{
//send cm
send_cm(char.name,
{"al_storage":object});
}
else
{
//send pm
setTimeout(()=>
parent.socket.emit("say", {
message: JSON.stringify({"al_storage":object}),
name: char.name
}),550 * cnt);
cnt += 1;
}
});
}
});
expose(
function set(k,v)
{
const obj = {};
obj[k] = v;
return sto.update_all(obj);
});
expose(
function get(key)
{
return variables[key];
});
expose(
class Char_Key {
constructor(_key) {
this.key = _key;
}
_access(_char = character.name){
return "c_"+_char+"_"+this.key;}
update(val, _char = character.name){
return sto.set(this._access(_char),val);}
obtain(_char = character.name){
return sto.get(this._access(_char));}
update_all(val)
{
const delta = {};
for(let profile of parent.X.characters)
{delta[this._access(profile.name)] = val;}
return sto.update_all(delta);
}
});
expose(
class Global_Key {
constructor(_key) {
this.key = _key;
}
_access(){
return "g_"+this.key;}
update(val){
return sto.set(this._access(),val);
}
obtain(){
return sto.get(this._access());
}
});
character.on("cm",data=>{
if(!al_social.is_mine(data.name))
return;
if(!data.message?.al_storage)
return;
sto.update_all(data.message.al_storage,true);
});
al_util.psock_on('pm', function(data) {
if(data.owner == character.name)
return;
if(!al_social.is_mine(data.owner))
return;
try {
const obj = JSON.parse(data.message);
if(!obj?.al_storage)
return;
sto.update_all(obj.al_storage,true);
} catch (e) {
//pm is not in our format; omit
}
});
expose(
function reload()
{
//doesnt work in albot
if(parent.X.codes[slot_no])
load_code(slot_no);
});
return sto;
})();
(function(){
al_storage.reload();
})();